wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / wined3d / surface.c
blob7f0a42c72c0aba8ce2413780ae3276306bcbe52f
1 /*
2 * IWineD3DSurface Implementation
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2002-2005 Jason Edmeades
7 * Copyright 2002-2003 Raphael Junqueira
8 * Copyright 2004 Christian Costa
9 * Copyright 2005 Oliver Stieber
10 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
11 * Copyright 2007-2008 Henri Verbeet
12 * Copyright 2006-2008 Roderick Colenbrander
13 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "config.h"
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;
50 ENTER_GL();
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);
71 LEAVE_GL();
73 context_release(context);
76 if (This->flags & SFLAG_DIBSECTION)
78 /* Release the DC. */
79 SelectObject(This->hDC, This->dib.holdbitmap);
80 DeleteDC(This->hDC);
81 /* Release the DIB section. */
82 DeleteObject(This->dib.DIBsection);
83 This->dib.bitmap_data = NULL;
84 This->resource.allocatedMemory = NULL;
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(&This->resource);
95 void surface_set_container(IWineD3DSurfaceImpl *surface, enum wined3d_container_type type, IWineD3DBase *container)
97 TRACE("surface %p, container %p.\n", surface, container);
99 if (!container && type != WINED3D_CONTAINER_NONE)
100 ERR("Setting NULL container of type %#x.\n", type);
102 if (type == WINED3D_CONTAINER_SWAPCHAIN)
104 surface->get_drawable_size = get_drawable_size_swapchain;
106 else
108 switch (wined3d_settings.offscreen_rendering_mode)
110 case ORM_FBO:
111 surface->get_drawable_size = get_drawable_size_fbo;
112 break;
114 case ORM_BACKBUFFER:
115 surface->get_drawable_size = get_drawable_size_backbuffer;
116 break;
118 default:
119 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
120 return;
124 surface->container.type = type;
125 surface->container.u.base = container;
128 struct blt_info
130 GLenum binding;
131 GLenum bind_target;
132 enum tex_types tex_type;
133 GLfloat coords[4][3];
136 struct float_rect
138 float l;
139 float t;
140 float r;
141 float b;
144 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
146 f->l = ((r->left * 2.0f) / w) - 1.0f;
147 f->t = ((r->top * 2.0f) / h) - 1.0f;
148 f->r = ((r->right * 2.0f) / w) - 1.0f;
149 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
152 static void surface_get_blt_info(GLenum target, const RECT *rect, GLsizei w, GLsizei h, struct blt_info *info)
154 GLfloat (*coords)[3] = info->coords;
155 struct float_rect f;
157 switch (target)
159 default:
160 FIXME("Unsupported texture target %#x\n", target);
161 /* Fall back to GL_TEXTURE_2D */
162 case GL_TEXTURE_2D:
163 info->binding = GL_TEXTURE_BINDING_2D;
164 info->bind_target = GL_TEXTURE_2D;
165 info->tex_type = tex_2d;
166 coords[0][0] = (float)rect->left / w;
167 coords[0][1] = (float)rect->top / h;
168 coords[0][2] = 0.0f;
170 coords[1][0] = (float)rect->right / w;
171 coords[1][1] = (float)rect->top / h;
172 coords[1][2] = 0.0f;
174 coords[2][0] = (float)rect->left / w;
175 coords[2][1] = (float)rect->bottom / h;
176 coords[2][2] = 0.0f;
178 coords[3][0] = (float)rect->right / w;
179 coords[3][1] = (float)rect->bottom / h;
180 coords[3][2] = 0.0f;
181 break;
183 case GL_TEXTURE_RECTANGLE_ARB:
184 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
185 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
186 info->tex_type = tex_rect;
187 coords[0][0] = rect->left; coords[0][1] = rect->top; coords[0][2] = 0.0f;
188 coords[1][0] = rect->right; coords[1][1] = rect->top; coords[1][2] = 0.0f;
189 coords[2][0] = rect->left; coords[2][1] = rect->bottom; coords[2][2] = 0.0f;
190 coords[3][0] = rect->right; coords[3][1] = rect->bottom; coords[3][2] = 0.0f;
191 break;
193 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
194 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
195 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
196 info->tex_type = tex_cube;
197 cube_coords_float(rect, w, h, &f);
199 coords[0][0] = 1.0f; coords[0][1] = -f.t; coords[0][2] = -f.l;
200 coords[1][0] = 1.0f; coords[1][1] = -f.t; coords[1][2] = -f.r;
201 coords[2][0] = 1.0f; coords[2][1] = -f.b; coords[2][2] = -f.l;
202 coords[3][0] = 1.0f; coords[3][1] = -f.b; coords[3][2] = -f.r;
203 break;
205 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
206 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
207 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
208 info->tex_type = tex_cube;
209 cube_coords_float(rect, w, h, &f);
211 coords[0][0] = -1.0f; coords[0][1] = -f.t; coords[0][2] = f.l;
212 coords[1][0] = -1.0f; coords[1][1] = -f.t; coords[1][2] = f.r;
213 coords[2][0] = -1.0f; coords[2][1] = -f.b; coords[2][2] = f.l;
214 coords[3][0] = -1.0f; coords[3][1] = -f.b; coords[3][2] = f.r;
215 break;
217 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
218 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
219 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
220 info->tex_type = tex_cube;
221 cube_coords_float(rect, w, h, &f);
223 coords[0][0] = f.l; coords[0][1] = 1.0f; coords[0][2] = f.t;
224 coords[1][0] = f.r; coords[1][1] = 1.0f; coords[1][2] = f.t;
225 coords[2][0] = f.l; coords[2][1] = 1.0f; coords[2][2] = f.b;
226 coords[3][0] = f.r; coords[3][1] = 1.0f; coords[3][2] = f.b;
227 break;
229 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
230 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
231 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
232 info->tex_type = tex_cube;
233 cube_coords_float(rect, w, h, &f);
235 coords[0][0] = f.l; coords[0][1] = -1.0f; coords[0][2] = -f.t;
236 coords[1][0] = f.r; coords[1][1] = -1.0f; coords[1][2] = -f.t;
237 coords[2][0] = f.l; coords[2][1] = -1.0f; coords[2][2] = -f.b;
238 coords[3][0] = f.r; coords[3][1] = -1.0f; coords[3][2] = -f.b;
239 break;
241 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
242 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
243 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
244 info->tex_type = tex_cube;
245 cube_coords_float(rect, w, h, &f);
247 coords[0][0] = f.l; coords[0][1] = -f.t; coords[0][2] = 1.0f;
248 coords[1][0] = f.r; coords[1][1] = -f.t; coords[1][2] = 1.0f;
249 coords[2][0] = f.l; coords[2][1] = -f.b; coords[2][2] = 1.0f;
250 coords[3][0] = f.r; coords[3][1] = -f.b; coords[3][2] = 1.0f;
251 break;
253 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
254 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
255 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
256 info->tex_type = tex_cube;
257 cube_coords_float(rect, w, h, &f);
259 coords[0][0] = -f.l; coords[0][1] = -f.t; coords[0][2] = -1.0f;
260 coords[1][0] = -f.r; coords[1][1] = -f.t; coords[1][2] = -1.0f;
261 coords[2][0] = -f.l; coords[2][1] = -f.b; coords[2][2] = -1.0f;
262 coords[3][0] = -f.r; coords[3][1] = -f.b; coords[3][2] = -1.0f;
263 break;
267 static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
269 if (rect_in)
270 *rect_out = *rect_in;
271 else
273 rect_out->left = 0;
274 rect_out->top = 0;
275 rect_out->right = This->currentDesc.Width;
276 rect_out->bottom = This->currentDesc.Height;
280 /* GL locking and context activation is done by the caller */
281 void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
283 struct blt_info info;
285 surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
287 glEnable(info.bind_target);
288 checkGLcall("glEnable(bind_target)");
290 /* Bind the texture */
291 glBindTexture(info.bind_target, src_surface->texture_name);
292 checkGLcall("glBindTexture");
294 /* Filtering for StretchRect */
295 glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
296 wined3d_gl_mag_filter(magLookup, Filter));
297 checkGLcall("glTexParameteri");
298 glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
299 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
300 checkGLcall("glTexParameteri");
301 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
302 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
303 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
304 checkGLcall("glTexEnvi");
306 /* Draw a quad */
307 glBegin(GL_TRIANGLE_STRIP);
308 glTexCoord3fv(info.coords[0]);
309 glVertex2i(dst_rect->left, dst_rect->top);
311 glTexCoord3fv(info.coords[1]);
312 glVertex2i(dst_rect->right, dst_rect->top);
314 glTexCoord3fv(info.coords[2]);
315 glVertex2i(dst_rect->left, dst_rect->bottom);
317 glTexCoord3fv(info.coords[3]);
318 glVertex2i(dst_rect->right, dst_rect->bottom);
319 glEnd();
321 /* Unbind the texture */
322 glBindTexture(info.bind_target, 0);
323 checkGLcall("glBindTexture(info->bind_target, 0)");
325 /* We changed the filtering settings on the texture. Inform the
326 * container about this to get the filters reset properly next draw. */
327 if (src_surface->container.type == WINED3D_CONTAINER_TEXTURE)
329 IWineD3DBaseTextureImpl *texture = src_surface->container.u.texture;
330 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
331 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
332 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
336 static void surface_realize_palette(IWineD3DSurfaceImpl *surface)
338 struct wined3d_palette *palette = surface->palette;
340 TRACE("surface %p.\n", surface);
342 if (!palette) return;
344 if (surface->resource.format->id == WINED3DFMT_P8_UINT
345 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
347 if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
349 /* Make sure the texture is up to date. This call doesn't do
350 * anything if the texture is already up to date. */
351 surface_load_location(surface, SFLAG_INTEXTURE, NULL);
353 /* We want to force a palette refresh, so mark the drawable as not being up to date */
354 surface_modify_location(surface, SFLAG_INDRAWABLE, FALSE);
356 else
358 if (!(surface->flags & SFLAG_INSYSMEM))
360 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
361 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
363 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
367 if (surface->flags & SFLAG_DIBSECTION)
369 RGBQUAD col[256];
370 unsigned int i;
372 TRACE("Updating the DC's palette.\n");
374 for (i = 0; i < 256; ++i)
376 col[i].rgbRed = palette->palents[i].peRed;
377 col[i].rgbGreen = palette->palents[i].peGreen;
378 col[i].rgbBlue = palette->palents[i].peBlue;
379 col[i].rgbReserved = 0;
381 SetDIBColorTable(surface->hDC, 0, 256, col);
384 /* Propagate the changes to the drawable when we have a palette. */
385 if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
386 surface_load_location(surface, SFLAG_INDRAWABLE, NULL);
389 static HRESULT surface_draw_overlay(IWineD3DSurfaceImpl *surface)
391 HRESULT hr;
393 /* If there's no destination surface there is nothing to do. */
394 if (!surface->overlay_dest)
395 return WINED3D_OK;
397 /* Blt calls ModifyLocation on the dest surface, which in turn calls
398 * DrawOverlay to update the overlay. Prevent an endless recursion. */
399 if (surface->overlay_dest->flags & SFLAG_INOVERLAYDRAW)
400 return WINED3D_OK;
402 surface->overlay_dest->flags |= SFLAG_INOVERLAYDRAW;
403 hr = IWineD3DSurface_Blt((IWineD3DSurface *)surface->overlay_dest,
404 &surface->overlay_destrect, (IWineD3DSurface *)surface, &surface->overlay_srcrect,
405 WINEDDBLT_WAIT, NULL, WINED3DTEXF_LINEAR);
406 surface->overlay_dest->flags &= ~SFLAG_INOVERLAYDRAW;
408 return hr;
411 /* Context activation is done by the caller. */
412 static void surface_remove_pbo(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
414 if (!surface->resource.heapMemory)
416 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), 0, surface->resource.size + RESOURCE_ALIGNMENT);
417 surface->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)surface->resource.heapMemory
418 + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
421 ENTER_GL();
422 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo));
423 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, surface->pbo)");
424 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0,
425 surface->resource.size, surface->resource.allocatedMemory));
426 checkGLcall("glGetBufferSubDataARB");
427 GL_EXTCALL(glDeleteBuffersARB(1, &surface->pbo));
428 checkGLcall("glDeleteBuffersARB");
429 LEAVE_GL();
431 surface->pbo = 0;
432 surface->flags &= ~SFLAG_PBO;
435 /* Do not call while under the GL lock. */
436 static void surface_unload(struct wined3d_resource *resource)
438 IWineD3DSurfaceImpl *surface = surface_from_resource(resource);
439 IWineD3DDeviceImpl *device = resource->device;
440 const struct wined3d_gl_info *gl_info;
441 renderbuffer_entry_t *entry, *entry2;
442 struct wined3d_context *context;
444 TRACE("surface %p.\n", surface);
446 if (resource->pool == WINED3DPOOL_DEFAULT)
448 /* Default pool resources are supposed to be destroyed before Reset is called.
449 * Implicit resources stay however. So this means we have an implicit render target
450 * or depth stencil. The content may be destroyed, but we still have to tear down
451 * opengl resources, so we cannot leave early.
453 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
454 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
455 * or the depth stencil into an FBO the texture or render buffer will be removed
456 * and all flags get lost
458 surface_init_sysmem(surface);
460 else
462 /* Load the surface into system memory */
463 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
464 surface_modify_location(surface, SFLAG_INDRAWABLE, FALSE);
466 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
467 surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
468 surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
470 context = context_acquire(device, NULL);
471 gl_info = context->gl_info;
473 /* Destroy PBOs, but load them into real sysmem before */
474 if (surface->flags & SFLAG_PBO)
475 surface_remove_pbo(surface, gl_info);
477 /* Destroy fbo render buffers. This is needed for implicit render targets, for
478 * all application-created targets the application has to release the surface
479 * before calling _Reset
481 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, renderbuffer_entry_t, entry)
483 ENTER_GL();
484 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
485 LEAVE_GL();
486 list_remove(&entry->entry);
487 HeapFree(GetProcessHeap(), 0, entry);
489 list_init(&surface->renderbuffers);
490 surface->current_renderbuffer = NULL;
492 /* If we're in a texture, the texture name belongs to the texture.
493 * Otherwise, destroy it. */
494 if (surface->container.type != WINED3D_CONTAINER_TEXTURE)
496 ENTER_GL();
497 glDeleteTextures(1, &surface->texture_name);
498 surface->texture_name = 0;
499 glDeleteTextures(1, &surface->texture_name_srgb);
500 surface->texture_name_srgb = 0;
501 LEAVE_GL();
504 context_release(context);
506 resource_unload(resource);
509 static const struct wined3d_resource_ops surface_resource_ops =
511 surface_unload,
514 static const struct wined3d_surface_ops surface_ops =
516 surface_realize_palette,
517 surface_draw_overlay,
520 HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
521 UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
522 UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
523 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
525 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
526 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
527 void (*cleanup)(IWineD3DSurfaceImpl *This);
528 unsigned int resource_size;
529 HRESULT hr;
531 if (multisample_quality > 0)
533 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality);
534 multisample_quality = 0;
537 /* Quick lockable sanity check.
538 * TODO: remove this after surfaces, usage and lockability have been debugged properly
539 * this function is too deep to need to care about things like this.
540 * Levels need to be checked too, since they all affect what can be done. */
541 switch (pool)
543 case WINED3DPOOL_SCRATCH:
544 if(!lockable)
546 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
547 "which are mutually exclusive, setting lockable to TRUE.\n");
548 lockable = TRUE;
550 break;
552 case WINED3DPOOL_SYSTEMMEM:
553 if (!lockable)
554 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
555 break;
557 case WINED3DPOOL_MANAGED:
558 if (usage & WINED3DUSAGE_DYNAMIC)
559 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
560 break;
562 case WINED3DPOOL_DEFAULT:
563 if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
564 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
565 break;
567 default:
568 FIXME("Unknown pool %#x.\n", pool);
569 break;
572 if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
574 FIXME("Trying to create a render target that isn't in the default pool.\n");
577 /* FIXME: Check that the format is supported by the device. */
579 resource_size = wined3d_format_calculate_size(format, alignment, width, height);
580 if (!resource_size) return WINED3DERR_INVALIDCALL;
582 /* Look at the implementation and set the correct Vtable. */
583 switch (surface_type)
585 case SURFACE_OPENGL:
586 surface->lpVtbl = &IWineD3DSurface_Vtbl;
587 cleanup = surface_cleanup;
588 break;
590 case SURFACE_GDI:
591 surface->lpVtbl = &IWineGDISurface_Vtbl;
592 cleanup = surface_gdi_cleanup;
593 break;
595 default:
596 ERR("Requested unknown surface implementation %#x.\n", surface_type);
597 return WINED3DERR_INVALIDCALL;
600 hr = resource_init(&surface->resource, WINED3DRTYPE_SURFACE, device, resource_size,
601 usage, format, pool, parent, parent_ops, &surface_resource_ops);
602 if (FAILED(hr))
604 WARN("Failed to initialize resource, returning %#x.\n", hr);
605 return hr;
608 /* "Standalone" surface. */
609 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
611 surface->currentDesc.Width = width;
612 surface->currentDesc.Height = height;
613 surface->currentDesc.MultiSampleType = multisample_type;
614 surface->currentDesc.MultiSampleQuality = multisample_quality;
615 surface->texture_level = level;
616 list_init(&surface->overlays);
618 /* Flags */
619 surface->flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
620 if (discard) surface->flags |= SFLAG_DISCARD;
621 if (lockable || format_id == WINED3DFMT_D16_LOCKABLE) surface->flags |= SFLAG_LOCKABLE;
623 /* Mark the texture as dirty so that it gets loaded first time around. */
624 surface_add_dirty_rect(surface, NULL);
625 list_init(&surface->renderbuffers);
627 TRACE("surface %p, memory %p, size %u\n", surface, surface->resource.allocatedMemory, surface->resource.size);
629 /* Call the private setup routine */
630 hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
631 if (FAILED(hr))
633 ERR("Private setup failed, returning %#x\n", hr);
634 cleanup(surface);
635 return hr;
638 return hr;
641 static void surface_force_reload(IWineD3DSurfaceImpl *surface)
643 surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
646 void surface_set_texture_name(IWineD3DSurfaceImpl *surface, GLuint new_name, BOOL srgb)
648 GLuint *name;
649 DWORD flag;
651 TRACE("surface %p, new_name %u, srgb %#x.\n", surface, new_name, srgb);
653 if(srgb)
655 name = &surface->texture_name_srgb;
656 flag = SFLAG_INSRGBTEX;
658 else
660 name = &surface->texture_name;
661 flag = SFLAG_INTEXTURE;
664 if (!*name && new_name)
666 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
667 * surface has no texture name yet. See if we can get rid of this. */
668 if (surface->flags & flag)
669 ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag));
670 surface_modify_location(surface, flag, FALSE);
673 *name = new_name;
674 surface_force_reload(surface);
677 void surface_set_texture_target(IWineD3DSurfaceImpl *surface, GLenum target)
679 TRACE("surface %p, target %#x.\n", surface, target);
681 if (surface->texture_target != target)
683 if (target == GL_TEXTURE_RECTANGLE_ARB)
685 surface->flags &= ~SFLAG_NORMCOORD;
687 else if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
689 surface->flags |= SFLAG_NORMCOORD;
692 surface->texture_target = target;
693 surface_force_reload(surface);
696 /* Context activation is done by the caller. */
697 void surface_bind(IWineD3DSurfaceImpl *surface, BOOL srgb)
699 TRACE("surface %p, srgb %#x.\n", surface, srgb);
701 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
703 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
704 const struct wined3d_gl_info *gl_info = &texture->resource.device->adapter->gl_info;
706 TRACE("Passing to container (%p).\n", texture);
707 texture->baseTexture.texture_ops->texture_bind(texture, gl_info, srgb);
709 else
711 if (surface->texture_level)
713 ERR("Standalone surface %p is non-zero texture level %u.\n",
714 surface, surface->texture_level);
717 if (srgb)
718 ERR("Trying to bind standalone surface %p as sRGB.\n", surface);
720 ENTER_GL();
722 if (!surface->texture_name)
724 glGenTextures(1, &surface->texture_name);
725 checkGLcall("glGenTextures");
727 TRACE("Surface %p given name %u.\n", surface, surface->texture_name);
729 glBindTexture(surface->texture_target, surface->texture_name);
730 checkGLcall("glBindTexture");
731 glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
732 glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
733 glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
734 glTexParameteri(surface->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
735 glTexParameteri(surface->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
736 checkGLcall("glTexParameteri");
738 else
740 glBindTexture(surface->texture_target, surface->texture_name);
741 checkGLcall("glBindTexture");
744 LEAVE_GL();
748 /* Context activation is done by the caller. */
749 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This, BOOL srgb) {
750 DWORD active_sampler;
752 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
753 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
754 * gl states. The current texture unit should always be a valid one.
756 * To be more specific, this is tricky because we can implicitly be called
757 * from sampler() in state.c. This means we can't touch anything other than
758 * whatever happens to be the currently active texture, or we would risk
759 * marking already applied sampler states dirty again.
761 * TODO: Track the current active texture per GL context instead of using glGet
763 GLint active_texture;
764 ENTER_GL();
765 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
766 LEAVE_GL();
767 active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
769 if (active_sampler != WINED3D_UNMAPPED_STAGE)
771 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
773 surface_bind(This, srgb);
776 /* This function checks if the primary render target uses the 8bit paletted format. */
777 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
779 if (device->render_targets && device->render_targets[0])
781 IWineD3DSurfaceImpl *render_target = device->render_targets[0];
782 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
783 && (render_target->resource.format->id == WINED3DFMT_P8_UINT))
784 return TRUE;
786 return FALSE;
789 /* This call just downloads data, the caller is responsible for binding the
790 * correct texture. */
791 /* Context activation is done by the caller. */
792 static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
794 const struct wined3d_format *format = This->resource.format;
796 /* Only support read back of converted P8 surfaces */
797 if (This->flags & SFLAG_CONVERTED && format->id != WINED3DFMT_P8_UINT)
799 FIXME("Readback conversion not supported for format %s.\n", debug_d3dformat(format->id));
800 return;
803 ENTER_GL();
805 if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
807 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
808 This, This->texture_level, format->glFormat, format->glType,
809 This->resource.allocatedMemory);
811 if (This->flags & SFLAG_PBO)
813 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
814 checkGLcall("glBindBufferARB");
815 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
816 checkGLcall("glGetCompressedTexImageARB");
817 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
818 checkGLcall("glBindBufferARB");
820 else
822 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
823 This->texture_level, This->resource.allocatedMemory));
824 checkGLcall("glGetCompressedTexImageARB");
827 LEAVE_GL();
828 } else {
829 void *mem;
830 GLenum gl_format = format->glFormat;
831 GLenum gl_type = format->glType;
832 int src_pitch = 0;
833 int dst_pitch = 0;
835 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
836 if (format->id == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
838 gl_format = GL_ALPHA;
839 gl_type = GL_UNSIGNED_BYTE;
842 if (This->flags & SFLAG_NONPOW2)
844 unsigned char alignment = This->resource.device->surface_alignment;
845 src_pitch = format->byte_count * This->pow2Width;
846 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
847 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
848 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
849 } else {
850 mem = This->resource.allocatedMemory;
853 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
854 This, This->texture_level, gl_format, gl_type, mem);
856 if (This->flags & SFLAG_PBO)
858 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
859 checkGLcall("glBindBufferARB");
861 glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, NULL);
862 checkGLcall("glGetTexImage");
864 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
865 checkGLcall("glBindBufferARB");
866 } else {
867 glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, mem);
868 checkGLcall("glGetTexImage");
870 LEAVE_GL();
872 if (This->flags & SFLAG_NONPOW2)
874 const BYTE *src_data;
875 BYTE *dst_data;
876 UINT y;
878 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
879 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
880 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
882 * We're doing this...
884 * instead of boxing the texture :
885 * |<-texture width ->| -->pow2width| /\
886 * |111111111111111111| | |
887 * |222 Texture 222222| boxed empty | texture height
888 * |3333 Data 33333333| | |
889 * |444444444444444444| | \/
890 * ----------------------------------- |
891 * | boxed empty | boxed empty | pow2height
892 * | | | \/
893 * -----------------------------------
896 * we're repacking the data to the expected texture width
898 * |<-texture width ->| -->pow2width| /\
899 * |111111111111111111222222222222222| |
900 * |222333333333333333333444444444444| texture height
901 * |444444 | |
902 * | | \/
903 * | | |
904 * | empty | pow2height
905 * | | \/
906 * -----------------------------------
908 * == is the same as
910 * |<-texture width ->| /\
911 * |111111111111111111|
912 * |222222222222222222|texture height
913 * |333333333333333333|
914 * |444444444444444444| \/
915 * --------------------
917 * this also means that any references to allocatedMemory should work with the data as if were a
918 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
920 * internally the texture is still stored in a boxed format so any references to textureName will
921 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
923 * Performance should not be an issue, because applications normally do not lock the surfaces when
924 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
925 * and doesn't have to be re-read.
927 src_data = mem;
928 dst_data = This->resource.allocatedMemory;
929 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
930 for (y = 1 ; y < This->currentDesc.Height; y++) {
931 /* skip the first row */
932 src_data += src_pitch;
933 dst_data += dst_pitch;
934 memcpy(dst_data, src_data, dst_pitch);
937 HeapFree(GetProcessHeap(), 0, mem);
941 /* Surface has now been downloaded */
942 This->flags |= SFLAG_INSYSMEM;
945 /* This call just uploads data, the caller is responsible for binding the
946 * correct texture. */
947 /* Context activation is done by the caller. */
948 static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
949 const struct wined3d_format *format, BOOL srgb, const GLvoid *data)
951 GLsizei width = This->currentDesc.Width;
952 GLsizei height = This->currentDesc.Height;
953 GLenum internal;
955 if (srgb)
957 internal = format->glGammaInternal;
959 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
961 internal = format->rtInternal;
963 else
965 internal = format->glInternal;
968 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
969 This, internal, width, height, format->glFormat, format->glType, data);
970 TRACE("target %#x, level %u, resource size %u.\n",
971 This->texture_target, This->texture_level, This->resource.size);
973 if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
975 ENTER_GL();
977 if (This->flags & SFLAG_PBO)
979 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
980 checkGLcall("glBindBufferARB");
982 TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
983 data = NULL;
986 if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
988 TRACE("Calling glCompressedTexSubImage2DARB.\n");
990 GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
991 0, 0, width, height, internal, This->resource.size, data));
992 checkGLcall("glCompressedTexSubImage2DARB");
994 else
996 TRACE("Calling glTexSubImage2D.\n");
998 glTexSubImage2D(This->texture_target, This->texture_level,
999 0, 0, width, height, format->glFormat, format->glType, data);
1000 checkGLcall("glTexSubImage2D");
1003 if (This->flags & SFLAG_PBO)
1005 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1006 checkGLcall("glBindBufferARB");
1009 LEAVE_GL();
1011 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
1013 IWineD3DDeviceImpl *device = This->resource.device;
1014 unsigned int i;
1016 for (i = 0; i < device->numContexts; ++i)
1018 context_surface_update(device->contexts[i], This);
1023 /* This call just allocates the texture, the caller is responsible for binding
1024 * the correct texture. */
1025 /* Context activation is done by the caller. */
1026 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
1027 const struct wined3d_format *format, BOOL srgb)
1029 BOOL enable_client_storage = FALSE;
1030 GLsizei width = This->pow2Width;
1031 GLsizei height = This->pow2Height;
1032 const BYTE *mem = NULL;
1033 GLenum internal;
1035 if (srgb)
1037 internal = format->glGammaInternal;
1039 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
1041 internal = format->rtInternal;
1043 else
1045 internal = format->glInternal;
1048 if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
1050 TRACE("(%p) : Creating surface (target %#x) level %d, d3d format %s, internal format %#x, width %d, height %d, gl format %#x, gl type=%#x\n",
1051 This, This->texture_target, This->texture_level, debug_d3dformat(format->id),
1052 internal, width, height, format->glFormat, format->glType);
1054 ENTER_GL();
1056 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1058 if (This->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED)
1059 || !This->resource.allocatedMemory)
1061 /* In some cases we want to disable client storage.
1062 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
1063 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
1064 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
1065 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
1067 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1068 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1069 This->flags &= ~SFLAG_CLIENT;
1070 enable_client_storage = TRUE;
1072 else
1074 This->flags |= SFLAG_CLIENT;
1076 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
1077 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
1079 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1083 if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
1085 GL_EXTCALL(glCompressedTexImage2DARB(This->texture_target, This->texture_level,
1086 internal, width, height, 0, This->resource.size, mem));
1087 checkGLcall("glCompressedTexImage2DARB");
1089 else
1091 glTexImage2D(This->texture_target, This->texture_level,
1092 internal, width, height, 0, format->glFormat, format->glType, mem);
1093 checkGLcall("glTexImage2D");
1096 if(enable_client_storage) {
1097 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1098 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1100 LEAVE_GL();
1103 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1104 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1105 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1106 /* GL locking is done by the caller */
1107 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl *surface, unsigned int width, unsigned int height)
1109 const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
1110 renderbuffer_entry_t *entry;
1111 GLuint renderbuffer = 0;
1112 unsigned int src_width, src_height;
1114 src_width = surface->pow2Width;
1115 src_height = surface->pow2Height;
1117 /* A depth stencil smaller than the render target is not valid */
1118 if (width > src_width || height > src_height) return;
1120 /* Remove any renderbuffer set if the sizes match */
1121 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
1122 || (width == src_width && height == src_height))
1124 surface->current_renderbuffer = NULL;
1125 return;
1128 /* Look if we've already got a renderbuffer of the correct dimensions */
1129 LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, renderbuffer_entry_t, entry)
1131 if (entry->width == width && entry->height == height)
1133 renderbuffer = entry->id;
1134 surface->current_renderbuffer = entry;
1135 break;
1139 if (!renderbuffer)
1141 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1142 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1143 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
1144 surface->resource.format->glInternal, width, height);
1146 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
1147 entry->width = width;
1148 entry->height = height;
1149 entry->id = renderbuffer;
1150 list_add_head(&surface->renderbuffers, &entry->entry);
1152 surface->current_renderbuffer = entry;
1155 checkGLcall("set_compatible_renderbuffer");
1158 GLenum surface_get_gl_buffer(IWineD3DSurfaceImpl *surface)
1160 IWineD3DSwapChainImpl *swapchain = surface->container.u.swapchain;
1162 TRACE("surface %p.\n", surface);
1164 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN)
1166 ERR("Surface %p is not on a swapchain.\n", surface);
1167 return GL_NONE;
1170 if (swapchain->back_buffers && swapchain->back_buffers[0] == surface)
1172 if (swapchain->render_to_fbo)
1174 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
1175 return GL_COLOR_ATTACHMENT0;
1177 TRACE("Returning GL_BACK\n");
1178 return GL_BACK;
1180 else if (surface == swapchain->front_buffer)
1182 TRACE("Returning GL_FRONT\n");
1183 return GL_FRONT;
1186 FIXME("Higher back buffer, returning GL_BACK\n");
1187 return GL_BACK;
1190 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
1191 void surface_add_dirty_rect(IWineD3DSurfaceImpl *surface, const RECT *dirty_rect)
1193 TRACE("surface %p, dirty_rect %s.\n", surface, wine_dbgstr_rect(dirty_rect));
1195 if (!(surface->flags & SFLAG_INSYSMEM) && (surface->flags & SFLAG_INTEXTURE))
1196 /* No partial locking for textures yet. */
1197 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1199 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1200 if (dirty_rect)
1202 surface->dirtyRect.left = min(surface->dirtyRect.left, dirty_rect->left);
1203 surface->dirtyRect.top = min(surface->dirtyRect.top, dirty_rect->top);
1204 surface->dirtyRect.right = max(surface->dirtyRect.right, dirty_rect->right);
1205 surface->dirtyRect.bottom = max(surface->dirtyRect.bottom, dirty_rect->bottom);
1207 else
1209 surface->dirtyRect.left = 0;
1210 surface->dirtyRect.top = 0;
1211 surface->dirtyRect.right = surface->currentDesc.Width;
1212 surface->dirtyRect.bottom = surface->currentDesc.Height;
1215 /* if the container is a basetexture then mark it dirty. */
1216 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1218 TRACE("Passing to container.\n");
1219 basetexture_set_dirty(surface->container.u.texture, TRUE);
1223 static BOOL surface_convert_color_to_float(IWineD3DSurfaceImpl *surface, DWORD color, WINED3DCOLORVALUE *float_color)
1225 const struct wined3d_format *format = surface->resource.format;
1226 IWineD3DDeviceImpl *device = surface->resource.device;
1228 switch (format->id)
1230 case WINED3DFMT_P8_UINT:
1231 if (surface->palette)
1233 float_color->r = surface->palette->palents[color].peRed / 255.0f;
1234 float_color->g = surface->palette->palents[color].peGreen / 255.0f;
1235 float_color->b = surface->palette->palents[color].peBlue / 255.0f;
1237 else
1239 float_color->r = 0.0f;
1240 float_color->g = 0.0f;
1241 float_color->b = 0.0f;
1243 float_color->a = primary_render_target_is_p8(device) ? color / 255.0f : 1.0f;
1244 break;
1246 case WINED3DFMT_B5G6R5_UNORM:
1247 float_color->r = ((color >> 11) & 0x1f) / 31.0f;
1248 float_color->g = ((color >> 5) & 0x3f) / 63.0f;
1249 float_color->b = (color & 0x1f) / 31.0f;
1250 float_color->a = 1.0f;
1251 break;
1253 case WINED3DFMT_B8G8R8_UNORM:
1254 case WINED3DFMT_B8G8R8X8_UNORM:
1255 float_color->r = D3DCOLOR_R(color);
1256 float_color->g = D3DCOLOR_G(color);
1257 float_color->b = D3DCOLOR_B(color);
1258 float_color->a = 1.0f;
1259 break;
1261 case WINED3DFMT_B8G8R8A8_UNORM:
1262 float_color->r = D3DCOLOR_R(color);
1263 float_color->g = D3DCOLOR_G(color);
1264 float_color->b = D3DCOLOR_B(color);
1265 float_color->a = D3DCOLOR_A(color);
1266 break;
1268 default:
1269 ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
1270 return FALSE;
1273 return TRUE;
1276 HRESULT surface_load(IWineD3DSurfaceImpl *surface, BOOL srgb)
1278 DWORD flag = srgb ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
1280 TRACE("surface %p, srgb %#x.\n", surface, srgb);
1282 if (surface->resource.pool == WINED3DPOOL_SCRATCH)
1284 ERR("Not supported on scratch surfaces.\n");
1285 return WINED3DERR_INVALIDCALL;
1288 if (!(surface->flags & flag))
1290 TRACE("Reloading because surface is dirty\n");
1292 /* Reload if either the texture and sysmem have different ideas about the
1293 * color key, or the actual key values changed. */
1294 else if (!(surface->flags & SFLAG_GLCKEY) != !(surface->CKeyFlags & WINEDDSD_CKSRCBLT)
1295 || ((surface->CKeyFlags & WINEDDSD_CKSRCBLT)
1296 && (surface->glCKey.dwColorSpaceLowValue != surface->SrcBltCKey.dwColorSpaceLowValue
1297 || surface->glCKey.dwColorSpaceHighValue != surface->SrcBltCKey.dwColorSpaceHighValue)))
1299 TRACE("Reloading because of color keying\n");
1300 /* To perform the color key conversion we need a sysmem copy of
1301 * the surface. Make sure we have it. */
1303 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1304 /* Make sure the texture is reloaded because of the color key change,
1305 * this kills performance though :( */
1306 /* TODO: This is not necessarily needed with hw palettized texture support. */
1307 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1309 else
1311 TRACE("surface is already in texture\n");
1312 return WINED3D_OK;
1315 /* No partial locking for textures yet. */
1316 surface_load_location(surface, flag, NULL);
1318 if (!(surface->flags & SFLAG_DONOTFREE))
1320 HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory);
1321 surface->resource.allocatedMemory = NULL;
1322 surface->resource.heapMemory = NULL;
1323 surface_modify_location(surface, SFLAG_INSYSMEM, FALSE);
1326 return WINED3D_OK;
1329 /* Do not call while under the GL lock. */
1330 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1332 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1333 ULONG ref = InterlockedDecrement(&This->resource.ref);
1334 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
1336 if (!ref)
1338 surface_cleanup(This);
1339 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
1341 TRACE("(%p) Released.\n", This);
1342 HeapFree(GetProcessHeap(), 0, This);
1345 return ref;
1348 /* ****************************************************
1349 IWineD3DSurface IWineD3DResource parts follow
1350 **************************************************** */
1352 /* Do not call while under the GL lock. */
1353 void surface_internal_preload(IWineD3DSurfaceImpl *surface, enum WINED3DSRGB srgb)
1355 IWineD3DDeviceImpl *device = surface->resource.device;
1357 TRACE("iface %p, srgb %#x.\n", surface, srgb);
1359 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1361 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
1363 TRACE("Passing to container (%p).\n", texture);
1364 texture->baseTexture.texture_ops->texture_preload(texture, srgb);
1366 else
1368 struct wined3d_context *context = NULL;
1370 TRACE("(%p) : About to load surface\n", surface);
1372 if (!device->isInDraw) context = context_acquire(device, NULL);
1374 if (surface->resource.format->id == WINED3DFMT_P8_UINT
1375 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
1377 if (palette9_changed(surface))
1379 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1380 /* TODO: This is not necessarily needed with hw palettized texture support */
1381 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1382 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1383 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1387 surface_load(surface, srgb == SRGB_SRGB ? TRUE : FALSE);
1389 if (surface->resource.pool == WINED3DPOOL_DEFAULT)
1391 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1392 GLclampf tmp;
1393 tmp = 0.9f;
1394 ENTER_GL();
1395 glPrioritizeTextures(1, &surface->texture_name, &tmp);
1396 LEAVE_GL();
1399 if (context) context_release(context);
1403 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface)
1405 surface_internal_preload((IWineD3DSurfaceImpl *)iface, SRGB_ANY);
1408 BOOL surface_init_sysmem(IWineD3DSurfaceImpl *surface)
1410 if (!surface->resource.allocatedMemory)
1412 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1413 surface->resource.size + RESOURCE_ALIGNMENT);
1414 if (!surface->resource.heapMemory)
1416 ERR("Out of memory\n");
1417 return FALSE;
1419 surface->resource.allocatedMemory =
1420 (BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1422 else
1424 memset(surface->resource.allocatedMemory, 0, surface->resource.size);
1427 surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1429 return TRUE;
1432 /* ******************************************************
1433 IWineD3DSurface IWineD3DSurface parts follow
1434 ****************************************************** */
1436 /* Read the framebuffer back into the surface */
1437 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1439 IWineD3DDeviceImpl *device = This->resource.device;
1440 const struct wined3d_gl_info *gl_info;
1441 struct wined3d_context *context;
1442 BYTE *mem;
1443 GLint fmt;
1444 GLint type;
1445 BYTE *row, *top, *bottom;
1446 int i;
1447 BOOL bpp;
1448 RECT local_rect;
1449 BOOL srcIsUpsideDown;
1450 GLint rowLen = 0;
1451 GLint skipPix = 0;
1452 GLint skipRow = 0;
1454 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1455 static BOOL warned = FALSE;
1456 if(!warned) {
1457 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1458 warned = TRUE;
1460 return;
1463 context = context_acquire(device, This);
1464 context_apply_blit_state(context, device);
1465 gl_info = context->gl_info;
1467 ENTER_GL();
1469 /* Select the correct read buffer, and give some debug output.
1470 * There is no need to keep track of the current read buffer or reset it, every part of the code
1471 * that reads sets the read buffer as desired.
1473 if (surface_is_offscreen(This))
1475 /* Mapping the primary render target which is not on a swapchain.
1476 * Read from the back buffer. */
1477 TRACE("Mapping offscreen render target.\n");
1478 glReadBuffer(device->offscreenBuffer);
1479 srcIsUpsideDown = TRUE;
1481 else
1483 /* Onscreen surfaces are always part of a swapchain */
1484 GLenum buffer = surface_get_gl_buffer(This);
1485 TRACE("Mapping %#x buffer.\n", buffer);
1486 glReadBuffer(buffer);
1487 checkGLcall("glReadBuffer");
1488 srcIsUpsideDown = FALSE;
1491 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1492 if(!rect) {
1493 local_rect.left = 0;
1494 local_rect.top = 0;
1495 local_rect.right = This->currentDesc.Width;
1496 local_rect.bottom = This->currentDesc.Height;
1497 } else {
1498 local_rect = *rect;
1500 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1502 switch (This->resource.format->id)
1504 case WINED3DFMT_P8_UINT:
1506 if (primary_render_target_is_p8(device))
1508 /* In case of P8 render targets the index is stored in the alpha component */
1509 fmt = GL_ALPHA;
1510 type = GL_UNSIGNED_BYTE;
1511 mem = dest;
1512 bpp = This->resource.format->byte_count;
1513 } else {
1514 /* GL can't return palettized data, so read ARGB pixels into a
1515 * separate block of memory and convert them into palettized format
1516 * in software. Slow, but if the app means to use palettized render
1517 * targets and locks it...
1519 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1520 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1521 * for the color channels when palettizing the colors.
1523 fmt = GL_RGB;
1524 type = GL_UNSIGNED_BYTE;
1525 pitch *= 3;
1526 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1527 if(!mem) {
1528 ERR("Out of memory\n");
1529 LEAVE_GL();
1530 return;
1532 bpp = This->resource.format->byte_count * 3;
1535 break;
1537 default:
1538 mem = dest;
1539 fmt = This->resource.format->glFormat;
1540 type = This->resource.format->glType;
1541 bpp = This->resource.format->byte_count;
1544 if (This->flags & SFLAG_PBO)
1546 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1547 checkGLcall("glBindBufferARB");
1548 if (mem)
1550 ERR("mem not null for pbo -- unexpected\n");
1551 mem = NULL;
1555 /* Save old pixel store pack state */
1556 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1557 checkGLcall("glGetIntegerv");
1558 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1559 checkGLcall("glGetIntegerv");
1560 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1561 checkGLcall("glGetIntegerv");
1563 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1564 glPixelStorei(GL_PACK_ROW_LENGTH, This->currentDesc.Width);
1565 checkGLcall("glPixelStorei");
1566 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1567 checkGLcall("glPixelStorei");
1568 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1569 checkGLcall("glPixelStorei");
1571 glReadPixels(local_rect.left, (!srcIsUpsideDown) ? (This->currentDesc.Height - local_rect.bottom) : local_rect.top ,
1572 local_rect.right - local_rect.left,
1573 local_rect.bottom - local_rect.top,
1574 fmt, type, mem);
1575 checkGLcall("glReadPixels");
1577 /* Reset previous pixel store pack state */
1578 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1579 checkGLcall("glPixelStorei");
1580 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1581 checkGLcall("glPixelStorei");
1582 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1583 checkGLcall("glPixelStorei");
1585 if (This->flags & SFLAG_PBO)
1587 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
1588 checkGLcall("glBindBufferARB");
1590 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1591 * to get a pointer to it and perform the flipping in software. This is a lot
1592 * faster than calling glReadPixels for each line. In case we want more speed
1593 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1594 if(!srcIsUpsideDown) {
1595 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1596 checkGLcall("glBindBufferARB");
1598 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1599 checkGLcall("glMapBufferARB");
1603 /* TODO: Merge this with the palettization loop below for P8 targets */
1604 if(!srcIsUpsideDown) {
1605 UINT len, off;
1606 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1607 Flip the lines in software */
1608 len = (local_rect.right - local_rect.left) * bpp;
1609 off = local_rect.left * bpp;
1611 row = HeapAlloc(GetProcessHeap(), 0, len);
1612 if(!row) {
1613 ERR("Out of memory\n");
1614 if (This->resource.format->id == WINED3DFMT_P8_UINT) HeapFree(GetProcessHeap(), 0, mem);
1615 LEAVE_GL();
1616 return;
1619 top = mem + pitch * local_rect.top;
1620 bottom = mem + pitch * (local_rect.bottom - 1);
1621 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
1622 memcpy(row, top + off, len);
1623 memcpy(top + off, bottom + off, len);
1624 memcpy(bottom + off, row, len);
1625 top += pitch;
1626 bottom -= pitch;
1628 HeapFree(GetProcessHeap(), 0, row);
1630 /* Unmap the temp PBO buffer */
1631 if (This->flags & SFLAG_PBO)
1633 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1634 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1638 LEAVE_GL();
1639 context_release(context);
1641 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1642 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1643 * the same color but we have no choice.
1644 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1646 if (This->resource.format->id == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(device))
1648 const PALETTEENTRY *pal = NULL;
1649 DWORD width = pitch / 3;
1650 int x, y, c;
1652 if(This->palette) {
1653 pal = This->palette->palents;
1654 } else {
1655 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1656 HeapFree(GetProcessHeap(), 0, mem);
1657 return ;
1660 for(y = local_rect.top; y < local_rect.bottom; y++) {
1661 for(x = local_rect.left; x < local_rect.right; x++) {
1662 /* start lines pixels */
1663 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1664 const BYTE *green = blue + 1;
1665 const BYTE *red = green + 1;
1667 for(c = 0; c < 256; c++) {
1668 if(*red == pal[c].peRed &&
1669 *green == pal[c].peGreen &&
1670 *blue == pal[c].peBlue)
1672 *((BYTE *) dest + y * width + x) = c;
1673 break;
1678 HeapFree(GetProcessHeap(), 0, mem);
1682 /* Read the framebuffer contents into a texture */
1683 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1685 IWineD3DDeviceImpl *device = This->resource.device;
1686 const struct wined3d_gl_info *gl_info;
1687 struct wined3d_context *context;
1689 if (!surface_is_offscreen(This))
1691 /* We would need to flip onscreen surfaces, but there's no efficient
1692 * way to do that here. It makes more sense for the caller to
1693 * explicitly go through sysmem. */
1694 ERR("Not supported for onscreen targets.\n");
1695 return;
1698 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1699 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1700 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1702 context = context_acquire(device, This);
1703 gl_info = context->gl_info;
1705 surface_prepare_texture(This, gl_info, srgb);
1706 surface_bind_and_dirtify(This, srgb);
1708 TRACE("Reading back offscreen render target %p.\n", This);
1710 ENTER_GL();
1712 glReadBuffer(device->offscreenBuffer);
1713 checkGLcall("glReadBuffer");
1715 glCopyTexSubImage2D(This->texture_target, This->texture_level,
1716 0, 0, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1717 checkGLcall("glCopyTexSubImage2D");
1719 LEAVE_GL();
1721 context_release(context);
1724 /* Context activation is done by the caller. */
1725 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl *surface,
1726 const struct wined3d_gl_info *gl_info, BOOL srgb)
1728 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1729 CONVERT_TYPES convert;
1730 struct wined3d_format format;
1732 if (surface->flags & alloc_flag) return;
1734 d3dfmt_get_conv(surface, TRUE, TRUE, &format, &convert);
1735 if (convert != NO_CONVERSION || format.convert) surface->flags |= SFLAG_CONVERTED;
1736 else surface->flags &= ~SFLAG_CONVERTED;
1738 surface_bind_and_dirtify(surface, srgb);
1739 surface_allocate_surface(surface, gl_info, &format, srgb);
1740 surface->flags |= alloc_flag;
1743 /* Context activation is done by the caller. */
1744 void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1746 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1748 IWineD3DBaseTextureImpl *texture = surface->container.u.texture;
1749 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
1750 UINT i;
1752 TRACE("surface %p is a subresource of texture %p.\n", surface, texture);
1754 for (i = 0; i < sub_count; ++i)
1756 IWineD3DSurfaceImpl *s = surface_from_resource(texture->baseTexture.sub_resources[i]);
1757 surface_prepare_texture_internal(s, gl_info, srgb);
1760 return;
1763 surface_prepare_texture_internal(surface, gl_info, srgb);
1766 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1768 IWineD3DDeviceImpl *device = This->resource.device;
1769 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1771 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1772 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1773 * changed
1775 if (!(This->flags & SFLAG_DYNLOCK))
1777 This->lockCount++;
1778 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1779 if(This->lockCount > MAXLOCKCOUNT) {
1780 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1781 This->flags |= SFLAG_DYNLOCK;
1785 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1786 * Also don't create a PBO for systemmem surfaces.
1788 if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->flags & SFLAG_DYNLOCK)
1789 && !(This->flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1790 && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1792 GLenum error;
1793 struct wined3d_context *context;
1795 context = context_acquire(device, NULL);
1796 ENTER_GL();
1798 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1799 error = glGetError();
1800 if (!This->pbo || error != GL_NO_ERROR)
1801 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1803 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1805 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1806 checkGLcall("glBindBufferARB");
1808 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1809 checkGLcall("glBufferDataARB");
1811 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1812 checkGLcall("glBindBufferARB");
1814 /* We don't need the system memory anymore and we can't even use it for PBOs */
1815 if (!(This->flags & SFLAG_CLIENT))
1817 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1818 This->resource.heapMemory = NULL;
1820 This->resource.allocatedMemory = NULL;
1821 This->flags |= SFLAG_PBO;
1822 LEAVE_GL();
1823 context_release(context);
1825 else if (!(This->resource.allocatedMemory || This->flags & SFLAG_PBO))
1827 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1828 * or a pbo to map
1830 if(!This->resource.heapMemory) {
1831 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1833 This->resource.allocatedMemory =
1834 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1835 if (This->flags & SFLAG_INSYSMEM)
1837 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1842 static HRESULT WINAPI IWineD3DSurfaceImpl_Map(IWineD3DSurface *iface,
1843 WINED3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD flags)
1845 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1846 IWineD3DDeviceImpl *device = This->resource.device;
1847 const RECT *pass_rect = pRect;
1849 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1850 iface, pLockedRect, wine_dbgstr_rect(pRect), flags);
1852 /* This is also done in the base class, but we have to verify this before loading any data from
1853 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1854 * may interfere, and all other bad things may happen
1856 if (This->flags & SFLAG_LOCKED)
1858 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1859 return WINED3DERR_INVALIDCALL;
1861 This->flags |= SFLAG_LOCKED;
1863 if (!(This->flags & SFLAG_LOCKABLE))
1865 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1868 if (flags & WINED3DLOCK_DISCARD)
1870 TRACE("WINED3DLOCK_DISCARD flag passed, marking SYSMEM as up to date.\n");
1871 surface_prepare_system_memory(This);
1872 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
1873 goto lock_end;
1876 /* surface_load_location() does not check if the rectangle specifies
1877 * the full surface. Most callers don't need that, so do it here. */
1878 if (pRect && !pRect->top && !pRect->left
1879 && pRect->right == This->currentDesc.Width
1880 && pRect->bottom == This->currentDesc.Height)
1882 pass_rect = NULL;
1885 if (!(wined3d_settings.rendertargetlock_mode == RTL_DISABLE
1886 && ((This->container.type == WINED3D_CONTAINER_SWAPCHAIN) || This == device->render_targets[0])))
1888 surface_load_location(This, SFLAG_INSYSMEM, pass_rect);
1891 lock_end:
1892 if (This->flags & SFLAG_PBO)
1894 const struct wined3d_gl_info *gl_info;
1895 struct wined3d_context *context;
1897 context = context_acquire(device, NULL);
1898 gl_info = context->gl_info;
1900 ENTER_GL();
1901 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1902 checkGLcall("glBindBufferARB");
1904 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1905 if(This->resource.allocatedMemory) {
1906 ERR("The surface already has PBO memory allocated!\n");
1909 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1910 checkGLcall("glMapBufferARB");
1912 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1913 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1914 checkGLcall("glBindBufferARB");
1916 LEAVE_GL();
1917 context_release(context);
1920 if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
1921 surface_add_dirty_rect(This, pRect);
1923 return IWineD3DBaseSurfaceImpl_Map(iface, pLockedRect, pRect, flags);
1926 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This,
1927 GLenum fmt, GLenum type, UINT bpp, const BYTE *mem)
1929 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1930 IWineD3DDeviceImpl *device = This->resource.device;
1931 const struct wined3d_gl_info *gl_info;
1932 struct wined3d_context *context;
1933 RECT rect;
1934 UINT w, h;
1936 if (This->flags & SFLAG_LOCKED)
1937 rect = This->lockedRect;
1938 else
1939 SetRect(&rect, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1941 mem += rect.top * pitch + rect.left * bpp;
1942 w = rect.right - rect.left;
1943 h = rect.bottom - rect.top;
1945 /* Activate the correct context for the render target */
1946 context = context_acquire(device, This);
1947 context_apply_blit_state(context, device);
1948 gl_info = context->gl_info;
1950 ENTER_GL();
1952 if (!surface_is_offscreen(This))
1954 GLenum buffer = surface_get_gl_buffer(This);
1955 TRACE("Unlocking %#x buffer.\n", buffer);
1956 context_set_draw_buffer(context, buffer);
1958 surface_translate_drawable_coords(This, context->win_handle, &rect);
1959 glPixelZoom(1.0f, -1.0f);
1961 else
1963 /* Primary offscreen render target */
1964 TRACE("Offscreen render target.\n");
1965 context_set_draw_buffer(context, device->offscreenBuffer);
1967 glPixelZoom(1.0f, 1.0f);
1970 glRasterPos3i(rect.left, rect.top, 1);
1971 checkGLcall("glRasterPos3i");
1973 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1974 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1976 if (This->flags & SFLAG_PBO)
1978 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1979 checkGLcall("glBindBufferARB");
1982 glDrawPixels(w, h, fmt, type, mem);
1983 checkGLcall("glDrawPixels");
1985 if (This->flags & SFLAG_PBO)
1987 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1988 checkGLcall("glBindBufferARB");
1991 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1992 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)");
1994 LEAVE_GL();
1995 context_release(context);
1998 static HRESULT WINAPI IWineD3DSurfaceImpl_Unmap(IWineD3DSurface *iface)
2000 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2001 IWineD3DDeviceImpl *device = This->resource.device;
2002 BOOL fullsurface;
2004 if (!(This->flags & SFLAG_LOCKED))
2006 WARN("trying to Unlock an unlocked surf@%p\n", This);
2007 return WINEDDERR_NOTLOCKED;
2010 if (This->flags & SFLAG_PBO)
2012 const struct wined3d_gl_info *gl_info;
2013 struct wined3d_context *context;
2015 TRACE("Freeing PBO memory\n");
2017 context = context_acquire(device, NULL);
2018 gl_info = context->gl_info;
2020 ENTER_GL();
2021 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
2022 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
2023 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
2024 checkGLcall("glUnmapBufferARB");
2025 LEAVE_GL();
2026 context_release(context);
2028 This->resource.allocatedMemory = NULL;
2031 TRACE("(%p) : dirtyfied(%d)\n", This, This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
2033 if (This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE))
2035 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
2036 goto unlock_end;
2039 if (This->container.type == WINED3D_CONTAINER_SWAPCHAIN
2040 || (device->render_targets && This == device->render_targets[0]))
2042 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
2043 static BOOL warned = FALSE;
2044 if(!warned) {
2045 ERR("The application tries to write to the render target, but render target locking is disabled\n");
2046 warned = TRUE;
2048 goto unlock_end;
2051 if (!This->dirtyRect.left && !This->dirtyRect.top
2052 && This->dirtyRect.right == This->currentDesc.Width
2053 && This->dirtyRect.bottom == This->currentDesc.Height)
2055 fullsurface = TRUE;
2056 } else {
2057 /* TODO: Proper partial rectangle tracking */
2058 fullsurface = FALSE;
2059 This->flags |= SFLAG_INSYSMEM;
2062 surface_load_location(This, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
2064 /* Partial rectangle tracking is not commonly implemented, it is only
2065 * done for render targets. INSYSMEM was set before to tell
2066 * surface_load_location() where to read the rectangle from.
2067 * Indrawable is set because all modifications from the partial
2068 * sysmem copy are written back to the drawable, thus the surface is
2069 * merged again in the drawable. The sysmem copy is not fully up to
2070 * date because only a subrectangle was read in Map(). */
2071 if (!fullsurface)
2072 surface_modify_location(This, SFLAG_INDRAWABLE, TRUE);
2074 This->dirtyRect.left = This->currentDesc.Width;
2075 This->dirtyRect.top = This->currentDesc.Height;
2076 This->dirtyRect.right = 0;
2077 This->dirtyRect.bottom = 0;
2079 else if (This->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2081 FIXME("Depth Stencil buffer locking is not implemented\n");
2084 unlock_end:
2085 This->flags &= ~SFLAG_LOCKED;
2086 memset(&This->lockedRect, 0, sizeof(RECT));
2088 /* Overlays have to be redrawn manually after changes with the GL implementation */
2089 if (This->overlay_dest)
2090 This->surface_ops->surface_draw_overlay(This);
2092 return WINED3D_OK;
2095 static void surface_release_client_storage(IWineD3DSurfaceImpl *surface)
2097 struct wined3d_context *context;
2099 context = context_acquire(surface->resource.device, NULL);
2101 ENTER_GL();
2102 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
2103 if (surface->texture_name)
2105 surface_bind_and_dirtify(surface, FALSE);
2106 glTexImage2D(surface->texture_target, surface->texture_level,
2107 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2109 if (surface->texture_name_srgb)
2111 surface_bind_and_dirtify(surface, TRUE);
2112 glTexImage2D(surface->texture_target, surface->texture_level,
2113 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2115 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
2117 LEAVE_GL();
2118 context_release(context);
2120 surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
2121 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
2122 surface_force_reload(surface);
2125 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
2127 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2128 WINED3DLOCKED_RECT lock;
2129 HRESULT hr;
2130 RGBQUAD col[256];
2132 TRACE("(%p)->(%p)\n",This,pHDC);
2134 if (This->flags & SFLAG_USERPTR)
2136 ERR("Not supported on surfaces with an application-provided surfaces\n");
2137 return WINEDDERR_NODC;
2140 /* Give more detailed info for ddraw */
2141 if (This->flags & SFLAG_DCINUSE)
2142 return WINEDDERR_DCALREADYCREATED;
2144 /* Can't GetDC if the surface is locked */
2145 if (This->flags & SFLAG_LOCKED)
2146 return WINED3DERR_INVALIDCALL;
2148 memset(&lock, 0, sizeof(lock)); /* To be sure */
2150 /* Create a DIB section if there isn't a hdc yet */
2151 if (!This->hDC)
2153 if (This->flags & SFLAG_CLIENT)
2155 surface_load_location(This, SFLAG_INSYSMEM, NULL);
2156 surface_release_client_storage(This);
2158 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
2159 if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2161 /* Use the dib section from now on if we are not using a PBO */
2162 if (!(This->flags & SFLAG_PBO))
2163 This->resource.allocatedMemory = This->dib.bitmap_data;
2166 /* Map the surface */
2167 hr = IWineD3DSurface_Map(iface, &lock, NULL, 0);
2169 /* Sync the DIB with the PBO. This can't be done earlier because Map()
2170 * activates the allocatedMemory. */
2171 if (This->flags & SFLAG_PBO)
2172 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2174 if (FAILED(hr))
2176 ERR("IWineD3DSurface_Map failed, hr %#x.\n", hr);
2177 /* keep the dib section */
2178 return hr;
2181 if (This->resource.format->id == WINED3DFMT_P8_UINT
2182 || This->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
2184 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2185 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2186 unsigned int n;
2187 const PALETTEENTRY *pal = NULL;
2189 if(This->palette) {
2190 pal = This->palette->palents;
2191 } else {
2192 IWineD3DSurfaceImpl *dds_primary;
2193 IWineD3DSwapChainImpl *swapchain;
2194 swapchain = This->resource.device->swapchains[0];
2195 dds_primary = swapchain->front_buffer;
2196 if (dds_primary && dds_primary->palette)
2197 pal = dds_primary->palette->palents;
2200 if (pal) {
2201 for (n=0; n<256; n++) {
2202 col[n].rgbRed = pal[n].peRed;
2203 col[n].rgbGreen = pal[n].peGreen;
2204 col[n].rgbBlue = pal[n].peBlue;
2205 col[n].rgbReserved = 0;
2207 SetDIBColorTable(This->hDC, 0, 256, col);
2211 *pHDC = This->hDC;
2212 TRACE("returning %p\n",*pHDC);
2213 This->flags |= SFLAG_DCINUSE;
2215 return WINED3D_OK;
2218 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2220 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2222 TRACE("(%p)->(%p)\n",This,hDC);
2224 if (!(This->flags & SFLAG_DCINUSE))
2225 return WINEDDERR_NODC;
2227 if (This->hDC !=hDC) {
2228 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2229 return WINEDDERR_NODC;
2232 if ((This->flags & SFLAG_PBO) && This->resource.allocatedMemory)
2234 /* Copy the contents of the DIB over to the PBO */
2235 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2238 /* we locked first, so unlock now */
2239 IWineD3DSurface_Unmap(iface);
2241 This->flags &= ~SFLAG_DCINUSE;
2243 return WINED3D_OK;
2246 /* ******************************************************
2247 IWineD3DSurface Internal (No mapping to directx api) parts follow
2248 ****************************************************** */
2250 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck,
2251 BOOL use_texturing, struct wined3d_format *format, CONVERT_TYPES *convert)
2253 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2254 IWineD3DDeviceImpl *device = This->resource.device;
2255 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2256 BOOL blit_supported = FALSE;
2258 /* Copy the default values from the surface. Below we might perform fixups */
2259 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2260 *format = *This->resource.format;
2261 *convert = NO_CONVERSION;
2263 /* Ok, now look if we have to do any conversion */
2264 switch (This->resource.format->id)
2266 case WINED3DFMT_P8_UINT:
2267 /* Below the call to blit_supported is disabled for Wine 1.2
2268 * because the function isn't operating correctly yet. At the
2269 * moment 8-bit blits are handled in software and if certain GL
2270 * extensions are around, surface conversion is performed at
2271 * upload time. The blit_supported call recognizes it as a
2272 * destination fixup. This type of upload 'fixup' and 8-bit to
2273 * 8-bit blits need to be handled by the blit_shader.
2274 * TODO: get rid of this #if 0. */
2275 #if 0
2276 blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
2277 &rect, This->resource.usage, This->resource.pool, This->resource.format,
2278 &rect, This->resource.usage, This->resource.pool, This->resource.format);
2279 #endif
2280 blit_supported = gl_info->supported[EXT_PALETTED_TEXTURE] || gl_info->supported[ARB_FRAGMENT_PROGRAM];
2282 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2283 * texturing. Further also use conversion in case of color keying.
2284 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2285 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2286 * conflicts with this.
2288 if (!((blit_supported && device->render_targets && This == device->render_targets[0]))
2289 || colorkey_active || !use_texturing)
2291 format->glFormat = GL_RGBA;
2292 format->glInternal = GL_RGBA;
2293 format->glType = GL_UNSIGNED_BYTE;
2294 format->conv_byte_count = 4;
2295 if (colorkey_active)
2296 *convert = CONVERT_PALETTED_CK;
2297 else
2298 *convert = CONVERT_PALETTED;
2300 break;
2302 case WINED3DFMT_B2G3R3_UNORM:
2303 /* **********************
2304 GL_UNSIGNED_BYTE_3_3_2
2305 ********************** */
2306 if (colorkey_active) {
2307 /* This texture format will never be used.. So do not care about color keying
2308 up until the point in time it will be needed :-) */
2309 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2311 break;
2313 case WINED3DFMT_B5G6R5_UNORM:
2314 if (colorkey_active)
2316 *convert = CONVERT_CK_565;
2317 format->glFormat = GL_RGBA;
2318 format->glInternal = GL_RGB5_A1;
2319 format->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2320 format->conv_byte_count = 2;
2322 break;
2324 case WINED3DFMT_B5G5R5X1_UNORM:
2325 if (colorkey_active)
2327 *convert = CONVERT_CK_5551;
2328 format->glFormat = GL_BGRA;
2329 format->glInternal = GL_RGB5_A1;
2330 format->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2331 format->conv_byte_count = 2;
2333 break;
2335 case WINED3DFMT_B8G8R8_UNORM:
2336 if (colorkey_active)
2338 *convert = CONVERT_CK_RGB24;
2339 format->glFormat = GL_RGBA;
2340 format->glInternal = GL_RGBA8;
2341 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2342 format->conv_byte_count = 4;
2344 break;
2346 case WINED3DFMT_B8G8R8X8_UNORM:
2347 if (colorkey_active)
2349 *convert = CONVERT_RGB32_888;
2350 format->glFormat = GL_RGBA;
2351 format->glInternal = GL_RGBA8;
2352 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2353 format->conv_byte_count = 4;
2355 break;
2357 default:
2358 break;
2361 return WINED3D_OK;
2364 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2366 IWineD3DDeviceImpl *device = This->resource.device;
2367 struct wined3d_palette *pal = This->palette;
2368 BOOL index_in_alpha = FALSE;
2369 unsigned int i;
2371 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2372 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2373 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2374 * duplicate entries. Store the color key in the unused alpha component to speed the
2375 * download up and to make conversion unneeded. */
2376 index_in_alpha = primary_render_target_is_p8(device);
2378 if (!pal)
2380 UINT dxVersion = device->wined3d->dxVersion;
2382 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2383 if (dxVersion <= 7)
2385 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2386 if (index_in_alpha)
2388 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2389 * there's no palette at this time. */
2390 for (i = 0; i < 256; i++) table[i][3] = i;
2393 else
2395 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2396 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2397 * capability flag is present (wine does advertise this capability) */
2398 for (i = 0; i < 256; ++i)
2400 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2401 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2402 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2403 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2407 else
2409 TRACE("Using surface palette %p\n", pal);
2410 /* Get the surface's palette */
2411 for (i = 0; i < 256; ++i)
2413 table[i][0] = pal->palents[i].peRed;
2414 table[i][1] = pal->palents[i].peGreen;
2415 table[i][2] = pal->palents[i].peBlue;
2417 /* When index_in_alpha is set the palette index is stored in the
2418 * alpha component. In case of a readback we can then read
2419 * GL_ALPHA. Color keying is handled in BltOverride using a
2420 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2421 * color key itself is passed to glAlphaFunc in other cases the
2422 * alpha component of pixels that should be masked away is set to 0. */
2423 if (index_in_alpha)
2425 table[i][3] = i;
2427 else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2428 && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2430 table[i][3] = 0x00;
2432 else if (pal->flags & WINEDDPCAPS_ALPHA)
2434 table[i][3] = pal->palents[i].peFlags;
2436 else
2438 table[i][3] = 0xFF;
2444 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2445 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2447 const BYTE *source;
2448 BYTE *dest;
2449 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2451 switch (convert) {
2452 case NO_CONVERSION:
2454 memcpy(dst, src, pitch * height);
2455 break;
2457 case CONVERT_PALETTED:
2458 case CONVERT_PALETTED_CK:
2460 BYTE table[256][4];
2461 unsigned int x, y;
2463 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2465 for (y = 0; y < height; y++)
2467 source = src + pitch * y;
2468 dest = dst + outpitch * y;
2469 /* This is an 1 bpp format, using the width here is fine */
2470 for (x = 0; x < width; x++) {
2471 BYTE color = *source++;
2472 *dest++ = table[color][0];
2473 *dest++ = table[color][1];
2474 *dest++ = table[color][2];
2475 *dest++ = table[color][3];
2479 break;
2481 case CONVERT_CK_565:
2483 /* Converting the 565 format in 5551 packed to emulate color-keying.
2485 Note : in all these conversion, it would be best to average the averaging
2486 pixels to get the color of the pixel that will be color-keyed to
2487 prevent 'color bleeding'. This will be done later on if ever it is
2488 too visible.
2490 Note2: Nvidia documents say that their driver does not support alpha + color keying
2491 on the same surface and disables color keying in such a case
2493 unsigned int x, y;
2494 const WORD *Source;
2495 WORD *Dest;
2497 TRACE("Color keyed 565\n");
2499 for (y = 0; y < height; y++) {
2500 Source = (const WORD *)(src + y * pitch);
2501 Dest = (WORD *) (dst + y * outpitch);
2502 for (x = 0; x < width; x++ ) {
2503 WORD color = *Source++;
2504 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2505 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2506 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2507 *Dest |= 0x0001;
2509 Dest++;
2513 break;
2515 case CONVERT_CK_5551:
2517 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2518 unsigned int x, y;
2519 const WORD *Source;
2520 WORD *Dest;
2521 TRACE("Color keyed 5551\n");
2522 for (y = 0; y < height; y++) {
2523 Source = (const WORD *)(src + y * pitch);
2524 Dest = (WORD *) (dst + y * outpitch);
2525 for (x = 0; x < width; x++ ) {
2526 WORD color = *Source++;
2527 *Dest = color;
2528 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2529 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2530 *Dest |= (1 << 15);
2532 else {
2533 *Dest &= ~(1 << 15);
2535 Dest++;
2539 break;
2541 case CONVERT_CK_RGB24:
2543 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2544 unsigned int x, y;
2545 for (y = 0; y < height; y++)
2547 source = src + pitch * y;
2548 dest = dst + outpitch * y;
2549 for (x = 0; x < width; x++) {
2550 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2551 DWORD dstcolor = color << 8;
2552 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2553 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2554 dstcolor |= 0xff;
2556 *(DWORD*)dest = dstcolor;
2557 source += 3;
2558 dest += 4;
2562 break;
2564 case CONVERT_RGB32_888:
2566 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2567 unsigned int x, y;
2568 for (y = 0; y < height; y++)
2570 source = src + pitch * y;
2571 dest = dst + outpitch * y;
2572 for (x = 0; x < width; x++) {
2573 DWORD color = 0xffffff & *(const DWORD*)source;
2574 DWORD dstcolor = color << 8;
2575 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2576 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2577 dstcolor |= 0xff;
2579 *(DWORD*)dest = dstcolor;
2580 source += 4;
2581 dest += 4;
2585 break;
2587 default:
2588 ERR("Unsupported conversion type %#x.\n", convert);
2590 return WINED3D_OK;
2593 BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2595 IWineD3DDeviceImpl *device = This->resource.device;
2597 if (This->palette || (This->resource.format->id != WINED3DFMT_P8_UINT
2598 && This->resource.format->id != WINED3DFMT_P8_UINT_A8_UNORM))
2600 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2601 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2603 return FALSE;
2606 if (This->palette9)
2608 if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2610 return FALSE;
2612 } else {
2613 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2615 memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2616 return TRUE;
2619 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, enum wined3d_format_id format)
2621 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2622 HRESULT hr;
2624 TRACE("(%p) : Calling base function first\n", This);
2625 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2626 if (SUCCEEDED(hr))
2628 This->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2629 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format->glFormat,
2630 This->resource.format->glInternal, This->resource.format->glType);
2632 return hr;
2635 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2636 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2638 TRACE("iface %p, mem %p.\n", iface, Mem);
2640 if (This->flags & (SFLAG_LOCKED | SFLAG_DCINUSE))
2642 WARN("Surface is locked or the HDC is in use\n");
2643 return WINED3DERR_INVALIDCALL;
2646 if(Mem && Mem != This->resource.allocatedMemory) {
2647 void *release = NULL;
2649 /* Do I have to copy the old surface content? */
2650 if (This->flags & SFLAG_DIBSECTION)
2652 SelectObject(This->hDC, This->dib.holdbitmap);
2653 DeleteDC(This->hDC);
2654 /* Release the DIB section */
2655 DeleteObject(This->dib.DIBsection);
2656 This->dib.bitmap_data = NULL;
2657 This->resource.allocatedMemory = NULL;
2658 This->hDC = NULL;
2659 This->flags &= ~SFLAG_DIBSECTION;
2661 else if (!(This->flags & SFLAG_USERPTR))
2663 release = This->resource.heapMemory;
2664 This->resource.heapMemory = NULL;
2666 This->resource.allocatedMemory = Mem;
2667 This->flags |= SFLAG_USERPTR;
2669 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2670 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2672 /* For client textures opengl has to be notified */
2673 if (This->flags & SFLAG_CLIENT)
2674 surface_release_client_storage(This);
2676 /* Now free the old memory if any */
2677 HeapFree(GetProcessHeap(), 0, release);
2679 else if (This->flags & SFLAG_USERPTR)
2681 /* Map and GetDC will re-create the dib section and allocated memory. */
2682 This->resource.allocatedMemory = NULL;
2683 /* HeapMemory should be NULL already */
2684 if (This->resource.heapMemory)
2685 ERR("User pointer surface has heap memory allocated.\n");
2686 This->flags &= ~(SFLAG_USERPTR | SFLAG_INSYSMEM);
2688 if (This->flags & SFLAG_CLIENT)
2689 surface_release_client_storage(This);
2691 surface_prepare_system_memory(This);
2692 surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2694 return WINED3D_OK;
2697 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2699 /* Flip the surface contents */
2700 /* Flip the DC */
2702 HDC tmp;
2703 tmp = front->hDC;
2704 front->hDC = back->hDC;
2705 back->hDC = tmp;
2708 /* Flip the DIBsection */
2710 HBITMAP tmp;
2711 BOOL hasDib = front->flags & SFLAG_DIBSECTION;
2712 tmp = front->dib.DIBsection;
2713 front->dib.DIBsection = back->dib.DIBsection;
2714 back->dib.DIBsection = tmp;
2716 if (back->flags & SFLAG_DIBSECTION) front->flags |= SFLAG_DIBSECTION;
2717 else front->flags &= ~SFLAG_DIBSECTION;
2718 if (hasDib) back->flags |= SFLAG_DIBSECTION;
2719 else back->flags &= ~SFLAG_DIBSECTION;
2722 /* Flip the surface data */
2724 void* tmp;
2726 tmp = front->dib.bitmap_data;
2727 front->dib.bitmap_data = back->dib.bitmap_data;
2728 back->dib.bitmap_data = tmp;
2730 tmp = front->resource.allocatedMemory;
2731 front->resource.allocatedMemory = back->resource.allocatedMemory;
2732 back->resource.allocatedMemory = tmp;
2734 tmp = front->resource.heapMemory;
2735 front->resource.heapMemory = back->resource.heapMemory;
2736 back->resource.heapMemory = tmp;
2739 /* Flip the PBO */
2741 GLuint tmp_pbo = front->pbo;
2742 front->pbo = back->pbo;
2743 back->pbo = tmp_pbo;
2746 /* client_memory should not be different, but just in case */
2748 BOOL tmp;
2749 tmp = front->dib.client_memory;
2750 front->dib.client_memory = back->dib.client_memory;
2751 back->dib.client_memory = tmp;
2754 /* Flip the opengl texture */
2756 GLuint tmp;
2758 tmp = back->texture_name;
2759 back->texture_name = front->texture_name;
2760 front->texture_name = tmp;
2762 tmp = back->texture_name_srgb;
2763 back->texture_name_srgb = front->texture_name_srgb;
2764 front->texture_name_srgb = tmp;
2766 resource_unload(&back->resource);
2767 resource_unload(&front->resource);
2771 DWORD tmp_flags = back->flags;
2772 back->flags = front->flags;
2773 front->flags = tmp_flags;
2777 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD flags)
2779 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2780 IWineD3DSwapChainImpl *swapchain = NULL;
2782 TRACE("iface %p, override %p, flags %#x.\n", iface, override, flags);
2784 /* Flipping is only supported on RenderTargets and overlays*/
2785 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2786 WARN("Tried to flip a non-render target, non-overlay surface\n");
2787 return WINEDDERR_NOTFLIPPABLE;
2790 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2791 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2793 /* Update the overlay if it is visible */
2794 if (This->overlay_dest)
2795 return This->surface_ops->surface_draw_overlay(This);
2796 else
2797 return WINED3D_OK;
2800 if(override) {
2801 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2802 * FIXME("(%p) Target override is not supported by now\n", This);
2803 * Additionally, it isn't really possible to support triple-buffering
2804 * properly on opengl at all
2808 if (This->container.type != WINED3D_CONTAINER_SWAPCHAIN)
2810 ERR("Flipped surface is not on a swapchain\n");
2811 return WINEDDERR_NOTFLIPPABLE;
2813 swapchain = This->container.u.swapchain;
2815 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2816 * and only d3d8 and d3d9 apps specify the presentation interval
2818 if (!(flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)))
2819 /* Most common case first to avoid wasting time on all the other cases */
2820 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2821 else if (flags & WINEDDFLIP_NOVSYNC)
2822 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2823 else if (flags & WINEDDFLIP_INTERVAL2)
2824 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2825 else if (flags & WINEDDFLIP_INTERVAL3)
2826 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2827 else
2828 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2830 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2831 return IWineD3DSwapChain_Present((IWineD3DSwapChain *)swapchain,
2832 NULL, NULL, swapchain->win_handle, NULL, 0);
2835 /* Does a direct frame buffer -> texture copy. Stretching is done
2836 * with single pixel copy calls
2838 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2839 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2841 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2842 float xrel, yrel;
2843 UINT row;
2844 struct wined3d_context *context;
2845 BOOL upsidedown = FALSE;
2846 RECT dst_rect = *dst_rect_in;
2848 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2849 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2851 if(dst_rect.top > dst_rect.bottom) {
2852 UINT tmp = dst_rect.bottom;
2853 dst_rect.bottom = dst_rect.top;
2854 dst_rect.top = tmp;
2855 upsidedown = TRUE;
2858 context = context_acquire(device, src_surface);
2859 context_apply_blit_state(context, device);
2860 surface_internal_preload(dst_surface, SRGB_RGB);
2861 ENTER_GL();
2863 /* Bind the target texture */
2864 glBindTexture(dst_surface->texture_target, dst_surface->texture_name);
2865 checkGLcall("glBindTexture");
2866 if (surface_is_offscreen(src_surface))
2868 TRACE("Reading from an offscreen target\n");
2869 upsidedown = !upsidedown;
2870 glReadBuffer(device->offscreenBuffer);
2872 else
2874 glReadBuffer(surface_get_gl_buffer(src_surface));
2876 checkGLcall("glReadBuffer");
2878 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
2879 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
2881 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2883 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2885 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2886 ERR("Texture filtering not supported in direct blit\n");
2889 else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
2890 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2892 ERR("Texture filtering not supported in direct blit\n");
2895 if (upsidedown
2896 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2897 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2899 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2901 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2902 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
2903 src_rect->left, src_surface->currentDesc.Height - src_rect->bottom,
2904 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
2905 } else {
2906 UINT yoffset = src_surface->currentDesc.Height - src_rect->top + dst_rect.top - 1;
2907 /* I have to process this row by row to swap the image,
2908 * otherwise it would be upside down, so stretching in y direction
2909 * doesn't cost extra time
2911 * However, stretching in x direction can be avoided if not necessary
2913 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
2914 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2916 /* Well, that stuff works, but it's very slow.
2917 * find a better way instead
2919 UINT col;
2921 for (col = dst_rect.left; col < dst_rect.right; ++col)
2923 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2924 dst_rect.left + col /* x offset */, row /* y offset */,
2925 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
2928 else
2930 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2931 dst_rect.left /* x offset */, row /* y offset */,
2932 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
2936 checkGLcall("glCopyTexSubImage2D");
2938 LEAVE_GL();
2939 context_release(context);
2941 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2942 * path is never entered
2944 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
2947 /* Uses the hardware to stretch and flip the image */
2948 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2949 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2951 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2952 GLuint src, backup = 0;
2953 IWineD3DSwapChainImpl *src_swapchain = NULL;
2954 float left, right, top, bottom; /* Texture coordinates */
2955 UINT fbwidth = src_surface->currentDesc.Width;
2956 UINT fbheight = src_surface->currentDesc.Height;
2957 struct wined3d_context *context;
2958 GLenum drawBuffer = GL_BACK;
2959 GLenum texture_target;
2960 BOOL noBackBufferBackup;
2961 BOOL src_offscreen;
2962 BOOL upsidedown = FALSE;
2963 RECT dst_rect = *dst_rect_in;
2965 TRACE("Using hwstretch blit\n");
2966 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2967 context = context_acquire(device, src_surface);
2968 context_apply_blit_state(context, device);
2969 surface_internal_preload(dst_surface, SRGB_RGB);
2971 src_offscreen = surface_is_offscreen(src_surface);
2972 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2973 if (!noBackBufferBackup && !src_surface->texture_name)
2975 /* Get it a description */
2976 surface_internal_preload(src_surface, SRGB_RGB);
2978 ENTER_GL();
2980 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2981 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2983 if (context->aux_buffers >= 2)
2985 /* Got more than one aux buffer? Use the 2nd aux buffer */
2986 drawBuffer = GL_AUX1;
2988 else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
2990 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2991 drawBuffer = GL_AUX0;
2994 if(noBackBufferBackup) {
2995 glGenTextures(1, &backup);
2996 checkGLcall("glGenTextures");
2997 glBindTexture(GL_TEXTURE_2D, backup);
2998 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2999 texture_target = GL_TEXTURE_2D;
3000 } else {
3001 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
3002 * we are reading from the back buffer, the backup can be used as source texture
3004 texture_target = src_surface->texture_target;
3005 glBindTexture(texture_target, src_surface->texture_name);
3006 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
3007 glEnable(texture_target);
3008 checkGLcall("glEnable(texture_target)");
3010 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3011 src_surface->flags &= ~SFLAG_INTEXTURE;
3014 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3015 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3017 if(dst_rect.top > dst_rect.bottom) {
3018 UINT tmp = dst_rect.bottom;
3019 dst_rect.bottom = dst_rect.top;
3020 dst_rect.top = tmp;
3021 upsidedown = TRUE;
3024 if (src_offscreen)
3026 TRACE("Reading from an offscreen target\n");
3027 upsidedown = !upsidedown;
3028 glReadBuffer(device->offscreenBuffer);
3030 else
3032 glReadBuffer(surface_get_gl_buffer(src_surface));
3035 /* TODO: Only back up the part that will be overwritten */
3036 glCopyTexSubImage2D(texture_target, 0,
3037 0, 0 /* read offsets */,
3038 0, 0,
3039 fbwidth,
3040 fbheight);
3042 checkGLcall("glCopyTexSubImage2D");
3044 /* No issue with overriding these - the sampler is dirty due to blit usage */
3045 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3046 wined3d_gl_mag_filter(magLookup, Filter));
3047 checkGLcall("glTexParameteri");
3048 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3049 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3050 checkGLcall("glTexParameteri");
3052 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3053 src_swapchain = src_surface->container.u.swapchain;
3054 if (!src_swapchain || src_surface == src_swapchain->back_buffers[0])
3056 src = backup ? backup : src_surface->texture_name;
3058 else
3060 glReadBuffer(GL_FRONT);
3061 checkGLcall("glReadBuffer(GL_FRONT)");
3063 glGenTextures(1, &src);
3064 checkGLcall("glGenTextures(1, &src)");
3065 glBindTexture(GL_TEXTURE_2D, src);
3066 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3068 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3069 * out for power of 2 sizes
3071 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3072 src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3073 checkGLcall("glTexImage2D");
3074 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3075 0, 0 /* read offsets */,
3076 0, 0,
3077 fbwidth,
3078 fbheight);
3080 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3081 checkGLcall("glTexParameteri");
3082 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3083 checkGLcall("glTexParameteri");
3085 glReadBuffer(GL_BACK);
3086 checkGLcall("glReadBuffer(GL_BACK)");
3088 if(texture_target != GL_TEXTURE_2D) {
3089 glDisable(texture_target);
3090 glEnable(GL_TEXTURE_2D);
3091 texture_target = GL_TEXTURE_2D;
3094 checkGLcall("glEnd and previous");
3096 left = src_rect->left;
3097 right = src_rect->right;
3099 if (!upsidedown)
3101 top = src_surface->currentDesc.Height - src_rect->top;
3102 bottom = src_surface->currentDesc.Height - src_rect->bottom;
3104 else
3106 top = src_surface->currentDesc.Height - src_rect->bottom;
3107 bottom = src_surface->currentDesc.Height - src_rect->top;
3110 if (src_surface->flags & SFLAG_NORMCOORD)
3112 left /= src_surface->pow2Width;
3113 right /= src_surface->pow2Width;
3114 top /= src_surface->pow2Height;
3115 bottom /= src_surface->pow2Height;
3118 /* draw the source texture stretched and upside down. The correct surface is bound already */
3119 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3120 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3122 context_set_draw_buffer(context, drawBuffer);
3123 glReadBuffer(drawBuffer);
3125 glBegin(GL_QUADS);
3126 /* bottom left */
3127 glTexCoord2f(left, bottom);
3128 glVertex2i(0, 0);
3130 /* top left */
3131 glTexCoord2f(left, top);
3132 glVertex2i(0, dst_rect.bottom - dst_rect.top);
3134 /* top right */
3135 glTexCoord2f(right, top);
3136 glVertex2i(dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3138 /* bottom right */
3139 glTexCoord2f(right, bottom);
3140 glVertex2i(dst_rect.right - dst_rect.left, 0);
3141 glEnd();
3142 checkGLcall("glEnd and previous");
3144 if (texture_target != dst_surface->texture_target)
3146 glDisable(texture_target);
3147 glEnable(dst_surface->texture_target);
3148 texture_target = dst_surface->texture_target;
3151 /* Now read the stretched and upside down image into the destination texture */
3152 glBindTexture(texture_target, dst_surface->texture_name);
3153 checkGLcall("glBindTexture");
3154 glCopyTexSubImage2D(texture_target,
3156 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3157 0, 0, /* We blitted the image to the origin */
3158 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3159 checkGLcall("glCopyTexSubImage2D");
3161 if(drawBuffer == GL_BACK) {
3162 /* Write the back buffer backup back */
3163 if(backup) {
3164 if(texture_target != GL_TEXTURE_2D) {
3165 glDisable(texture_target);
3166 glEnable(GL_TEXTURE_2D);
3167 texture_target = GL_TEXTURE_2D;
3169 glBindTexture(GL_TEXTURE_2D, backup);
3170 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3172 else
3174 if (texture_target != src_surface->texture_target)
3176 glDisable(texture_target);
3177 glEnable(src_surface->texture_target);
3178 texture_target = src_surface->texture_target;
3180 glBindTexture(src_surface->texture_target, src_surface->texture_name);
3181 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3184 glBegin(GL_QUADS);
3185 /* top left */
3186 glTexCoord2f(0.0f, 0.0f);
3187 glVertex2i(0, fbheight);
3189 /* bottom left */
3190 glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
3191 glVertex2i(0, 0);
3193 /* bottom right */
3194 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3195 (float)fbheight / (float)src_surface->pow2Height);
3196 glVertex2i(fbwidth, 0);
3198 /* top right */
3199 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
3200 glVertex2i(fbwidth, fbheight);
3201 glEnd();
3203 glDisable(texture_target);
3204 checkGLcall("glDisable(texture_target)");
3206 /* Cleanup */
3207 if (src != src_surface->texture_name && src != backup)
3209 glDeleteTextures(1, &src);
3210 checkGLcall("glDeleteTextures(1, &src)");
3212 if(backup) {
3213 glDeleteTextures(1, &backup);
3214 checkGLcall("glDeleteTextures(1, &backup)");
3217 LEAVE_GL();
3219 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3221 context_release(context);
3223 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3224 * path is never entered
3226 surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
3229 /* Until the blit_shader is ready, define some prototypes here. */
3230 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
3231 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
3232 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format);
3234 /* Front buffer coordinates are always full screen coordinates, but our GL
3235 * drawable is limited to the window's client area. The sysmem and texture
3236 * copies do have the full screen size. Note that GL has a bottom-left
3237 * origin, while D3D has a top-left origin. */
3238 void surface_translate_drawable_coords(IWineD3DSurfaceImpl *surface, HWND window, RECT *rect)
3240 UINT drawable_height;
3242 if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
3243 && surface == surface->container.u.swapchain->front_buffer)
3245 POINT offset = {0, 0};
3246 RECT windowsize;
3248 ScreenToClient(window, &offset);
3249 OffsetRect(rect, offset.x, offset.y);
3251 GetClientRect(window, &windowsize);
3252 drawable_height = windowsize.bottom - windowsize.top;
3254 else
3256 drawable_height = surface->currentDesc.Height;
3259 rect->top = drawable_height - rect->top;
3260 rect->bottom = drawable_height - rect->bottom;
3263 static BOOL surface_is_full_rect(IWineD3DSurfaceImpl *surface, const RECT *r)
3265 if ((r->left && r->right) || abs(r->right - r->left) != surface->currentDesc.Width)
3266 return FALSE;
3267 if ((r->top && r->bottom) || abs(r->bottom - r->top) != surface->currentDesc.Height)
3268 return FALSE;
3269 return TRUE;
3272 /* blit between surface locations. onscreen on different swapchains is not supported.
3273 * depth / stencil is not supported. */
3274 static void surface_blt_fbo(IWineD3DDeviceImpl *device, const WINED3DTEXTUREFILTERTYPE filter,
3275 IWineD3DSurfaceImpl *src_surface, DWORD src_location, const RECT *src_rect_in,
3276 IWineD3DSurfaceImpl *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
3278 const struct wined3d_gl_info *gl_info;
3279 struct wined3d_context *context;
3280 RECT src_rect, dst_rect;
3281 GLenum gl_filter;
3283 TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
3284 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3285 src_surface, debug_surflocation(src_location), wine_dbgstr_rect(src_rect_in));
3286 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3287 dst_surface, debug_surflocation(dst_location), wine_dbgstr_rect(dst_rect_in));
3289 src_rect = *src_rect_in;
3290 dst_rect = *dst_rect_in;
3292 switch (filter)
3294 case WINED3DTEXF_LINEAR:
3295 gl_filter = GL_LINEAR;
3296 break;
3298 default:
3299 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
3300 case WINED3DTEXF_NONE:
3301 case WINED3DTEXF_POINT:
3302 gl_filter = GL_NEAREST;
3303 break;
3306 if (src_location == SFLAG_INDRAWABLE && surface_is_offscreen(src_surface))
3307 src_location = SFLAG_INTEXTURE;
3308 if (dst_location == SFLAG_INDRAWABLE && surface_is_offscreen(dst_surface))
3309 dst_location = SFLAG_INTEXTURE;
3311 /* Make sure the locations are up-to-date. Loading the destination
3312 * surface isn't required if the entire surface is overwritten. (And is
3313 * in fact harmful if we're being called by surface_load_location() with
3314 * the purpose of loading the destination surface.) */
3315 surface_load_location(src_surface, src_location, NULL);
3316 if (!surface_is_full_rect(dst_surface, &dst_rect))
3317 surface_load_location(dst_surface, dst_location, NULL);
3319 if (src_location == SFLAG_INDRAWABLE) context = context_acquire(device, src_surface);
3320 else if (dst_location == SFLAG_INDRAWABLE) context = context_acquire(device, dst_surface);
3321 else context = context_acquire(device, NULL);
3323 if (!context->valid)
3325 context_release(context);
3326 WARN("Invalid context, skipping blit.\n");
3327 return;
3330 gl_info = context->gl_info;
3332 if (src_location == SFLAG_INDRAWABLE)
3334 GLenum buffer = surface_get_gl_buffer(src_surface);
3336 TRACE("Source surface %p is onscreen.\n", src_surface);
3338 surface_translate_drawable_coords(src_surface, context->win_handle, &src_rect);
3340 ENTER_GL();
3341 context_bind_fbo(context, GL_READ_FRAMEBUFFER, NULL);
3342 glReadBuffer(buffer);
3343 checkGLcall("glReadBuffer()");
3345 else
3347 TRACE("Source surface %p is offscreen.\n", src_surface);
3348 ENTER_GL();
3349 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
3350 glReadBuffer(GL_COLOR_ATTACHMENT0);
3351 checkGLcall("glReadBuffer()");
3353 LEAVE_GL();
3355 if (dst_location == SFLAG_INDRAWABLE)
3357 GLenum buffer = surface_get_gl_buffer(dst_surface);
3359 TRACE("Destination surface %p is onscreen.\n", dst_surface);
3361 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3363 ENTER_GL();
3364 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
3365 context_set_draw_buffer(context, buffer);
3367 else
3369 TRACE("Destination surface %p is offscreen.\n", dst_surface);
3371 ENTER_GL();
3372 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
3373 context_set_draw_buffer(context, GL_COLOR_ATTACHMENT0);
3376 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3377 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE));
3378 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1));
3379 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2));
3380 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3));
3382 glDisable(GL_SCISSOR_TEST);
3383 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3385 gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
3386 dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
3387 checkGLcall("glBlitFramebuffer()");
3389 LEAVE_GL();
3391 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3393 context_release(context);
3396 static void surface_blt_to_drawable(IWineD3DDeviceImpl *device,
3397 WINED3DTEXTUREFILTERTYPE filter, BOOL color_key,
3398 IWineD3DSurfaceImpl *src_surface, const RECT *src_rect_in,
3399 IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect_in)
3401 IWineD3DSwapChainImpl *swapchain = NULL;
3402 struct wined3d_context *context;
3403 RECT src_rect, dst_rect;
3405 src_rect = *src_rect_in;
3406 dst_rect = *dst_rect_in;
3408 if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3409 swapchain = dst_surface->container.u.swapchain;
3411 /* Make sure the surface is up-to-date. This should probably use
3412 * surface_load_location() and worry about the destination surface too,
3413 * unless we're overwriting it completely. */
3414 surface_internal_preload(src_surface, SRGB_RGB);
3416 /* Activate the destination context, set it up for blitting */
3417 context = context_acquire(device, dst_surface);
3418 context_apply_blit_state(context, device);
3420 if (!surface_is_offscreen(dst_surface))
3421 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3423 device->blitter->set_shader(device->blit_priv, context->gl_info, src_surface);
3425 ENTER_GL();
3427 if (color_key)
3429 glEnable(GL_ALPHA_TEST);
3430 checkGLcall("glEnable(GL_ALPHA_TEST)");
3432 /* When the primary render target uses P8, the alpha component
3433 * contains the palette index. Which means that the colorkey is one of
3434 * the palette entries. In other cases pixels that should be masked
3435 * away have alpha set to 0. */
3436 if (primary_render_target_is_p8(device))
3437 glAlphaFunc(GL_NOTEQUAL, (float)src_surface->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3438 else
3439 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3440 checkGLcall("glAlphaFunc");
3442 else
3444 glDisable(GL_ALPHA_TEST);
3445 checkGLcall("glDisable(GL_ALPHA_TEST)");
3448 draw_textured_quad(src_surface, &src_rect, &dst_rect, filter);
3450 if (color_key)
3452 glDisable(GL_ALPHA_TEST);
3453 checkGLcall("glDisable(GL_ALPHA_TEST)");
3456 LEAVE_GL();
3458 /* Leave the opengl state valid for blitting */
3459 device->blitter->unset_shader(context->gl_info);
3461 if (wined3d_settings.strict_draw_ordering || (swapchain
3462 && (dst_surface == swapchain->front_buffer || swapchain->num_contexts > 1)))
3463 wglFlush(); /* Flush to ensure ordering across contexts. */
3465 context_release(context);
3468 /* Do not call while under the GL lock. */
3469 HRESULT surface_color_fill(IWineD3DSurfaceImpl *s, const RECT *rect, const WINED3DCOLORVALUE *color)
3471 IWineD3DDeviceImpl *device = s->resource.device;
3472 const struct blit_shader *blitter;
3474 blitter = wined3d_select_blitter(&device->adapter->gl_info, BLIT_OP_COLOR_FILL,
3475 NULL, 0, 0, NULL, rect, s->resource.usage, s->resource.pool, s->resource.format);
3476 if (!blitter)
3478 FIXME("No blitter is capable of performing the requested color fill operation.\n");
3479 return WINED3DERR_INVALIDCALL;
3482 return blitter->color_fill(device, s, rect, color);
3485 /* Not called from the VTable */
3486 /* Do not call while under the GL lock. */
3487 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *dst_surface, const RECT *DestRect,
3488 IWineD3DSurfaceImpl *src_surface, const RECT *SrcRect, DWORD flags, const WINEDDBLTFX *DDBltFx,
3489 WINED3DTEXTUREFILTERTYPE Filter)
3491 IWineD3DDeviceImpl *device = dst_surface->resource.device;
3492 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3493 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3494 RECT dst_rect, src_rect;
3496 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3497 dst_surface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3498 flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3500 /* Get the swapchain. One of the surfaces has to be a primary surface */
3501 if (dst_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3503 WARN("Destination is in sysmem, rejecting gl blt\n");
3504 return WINED3DERR_INVALIDCALL;
3507 if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3508 dstSwapchain = dst_surface->container.u.swapchain;
3510 if (src_surface)
3512 if (src_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3514 WARN("Src is in sysmem, rejecting gl blt\n");
3515 return WINED3DERR_INVALIDCALL;
3518 if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3519 srcSwapchain = src_surface->container.u.swapchain;
3522 /* Early sort out of cases where no render target is used */
3523 if (!dstSwapchain && !srcSwapchain
3524 && src_surface != device->render_targets[0]
3525 && dst_surface != device->render_targets[0])
3527 TRACE("No surface is render target, not using hardware blit.\n");
3528 return WINED3DERR_INVALIDCALL;
3531 /* No destination color keying supported */
3532 if (flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE))
3534 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3535 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3536 return WINED3DERR_INVALIDCALL;
3539 surface_get_rect(dst_surface, DestRect, &dst_rect);
3540 if (src_surface) surface_get_rect(src_surface, SrcRect, &src_rect);
3542 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3543 if (dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->back_buffers
3544 && dst_surface == dstSwapchain->front_buffer
3545 && src_surface == dstSwapchain->back_buffers[0])
3547 /* Half-Life does a Blt from the back buffer to the front buffer,
3548 * Full surface size, no flags... Use present instead
3550 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3553 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3554 while(1)
3556 TRACE("Looking if a Present can be done...\n");
3557 /* Source Rectangle must be full surface */
3558 if (src_rect.left || src_rect.top
3559 || src_rect.right != src_surface->currentDesc.Width
3560 || src_rect.bottom != src_surface->currentDesc.Height)
3562 TRACE("No, Source rectangle doesn't match\n");
3563 break;
3566 /* No stretching may occur */
3567 if(src_rect.right != dst_rect.right - dst_rect.left ||
3568 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3569 TRACE("No, stretching is done\n");
3570 break;
3573 /* Destination must be full surface or match the clipping rectangle */
3574 if (dst_surface->clipper && dst_surface->clipper->hWnd)
3576 RECT cliprect;
3577 POINT pos[2];
3578 GetClientRect(dst_surface->clipper->hWnd, &cliprect);
3579 pos[0].x = dst_rect.left;
3580 pos[0].y = dst_rect.top;
3581 pos[1].x = dst_rect.right;
3582 pos[1].y = dst_rect.bottom;
3583 MapWindowPoints(GetDesktopWindow(), dst_surface->clipper->hWnd, pos, 2);
3585 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3586 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3588 TRACE("No, dest rectangle doesn't match(clipper)\n");
3589 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3590 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3591 break;
3594 else if (dst_rect.left || dst_rect.top
3595 || dst_rect.right != dst_surface->currentDesc.Width
3596 || dst_rect.bottom != dst_surface->currentDesc.Height)
3598 TRACE("No, dest rectangle doesn't match(surface size)\n");
3599 break;
3602 TRACE("Yes\n");
3604 /* These flags are unimportant for the flag check, remove them */
3605 if (!(flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)))
3607 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3609 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3610 * take very long, while a flip is fast.
3611 * This applies to Half-Life, which does such Blts every time it finished
3612 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3613 * menu. This is also used by all apps when they do windowed rendering
3615 * The problem is that flipping is not really the same as copying. After a
3616 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3617 * untouched. Therefore it's necessary to override the swap effect
3618 * and to set it back after the flip.
3620 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3621 * testcases.
3624 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3625 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3627 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3628 IWineD3DSwapChain_Present((IWineD3DSwapChain *)dstSwapchain,
3629 NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3631 dstSwapchain->presentParms.SwapEffect = orig_swap;
3633 return WINED3D_OK;
3635 break;
3638 TRACE("Unsupported blit between buffers on the same swapchain\n");
3639 return WINED3DERR_INVALIDCALL;
3640 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3641 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3642 return WINED3DERR_INVALIDCALL;
3643 } else if(dstSwapchain && srcSwapchain) {
3644 FIXME("Implement hardware blit between two different swapchains\n");
3645 return WINED3DERR_INVALIDCALL;
3647 else if (dstSwapchain)
3649 /* Handled with regular texture -> swapchain blit */
3650 if (src_surface == device->render_targets[0])
3651 TRACE("Blit from active render target to a swapchain\n");
3653 else if (srcSwapchain && dst_surface == device->render_targets[0])
3655 FIXME("Implement blit from a swapchain to the active render target\n");
3656 return WINED3DERR_INVALIDCALL;
3659 if ((srcSwapchain || src_surface == device->render_targets[0]) && !dstSwapchain)
3661 /* Blit from render target to texture */
3662 BOOL stretchx;
3664 /* P8 read back is not implemented */
3665 if (src_surface->resource.format->id == WINED3DFMT_P8_UINT
3666 || dst_surface->resource.format->id == WINED3DFMT_P8_UINT)
3668 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3669 return WINED3DERR_INVALIDCALL;
3672 if (flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3674 TRACE("Color keying not supported by frame buffer to texture blit\n");
3675 return WINED3DERR_INVALIDCALL;
3676 /* Destination color key is checked above */
3679 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3680 stretchx = TRUE;
3681 } else {
3682 stretchx = FALSE;
3685 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3686 * flip the image nor scale it.
3688 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3689 * -> If the app wants a image width an unscaled width, copy it line per line
3690 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3691 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3692 * back buffer. This is slower than reading line per line, thus not used for flipping
3693 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3694 * pixel by pixel
3696 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3697 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3698 * backends.
3700 if (fbo_blit_supported(gl_info, BLIT_OP_BLIT,
3701 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3702 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3704 surface_blt_fbo(device, Filter,
3705 src_surface, SFLAG_INDRAWABLE, &src_rect,
3706 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3707 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3709 else if (!stretchx || dst_rect.right - dst_rect.left > src_surface->currentDesc.Width
3710 || dst_rect.bottom - dst_rect.top > src_surface->currentDesc.Height)
3712 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3713 fb_copy_to_texture_direct(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3714 } else {
3715 TRACE("Using hardware stretching to flip / stretch the texture\n");
3716 fb_copy_to_texture_hwstretch(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3719 if (!(dst_surface->flags & SFLAG_DONOTFREE))
3721 HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory);
3722 dst_surface->resource.allocatedMemory = NULL;
3723 dst_surface->resource.heapMemory = NULL;
3725 else
3727 dst_surface->flags &= ~SFLAG_INSYSMEM;
3730 return WINED3D_OK;
3732 else if (src_surface)
3734 /* Blit from offscreen surface to render target */
3735 DWORD oldCKeyFlags = src_surface->CKeyFlags;
3736 WINEDDCOLORKEY oldBltCKey = src_surface->SrcBltCKey;
3738 TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
3740 if (!(flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3741 && fbo_blit_supported(gl_info, BLIT_OP_BLIT,
3742 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3743 src_surface->resource.format,
3744 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3745 dst_surface->resource.format))
3747 TRACE("Using surface_blt_fbo.\n");
3748 /* The source is always a texture, but never the currently active render target, and the texture
3749 * contents are never upside down. */
3750 surface_blt_fbo(device, Filter,
3751 src_surface, SFLAG_INDRAWABLE, &src_rect,
3752 dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3753 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3754 return WINED3D_OK;
3757 if (!(flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3758 && arbfp_blit.blit_supported(gl_info, BLIT_OP_BLIT,
3759 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3760 src_surface->resource.format,
3761 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3762 dst_surface->resource.format))
3764 return arbfp_blit_surface(device, src_surface, &src_rect, dst_surface, &dst_rect, BLIT_OP_BLIT, Filter);
3767 if (!device->blitter->blit_supported(gl_info, BLIT_OP_BLIT,
3768 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3769 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3771 FIXME("Unsupported blit operation falling back to software\n");
3772 return WINED3DERR_INVALIDCALL;
3775 /* Color keying: Check if we have to do a color keyed blt,
3776 * and if not check if a color key is activated.
3778 * Just modify the color keying parameters in the surface and restore them afterwards
3779 * The surface keeps track of the color key last used to load the opengl surface.
3780 * PreLoad will catch the change to the flags and color key and reload if necessary.
3782 if (flags & WINEDDBLT_KEYSRC)
3784 /* Use color key from surface */
3786 else if (flags & WINEDDBLT_KEYSRCOVERRIDE)
3788 /* Use color key from DDBltFx */
3789 src_surface->CKeyFlags |= WINEDDSD_CKSRCBLT;
3790 src_surface->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3792 else
3794 /* Do not use color key */
3795 src_surface->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3798 surface_blt_to_drawable(device, Filter, flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE),
3799 src_surface, &src_rect, dst_surface, &dst_rect);
3801 /* Restore the color key parameters */
3802 src_surface->CKeyFlags = oldCKeyFlags;
3803 src_surface->SrcBltCKey = oldBltCKey;
3805 surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3807 return WINED3D_OK;
3809 else
3811 /* Source-Less Blit to render target */
3812 if (flags & WINEDDBLT_COLORFILL)
3814 WINED3DCOLORVALUE color;
3816 TRACE("Colorfill\n");
3818 /* The color as given in the Blt function is in the surface format. */
3819 if (!surface_convert_color_to_float(dst_surface, DDBltFx->u5.dwFillColor, &color))
3820 return WINED3DERR_INVALIDCALL;
3822 return surface_color_fill(dst_surface, &dst_rect, &color);
3826 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3827 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3828 return WINED3DERR_INVALIDCALL;
3831 static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3832 IWineD3DSurface *src_surface, const RECT *src_rect, DWORD flags, const WINEDDBLTFX *DDBltFx)
3834 IWineD3DDeviceImpl *device = This->resource.device;
3835 float depth;
3837 if (flags & WINEDDBLT_DEPTHFILL)
3839 switch (This->resource.format->id)
3841 case WINED3DFMT_D16_UNORM:
3842 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3843 break;
3844 case WINED3DFMT_S1_UINT_D15_UNORM:
3845 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00007fff;
3846 break;
3847 case WINED3DFMT_D24_UNORM_S8_UINT:
3848 case WINED3DFMT_X8D24_UNORM:
3849 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3850 break;
3851 case WINED3DFMT_D32_UNORM:
3852 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3853 break;
3854 default:
3855 depth = 0.0f;
3856 ERR("Unexpected format for depth fill: %s.\n", debug_d3dformat(This->resource.format->id));
3859 return IWineD3DDevice_Clear((IWineD3DDevice *)device, DestRect ? 1 : 0, DestRect,
3860 WINED3DCLEAR_ZBUFFER, 0x00000000, depth, 0x00000000);
3863 FIXME("(%p): Unsupp depthstencil blit\n", This);
3864 return WINED3DERR_INVALIDCALL;
3867 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect,
3868 IWineD3DSurface *src_surface, const RECT *SrcRect, DWORD flags,
3869 const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter)
3871 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3872 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
3873 IWineD3DDeviceImpl *device = This->resource.device;
3875 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3876 iface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3877 flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3878 TRACE("Usage is %s.\n", debug_d3dusage(This->resource.usage));
3880 if ((This->flags & SFLAG_LOCKED) || (src && (src->flags & SFLAG_LOCKED)))
3882 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3883 return WINEDDERR_SURFACEBUSY;
3886 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3887 * except depth blits, which seem to work
3889 if (This == device->depth_stencil || (src && src == device->depth_stencil))
3891 if (device->inScene && !(flags & WINEDDBLT_DEPTHFILL))
3893 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3894 return WINED3DERR_INVALIDCALL;
3896 else if (SUCCEEDED(IWineD3DSurfaceImpl_BltZ(This, DestRect, src_surface, SrcRect, flags, DDBltFx)))
3898 TRACE("Z Blit override handled the blit\n");
3899 return WINED3D_OK;
3903 /* Special cases for RenderTargets */
3904 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3905 || (src && (src->resource.usage & WINED3DUSAGE_RENDERTARGET)))
3907 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This, DestRect, src, SrcRect, flags, DDBltFx, Filter)))
3908 return WINED3D_OK;
3911 /* For the rest call the X11 surface implementation.
3912 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3913 * other Blts are rather rare. */
3914 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, src_surface, SrcRect, flags, DDBltFx, Filter);
3917 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3918 IWineD3DSurface *src_surface, const RECT *rsrc, DWORD trans)
3920 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3921 IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
3922 IWineD3DDeviceImpl *device = This->resource.device;
3924 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3925 iface, dstx, dsty, src_surface, wine_dbgstr_rect(rsrc), trans);
3927 if ((This->flags & SFLAG_LOCKED) || (src->flags & SFLAG_LOCKED))
3929 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3930 return WINEDDERR_SURFACEBUSY;
3933 if (device->inScene && (This == device->depth_stencil || src == device->depth_stencil))
3935 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3936 return WINED3DERR_INVALIDCALL;
3939 /* Special cases for RenderTargets */
3940 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3941 || (src->resource.usage & WINED3DUSAGE_RENDERTARGET))
3944 RECT SrcRect, DstRect;
3945 DWORD flags = 0;
3947 surface_get_rect(src, rsrc, &SrcRect);
3949 DstRect.left = dstx;
3950 DstRect.top=dsty;
3951 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3952 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3954 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3955 if (trans & WINEDDBLTFAST_SRCCOLORKEY)
3956 flags |= WINEDDBLT_KEYSRC;
3957 if (trans & WINEDDBLTFAST_DESTCOLORKEY)
3958 flags |= WINEDDBLT_KEYDEST;
3959 if (trans & WINEDDBLTFAST_WAIT)
3960 flags |= WINEDDBLT_WAIT;
3961 if (trans & WINEDDBLTFAST_DONOTWAIT)
3962 flags |= WINEDDBLT_DONOTWAIT;
3964 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This,
3965 &DstRect, src, &SrcRect, flags, NULL, WINED3DTEXF_POINT)))
3966 return WINED3D_OK;
3969 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, src_surface, rsrc, trans);
3972 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3973 /** Check against the maximum texture sizes supported by the video card **/
3974 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3975 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
3976 unsigned int pow2Width, pow2Height;
3978 This->surface_ops = &surface_ops;
3980 This->texture_name = 0;
3981 This->texture_target = GL_TEXTURE_2D;
3983 /* Non-power2 support */
3984 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
3986 pow2Width = This->currentDesc.Width;
3987 pow2Height = This->currentDesc.Height;
3989 else
3991 /* Find the nearest pow2 match */
3992 pow2Width = pow2Height = 1;
3993 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3994 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3996 This->pow2Width = pow2Width;
3997 This->pow2Height = pow2Height;
3999 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height)
4001 /* TODO: Add support for non power two compressed textures. */
4002 if (This->resource.format->flags & WINED3DFMT_FLAG_COMPRESSED)
4004 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
4005 This, This->currentDesc.Width, This->currentDesc.Height);
4006 return WINED3DERR_NOTAVAILABLE;
4010 if (pow2Width != This->currentDesc.Width
4011 || pow2Height != This->currentDesc.Height)
4013 This->flags |= SFLAG_NONPOW2;
4016 TRACE("%p\n", This);
4017 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
4018 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
4020 /* one of three options
4021 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)
4022 2: Set the texture to the maximum size (bad idea)
4023 3: WARN and return WINED3DERR_NOTAVAILABLE;
4024 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.
4026 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4028 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4029 return WINED3DERR_NOTAVAILABLE;
4032 /* We should never use this surface in combination with OpenGL! */
4033 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4035 else
4037 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4038 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4039 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4041 if (This->flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4042 && !(This->resource.format->id == WINED3DFMT_P8_UINT
4043 && gl_info->supported[EXT_PALETTED_TEXTURE]
4044 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4046 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4047 This->pow2Width = This->currentDesc.Width;
4048 This->pow2Height = This->currentDesc.Height;
4049 This->flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4053 switch (wined3d_settings.offscreen_rendering_mode)
4055 case ORM_FBO:
4056 This->get_drawable_size = get_drawable_size_fbo;
4057 break;
4059 case ORM_BACKBUFFER:
4060 This->get_drawable_size = get_drawable_size_backbuffer;
4061 break;
4063 default:
4064 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
4065 return WINED3DERR_INVALIDCALL;
4068 This->flags |= SFLAG_INSYSMEM;
4070 return WINED3D_OK;
4073 /* GL locking is done by the caller */
4074 static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4075 GLuint texture, GLsizei w, GLsizei h, GLenum target)
4077 IWineD3DDeviceImpl *device = This->resource.device;
4078 GLint compare_mode = GL_NONE;
4079 struct blt_info info;
4080 GLint old_binding = 0;
4081 RECT rect;
4083 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4085 glDisable(GL_CULL_FACE);
4086 glDisable(GL_BLEND);
4087 glDisable(GL_ALPHA_TEST);
4088 glDisable(GL_SCISSOR_TEST);
4089 glDisable(GL_STENCIL_TEST);
4090 glEnable(GL_DEPTH_TEST);
4091 glDepthFunc(GL_ALWAYS);
4092 glDepthMask(GL_TRUE);
4093 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4094 glViewport(0, 0, w, h);
4096 SetRect(&rect, 0, h, w, 0);
4097 surface_get_blt_info(target, &rect, w, h, &info);
4098 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4099 glGetIntegerv(info.binding, &old_binding);
4100 glBindTexture(info.bind_target, texture);
4101 if (gl_info->supported[ARB_SHADOW])
4103 glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
4104 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
4107 device->shader_backend->shader_select_depth_blt(device->shader_priv,
4108 gl_info, info.tex_type, &This->ds_current_size);
4110 glBegin(GL_TRIANGLE_STRIP);
4111 glTexCoord3fv(info.coords[0]);
4112 glVertex2f(-1.0f, -1.0f);
4113 glTexCoord3fv(info.coords[1]);
4114 glVertex2f(1.0f, -1.0f);
4115 glTexCoord3fv(info.coords[2]);
4116 glVertex2f(-1.0f, 1.0f);
4117 glTexCoord3fv(info.coords[3]);
4118 glVertex2f(1.0f, 1.0f);
4119 glEnd();
4121 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
4122 glBindTexture(info.bind_target, old_binding);
4124 glPopAttrib();
4126 device->shader_backend->shader_deselect_depth_blt(device->shader_priv, gl_info);
4129 void surface_modify_ds_location(IWineD3DSurfaceImpl *surface,
4130 DWORD location, UINT w, UINT h)
4132 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);
4134 if (location & ~SFLAG_DS_LOCATIONS)
4135 FIXME("Invalid location (%#x) specified.\n", location);
4137 surface->ds_current_size.cx = w;
4138 surface->ds_current_size.cy = h;
4139 surface->flags &= ~SFLAG_DS_LOCATIONS;
4140 surface->flags |= location;
4143 /* Context activation is done by the caller. */
4144 void surface_load_ds_location(IWineD3DSurfaceImpl *surface, struct wined3d_context *context, DWORD location)
4146 IWineD3DDeviceImpl *device = surface->resource.device;
4147 const struct wined3d_gl_info *gl_info = context->gl_info;
4149 TRACE("surface %p, new location %#x.\n", surface, location);
4151 /* TODO: Make this work for modes other than FBO */
4152 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4154 if (!(surface->flags & location))
4156 surface->ds_current_size.cx = 0;
4157 surface->ds_current_size.cy = 0;
4160 if (surface->ds_current_size.cx == surface->currentDesc.Width
4161 && surface->ds_current_size.cy == surface->currentDesc.Height)
4163 TRACE("Location (%#x) is already up to date.\n", location);
4164 return;
4167 if (surface->current_renderbuffer)
4169 FIXME("Not supported with fixed up depth stencil.\n");
4170 return;
4173 if (!(surface->flags & SFLAG_LOCATIONS))
4175 FIXME("No up to date depth stencil location.\n");
4176 surface->flags |= location;
4177 return;
4180 if (location == SFLAG_DS_OFFSCREEN)
4182 GLint old_binding = 0;
4183 GLenum bind_target;
4184 GLsizei w, h;
4186 /* The render target is allowed to be smaller than the depth/stencil
4187 * buffer, so the onscreen depth/stencil buffer is potentially smaller
4188 * than the offscreen surface. Don't overwrite the offscreen surface
4189 * with undefined data. */
4190 w = min(surface->currentDesc.Width, context->swapchain->presentParms.BackBufferWidth);
4191 h = min(surface->currentDesc.Height, context->swapchain->presentParms.BackBufferHeight);
4193 TRACE("Copying onscreen depth buffer to depth texture.\n");
4195 ENTER_GL();
4197 if (!device->depth_blt_texture)
4199 glGenTextures(1, &device->depth_blt_texture);
4202 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4203 * directly on the FBO texture. That's because we need to flip. */
4204 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4205 if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4207 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4208 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4210 else
4212 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4213 bind_target = GL_TEXTURE_2D;
4215 glBindTexture(bind_target, device->depth_blt_texture);
4216 glCopyTexImage2D(bind_target, surface->texture_level, surface->resource.format->glInternal, 0, 0, w, h, 0);
4217 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4218 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4219 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4220 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4221 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4222 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4223 glBindTexture(bind_target, old_binding);
4225 /* Setup the destination */
4226 if (!device->depth_blt_rb)
4228 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4229 checkGLcall("glGenRenderbuffersEXT");
4231 if (device->depth_blt_rb_w != w || device->depth_blt_rb_h != h)
4233 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4234 checkGLcall("glBindRenderbufferEXT");
4235 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h);
4236 checkGLcall("glRenderbufferStorageEXT");
4237 device->depth_blt_rb_w = w;
4238 device->depth_blt_rb_h = h;
4241 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4242 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4243 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4244 checkGLcall("glFramebufferRenderbufferEXT");
4245 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, surface, FALSE);
4247 /* Do the actual blit */
4248 surface_depth_blt(surface, gl_info, device->depth_blt_texture, w, h, bind_target);
4249 checkGLcall("depth_blt");
4251 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4252 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4254 LEAVE_GL();
4256 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4258 else if (location == SFLAG_DS_ONSCREEN)
4260 TRACE("Copying depth texture to onscreen depth buffer.\n");
4262 ENTER_GL();
4264 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4265 surface_depth_blt(surface, gl_info, surface->texture_name,
4266 surface->currentDesc.Width, surface->currentDesc.Height, surface->texture_target);
4267 checkGLcall("depth_blt");
4269 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4271 LEAVE_GL();
4273 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4275 else
4277 ERR("Invalid location (%#x) specified.\n", location);
4280 surface->flags |= location;
4281 surface->ds_current_size.cx = surface->currentDesc.Width;
4282 surface->ds_current_size.cy = surface->currentDesc.Height;
4285 void surface_modify_location(IWineD3DSurfaceImpl *surface, DWORD flag, BOOL persistent)
4287 const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
4288 IWineD3DSurfaceImpl *overlay;
4290 TRACE("surface %p, location %s, persistent %#x.\n",
4291 surface, debug_surflocation(flag), persistent);
4293 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4295 if (surface_is_offscreen(surface))
4297 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4298 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4300 else
4302 TRACE("Surface %p is an onscreen surface.\n", surface);
4306 if (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)
4307 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4309 flag |= (SFLAG_INTEXTURE | SFLAG_INSRGBTEX);
4312 if (persistent)
4314 if (((surface->flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE))
4315 || ((surface->flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX)))
4317 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4319 TRACE("Passing to container.\n");
4320 basetexture_set_dirty(surface->container.u.texture, TRUE);
4323 surface->flags &= ~SFLAG_LOCATIONS;
4324 surface->flags |= flag;
4326 /* Redraw emulated overlays, if any */
4327 if (flag & SFLAG_INDRAWABLE && !list_empty(&surface->overlays))
4329 LIST_FOR_EACH_ENTRY(overlay, &surface->overlays, IWineD3DSurfaceImpl, overlay_entry)
4331 overlay->surface_ops->surface_draw_overlay(overlay);
4335 else
4337 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)))
4339 if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4341 TRACE("Passing to container\n");
4342 basetexture_set_dirty(surface->container.u.texture, TRUE);
4345 surface->flags &= ~flag;
4348 if (!(surface->flags & SFLAG_LOCATIONS))
4350 ERR("Surface %p does not have any up to date location.\n", surface);
4354 HRESULT surface_load_location(IWineD3DSurfaceImpl *surface, DWORD flag, const RECT *rect)
4356 IWineD3DDeviceImpl *device = surface->resource.device;
4357 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4358 BOOL drawable_read_ok = surface_is_offscreen(surface);
4359 struct wined3d_format format;
4360 CONVERT_TYPES convert;
4361 int width, pitch, outpitch;
4362 BYTE *mem;
4363 BOOL in_fbo = FALSE;
4365 TRACE("surface %p, location %s, rect %s.\n", surface, debug_surflocation(flag), wine_dbgstr_rect(rect));
4367 if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
4369 if (flag == SFLAG_INTEXTURE)
4371 struct wined3d_context *context = context_acquire(device, NULL);
4372 surface_load_ds_location(surface, context, SFLAG_DS_OFFSCREEN);
4373 context_release(context);
4374 return WINED3D_OK;
4376 else
4378 FIXME("Unimplemented location %s for depth/stencil buffers.\n", debug_surflocation(flag));
4379 return WINED3DERR_INVALIDCALL;
4383 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4385 if (surface_is_offscreen(surface))
4387 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4388 * Prefer SFLAG_INTEXTURE. */
4389 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4390 drawable_read_ok = FALSE;
4391 in_fbo = TRUE;
4393 else
4395 TRACE("Surface %p is an onscreen surface.\n", surface);
4399 if (flag == SFLAG_INSRGBTEX && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4401 flag = SFLAG_INTEXTURE;
4404 if (surface->flags & flag)
4406 TRACE("Location already up to date\n");
4407 return WINED3D_OK;
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);
4433 else
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 (wined3d_settings.rendertargetlock_mode == RTL_READTEX)
4443 surface_load_location(surface, SFLAG_INTEXTURE, NULL);
4445 if (surface->flags & SFLAG_INTEXTURE)
4447 RECT r;
4449 surface_get_rect(surface, rect, &r);
4450 surface_blt_to_drawable(device, WINED3DTEXF_POINT, FALSE, surface, &r, surface, &r);
4452 else
4454 int byte_count;
4455 if ((surface->flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX)
4457 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4458 * values, otherwise we get incorrect values in the target. For now go the slow way
4459 * via a system memory copy
4461 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4464 d3dfmt_get_conv(surface, FALSE /* We need color keying */,
4465 FALSE /* We won't use textures */, &format, &convert);
4467 /* The width is in 'length' not in bytes */
4468 width = surface->currentDesc.Width;
4469 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4471 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4472 * but it isn't set (yet) in all cases it is getting called. */
4473 if ((convert != NO_CONVERSION) && (surface->flags & SFLAG_PBO))
4475 struct wined3d_context *context = NULL;
4477 TRACE("Removing the pbo attached to surface %p.\n", surface);
4479 if (!device->isInDraw) context = context_acquire(device, NULL);
4480 surface_remove_pbo(surface, gl_info);
4481 if (context) context_release(context);
4484 if ((convert != NO_CONVERSION) && surface->resource.allocatedMemory)
4486 int height = surface->currentDesc.Height;
4487 byte_count = format.conv_byte_count;
4489 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4490 outpitch = width * byte_count;
4491 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4493 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4494 if(!mem) {
4495 ERR("Out of memory %d, %d!\n", outpitch, height);
4496 return WINED3DERR_OUTOFVIDEOMEMORY;
4498 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4499 width, height, outpitch, convert, surface);
4501 surface->flags |= SFLAG_CONVERTED;
4503 else
4505 surface->flags &= ~SFLAG_CONVERTED;
4506 mem = surface->resource.allocatedMemory;
4507 byte_count = format.byte_count;
4510 flush_to_framebuffer_drawpixels(surface, format.glFormat, format.glType, byte_count, mem);
4512 /* Don't delete PBO memory */
4513 if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4514 HeapFree(GetProcessHeap(), 0, mem);
4517 else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4519 const DWORD attach_flags = WINED3DFMT_FLAG_FBO_ATTACHABLE | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
4521 if (drawable_read_ok && (surface->flags & SFLAG_INDRAWABLE))
4523 read_from_framebuffer_texture(surface, flag == SFLAG_INSRGBTEX);
4525 else if (surface->flags & (SFLAG_INSRGBTEX | SFLAG_INTEXTURE)
4526 && (surface->resource.format->flags & attach_flags) == attach_flags
4527 && fbo_blit_supported(gl_info, BLIT_OP_BLIT,
4528 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format,
4529 NULL, surface->resource.usage, surface->resource.pool, surface->resource.format))
4531 DWORD src_location = flag == SFLAG_INSRGBTEX ? SFLAG_INTEXTURE : SFLAG_INSRGBTEX;
4532 RECT rect = {0, 0, surface->currentDesc.Width, surface->currentDesc.Height};
4534 surface_blt_fbo(surface->resource.device, WINED3DTEXF_POINT,
4535 surface, src_location, &rect, surface, flag, &rect);
4537 else
4539 /* Upload from system memory */
4540 BOOL srgb = flag == SFLAG_INSRGBTEX;
4541 struct wined3d_context *context = NULL;
4543 d3dfmt_get_conv(surface, TRUE /* We need color keying */,
4544 TRUE /* We will use textures */, &format, &convert);
4546 if (srgb)
4548 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE)
4550 /* Performance warning... */
4551 FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface);
4552 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4555 else
4557 if ((surface->flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX)
4559 /* Performance warning... */
4560 FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface);
4561 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4564 if (!(surface->flags & SFLAG_INSYSMEM))
4566 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4567 /* Lets hope we get it from somewhere... */
4568 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4571 if (!device->isInDraw) context = context_acquire(device, NULL);
4573 surface_prepare_texture(surface, gl_info, srgb);
4574 surface_bind_and_dirtify(surface, srgb);
4576 if (surface->CKeyFlags & WINEDDSD_CKSRCBLT)
4578 surface->flags |= SFLAG_GLCKEY;
4579 surface->glCKey = surface->SrcBltCKey;
4581 else surface->flags &= ~SFLAG_GLCKEY;
4583 /* The width is in 'length' not in bytes */
4584 width = surface->currentDesc.Width;
4585 pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4587 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4588 * but it isn't set (yet) in all cases it is getting called. */
4589 if ((convert != NO_CONVERSION || format.convert) && (surface->flags & SFLAG_PBO))
4591 TRACE("Removing the pbo attached to surface %p.\n", surface);
4592 surface_remove_pbo(surface, gl_info);
4595 if (format.convert)
4597 /* This code is entered for texture formats which need a fixup. */
4598 int height = surface->currentDesc.Height;
4600 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4601 outpitch = width * format.conv_byte_count;
4602 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4604 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4605 if(!mem) {
4606 ERR("Out of memory %d, %d!\n", outpitch, height);
4607 if (context) context_release(context);
4608 return WINED3DERR_OUTOFVIDEOMEMORY;
4610 format.convert(surface->resource.allocatedMemory, mem, pitch, width, height);
4612 else if (convert != NO_CONVERSION && surface->resource.allocatedMemory)
4614 /* This code is only entered for color keying fixups */
4615 int height = surface->currentDesc.Height;
4617 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4618 outpitch = width * format.conv_byte_count;
4619 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4621 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4622 if(!mem) {
4623 ERR("Out of memory %d, %d!\n", outpitch, height);
4624 if (context) context_release(context);
4625 return WINED3DERR_OUTOFVIDEOMEMORY;
4627 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4628 width, height, outpitch, convert, surface);
4630 else
4632 mem = surface->resource.allocatedMemory;
4635 /* Make sure the correct pitch is used */
4636 ENTER_GL();
4637 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4638 LEAVE_GL();
4640 if (mem || (surface->flags & SFLAG_PBO))
4641 surface_upload_data(surface, gl_info, &format, srgb, mem);
4643 /* Restore the default pitch */
4644 ENTER_GL();
4645 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4646 LEAVE_GL();
4648 if (context) context_release(context);
4650 /* Don't delete PBO memory */
4651 if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4652 HeapFree(GetProcessHeap(), 0, mem);
4656 if (!rect) surface->flags |= flag;
4658 if (in_fbo && (surface->flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)))
4660 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4661 surface->flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4664 if (surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)
4665 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4667 surface->flags |= (SFLAG_INTEXTURE | SFLAG_INSRGBTEX);
4670 return WINED3D_OK;
4673 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4674 return SURFACE_OPENGL;
4677 BOOL surface_is_offscreen(IWineD3DSurfaceImpl *surface)
4679 IWineD3DSwapChainImpl *swapchain = surface->container.u.swapchain;
4681 /* Not on a swapchain - must be offscreen */
4682 if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN) return TRUE;
4684 /* The front buffer is always onscreen */
4685 if (surface == swapchain->front_buffer) return FALSE;
4687 /* If the swapchain is rendered to an FBO, the backbuffer is
4688 * offscreen, otherwise onscreen */
4689 return swapchain->render_to_fbo;
4692 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4694 /* IUnknown */
4695 IWineD3DBaseSurfaceImpl_QueryInterface,
4696 IWineD3DBaseSurfaceImpl_AddRef,
4697 IWineD3DSurfaceImpl_Release,
4698 /* IWineD3DResource */
4699 IWineD3DBaseSurfaceImpl_GetParent,
4700 IWineD3DBaseSurfaceImpl_SetPrivateData,
4701 IWineD3DBaseSurfaceImpl_GetPrivateData,
4702 IWineD3DBaseSurfaceImpl_FreePrivateData,
4703 IWineD3DBaseSurfaceImpl_SetPriority,
4704 IWineD3DBaseSurfaceImpl_GetPriority,
4705 IWineD3DSurfaceImpl_PreLoad,
4706 IWineD3DBaseSurfaceImpl_GetType,
4707 /* IWineD3DSurface */
4708 IWineD3DBaseSurfaceImpl_GetResource,
4709 IWineD3DBaseSurfaceImpl_GetDesc,
4710 IWineD3DSurfaceImpl_Map,
4711 IWineD3DSurfaceImpl_Unmap,
4712 IWineD3DSurfaceImpl_GetDC,
4713 IWineD3DSurfaceImpl_ReleaseDC,
4714 IWineD3DSurfaceImpl_Flip,
4715 IWineD3DSurfaceImpl_Blt,
4716 IWineD3DBaseSurfaceImpl_GetBltStatus,
4717 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4718 IWineD3DBaseSurfaceImpl_IsLost,
4719 IWineD3DBaseSurfaceImpl_Restore,
4720 IWineD3DSurfaceImpl_BltFast,
4721 IWineD3DBaseSurfaceImpl_GetPalette,
4722 IWineD3DBaseSurfaceImpl_SetPalette,
4723 IWineD3DBaseSurfaceImpl_SetColorKey,
4724 IWineD3DBaseSurfaceImpl_GetPitch,
4725 IWineD3DSurfaceImpl_SetMem,
4726 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4727 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4728 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4729 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4730 IWineD3DBaseSurfaceImpl_SetClipper,
4731 IWineD3DBaseSurfaceImpl_GetClipper,
4732 /* Internal use: */
4733 IWineD3DSurfaceImpl_SetFormat,
4734 IWineD3DSurfaceImpl_PrivateSetup,
4735 IWineD3DSurfaceImpl_GetImplType,
4738 static HRESULT ffp_blit_alloc(IWineD3DDeviceImpl *device) { return WINED3D_OK; }
4739 /* Context activation is done by the caller. */
4740 static void ffp_blit_free(IWineD3DDeviceImpl *device) { }
4742 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4743 /* Context activation is done by the caller. */
4744 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4746 BYTE table[256][4];
4747 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4749 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4751 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4752 ENTER_GL();
4753 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4754 LEAVE_GL();
4757 /* Context activation is done by the caller. */
4758 static HRESULT ffp_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, IWineD3DSurfaceImpl *surface)
4760 enum complex_fixup fixup = get_complex_fixup(surface->resource.format->color_fixup);
4762 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4763 * else the surface is converted in software at upload time in LoadLocation.
4765 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4766 ffp_blit_p8_upload_palette(surface, gl_info);
4768 ENTER_GL();
4769 glEnable(surface->texture_target);
4770 checkGLcall("glEnable(surface->texture_target)");
4771 LEAVE_GL();
4772 return WINED3D_OK;
4775 /* Context activation is done by the caller. */
4776 static void ffp_blit_unset(const struct wined3d_gl_info *gl_info)
4778 ENTER_GL();
4779 glDisable(GL_TEXTURE_2D);
4780 checkGLcall("glDisable(GL_TEXTURE_2D)");
4781 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4783 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4784 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4786 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4788 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4789 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4791 LEAVE_GL();
4794 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4795 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
4796 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
4798 enum complex_fixup src_fixup;
4800 if (blit_op == BLIT_OP_COLOR_FILL)
4802 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4804 TRACE("Color fill not supported\n");
4805 return FALSE;
4808 return TRUE;
4811 src_fixup = get_complex_fixup(src_format->color_fixup);
4812 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4814 TRACE("Checking support for fixup:\n");
4815 dump_color_fixup_desc(src_format->color_fixup);
4818 if (blit_op != BLIT_OP_BLIT)
4820 TRACE("Unsupported blit_op=%d\n", blit_op);
4821 return FALSE;
4824 if (!is_identity_fixup(dst_format->color_fixup))
4826 TRACE("Destination fixups are not supported\n");
4827 return FALSE;
4830 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4832 TRACE("P8 fixup supported\n");
4833 return TRUE;
4836 /* We only support identity conversions. */
4837 if (is_identity_fixup(src_format->color_fixup))
4839 TRACE("[OK]\n");
4840 return TRUE;
4843 TRACE("[FAILED]\n");
4844 return FALSE;
4847 /* Do not call while under the GL lock. */
4848 static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
4849 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
4851 const RECT draw_rect = {0, 0, dst_surface->currentDesc.Width, dst_surface->currentDesc.Height};
4853 return device_clear_render_targets(device, 1 /* rt_count */, &dst_surface, 1 /* rect_count */,
4854 dst_rect, &draw_rect, WINED3DCLEAR_TARGET, color, 0.0f /* depth */, 0 /* stencil */);
4857 const struct blit_shader ffp_blit = {
4858 ffp_blit_alloc,
4859 ffp_blit_free,
4860 ffp_blit_set,
4861 ffp_blit_unset,
4862 ffp_blit_supported,
4863 ffp_blit_color_fill
4866 static HRESULT cpu_blit_alloc(IWineD3DDeviceImpl *device)
4868 return WINED3D_OK;
4871 /* Context activation is done by the caller. */
4872 static void cpu_blit_free(IWineD3DDeviceImpl *device)
4876 /* Context activation is done by the caller. */
4877 static HRESULT cpu_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, IWineD3DSurfaceImpl *surface)
4879 return WINED3D_OK;
4882 /* Context activation is done by the caller. */
4883 static void cpu_blit_unset(const struct wined3d_gl_info *gl_info)
4887 static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4888 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
4889 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
4891 if (blit_op == BLIT_OP_COLOR_FILL)
4893 return TRUE;
4896 return FALSE;
4899 /* Do not call while under the GL lock. */
4900 static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
4901 const RECT *dst_rect, const WINED3DCOLORVALUE *color)
4903 WINEDDBLTFX BltFx;
4905 memset(&BltFx, 0, sizeof(BltFx));
4906 BltFx.dwSize = sizeof(BltFx);
4907 BltFx.u5.dwFillColor = wined3d_format_convert_from_float(dst_surface->resource.format, color);
4908 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect,
4909 NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
4912 const struct blit_shader cpu_blit = {
4913 cpu_blit_alloc,
4914 cpu_blit_free,
4915 cpu_blit_set,
4916 cpu_blit_unset,
4917 cpu_blit_supported,
4918 cpu_blit_color_fill
4921 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4922 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
4923 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
4925 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
4926 return FALSE;
4928 /* We only support blitting. Things like color keying / color fill should
4929 * be handled by other blitters.
4931 if (blit_op != BLIT_OP_BLIT)
4932 return FALSE;
4934 /* Source and/or destination need to be on the GL side */
4935 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
4936 return FALSE;
4938 if (!((src_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET))
4939 && ((dst_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
4940 return FALSE;
4942 if (!(src_format->id == dst_format->id
4943 || (is_identity_fixup(src_format->color_fixup)
4944 && is_identity_fixup(dst_format->color_fixup))))
4945 return FALSE;
4947 return TRUE;