wined3d: Add NPOT quirk for GeforceFX 5200.
[wine/testsucceed.git] / dlls / wined3d / surface.c
blobd363feb1da063e9544567a6634fde990fd8d51ac
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 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 IWineD3DDeviceImpl *device = This->resource.device;
40 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
41 struct wined3d_context *context = NULL;
42 renderbuffer_entry_t *entry, *entry2;
44 TRACE("(%p) : Cleaning up.\n", This);
46 /* Need a context to destroy the texture. Use the currently active render
47 * target, but only if the primary render target exists. Otherwise
48 * lastActiveRenderTarget is garbage. When destroying the primary render
49 * target, Uninit3D() will activate a context before doing anything. */
50 if (device->render_targets && device->render_targets[0])
52 context = context_acquire(device, NULL);
55 ENTER_GL();
57 if (This->texture_name)
59 /* Release the OpenGL texture. */
60 TRACE("Deleting texture %u.\n", This->texture_name);
61 glDeleteTextures(1, &This->texture_name);
64 if (This->Flags & SFLAG_PBO)
66 /* Delete the PBO. */
67 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
70 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry)
72 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
73 HeapFree(GetProcessHeap(), 0, entry);
76 LEAVE_GL();
78 if (This->Flags & SFLAG_DIBSECTION)
80 /* Release the DC. */
81 SelectObject(This->hDC, This->dib.holdbitmap);
82 DeleteDC(This->hDC);
83 /* Release the DIB section. */
84 DeleteObject(This->dib.DIBsection);
85 This->dib.bitmap_data = NULL;
86 This->resource.allocatedMemory = NULL;
89 if (This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem((IWineD3DSurface *)This, NULL);
90 if (This->overlay_dest) list_remove(&This->overlay_entry);
92 HeapFree(GetProcessHeap(), 0, This->palette9);
94 resource_cleanup((IWineD3DResource *)This);
96 if (context) context_release(context);
99 UINT surface_calculate_size(const struct wined3d_format_desc *format_desc, UINT alignment, UINT width, UINT height)
101 UINT size;
103 if (format_desc->format == WINED3DFMT_UNKNOWN)
105 size = 0;
107 else if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
109 UINT row_block_count = (width + format_desc->block_width - 1) / format_desc->block_width;
110 UINT row_count = (height + format_desc->block_height - 1) / format_desc->block_height;
111 size = row_count * row_block_count * format_desc->block_byte_count;
113 else
115 /* The pitch is a multiple of 4 bytes. */
116 size = height * (((width * format_desc->byte_count) + alignment - 1) & ~(alignment - 1));
119 if (format_desc->heightscale != 0.0f) size *= format_desc->heightscale;
121 return size;
124 struct blt_info
126 GLenum binding;
127 GLenum bind_target;
128 enum tex_types tex_type;
129 GLfloat coords[4][3];
132 struct float_rect
134 float l;
135 float t;
136 float r;
137 float b;
140 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
142 f->l = ((r->left * 2.0f) / w) - 1.0f;
143 f->t = ((r->top * 2.0f) / h) - 1.0f;
144 f->r = ((r->right * 2.0f) / w) - 1.0f;
145 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
148 static void surface_get_blt_info(GLenum target, const RECT *rect_in, GLsizei w, GLsizei h, struct blt_info *info)
150 GLfloat (*coords)[3] = info->coords;
151 RECT rect;
152 struct float_rect f;
154 if (rect_in)
155 rect = *rect_in;
156 else
158 rect.left = 0;
159 rect.top = h;
160 rect.right = w;
161 rect.bottom = 0;
164 switch (target)
166 default:
167 FIXME("Unsupported texture target %#x\n", target);
168 /* Fall back to GL_TEXTURE_2D */
169 case GL_TEXTURE_2D:
170 info->binding = GL_TEXTURE_BINDING_2D;
171 info->bind_target = GL_TEXTURE_2D;
172 info->tex_type = tex_2d;
173 coords[0][0] = (float)rect.left / w;
174 coords[0][1] = (float)rect.top / h;
175 coords[0][2] = 0.0f;
177 coords[1][0] = (float)rect.right / w;
178 coords[1][1] = (float)rect.top / h;
179 coords[1][2] = 0.0f;
181 coords[2][0] = (float)rect.left / w;
182 coords[2][1] = (float)rect.bottom / h;
183 coords[2][2] = 0.0f;
185 coords[3][0] = (float)rect.right / w;
186 coords[3][1] = (float)rect.bottom / h;
187 coords[3][2] = 0.0f;
188 break;
190 case GL_TEXTURE_RECTANGLE_ARB:
191 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
192 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
193 info->tex_type = tex_rect;
194 coords[0][0] = rect.left; coords[0][1] = rect.top; coords[0][2] = 0.0f;
195 coords[1][0] = rect.right; coords[1][1] = rect.top; coords[1][2] = 0.0f;
196 coords[2][0] = rect.left; coords[2][1] = rect.bottom; coords[2][2] = 0.0f;
197 coords[3][0] = rect.right; coords[3][1] = rect.bottom; coords[3][2] = 0.0f;
198 break;
200 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
201 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
202 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
203 info->tex_type = tex_cube;
204 cube_coords_float(&rect, w, h, &f);
206 coords[0][0] = 1.0f; coords[0][1] = -f.t; coords[0][2] = -f.l;
207 coords[1][0] = 1.0f; coords[1][1] = -f.t; coords[1][2] = -f.r;
208 coords[2][0] = 1.0f; coords[2][1] = -f.b; coords[2][2] = -f.l;
209 coords[3][0] = 1.0f; coords[3][1] = -f.b; coords[3][2] = -f.r;
210 break;
212 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
213 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
214 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
215 info->tex_type = tex_cube;
216 cube_coords_float(&rect, w, h, &f);
218 coords[0][0] = -1.0f; coords[0][1] = -f.t; coords[0][2] = f.l;
219 coords[1][0] = -1.0f; coords[1][1] = -f.t; coords[1][2] = f.r;
220 coords[2][0] = -1.0f; coords[2][1] = -f.b; coords[2][2] = f.l;
221 coords[3][0] = -1.0f; coords[3][1] = -f.b; coords[3][2] = f.r;
222 break;
224 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
225 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
226 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
227 info->tex_type = tex_cube;
228 cube_coords_float(&rect, w, h, &f);
230 coords[0][0] = f.l; coords[0][1] = 1.0f; coords[0][2] = f.t;
231 coords[1][0] = f.r; coords[1][1] = 1.0f; coords[1][2] = f.t;
232 coords[2][0] = f.l; coords[2][1] = 1.0f; coords[2][2] = f.b;
233 coords[3][0] = f.r; coords[3][1] = 1.0f; coords[3][2] = f.b;
234 break;
236 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
237 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
238 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
239 info->tex_type = tex_cube;
240 cube_coords_float(&rect, w, h, &f);
242 coords[0][0] = f.l; coords[0][1] = -1.0f; coords[0][2] = -f.t;
243 coords[1][0] = f.r; coords[1][1] = -1.0f; coords[1][2] = -f.t;
244 coords[2][0] = f.l; coords[2][1] = -1.0f; coords[2][2] = -f.b;
245 coords[3][0] = f.r; coords[3][1] = -1.0f; coords[3][2] = -f.b;
246 break;
248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
249 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
250 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
251 info->tex_type = tex_cube;
252 cube_coords_float(&rect, w, h, &f);
254 coords[0][0] = f.l; coords[0][1] = -f.t; coords[0][2] = 1.0f;
255 coords[1][0] = f.r; coords[1][1] = -f.t; coords[1][2] = 1.0f;
256 coords[2][0] = f.l; coords[2][1] = -f.b; coords[2][2] = 1.0f;
257 coords[3][0] = f.r; coords[3][1] = -f.b; coords[3][2] = 1.0f;
258 break;
260 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
261 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
262 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
263 info->tex_type = tex_cube;
264 cube_coords_float(&rect, w, h, &f);
266 coords[0][0] = -f.l; coords[0][1] = -f.t; coords[0][2] = -1.0f;
267 coords[1][0] = -f.r; coords[1][1] = -f.t; coords[1][2] = -1.0f;
268 coords[2][0] = -f.l; coords[2][1] = -f.b; coords[2][2] = -1.0f;
269 coords[3][0] = -f.r; coords[3][1] = -f.b; coords[3][2] = -1.0f;
270 break;
274 static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
276 if (rect_in)
277 *rect_out = *rect_in;
278 else
280 rect_out->left = 0;
281 rect_out->top = 0;
282 rect_out->right = This->currentDesc.Width;
283 rect_out->bottom = This->currentDesc.Height;
287 /* GL locking and context activation is done by the caller */
288 void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
290 IWineD3DBaseTextureImpl *texture;
291 struct blt_info info;
293 surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
295 glEnable(info.bind_target);
296 checkGLcall("glEnable(bind_target)");
298 /* Bind the texture */
299 glBindTexture(info.bind_target, src_surface->texture_name);
300 checkGLcall("glBindTexture");
302 /* Filtering for StretchRect */
303 glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
304 wined3d_gl_mag_filter(magLookup, Filter));
305 checkGLcall("glTexParameteri");
306 glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
307 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
308 checkGLcall("glTexParameteri");
309 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
310 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
311 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
312 checkGLcall("glTexEnvi");
314 /* Draw a quad */
315 glBegin(GL_TRIANGLE_STRIP);
316 glTexCoord3fv(info.coords[0]);
317 glVertex2i(dst_rect->left, dst_rect->top);
319 glTexCoord3fv(info.coords[1]);
320 glVertex2i(dst_rect->right, dst_rect->top);
322 glTexCoord3fv(info.coords[2]);
323 glVertex2i(dst_rect->left, dst_rect->bottom);
325 glTexCoord3fv(info.coords[3]);
326 glVertex2i(dst_rect->right, dst_rect->bottom);
327 glEnd();
329 /* Unbind the texture */
330 glBindTexture(info.bind_target, 0);
331 checkGLcall("glBindTexture(info->bind_target, 0)");
333 /* We changed the filtering settings on the texture. Inform the
334 * container about this to get the filters reset properly next draw. */
335 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)src_surface, &IID_IWineD3DBaseTexture, (void **)&texture)))
337 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
338 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
339 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
340 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture);
344 HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
345 UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
346 UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format,
347 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
349 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
350 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
351 void (*cleanup)(IWineD3DSurfaceImpl *This);
352 unsigned int resource_size;
353 HRESULT hr;
355 if (multisample_quality > 0)
357 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality);
358 multisample_quality = 0;
361 /* FIXME: Check that the format is supported by the device. */
363 resource_size = surface_calculate_size(format_desc, alignment, width, height);
365 /* Look at the implementation and set the correct Vtable. */
366 switch (surface_type)
368 case SURFACE_OPENGL:
369 surface->lpVtbl = &IWineD3DSurface_Vtbl;
370 cleanup = surface_cleanup;
371 break;
373 case SURFACE_GDI:
374 surface->lpVtbl = &IWineGDISurface_Vtbl;
375 cleanup = surface_gdi_cleanup;
376 break;
378 default:
379 ERR("Requested unknown surface implementation %#x.\n", surface_type);
380 return WINED3DERR_INVALIDCALL;
383 hr = resource_init((IWineD3DResource *)surface, WINED3DRTYPE_SURFACE,
384 device, resource_size, usage, format_desc, pool, parent, parent_ops);
385 if (FAILED(hr))
387 WARN("Failed to initialize resource, returning %#x.\n", hr);
388 return hr;
391 /* "Standalone" surface. */
392 IWineD3DSurface_SetContainer((IWineD3DSurface *)surface, NULL);
394 surface->currentDesc.Width = width;
395 surface->currentDesc.Height = height;
396 surface->currentDesc.MultiSampleType = multisample_type;
397 surface->currentDesc.MultiSampleQuality = multisample_quality;
398 surface->texture_level = level;
399 list_init(&surface->overlays);
401 /* Flags */
402 surface->Flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
403 if (discard) surface->Flags |= SFLAG_DISCARD;
404 if (lockable || format == WINED3DFMT_D16_LOCKABLE) surface->Flags |= SFLAG_LOCKABLE;
406 /* Quick lockable sanity check.
407 * TODO: remove this after surfaces, usage and lockability have been debugged properly
408 * this function is too deep to need to care about things like this.
409 * Levels need to be checked too, since they all affect what can be done. */
410 switch (pool)
412 case WINED3DPOOL_SCRATCH:
413 if(!lockable)
415 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
416 "which are mutually exclusive, setting lockable to TRUE.\n");
417 lockable = TRUE;
419 break;
421 case WINED3DPOOL_SYSTEMMEM:
422 if (!lockable)
423 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
424 break;
426 case WINED3DPOOL_MANAGED:
427 if (usage & WINED3DUSAGE_DYNAMIC)
428 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
429 break;
431 case WINED3DPOOL_DEFAULT:
432 if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
433 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
434 break;
436 default:
437 FIXME("Unknown pool %#x.\n", pool);
438 break;
441 if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
443 FIXME("Trying to create a render target that isn't in the default pool.\n");
446 /* Mark the texture as dirty so that it gets loaded first time around. */
447 surface_add_dirty_rect(surface, NULL);
448 list_init(&surface->renderbuffers);
450 TRACE("surface %p, memory %p, size %u\n", surface, surface->resource.allocatedMemory, surface->resource.size);
452 /* Call the private setup routine */
453 hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
454 if (FAILED(hr))
456 ERR("Private setup failed, returning %#x\n", hr);
457 cleanup(surface);
458 return hr;
461 return hr;
464 static void surface_force_reload(IWineD3DSurfaceImpl *surface)
466 surface->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
469 void surface_set_texture_name(IWineD3DSurfaceImpl *surface, GLuint new_name, BOOL srgb)
471 GLuint *name;
472 DWORD flag;
474 TRACE("surface %p, new_name %u, srgb %#x.\n", surface, new_name, srgb);
476 if(srgb)
478 name = &surface->texture_name_srgb;
479 flag = SFLAG_INSRGBTEX;
481 else
483 name = &surface->texture_name;
484 flag = SFLAG_INTEXTURE;
487 if (!*name && new_name)
489 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
490 * surface has no texture name yet. See if we can get rid of this. */
491 if (surface->Flags & flag)
492 ERR("Surface has SFLAG_INTEXTURE set, but no texture name\n");
493 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)surface, flag, FALSE);
496 *name = new_name;
497 surface_force_reload(surface);
500 void surface_set_texture_target(IWineD3DSurfaceImpl *surface, GLenum target)
502 TRACE("surface %p, target %#x.\n", surface, target);
504 if (surface->texture_target != target)
506 if (target == GL_TEXTURE_RECTANGLE_ARB)
508 surface->Flags &= ~SFLAG_NORMCOORD;
510 else if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
512 surface->Flags |= SFLAG_NORMCOORD;
515 surface->texture_target = target;
516 surface_force_reload(surface);
519 /* Context activation is done by the caller. */
520 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This, BOOL srgb) {
521 DWORD active_sampler;
523 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
524 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
525 * gl states. The current texture unit should always be a valid one.
527 * To be more specific, this is tricky because we can implicitly be called
528 * from sampler() in state.c. This means we can't touch anything other than
529 * whatever happens to be the currently active texture, or we would risk
530 * marking already applied sampler states dirty again.
532 * TODO: Track the current active texture per GL context instead of using glGet
534 GLint active_texture;
535 ENTER_GL();
536 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
537 LEAVE_GL();
538 active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
540 if (active_sampler != WINED3D_UNMAPPED_STAGE)
542 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
544 IWineD3DSurface_BindTexture((IWineD3DSurface *)This, srgb);
547 /* This function checks if the primary render target uses the 8bit paletted format. */
548 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
550 if (device->render_targets && device->render_targets[0])
552 IWineD3DSurfaceImpl *render_target = device->render_targets[0];
553 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
554 && (render_target->resource.format_desc->format == WINED3DFMT_P8_UINT))
555 return TRUE;
557 return FALSE;
560 /* This call just downloads data, the caller is responsible for binding the
561 * correct texture. */
562 /* Context activation is done by the caller. */
563 static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
565 const struct wined3d_format_desc *format_desc = This->resource.format_desc;
567 /* Only support read back of converted P8 surfaces */
568 if (This->Flags & SFLAG_CONVERTED && format_desc->format != WINED3DFMT_P8_UINT)
570 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(format_desc->format));
571 return;
574 ENTER_GL();
576 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
578 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
579 This, This->texture_level, format_desc->glFormat, format_desc->glType,
580 This->resource.allocatedMemory);
582 if (This->Flags & SFLAG_PBO)
584 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
585 checkGLcall("glBindBufferARB");
586 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
587 checkGLcall("glGetCompressedTexImageARB");
588 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
589 checkGLcall("glBindBufferARB");
591 else
593 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
594 This->texture_level, This->resource.allocatedMemory));
595 checkGLcall("glGetCompressedTexImageARB");
598 LEAVE_GL();
599 } else {
600 void *mem;
601 GLenum format = format_desc->glFormat;
602 GLenum type = format_desc->glType;
603 int src_pitch = 0;
604 int dst_pitch = 0;
606 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
607 if (format_desc->format == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
609 format = GL_ALPHA;
610 type = GL_UNSIGNED_BYTE;
613 if (This->Flags & SFLAG_NONPOW2) {
614 unsigned char alignment = This->resource.device->surface_alignment;
615 src_pitch = format_desc->byte_count * This->pow2Width;
616 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
617 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
618 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
619 } else {
620 mem = This->resource.allocatedMemory;
623 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
624 This, This->texture_level, format, type, mem);
626 if(This->Flags & SFLAG_PBO) {
627 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
628 checkGLcall("glBindBufferARB");
630 glGetTexImage(This->texture_target, This->texture_level, format, type, NULL);
631 checkGLcall("glGetTexImage");
633 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
634 checkGLcall("glBindBufferARB");
635 } else {
636 glGetTexImage(This->texture_target, This->texture_level, format, type, mem);
637 checkGLcall("glGetTexImage");
639 LEAVE_GL();
641 if (This->Flags & SFLAG_NONPOW2) {
642 const BYTE *src_data;
643 BYTE *dst_data;
644 UINT y;
646 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
647 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
648 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
650 * We're doing this...
652 * instead of boxing the texture :
653 * |<-texture width ->| -->pow2width| /\
654 * |111111111111111111| | |
655 * |222 Texture 222222| boxed empty | texture height
656 * |3333 Data 33333333| | |
657 * |444444444444444444| | \/
658 * ----------------------------------- |
659 * | boxed empty | boxed empty | pow2height
660 * | | | \/
661 * -----------------------------------
664 * we're repacking the data to the expected texture width
666 * |<-texture width ->| -->pow2width| /\
667 * |111111111111111111222222222222222| |
668 * |222333333333333333333444444444444| texture height
669 * |444444 | |
670 * | | \/
671 * | | |
672 * | empty | pow2height
673 * | | \/
674 * -----------------------------------
676 * == is the same as
678 * |<-texture width ->| /\
679 * |111111111111111111|
680 * |222222222222222222|texture height
681 * |333333333333333333|
682 * |444444444444444444| \/
683 * --------------------
685 * this also means that any references to allocatedMemory should work with the data as if were a
686 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
688 * internally the texture is still stored in a boxed format so any references to textureName will
689 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
691 * Performance should not be an issue, because applications normally do not lock the surfaces when
692 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
693 * and doesn't have to be re-read.
695 src_data = mem;
696 dst_data = This->resource.allocatedMemory;
697 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
698 for (y = 1 ; y < This->currentDesc.Height; y++) {
699 /* skip the first row */
700 src_data += src_pitch;
701 dst_data += dst_pitch;
702 memcpy(dst_data, src_data, dst_pitch);
705 HeapFree(GetProcessHeap(), 0, mem);
709 /* Surface has now been downloaded */
710 This->Flags |= SFLAG_INSYSMEM;
713 /* This call just uploads data, the caller is responsible for binding the
714 * correct texture. */
715 /* Context activation is done by the caller. */
716 static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
717 const struct wined3d_format_desc *format_desc, BOOL srgb, const GLvoid *data)
719 GLsizei width = This->currentDesc.Width;
720 GLsizei height = This->currentDesc.Height;
721 GLenum internal;
723 if (srgb)
725 internal = format_desc->glGammaInternal;
727 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
729 internal = format_desc->rtInternal;
731 else
733 internal = format_desc->glInternal;
736 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
737 This, internal, width, height, format_desc->glFormat, format_desc->glType, data);
738 TRACE("target %#x, level %u, resource size %u.\n",
739 This->texture_target, This->texture_level, This->resource.size);
741 if (format_desc->heightscale != 1.0f && format_desc->heightscale != 0.0f) height *= format_desc->heightscale;
743 ENTER_GL();
745 if (This->Flags & SFLAG_PBO)
747 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
748 checkGLcall("glBindBufferARB");
750 TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
751 data = NULL;
754 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
756 TRACE("Calling glCompressedTexSubImage2DARB.\n");
758 GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
759 0, 0, width, height, internal, This->resource.size, data));
760 checkGLcall("glCompressedTexSubImage2DARB");
762 else
764 TRACE("Calling glTexSubImage2D.\n");
766 glTexSubImage2D(This->texture_target, This->texture_level,
767 0, 0, width, height, format_desc->glFormat, format_desc->glType, data);
768 checkGLcall("glTexSubImage2D");
771 if (This->Flags & SFLAG_PBO)
773 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
774 checkGLcall("glBindBufferARB");
777 LEAVE_GL();
779 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
781 IWineD3DDeviceImpl *device = This->resource.device;
782 unsigned int i;
784 for (i = 0; i < device->numContexts; ++i)
786 context_surface_update(device->contexts[i], This);
791 /* This call just allocates the texture, the caller is responsible for binding
792 * the correct texture. */
793 /* Context activation is done by the caller. */
794 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
795 const struct wined3d_format_desc *format_desc, BOOL srgb)
797 BOOL enable_client_storage = FALSE;
798 GLsizei width = This->pow2Width;
799 GLsizei height = This->pow2Height;
800 const BYTE *mem = NULL;
801 GLenum internal;
803 if (srgb)
805 internal = format_desc->glGammaInternal;
807 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
809 internal = format_desc->rtInternal;
811 else
813 internal = format_desc->glInternal;
816 if (format_desc->heightscale != 1.0f && format_desc->heightscale != 0.0f) height *= format_desc->heightscale;
818 TRACE("(%p) : Creating surface (target %#x) level %d, d3d format %s, internal format %#x, width %d, height %d, gl format %#x, gl type=%#x\n",
819 This, This->texture_target, This->texture_level, debug_d3dformat(format_desc->format),
820 internal, width, height, format_desc->glFormat, format_desc->glType);
822 ENTER_GL();
824 if (gl_info->supported[APPLE_CLIENT_STORAGE])
826 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
827 /* In some cases we want to disable client storage.
828 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
829 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
830 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
831 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
833 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
834 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
835 This->Flags &= ~SFLAG_CLIENT;
836 enable_client_storage = TRUE;
837 } else {
838 This->Flags |= SFLAG_CLIENT;
840 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
841 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
843 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
847 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED && mem)
849 GL_EXTCALL(glCompressedTexImage2DARB(This->texture_target, This->texture_level,
850 internal, width, height, 0, This->resource.size, mem));
852 else
854 glTexImage2D(This->texture_target, This->texture_level,
855 internal, width, height, 0, format_desc->glFormat, format_desc->glType, mem);
856 checkGLcall("glTexImage2D");
859 if(enable_client_storage) {
860 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
861 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
863 LEAVE_GL();
866 /* In D3D the depth stencil dimensions have to be greater than or equal to the
867 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
868 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
869 /* GL locking is done by the caller */
870 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl *surface, unsigned int width, unsigned int height)
872 const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
873 renderbuffer_entry_t *entry;
874 GLuint renderbuffer = 0;
875 unsigned int src_width, src_height;
877 src_width = surface->pow2Width;
878 src_height = surface->pow2Height;
880 /* A depth stencil smaller than the render target is not valid */
881 if (width > src_width || height > src_height) return;
883 /* Remove any renderbuffer set if the sizes match */
884 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
885 || (width == src_width && height == src_height))
887 surface->current_renderbuffer = NULL;
888 return;
891 /* Look if we've already got a renderbuffer of the correct dimensions */
892 LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, renderbuffer_entry_t, entry)
894 if (entry->width == width && entry->height == height)
896 renderbuffer = entry->id;
897 surface->current_renderbuffer = entry;
898 break;
902 if (!renderbuffer)
904 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
905 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
906 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
907 surface->resource.format_desc->glInternal, width, height);
909 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
910 entry->width = width;
911 entry->height = height;
912 entry->id = renderbuffer;
913 list_add_head(&surface->renderbuffers, &entry->entry);
915 surface->current_renderbuffer = entry;
918 checkGLcall("set_compatible_renderbuffer");
921 GLenum surface_get_gl_buffer(IWineD3DSurfaceImpl *surface)
923 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)surface->container;
925 TRACE("surface %p.\n", surface);
927 if (!(surface->Flags & SFLAG_SWAPCHAIN))
929 ERR("Surface %p is not on a swapchain.\n", surface);
930 return GL_NONE;
933 if (swapchain->back_buffers && swapchain->back_buffers[0] == surface)
935 if (swapchain->render_to_fbo)
937 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
938 return GL_COLOR_ATTACHMENT0;
940 TRACE("Returning GL_BACK\n");
941 return GL_BACK;
943 else if (surface == swapchain->front_buffer)
945 TRACE("Returning GL_FRONT\n");
946 return GL_FRONT;
949 FIXME("Higher back buffer, returning GL_BACK\n");
950 return GL_BACK;
953 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
954 void surface_add_dirty_rect(IWineD3DSurfaceImpl *surface, const RECT *dirty_rect)
956 IWineD3DBaseTexture *baseTexture = NULL;
958 TRACE("surface %p, dirty_rect %s.\n", surface, wine_dbgstr_rect(dirty_rect));
960 if (!(surface->Flags & SFLAG_INSYSMEM) && (surface->Flags & SFLAG_INTEXTURE))
961 /* No partial locking for textures yet. */
962 IWineD3DSurface_LoadLocation((IWineD3DSurface *)surface, SFLAG_INSYSMEM, NULL);
964 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)surface, SFLAG_INSYSMEM, TRUE);
965 if (dirty_rect)
967 surface->dirtyRect.left = min(surface->dirtyRect.left, dirty_rect->left);
968 surface->dirtyRect.top = min(surface->dirtyRect.top, dirty_rect->top);
969 surface->dirtyRect.right = max(surface->dirtyRect.right, dirty_rect->right);
970 surface->dirtyRect.bottom = max(surface->dirtyRect.bottom, dirty_rect->bottom);
972 else
974 surface->dirtyRect.left = 0;
975 surface->dirtyRect.top = 0;
976 surface->dirtyRect.right = surface->currentDesc.Width;
977 surface->dirtyRect.bottom = surface->currentDesc.Height;
980 /* if the container is a basetexture then mark it dirty. */
981 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)surface,
982 &IID_IWineD3DBaseTexture, (void **)&baseTexture)))
984 TRACE("Passing to container\n");
985 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
986 IWineD3DBaseTexture_Release(baseTexture);
990 static BOOL surface_convert_color_to_argb(IWineD3DSurfaceImpl *This, DWORD color, DWORD *argb_color)
992 IWineD3DDeviceImpl *device = This->resource.device;
994 switch(This->resource.format_desc->format)
996 case WINED3DFMT_P8_UINT:
998 DWORD alpha;
1000 if (primary_render_target_is_p8(device))
1001 alpha = color << 24;
1002 else
1003 alpha = 0xFF000000;
1005 if (This->palette) {
1006 *argb_color = (alpha |
1007 (This->palette->palents[color].peRed << 16) |
1008 (This->palette->palents[color].peGreen << 8) |
1009 (This->palette->palents[color].peBlue));
1010 } else {
1011 *argb_color = alpha;
1014 break;
1016 case WINED3DFMT_B5G6R5_UNORM:
1018 if (color == 0xFFFF) {
1019 *argb_color = 0xFFFFFFFF;
1020 } else {
1021 *argb_color = ((0xFF000000) |
1022 ((color & 0xF800) << 8) |
1023 ((color & 0x07E0) << 5) |
1024 ((color & 0x001F) << 3));
1027 break;
1029 case WINED3DFMT_B8G8R8_UNORM:
1030 case WINED3DFMT_B8G8R8X8_UNORM:
1031 *argb_color = 0xFF000000 | color;
1032 break;
1034 case WINED3DFMT_B8G8R8A8_UNORM:
1035 *argb_color = color;
1036 break;
1038 default:
1039 ERR("Unhandled conversion from %s to ARGB!\n", debug_d3dformat(This->resource.format_desc->format));
1040 return FALSE;
1042 return TRUE;
1045 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1047 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1048 ULONG ref = InterlockedDecrement(&This->resource.ref);
1049 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
1051 if (!ref)
1053 surface_cleanup(This);
1054 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
1056 TRACE("(%p) Released.\n", This);
1057 HeapFree(GetProcessHeap(), 0, This);
1060 return ref;
1063 /* ****************************************************
1064 IWineD3DSurface IWineD3DResource parts follow
1065 **************************************************** */
1067 void surface_internal_preload(IWineD3DSurfaceImpl *surface, enum WINED3DSRGB srgb)
1069 /* TODO: check for locks */
1070 IWineD3DDeviceImpl *device = surface->resource.device;
1071 IWineD3DBaseTexture *baseTexture = NULL;
1073 TRACE("(%p)Checking to see if the container is a base texture\n", surface);
1074 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)surface,
1075 &IID_IWineD3DBaseTexture, (void **)&baseTexture)))
1077 IWineD3DBaseTextureImpl *tex_impl = (IWineD3DBaseTextureImpl *)baseTexture;
1078 TRACE("Passing to container\n");
1079 tex_impl->baseTexture.internal_preload(baseTexture, srgb);
1080 IWineD3DBaseTexture_Release(baseTexture);
1081 } else {
1082 struct wined3d_context *context = NULL;
1084 TRACE("(%p) : About to load surface\n", surface);
1086 if (!device->isInDraw) context = context_acquire(device, NULL);
1088 if (surface->resource.format_desc->format == WINED3DFMT_P8_UINT
1089 || surface->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
1091 if (palette9_changed(surface))
1093 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1094 /* TODO: This is not necessarily needed with hw palettized texture support */
1095 IWineD3DSurface_LoadLocation((IWineD3DSurface *)surface, SFLAG_INSYSMEM, NULL);
1096 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1097 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)surface, SFLAG_INTEXTURE, FALSE);
1101 IWineD3DSurface_LoadTexture((IWineD3DSurface *)surface, srgb == SRGB_SRGB ? TRUE : FALSE);
1103 if (surface->resource.pool == WINED3DPOOL_DEFAULT)
1105 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1106 GLclampf tmp;
1107 tmp = 0.9f;
1108 ENTER_GL();
1109 glPrioritizeTextures(1, &surface->texture_name, &tmp);
1110 LEAVE_GL();
1113 if (context) context_release(context);
1117 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface)
1119 surface_internal_preload((IWineD3DSurfaceImpl *)iface, SRGB_ANY);
1122 /* Context activation is done by the caller. */
1123 static void surface_remove_pbo(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
1125 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1126 This->resource.allocatedMemory =
1127 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1129 ENTER_GL();
1130 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1131 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1132 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
1133 checkGLcall("glGetBufferSubDataARB");
1134 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
1135 checkGLcall("glDeleteBuffersARB");
1136 LEAVE_GL();
1138 This->pbo = 0;
1139 This->Flags &= ~SFLAG_PBO;
1142 BOOL surface_init_sysmem(IWineD3DSurfaceImpl *surface)
1144 if (!surface->resource.allocatedMemory)
1146 surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1147 surface->resource.size + RESOURCE_ALIGNMENT);
1148 if (!surface->resource.heapMemory)
1150 ERR("Out of memory\n");
1151 return FALSE;
1153 surface->resource.allocatedMemory =
1154 (BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1156 else
1158 memset(surface->resource.allocatedMemory, 0, surface->resource.size);
1161 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)surface, SFLAG_INSYSMEM, TRUE);
1162 return TRUE;
1165 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
1166 IWineD3DBaseTexture *texture = NULL;
1167 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1168 IWineD3DDeviceImpl *device = This->resource.device;
1169 const struct wined3d_gl_info *gl_info;
1170 renderbuffer_entry_t *entry, *entry2;
1171 struct wined3d_context *context;
1173 TRACE("(%p)\n", iface);
1175 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
1176 /* Default pool resources are supposed to be destroyed before Reset is called.
1177 * Implicit resources stay however. So this means we have an implicit render target
1178 * or depth stencil. The content may be destroyed, but we still have to tear down
1179 * opengl resources, so we cannot leave early.
1181 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1182 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1183 * or the depth stencil into an FBO the texture or render buffer will be removed
1184 * and all flags get lost
1186 surface_init_sysmem(This);
1188 else
1190 /* Load the surface into system memory */
1191 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
1192 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
1194 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
1195 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSRGBTEX, FALSE);
1196 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
1198 context = context_acquire(device, NULL);
1199 gl_info = context->gl_info;
1201 /* Destroy PBOs, but load them into real sysmem before */
1202 if (This->Flags & SFLAG_PBO)
1203 surface_remove_pbo(This, gl_info);
1205 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1206 * all application-created targets the application has to release the surface
1207 * before calling _Reset
1209 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
1210 ENTER_GL();
1211 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1212 LEAVE_GL();
1213 list_remove(&entry->entry);
1214 HeapFree(GetProcessHeap(), 0, entry);
1216 list_init(&This->renderbuffers);
1217 This->current_renderbuffer = NULL;
1219 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
1220 * destroy it
1222 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
1223 if(!texture) {
1224 ENTER_GL();
1225 glDeleteTextures(1, &This->texture_name);
1226 This->texture_name = 0;
1227 glDeleteTextures(1, &This->texture_name_srgb);
1228 This->texture_name_srgb = 0;
1229 LEAVE_GL();
1230 } else {
1231 IWineD3DBaseTexture_Release(texture);
1234 context_release(context);
1237 /* ******************************************************
1238 IWineD3DSurface IWineD3DSurface parts follow
1239 ****************************************************** */
1241 /* Read the framebuffer back into the surface */
1242 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1244 IWineD3DDeviceImpl *device = This->resource.device;
1245 const struct wined3d_gl_info *gl_info;
1246 struct wined3d_context *context;
1247 BYTE *mem;
1248 GLint fmt;
1249 GLint type;
1250 BYTE *row, *top, *bottom;
1251 int i;
1252 BOOL bpp;
1253 RECT local_rect;
1254 BOOL srcIsUpsideDown;
1255 GLint rowLen = 0;
1256 GLint skipPix = 0;
1257 GLint skipRow = 0;
1259 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1260 static BOOL warned = FALSE;
1261 if(!warned) {
1262 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1263 warned = TRUE;
1265 return;
1268 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
1269 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
1270 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
1271 * context->last_was_blit set on the unlock.
1273 context = context_acquire(device, This);
1274 context_apply_blit_state(context, device);
1275 gl_info = context->gl_info;
1277 ENTER_GL();
1279 /* Select the correct read buffer, and give some debug output.
1280 * There is no need to keep track of the current read buffer or reset it, every part of the code
1281 * that reads sets the read buffer as desired.
1283 if (surface_is_offscreen(This))
1285 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1286 * Read from the back buffer
1288 TRACE("Locking offscreen render target\n");
1289 glReadBuffer(device->offscreenBuffer);
1290 srcIsUpsideDown = TRUE;
1292 else
1294 /* Onscreen surfaces are always part of a swapchain */
1295 GLenum buffer = surface_get_gl_buffer(This);
1296 TRACE("Locking %#x buffer\n", buffer);
1297 glReadBuffer(buffer);
1298 checkGLcall("glReadBuffer");
1299 srcIsUpsideDown = FALSE;
1302 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1303 if(!rect) {
1304 local_rect.left = 0;
1305 local_rect.top = 0;
1306 local_rect.right = This->currentDesc.Width;
1307 local_rect.bottom = This->currentDesc.Height;
1308 } else {
1309 local_rect = *rect;
1311 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1313 switch(This->resource.format_desc->format)
1315 case WINED3DFMT_P8_UINT:
1317 if (primary_render_target_is_p8(device))
1319 /* In case of P8 render targets the index is stored in the alpha component */
1320 fmt = GL_ALPHA;
1321 type = GL_UNSIGNED_BYTE;
1322 mem = dest;
1323 bpp = This->resource.format_desc->byte_count;
1324 } else {
1325 /* GL can't return palettized data, so read ARGB pixels into a
1326 * separate block of memory and convert them into palettized format
1327 * in software. Slow, but if the app means to use palettized render
1328 * targets and locks it...
1330 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1331 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1332 * for the color channels when palettizing the colors.
1334 fmt = GL_RGB;
1335 type = GL_UNSIGNED_BYTE;
1336 pitch *= 3;
1337 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1338 if(!mem) {
1339 ERR("Out of memory\n");
1340 LEAVE_GL();
1341 return;
1343 bpp = This->resource.format_desc->byte_count * 3;
1346 break;
1348 default:
1349 mem = dest;
1350 fmt = This->resource.format_desc->glFormat;
1351 type = This->resource.format_desc->glType;
1352 bpp = This->resource.format_desc->byte_count;
1355 if(This->Flags & SFLAG_PBO) {
1356 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1357 checkGLcall("glBindBufferARB");
1358 if(mem != NULL) {
1359 ERR("mem not null for pbo -- unexpected\n");
1360 mem = NULL;
1364 /* Save old pixel store pack state */
1365 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1366 checkGLcall("glGetIntegerv");
1367 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1368 checkGLcall("glGetIntegerv");
1369 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1370 checkGLcall("glGetIntegerv");
1372 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1373 glPixelStorei(GL_PACK_ROW_LENGTH, This->currentDesc.Width);
1374 checkGLcall("glPixelStorei");
1375 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1376 checkGLcall("glPixelStorei");
1377 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1378 checkGLcall("glPixelStorei");
1380 glReadPixels(local_rect.left, (!srcIsUpsideDown) ? (This->currentDesc.Height - local_rect.bottom) : local_rect.top ,
1381 local_rect.right - local_rect.left,
1382 local_rect.bottom - local_rect.top,
1383 fmt, type, mem);
1384 checkGLcall("glReadPixels");
1386 /* Reset previous pixel store pack state */
1387 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1388 checkGLcall("glPixelStorei");
1389 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1390 checkGLcall("glPixelStorei");
1391 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1392 checkGLcall("glPixelStorei");
1394 if(This->Flags & SFLAG_PBO) {
1395 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
1396 checkGLcall("glBindBufferARB");
1398 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1399 * to get a pointer to it and perform the flipping in software. This is a lot
1400 * faster than calling glReadPixels for each line. In case we want more speed
1401 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1402 if(!srcIsUpsideDown) {
1403 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1404 checkGLcall("glBindBufferARB");
1406 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1407 checkGLcall("glMapBufferARB");
1411 /* TODO: Merge this with the palettization loop below for P8 targets */
1412 if(!srcIsUpsideDown) {
1413 UINT len, off;
1414 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1415 Flip the lines in software */
1416 len = (local_rect.right - local_rect.left) * bpp;
1417 off = local_rect.left * bpp;
1419 row = HeapAlloc(GetProcessHeap(), 0, len);
1420 if(!row) {
1421 ERR("Out of memory\n");
1422 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT) HeapFree(GetProcessHeap(), 0, mem);
1423 LEAVE_GL();
1424 return;
1427 top = mem + pitch * local_rect.top;
1428 bottom = mem + pitch * (local_rect.bottom - 1);
1429 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
1430 memcpy(row, top + off, len);
1431 memcpy(top + off, bottom + off, len);
1432 memcpy(bottom + off, row, len);
1433 top += pitch;
1434 bottom -= pitch;
1436 HeapFree(GetProcessHeap(), 0, row);
1438 /* Unmap the temp PBO buffer */
1439 if(This->Flags & SFLAG_PBO) {
1440 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1441 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1445 LEAVE_GL();
1446 context_release(context);
1448 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1449 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1450 * the same color but we have no choice.
1451 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1453 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(device))
1455 const PALETTEENTRY *pal = NULL;
1456 DWORD width = pitch / 3;
1457 int x, y, c;
1459 if(This->palette) {
1460 pal = This->palette->palents;
1461 } else {
1462 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1463 HeapFree(GetProcessHeap(), 0, mem);
1464 return ;
1467 for(y = local_rect.top; y < local_rect.bottom; y++) {
1468 for(x = local_rect.left; x < local_rect.right; x++) {
1469 /* start lines pixels */
1470 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1471 const BYTE *green = blue + 1;
1472 const BYTE *red = green + 1;
1474 for(c = 0; c < 256; c++) {
1475 if(*red == pal[c].peRed &&
1476 *green == pal[c].peGreen &&
1477 *blue == pal[c].peBlue)
1479 *((BYTE *) dest + y * width + x) = c;
1480 break;
1485 HeapFree(GetProcessHeap(), 0, mem);
1489 /* Read the framebuffer contents into a texture */
1490 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1492 IWineD3DDeviceImpl *device = This->resource.device;
1493 const struct wined3d_gl_info *gl_info;
1494 struct wined3d_context *context;
1495 GLint prevRead;
1497 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1498 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1499 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1501 context = context_acquire(device, This);
1502 gl_info = context->gl_info;
1504 surface_prepare_texture(This, gl_info, srgb);
1505 surface_bind_and_dirtify(This, srgb);
1507 ENTER_GL();
1508 glGetIntegerv(GL_READ_BUFFER, &prevRead);
1509 LEAVE_GL();
1511 /* Select the correct read buffer, and give some debug output.
1512 * There is no need to keep track of the current read buffer or reset it, every part of the code
1513 * that reads sets the read buffer as desired.
1515 if (!surface_is_offscreen(This))
1517 GLenum buffer = surface_get_gl_buffer(This);
1518 TRACE("Locking %#x buffer\n", buffer);
1520 ENTER_GL();
1521 glReadBuffer(buffer);
1522 checkGLcall("glReadBuffer");
1523 LEAVE_GL();
1525 else
1527 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1528 * Read from the back buffer
1530 TRACE("Locking offscreen render target\n");
1531 ENTER_GL();
1532 glReadBuffer(device->offscreenBuffer);
1533 checkGLcall("glReadBuffer");
1534 LEAVE_GL();
1537 ENTER_GL();
1538 /* If !SrcIsUpsideDown we should flip the surface.
1539 * This can be done using glCopyTexSubImage2D but this
1540 * is VERY slow, so don't do that. We should prevent
1541 * this code from getting called in such cases or perhaps
1542 * we can use FBOs */
1544 glCopyTexSubImage2D(This->texture_target, This->texture_level,
1545 0, 0, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1546 checkGLcall("glCopyTexSubImage2D");
1548 glReadBuffer(prevRead);
1549 checkGLcall("glReadBuffer");
1551 LEAVE_GL();
1553 context_release(context);
1555 TRACE("Updated target %d\n", This->texture_target);
1558 /* Context activation is done by the caller. */
1559 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl *surface,
1560 const struct wined3d_gl_info *gl_info, BOOL srgb)
1562 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1563 CONVERT_TYPES convert;
1564 struct wined3d_format_desc desc;
1566 if (surface->Flags & alloc_flag) return;
1568 d3dfmt_get_conv(surface, TRUE, TRUE, &desc, &convert);
1569 if(convert != NO_CONVERSION || desc.convert) surface->Flags |= SFLAG_CONVERTED;
1570 else surface->Flags &= ~SFLAG_CONVERTED;
1572 surface_bind_and_dirtify(surface, srgb);
1573 surface_allocate_surface(surface, gl_info, &desc, srgb);
1574 surface->Flags |= alloc_flag;
1577 /* Context activation is done by the caller. */
1578 void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1580 IWineD3DBaseTextureImpl *texture;
1582 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)surface,
1583 &IID_IWineD3DBaseTexture, (void **)&texture)))
1585 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
1586 UINT i;
1588 TRACE("surface %p is a subresource of texture %p.\n", surface, texture);
1590 for (i = 0; i < sub_count; ++i)
1592 IWineD3DSurfaceImpl *s = (IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i];
1593 surface_prepare_texture_internal(s, gl_info, srgb);
1596 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture);
1599 surface_prepare_texture_internal(surface, gl_info, srgb);
1602 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1604 IWineD3DDeviceImpl *device = This->resource.device;
1605 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1607 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1608 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1609 * changed
1611 if(!(This->Flags & SFLAG_DYNLOCK)) {
1612 This->lockCount++;
1613 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1614 if(This->lockCount > MAXLOCKCOUNT) {
1615 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1616 This->Flags |= SFLAG_DYNLOCK;
1620 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1621 * Also don't create a PBO for systemmem surfaces.
1623 if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->Flags & SFLAG_DYNLOCK)
1624 && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1625 && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1627 GLenum error;
1628 struct wined3d_context *context;
1630 context = context_acquire(device, NULL);
1631 ENTER_GL();
1633 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1634 error = glGetError();
1635 if(This->pbo == 0 || error != GL_NO_ERROR) {
1636 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1639 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1641 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1642 checkGLcall("glBindBufferARB");
1644 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1645 checkGLcall("glBufferDataARB");
1647 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1648 checkGLcall("glBindBufferARB");
1650 /* We don't need the system memory anymore and we can't even use it for PBOs */
1651 if(!(This->Flags & SFLAG_CLIENT)) {
1652 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1653 This->resource.heapMemory = NULL;
1655 This->resource.allocatedMemory = NULL;
1656 This->Flags |= SFLAG_PBO;
1657 LEAVE_GL();
1658 context_release(context);
1660 else if (!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO))
1662 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1663 * or a pbo to map
1665 if(!This->resource.heapMemory) {
1666 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1668 This->resource.allocatedMemory =
1669 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1670 if(This->Flags & SFLAG_INSYSMEM) {
1671 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1676 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
1677 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1678 IWineD3DDeviceImpl *device = This->resource.device;
1679 const RECT *pass_rect = pRect;
1681 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1682 iface, pLockedRect, wine_dbgstr_rect(pRect), Flags);
1684 /* This is also done in the base class, but we have to verify this before loading any data from
1685 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1686 * may interfere, and all other bad things may happen
1688 if (This->Flags & SFLAG_LOCKED) {
1689 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1690 return WINED3DERR_INVALIDCALL;
1692 This->Flags |= SFLAG_LOCKED;
1694 if (!(This->Flags & SFLAG_LOCKABLE))
1696 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1699 if (Flags & WINED3DLOCK_DISCARD) {
1700 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1701 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1702 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1703 This->Flags |= SFLAG_INSYSMEM;
1704 goto lock_end;
1707 if (This->Flags & SFLAG_INSYSMEM) {
1708 TRACE("Local copy is up to date, not downloading data\n");
1709 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1710 goto lock_end;
1713 /* IWineD3DSurface_LoadLocation() does not check if the rectangle specifies
1714 * the full surface. Most callers don't need that, so do it here. */
1715 if (pRect && pRect->top == 0 && pRect->left == 0
1716 && pRect->right == This->currentDesc.Width
1717 && pRect->bottom == This->currentDesc.Height)
1719 pass_rect = NULL;
1722 if (!(wined3d_settings.rendertargetlock_mode == RTL_DISABLE
1723 && ((This->Flags & SFLAG_SWAPCHAIN) || This == device->render_targets[0])))
1725 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pass_rect);
1728 lock_end:
1729 if (This->Flags & SFLAG_PBO)
1731 const struct wined3d_gl_info *gl_info;
1732 struct wined3d_context *context;
1734 context = context_acquire(device, NULL);
1735 gl_info = context->gl_info;
1737 ENTER_GL();
1738 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1739 checkGLcall("glBindBufferARB");
1741 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1742 if(This->resource.allocatedMemory) {
1743 ERR("The surface already has PBO memory allocated!\n");
1746 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1747 checkGLcall("glMapBufferARB");
1749 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1750 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1751 checkGLcall("glBindBufferARB");
1753 LEAVE_GL();
1754 context_release(context);
1757 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1758 /* Don't dirtify */
1759 } else {
1760 IWineD3DBaseTexture *pBaseTexture;
1762 * Dirtify on lock
1763 * as seen in msdn docs
1765 surface_add_dirty_rect(This, pRect);
1767 /** Dirtify Container if needed */
1768 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture))) {
1769 TRACE("Making container dirty\n");
1770 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1771 IWineD3DBaseTexture_Release(pBaseTexture);
1772 } else {
1773 TRACE("Surface is standalone, no need to dirty the container\n");
1777 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1780 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1781 GLint prev_store;
1782 GLint prev_rasterpos[4];
1783 GLint skipBytes = 0;
1784 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1785 IWineD3DDeviceImpl *device = This->resource.device;
1786 const struct wined3d_gl_info *gl_info;
1787 struct wined3d_context *context;
1789 /* Activate the correct context for the render target */
1790 context = context_acquire(device, This);
1791 context_apply_blit_state(context, device);
1792 gl_info = context->gl_info;
1794 ENTER_GL();
1796 if (!surface_is_offscreen(This))
1798 GLenum buffer = surface_get_gl_buffer(This);
1799 TRACE("Unlocking %#x buffer.\n", buffer);
1800 context_set_draw_buffer(context, buffer);
1802 else
1804 /* Primary offscreen render target */
1805 TRACE("Offscreen render target.\n");
1806 context_set_draw_buffer(context, device->offscreenBuffer);
1809 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1810 checkGLcall("glGetIntegerv");
1811 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1812 checkGLcall("glGetIntegerv");
1813 glPixelZoom(1.0f, -1.0f);
1814 checkGLcall("glPixelZoom");
1816 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1817 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1818 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1820 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1821 checkGLcall("glRasterPos3i");
1823 /* Some drivers(radeon dri, others?) don't like exceptions during
1824 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1825 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1826 * catch to put the dib section in InSync mode, which leads to a crash
1827 * and a blocked x server on my radeon card.
1829 * The following lines read the dib section so it is put in InSync mode
1830 * before glDrawPixels is called and the crash is prevented. There won't
1831 * be any interfering gdi accesses, because UnlockRect is called from
1832 * ReleaseDC, and the app won't use the dc any more afterwards.
1834 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1835 volatile BYTE read;
1836 read = This->resource.allocatedMemory[0];
1839 if(This->Flags & SFLAG_PBO) {
1840 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1841 checkGLcall("glBindBufferARB");
1844 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1845 if(This->Flags & SFLAG_LOCKED) {
1846 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1847 (This->lockedRect.bottom - This->lockedRect.top)-1,
1848 fmt, type,
1849 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1850 checkGLcall("glDrawPixels");
1851 } else {
1852 glDrawPixels(This->currentDesc.Width,
1853 This->currentDesc.Height,
1854 fmt, type, mem);
1855 checkGLcall("glDrawPixels");
1858 if(This->Flags & SFLAG_PBO) {
1859 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1860 checkGLcall("glBindBufferARB");
1863 glPixelZoom(1.0f, 1.0f);
1864 checkGLcall("glPixelZoom");
1866 glRasterPos3iv(&prev_rasterpos[0]);
1867 checkGLcall("glRasterPos3iv");
1869 /* Reset to previous pack row length */
1870 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1871 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH)");
1873 LEAVE_GL();
1874 context_release(context);
1877 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1878 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1879 IWineD3DDeviceImpl *device = This->resource.device;
1880 BOOL fullsurface;
1882 if (!(This->Flags & SFLAG_LOCKED)) {
1883 WARN("trying to Unlock an unlocked surf@%p\n", This);
1884 return WINEDDERR_NOTLOCKED;
1887 if (This->Flags & SFLAG_PBO)
1889 const struct wined3d_gl_info *gl_info;
1890 struct wined3d_context *context;
1892 TRACE("Freeing PBO memory\n");
1894 context = context_acquire(device, NULL);
1895 gl_info = context->gl_info;
1897 ENTER_GL();
1898 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1899 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1900 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1901 checkGLcall("glUnmapBufferARB");
1902 LEAVE_GL();
1903 context_release(context);
1905 This->resource.allocatedMemory = NULL;
1908 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1910 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1911 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1912 goto unlock_end;
1915 if ((This->Flags & SFLAG_SWAPCHAIN) || (device->render_targets && This == device->render_targets[0]))
1917 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1918 static BOOL warned = FALSE;
1919 if(!warned) {
1920 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1921 warned = TRUE;
1923 goto unlock_end;
1926 if(This->dirtyRect.left == 0 &&
1927 This->dirtyRect.top == 0 &&
1928 This->dirtyRect.right == This->currentDesc.Width &&
1929 This->dirtyRect.bottom == This->currentDesc.Height) {
1930 fullsurface = TRUE;
1931 } else {
1932 /* TODO: Proper partial rectangle tracking */
1933 fullsurface = FALSE;
1934 This->Flags |= SFLAG_INSYSMEM;
1937 switch(wined3d_settings.rendertargetlock_mode) {
1938 case RTL_READTEX:
1939 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1940 /* drop through */
1942 case RTL_READDRAW:
1943 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1944 break;
1947 if(!fullsurface) {
1948 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1949 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1950 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1951 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1952 * not fully up to date because only a subrectangle was read in LockRect.
1954 This->Flags &= ~SFLAG_INSYSMEM;
1955 This->Flags |= SFLAG_INDRAWABLE;
1958 This->dirtyRect.left = This->currentDesc.Width;
1959 This->dirtyRect.top = This->currentDesc.Height;
1960 This->dirtyRect.right = 0;
1961 This->dirtyRect.bottom = 0;
1963 else if (This == device->depth_stencil)
1965 FIXME("Depth Stencil buffer locking is not implemented\n");
1966 } else {
1967 /* The rest should be a normal texture */
1968 IWineD3DBaseTextureImpl *impl;
1969 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1970 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1971 * states need resetting
1973 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1974 if (impl->baseTexture.bindCount)
1975 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_SAMPLER(impl->baseTexture.sampler));
1976 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1980 unlock_end:
1981 This->Flags &= ~SFLAG_LOCKED;
1982 memset(&This->lockedRect, 0, sizeof(RECT));
1984 /* Overlays have to be redrawn manually after changes with the GL implementation */
1985 if(This->overlay_dest) {
1986 IWineD3DSurface_DrawOverlay(iface);
1988 return WINED3D_OK;
1991 static void surface_release_client_storage(IWineD3DSurfaceImpl *surface)
1993 struct wined3d_context *context;
1995 context = context_acquire(surface->resource.device, NULL);
1997 ENTER_GL();
1998 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1999 if (surface->texture_name)
2001 surface_bind_and_dirtify(surface, FALSE);
2002 glTexImage2D(surface->texture_target, surface->texture_level,
2003 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2005 if (surface->texture_name_srgb)
2007 surface_bind_and_dirtify(surface, TRUE);
2008 glTexImage2D(surface->texture_target, surface->texture_level,
2009 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2011 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
2013 LEAVE_GL();
2014 context_release(context);
2016 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)surface, SFLAG_INSRGBTEX, FALSE);
2017 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)surface, SFLAG_INTEXTURE, FALSE);
2018 surface_force_reload(surface);
2021 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
2023 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2024 WINED3DLOCKED_RECT lock;
2025 HRESULT hr;
2026 RGBQUAD col[256];
2028 TRACE("(%p)->(%p)\n",This,pHDC);
2030 if(This->Flags & SFLAG_USERPTR) {
2031 ERR("Not supported on surfaces with an application-provided surfaces\n");
2032 return WINEDDERR_NODC;
2035 /* Give more detailed info for ddraw */
2036 if (This->Flags & SFLAG_DCINUSE)
2037 return WINEDDERR_DCALREADYCREATED;
2039 /* Can't GetDC if the surface is locked */
2040 if (This->Flags & SFLAG_LOCKED)
2041 return WINED3DERR_INVALIDCALL;
2043 memset(&lock, 0, sizeof(lock)); /* To be sure */
2045 /* Create a DIB section if there isn't a hdc yet */
2046 if(!This->hDC) {
2047 if(This->Flags & SFLAG_CLIENT) {
2048 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2049 surface_release_client_storage(This);
2051 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
2052 if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2054 /* Use the dib section from now on if we are not using a PBO */
2055 if(!(This->Flags & SFLAG_PBO))
2056 This->resource.allocatedMemory = This->dib.bitmap_data;
2059 /* Lock the surface */
2060 hr = IWineD3DSurface_LockRect(iface,
2061 &lock,
2062 NULL,
2065 if(This->Flags & SFLAG_PBO) {
2066 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
2067 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2070 if(FAILED(hr)) {
2071 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
2072 /* keep the dib section */
2073 return hr;
2076 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
2077 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
2079 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2080 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2081 unsigned int n;
2082 const PALETTEENTRY *pal = NULL;
2084 if(This->palette) {
2085 pal = This->palette->palents;
2086 } else {
2087 IWineD3DSurfaceImpl *dds_primary;
2088 IWineD3DSwapChainImpl *swapchain;
2089 swapchain = (IWineD3DSwapChainImpl *)This->resource.device->swapchains[0];
2090 dds_primary = swapchain->front_buffer;
2091 if (dds_primary && dds_primary->palette)
2092 pal = dds_primary->palette->palents;
2095 if (pal) {
2096 for (n=0; n<256; n++) {
2097 col[n].rgbRed = pal[n].peRed;
2098 col[n].rgbGreen = pal[n].peGreen;
2099 col[n].rgbBlue = pal[n].peBlue;
2100 col[n].rgbReserved = 0;
2102 SetDIBColorTable(This->hDC, 0, 256, col);
2106 *pHDC = This->hDC;
2107 TRACE("returning %p\n",*pHDC);
2108 This->Flags |= SFLAG_DCINUSE;
2110 return WINED3D_OK;
2113 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2115 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2117 TRACE("(%p)->(%p)\n",This,hDC);
2119 if (!(This->Flags & SFLAG_DCINUSE))
2120 return WINEDDERR_NODC;
2122 if (This->hDC !=hDC) {
2123 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2124 return WINEDDERR_NODC;
2127 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
2128 /* Copy the contents of the DIB over to the PBO */
2129 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2132 /* we locked first, so unlock now */
2133 IWineD3DSurface_UnlockRect(iface);
2135 This->Flags &= ~SFLAG_DCINUSE;
2137 return WINED3D_OK;
2140 /* ******************************************************
2141 IWineD3DSurface Internal (No mapping to directx api) parts follow
2142 ****************************************************** */
2144 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, struct wined3d_format_desc *desc, CONVERT_TYPES *convert)
2146 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2147 IWineD3DDeviceImpl *device = This->resource.device;
2148 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2149 BOOL blit_supported = FALSE;
2151 /* Copy the default values from the surface. Below we might perform fixups */
2152 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2153 *desc = *This->resource.format_desc;
2154 *convert = NO_CONVERSION;
2156 /* Ok, now look if we have to do any conversion */
2157 switch(This->resource.format_desc->format)
2159 case WINED3DFMT_P8_UINT:
2160 /* ****************
2161 Paletted Texture
2162 **************** */
2164 /* Below the call to blit_supported is disabled for Wine 1.2 because the function isn't operating correctly yet.
2165 * At the moment 8-bit blits are handled in software and if certain GL extensions are around, surface conversion
2166 * is performed at upload time. The blit_supported call recognizes it as a destination fixup. This type of upload 'fixup'
2167 * and 8-bit to 8-bit blits need to be handled by the blit_shader.
2168 * TODO: get rid of this #if 0
2170 #if 0
2171 blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
2172 &rect, This->resource.usage, This->resource.pool,
2173 This->resource.format_desc, &rect, This->resource.usage,
2174 This->resource.pool, This->resource.format_desc);
2175 #endif
2176 blit_supported = gl_info->supported[EXT_PALETTED_TEXTURE] || gl_info->supported[ARB_FRAGMENT_PROGRAM];
2178 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2179 * texturing. Further also use conversion in case of color keying.
2180 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2181 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2182 * conflicts with this.
2184 if (!((blit_supported && device->render_targets && This == device->render_targets[0]))
2185 || colorkey_active || !use_texturing)
2187 desc->glFormat = GL_RGBA;
2188 desc->glInternal = GL_RGBA;
2189 desc->glType = GL_UNSIGNED_BYTE;
2190 desc->conv_byte_count = 4;
2191 if(colorkey_active) {
2192 *convert = CONVERT_PALETTED_CK;
2193 } else {
2194 *convert = CONVERT_PALETTED;
2197 break;
2199 case WINED3DFMT_B2G3R3_UNORM:
2200 /* **********************
2201 GL_UNSIGNED_BYTE_3_3_2
2202 ********************** */
2203 if (colorkey_active) {
2204 /* This texture format will never be used.. So do not care about color keying
2205 up until the point in time it will be needed :-) */
2206 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2208 break;
2210 case WINED3DFMT_B5G6R5_UNORM:
2211 if (colorkey_active) {
2212 *convert = CONVERT_CK_565;
2213 desc->glFormat = GL_RGBA;
2214 desc->glInternal = GL_RGB5_A1;
2215 desc->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2216 desc->conv_byte_count = 2;
2218 break;
2220 case WINED3DFMT_B5G5R5X1_UNORM:
2221 if (colorkey_active) {
2222 *convert = CONVERT_CK_5551;
2223 desc->glFormat = GL_BGRA;
2224 desc->glInternal = GL_RGB5_A1;
2225 desc->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2226 desc->conv_byte_count = 2;
2228 break;
2230 case WINED3DFMT_B8G8R8_UNORM:
2231 if (colorkey_active) {
2232 *convert = CONVERT_CK_RGB24;
2233 desc->glFormat = GL_RGBA;
2234 desc->glInternal = GL_RGBA8;
2235 desc->glType = GL_UNSIGNED_INT_8_8_8_8;
2236 desc->conv_byte_count = 4;
2238 break;
2240 case WINED3DFMT_B8G8R8X8_UNORM:
2241 if (colorkey_active) {
2242 *convert = CONVERT_RGB32_888;
2243 desc->glFormat = GL_RGBA;
2244 desc->glInternal = GL_RGBA8;
2245 desc->glType = GL_UNSIGNED_INT_8_8_8_8;
2246 desc->conv_byte_count = 4;
2248 break;
2250 default:
2251 break;
2254 return WINED3D_OK;
2257 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2259 IWineD3DDeviceImpl *device = This->resource.device;
2260 IWineD3DPaletteImpl *pal = This->palette;
2261 BOOL index_in_alpha = FALSE;
2262 unsigned int i;
2264 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2265 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2266 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2267 * duplicate entries. Store the color key in the unused alpha component to speed the
2268 * download up and to make conversion unneeded. */
2269 index_in_alpha = primary_render_target_is_p8(device);
2271 if (!pal)
2273 UINT dxVersion = ((IWineD3DImpl *)device->wined3d)->dxVersion;
2275 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2276 if (dxVersion <= 7)
2278 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2279 if (index_in_alpha)
2281 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2282 * there's no palette at this time. */
2283 for (i = 0; i < 256; i++) table[i][3] = i;
2286 else
2288 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2289 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2290 * capability flag is present (wine does advertise this capability) */
2291 for (i = 0; i < 256; ++i)
2293 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2294 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2295 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2296 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2300 else
2302 TRACE("Using surface palette %p\n", pal);
2303 /* Get the surface's palette */
2304 for (i = 0; i < 256; ++i)
2306 table[i][0] = pal->palents[i].peRed;
2307 table[i][1] = pal->palents[i].peGreen;
2308 table[i][2] = pal->palents[i].peBlue;
2310 /* When index_in_alpha is set the palette index is stored in the
2311 * alpha component. In case of a readback we can then read
2312 * GL_ALPHA. Color keying is handled in BltOverride using a
2313 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2314 * color key itself is passed to glAlphaFunc in other cases the
2315 * alpha component of pixels that should be masked away is set to 0. */
2316 if (index_in_alpha)
2318 table[i][3] = i;
2320 else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2321 && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2323 table[i][3] = 0x00;
2325 else if(pal->Flags & WINEDDPCAPS_ALPHA)
2327 table[i][3] = pal->palents[i].peFlags;
2329 else
2331 table[i][3] = 0xFF;
2337 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2338 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2340 const BYTE *source;
2341 BYTE *dest;
2342 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2344 switch (convert) {
2345 case NO_CONVERSION:
2347 memcpy(dst, src, pitch * height);
2348 break;
2350 case CONVERT_PALETTED:
2351 case CONVERT_PALETTED_CK:
2353 IWineD3DPaletteImpl* pal = This->palette;
2354 BYTE table[256][4];
2355 unsigned int x, y;
2357 if( pal == NULL) {
2358 /* TODO: If we are a sublevel, try to get the palette from level 0 */
2361 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2363 for (y = 0; y < height; y++)
2365 source = src + pitch * y;
2366 dest = dst + outpitch * y;
2367 /* This is an 1 bpp format, using the width here is fine */
2368 for (x = 0; x < width; x++) {
2369 BYTE color = *source++;
2370 *dest++ = table[color][0];
2371 *dest++ = table[color][1];
2372 *dest++ = table[color][2];
2373 *dest++ = table[color][3];
2377 break;
2379 case CONVERT_CK_565:
2381 /* Converting the 565 format in 5551 packed to emulate color-keying.
2383 Note : in all these conversion, it would be best to average the averaging
2384 pixels to get the color of the pixel that will be color-keyed to
2385 prevent 'color bleeding'. This will be done later on if ever it is
2386 too visible.
2388 Note2: Nvidia documents say that their driver does not support alpha + color keying
2389 on the same surface and disables color keying in such a case
2391 unsigned int x, y;
2392 const WORD *Source;
2393 WORD *Dest;
2395 TRACE("Color keyed 565\n");
2397 for (y = 0; y < height; y++) {
2398 Source = (const WORD *)(src + y * pitch);
2399 Dest = (WORD *) (dst + y * outpitch);
2400 for (x = 0; x < width; x++ ) {
2401 WORD color = *Source++;
2402 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2403 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2404 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2405 *Dest |= 0x0001;
2407 Dest++;
2411 break;
2413 case CONVERT_CK_5551:
2415 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2416 unsigned int x, y;
2417 const WORD *Source;
2418 WORD *Dest;
2419 TRACE("Color keyed 5551\n");
2420 for (y = 0; y < height; y++) {
2421 Source = (const WORD *)(src + y * pitch);
2422 Dest = (WORD *) (dst + y * outpitch);
2423 for (x = 0; x < width; x++ ) {
2424 WORD color = *Source++;
2425 *Dest = color;
2426 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2427 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2428 *Dest |= (1 << 15);
2430 else {
2431 *Dest &= ~(1 << 15);
2433 Dest++;
2437 break;
2439 case CONVERT_CK_RGB24:
2441 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2442 unsigned int x, y;
2443 for (y = 0; y < height; y++)
2445 source = src + pitch * y;
2446 dest = dst + outpitch * y;
2447 for (x = 0; x < width; x++) {
2448 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2449 DWORD dstcolor = color << 8;
2450 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2451 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2452 dstcolor |= 0xff;
2454 *(DWORD*)dest = dstcolor;
2455 source += 3;
2456 dest += 4;
2460 break;
2462 case CONVERT_RGB32_888:
2464 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2465 unsigned int x, y;
2466 for (y = 0; y < height; y++)
2468 source = src + pitch * y;
2469 dest = dst + outpitch * y;
2470 for (x = 0; x < width; x++) {
2471 DWORD color = 0xffffff & *(const DWORD*)source;
2472 DWORD dstcolor = color << 8;
2473 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2474 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2475 dstcolor |= 0xff;
2477 *(DWORD*)dest = dstcolor;
2478 source += 4;
2479 dest += 4;
2483 break;
2485 default:
2486 ERR("Unsupported conversion type %#x.\n", convert);
2488 return WINED3D_OK;
2491 BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2493 IWineD3DDeviceImpl *device = This->resource.device;
2495 if (This->palette || (This->resource.format_desc->format != WINED3DFMT_P8_UINT
2496 && This->resource.format_desc->format != WINED3DFMT_P8_UINT_A8_UNORM))
2498 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2499 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2501 return FALSE;
2504 if (This->palette9)
2506 if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2508 return FALSE;
2510 } else {
2511 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2513 memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2514 return TRUE;
2517 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2518 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2519 DWORD flag = srgb_mode ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
2521 if (!(This->Flags & flag)) {
2522 TRACE("Reloading because surface is dirty\n");
2523 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2524 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2525 /* Reload: vice versa OR */
2526 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2527 /* Also reload: Color key is active AND the color key has changed */
2528 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2529 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2530 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2531 TRACE("Reloading because of color keying\n");
2532 /* To perform the color key conversion we need a sysmem copy of
2533 * the surface. Make sure we have it
2536 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2537 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2538 /* TODO: This is not necessarily needed with hw palettized texture support */
2539 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2540 } else {
2541 TRACE("surface is already in texture\n");
2542 return WINED3D_OK;
2545 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2546 * These resources are not bound by device size or format restrictions. Because of this,
2547 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2548 * However, these resources can always be created, locked, and copied.
2550 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2552 FIXME("(%p) Operation not supported for scratch textures\n",This);
2553 return WINED3DERR_INVALIDCALL;
2556 IWineD3DSurface_LoadLocation(iface, flag, NULL /* no partial locking for textures yet */);
2558 if (!(This->Flags & SFLAG_DONOTFREE)) {
2559 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2560 This->resource.allocatedMemory = NULL;
2561 This->resource.heapMemory = NULL;
2562 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2565 return WINED3D_OK;
2568 /* Context activation is done by the caller. */
2569 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface, BOOL srgb) {
2570 /* TODO: check for locks */
2571 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2572 IWineD3DBaseTexture *baseTexture = NULL;
2574 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2575 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2576 TRACE("Passing to container\n");
2577 IWineD3DBaseTexture_BindTexture(baseTexture, srgb);
2578 IWineD3DBaseTexture_Release(baseTexture);
2580 else
2582 GLuint *name;
2584 TRACE("(%p) : Binding surface\n", This);
2586 name = srgb ? &This->texture_name_srgb : &This->texture_name;
2588 ENTER_GL();
2590 if (!This->texture_level)
2592 if (!*name) {
2593 glGenTextures(1, name);
2594 checkGLcall("glGenTextures");
2595 TRACE("Surface %p given name %d\n", This, *name);
2597 glBindTexture(This->texture_target, *name);
2598 checkGLcall("glBindTexture");
2599 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2600 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2601 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2602 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2603 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2604 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2605 glTexParameteri(This->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2606 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2607 glTexParameteri(This->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2608 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2610 /* This is where we should be reducing the amount of GLMemoryUsed */
2611 } else if (*name) {
2612 /* Mipmap surfaces should have a base texture container */
2613 ERR("Mipmap surface has a glTexture bound to it!\n");
2616 glBindTexture(This->texture_target, *name);
2617 checkGLcall("glBindTexture");
2619 LEAVE_GL();
2623 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2624 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2625 HRESULT hr;
2627 TRACE("(%p) : Calling base function first\n", This);
2628 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2629 if(SUCCEEDED(hr)) {
2630 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2631 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format_desc->glFormat,
2632 This->resource.format_desc->glInternal, This->resource.format_desc->glType);
2634 return hr;
2637 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2638 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2640 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2641 WARN("Surface is locked or the HDC is in use\n");
2642 return WINED3DERR_INVALIDCALL;
2645 if(Mem && Mem != This->resource.allocatedMemory) {
2646 void *release = NULL;
2648 /* Do I have to copy the old surface content? */
2649 if(This->Flags & SFLAG_DIBSECTION) {
2650 /* Release the DC. No need to hold the critical section for the update
2651 * Thread because this thread runs only on front buffers, but this method
2652 * fails for render targets in the check above.
2654 SelectObject(This->hDC, This->dib.holdbitmap);
2655 DeleteDC(This->hDC);
2656 /* Release the DIB section */
2657 DeleteObject(This->dib.DIBsection);
2658 This->dib.bitmap_data = NULL;
2659 This->resource.allocatedMemory = NULL;
2660 This->hDC = NULL;
2661 This->Flags &= ~SFLAG_DIBSECTION;
2662 } 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 | SFLAG_INSYSMEM;
2669 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2670 IWineD3DSurface_ModifyLocation(iface, 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);
2678 } else if(This->Flags & SFLAG_USERPTR) {
2679 /* LockRect and GetDC will re-create the dib section and allocated memory */
2680 This->resource.allocatedMemory = NULL;
2681 /* HeapMemory should be NULL already */
2682 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2683 This->Flags &= ~SFLAG_USERPTR;
2685 if (This->Flags & SFLAG_CLIENT)
2686 surface_release_client_storage(This);
2688 return WINED3D_OK;
2691 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2693 /* Flip the surface contents */
2694 /* Flip the DC */
2696 HDC tmp;
2697 tmp = front->hDC;
2698 front->hDC = back->hDC;
2699 back->hDC = tmp;
2702 /* Flip the DIBsection */
2704 HBITMAP tmp;
2705 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
2706 tmp = front->dib.DIBsection;
2707 front->dib.DIBsection = back->dib.DIBsection;
2708 back->dib.DIBsection = tmp;
2710 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
2711 else front->Flags &= ~SFLAG_DIBSECTION;
2712 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
2713 else back->Flags &= ~SFLAG_DIBSECTION;
2716 /* Flip the surface data */
2718 void* tmp;
2720 tmp = front->dib.bitmap_data;
2721 front->dib.bitmap_data = back->dib.bitmap_data;
2722 back->dib.bitmap_data = tmp;
2724 tmp = front->resource.allocatedMemory;
2725 front->resource.allocatedMemory = back->resource.allocatedMemory;
2726 back->resource.allocatedMemory = tmp;
2728 tmp = front->resource.heapMemory;
2729 front->resource.heapMemory = back->resource.heapMemory;
2730 back->resource.heapMemory = tmp;
2733 /* Flip the PBO */
2735 GLuint tmp_pbo = front->pbo;
2736 front->pbo = back->pbo;
2737 back->pbo = tmp_pbo;
2740 /* client_memory should not be different, but just in case */
2742 BOOL tmp;
2743 tmp = front->dib.client_memory;
2744 front->dib.client_memory = back->dib.client_memory;
2745 back->dib.client_memory = tmp;
2748 /* Flip the opengl texture */
2750 GLuint tmp;
2752 tmp = back->texture_name;
2753 back->texture_name = front->texture_name;
2754 front->texture_name = tmp;
2756 tmp = back->texture_name_srgb;
2757 back->texture_name_srgb = front->texture_name_srgb;
2758 front->texture_name_srgb = tmp;
2762 DWORD tmp_flags = back->Flags;
2763 back->Flags = front->Flags;
2764 front->Flags = tmp_flags;
2768 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2769 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2770 IWineD3DSwapChainImpl *swapchain = NULL;
2771 HRESULT hr;
2772 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2774 /* Flipping is only supported on RenderTargets and overlays*/
2775 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2776 WARN("Tried to flip a non-render target, non-overlay surface\n");
2777 return WINEDDERR_NOTFLIPPABLE;
2780 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2781 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2783 /* Update the overlay if it is visible */
2784 if(This->overlay_dest) {
2785 return IWineD3DSurface_DrawOverlay((IWineD3DSurface *) This);
2786 } else {
2787 return WINED3D_OK;
2791 if(override) {
2792 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2793 * FIXME("(%p) Target override is not supported by now\n", This);
2794 * Additionally, it isn't really possible to support triple-buffering
2795 * properly on opengl at all
2799 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2800 if(!swapchain) {
2801 ERR("Flipped surface is not on a swapchain\n");
2802 return WINEDDERR_NOTFLIPPABLE;
2805 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2806 * and only d3d8 and d3d9 apps specify the presentation interval
2808 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2809 /* Most common case first to avoid wasting time on all the other cases */
2810 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2811 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2812 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2813 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2814 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2815 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2816 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2817 } else {
2818 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2821 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2822 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *)swapchain,
2823 NULL, NULL, swapchain->win_handle, NULL, 0);
2824 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2825 return hr;
2828 /* Does a direct frame buffer -> texture copy. Stretching is done
2829 * with single pixel copy calls
2831 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2832 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2834 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2835 float xrel, yrel;
2836 UINT row;
2837 struct wined3d_context *context;
2838 BOOL upsidedown = FALSE;
2839 RECT dst_rect = *dst_rect_in;
2841 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2842 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2844 if(dst_rect.top > dst_rect.bottom) {
2845 UINT tmp = dst_rect.bottom;
2846 dst_rect.bottom = dst_rect.top;
2847 dst_rect.top = tmp;
2848 upsidedown = TRUE;
2851 context = context_acquire(device, src_surface);
2852 context_apply_blit_state(context, device);
2853 surface_internal_preload(dst_surface, SRGB_RGB);
2854 ENTER_GL();
2856 /* Bind the target texture */
2857 glBindTexture(dst_surface->texture_target, dst_surface->texture_name);
2858 checkGLcall("glBindTexture");
2859 if (surface_is_offscreen(src_surface))
2861 TRACE("Reading from an offscreen target\n");
2862 upsidedown = !upsidedown;
2863 glReadBuffer(device->offscreenBuffer);
2865 else
2867 glReadBuffer(surface_get_gl_buffer(src_surface));
2869 checkGLcall("glReadBuffer");
2871 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
2872 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
2874 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2876 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2878 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2879 ERR("Texture filtering not supported in direct blit\n");
2882 else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
2883 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2885 ERR("Texture filtering not supported in direct blit\n");
2888 if (upsidedown
2889 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2890 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2892 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2894 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2895 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
2896 src_rect->left, src_surface->currentDesc.Height - src_rect->bottom,
2897 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
2898 } else {
2899 UINT yoffset = src_surface->currentDesc.Height - src_rect->top + dst_rect.top - 1;
2900 /* I have to process this row by row to swap the image,
2901 * otherwise it would be upside down, so stretching in y direction
2902 * doesn't cost extra time
2904 * However, stretching in x direction can be avoided if not necessary
2906 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
2907 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2909 /* Well, that stuff works, but it's very slow.
2910 * find a better way instead
2912 UINT col;
2914 for (col = dst_rect.left; col < dst_rect.right; ++col)
2916 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2917 dst_rect.left + col /* x offset */, row /* y offset */,
2918 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
2921 else
2923 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2924 dst_rect.left /* x offset */, row /* y offset */,
2925 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
2929 checkGLcall("glCopyTexSubImage2D");
2931 LEAVE_GL();
2932 context_release(context);
2934 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2935 * path is never entered
2937 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)dst_surface, SFLAG_INTEXTURE, TRUE);
2940 /* Uses the hardware to stretch and flip the image */
2941 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2942 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2944 IWineD3DDeviceImpl *device = dst_surface->resource.device;
2945 GLuint src, backup = 0;
2946 IWineD3DSwapChainImpl *src_swapchain = NULL;
2947 float left, right, top, bottom; /* Texture coordinates */
2948 UINT fbwidth = src_surface->currentDesc.Width;
2949 UINT fbheight = src_surface->currentDesc.Height;
2950 struct wined3d_context *context;
2951 GLenum drawBuffer = GL_BACK;
2952 GLenum texture_target;
2953 BOOL noBackBufferBackup;
2954 BOOL src_offscreen;
2955 BOOL upsidedown = FALSE;
2956 RECT dst_rect = *dst_rect_in;
2958 TRACE("Using hwstretch blit\n");
2959 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2960 context = context_acquire(device, src_surface);
2961 context_apply_blit_state(context, device);
2962 surface_internal_preload(dst_surface, SRGB_RGB);
2964 src_offscreen = surface_is_offscreen(src_surface);
2965 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2966 if (!noBackBufferBackup && !src_surface->texture_name)
2968 /* Get it a description */
2969 surface_internal_preload(src_surface, SRGB_RGB);
2971 ENTER_GL();
2973 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2974 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2976 if (context->aux_buffers >= 2)
2978 /* Got more than one aux buffer? Use the 2nd aux buffer */
2979 drawBuffer = GL_AUX1;
2981 else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
2983 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2984 drawBuffer = GL_AUX0;
2987 if(noBackBufferBackup) {
2988 glGenTextures(1, &backup);
2989 checkGLcall("glGenTextures");
2990 glBindTexture(GL_TEXTURE_2D, backup);
2991 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2992 texture_target = GL_TEXTURE_2D;
2993 } else {
2994 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2995 * we are reading from the back buffer, the backup can be used as source texture
2997 texture_target = src_surface->texture_target;
2998 glBindTexture(texture_target, src_surface->texture_name);
2999 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
3000 glEnable(texture_target);
3001 checkGLcall("glEnable(texture_target)");
3003 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3004 src_surface->Flags &= ~SFLAG_INTEXTURE;
3007 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3008 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3010 if(dst_rect.top > dst_rect.bottom) {
3011 UINT tmp = dst_rect.bottom;
3012 dst_rect.bottom = dst_rect.top;
3013 dst_rect.top = tmp;
3014 upsidedown = TRUE;
3017 if (src_offscreen)
3019 TRACE("Reading from an offscreen target\n");
3020 upsidedown = !upsidedown;
3021 glReadBuffer(device->offscreenBuffer);
3023 else
3025 glReadBuffer(surface_get_gl_buffer(src_surface));
3028 /* TODO: Only back up the part that will be overwritten */
3029 glCopyTexSubImage2D(texture_target, 0,
3030 0, 0 /* read offsets */,
3031 0, 0,
3032 fbwidth,
3033 fbheight);
3035 checkGLcall("glCopyTexSubImage2D");
3037 /* No issue with overriding these - the sampler is dirty due to blit usage */
3038 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3039 wined3d_gl_mag_filter(magLookup, Filter));
3040 checkGLcall("glTexParameteri");
3041 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3042 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3043 checkGLcall("glTexParameteri");
3045 IWineD3DSurface_GetContainer((IWineD3DSurface *)src_surface, &IID_IWineD3DSwapChain, (void **)&src_swapchain);
3046 if (src_swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *)src_swapchain);
3047 if (!src_swapchain || src_surface == src_swapchain->back_buffers[0])
3049 src = backup ? backup : src_surface->texture_name;
3051 else
3053 glReadBuffer(GL_FRONT);
3054 checkGLcall("glReadBuffer(GL_FRONT)");
3056 glGenTextures(1, &src);
3057 checkGLcall("glGenTextures(1, &src)");
3058 glBindTexture(GL_TEXTURE_2D, src);
3059 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3061 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3062 * out for power of 2 sizes
3064 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3065 src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3066 checkGLcall("glTexImage2D");
3067 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3068 0, 0 /* read offsets */,
3069 0, 0,
3070 fbwidth,
3071 fbheight);
3073 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3074 checkGLcall("glTexParameteri");
3075 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3076 checkGLcall("glTexParameteri");
3078 glReadBuffer(GL_BACK);
3079 checkGLcall("glReadBuffer(GL_BACK)");
3081 if(texture_target != GL_TEXTURE_2D) {
3082 glDisable(texture_target);
3083 glEnable(GL_TEXTURE_2D);
3084 texture_target = GL_TEXTURE_2D;
3087 checkGLcall("glEnd and previous");
3089 left = src_rect->left;
3090 right = src_rect->right;
3092 if (upsidedown)
3094 top = src_surface->currentDesc.Height - src_rect->top;
3095 bottom = src_surface->currentDesc.Height - src_rect->bottom;
3097 else
3099 top = src_surface->currentDesc.Height - src_rect->bottom;
3100 bottom = src_surface->currentDesc.Height - src_rect->top;
3103 if (src_surface->Flags & SFLAG_NORMCOORD)
3105 left /= src_surface->pow2Width;
3106 right /= src_surface->pow2Width;
3107 top /= src_surface->pow2Height;
3108 bottom /= src_surface->pow2Height;
3111 /* draw the source texture stretched and upside down. The correct surface is bound already */
3112 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3113 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3115 context_set_draw_buffer(context, drawBuffer);
3116 glReadBuffer(drawBuffer);
3118 glBegin(GL_QUADS);
3119 /* bottom left */
3120 glTexCoord2f(left, bottom);
3121 glVertex2i(0, fbheight);
3123 /* top left */
3124 glTexCoord2f(left, top);
3125 glVertex2i(0, fbheight - dst_rect.bottom - dst_rect.top);
3127 /* top right */
3128 glTexCoord2f(right, top);
3129 glVertex2i(dst_rect.right - dst_rect.left, fbheight - dst_rect.bottom - dst_rect.top);
3131 /* bottom right */
3132 glTexCoord2f(right, bottom);
3133 glVertex2i(dst_rect.right - dst_rect.left, fbheight);
3134 glEnd();
3135 checkGLcall("glEnd and previous");
3137 if (texture_target != dst_surface->texture_target)
3139 glDisable(texture_target);
3140 glEnable(dst_surface->texture_target);
3141 texture_target = dst_surface->texture_target;
3144 /* Now read the stretched and upside down image into the destination texture */
3145 glBindTexture(texture_target, dst_surface->texture_name);
3146 checkGLcall("glBindTexture");
3147 glCopyTexSubImage2D(texture_target,
3149 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3150 0, 0, /* We blitted the image to the origin */
3151 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3152 checkGLcall("glCopyTexSubImage2D");
3154 if(drawBuffer == GL_BACK) {
3155 /* Write the back buffer backup back */
3156 if(backup) {
3157 if(texture_target != GL_TEXTURE_2D) {
3158 glDisable(texture_target);
3159 glEnable(GL_TEXTURE_2D);
3160 texture_target = GL_TEXTURE_2D;
3162 glBindTexture(GL_TEXTURE_2D, backup);
3163 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3165 else
3167 if (texture_target != src_surface->texture_target)
3169 glDisable(texture_target);
3170 glEnable(src_surface->texture_target);
3171 texture_target = src_surface->texture_target;
3173 glBindTexture(src_surface->texture_target, src_surface->texture_name);
3174 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3177 glBegin(GL_QUADS);
3178 /* top left */
3179 glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
3180 glVertex2i(0, 0);
3182 /* bottom left */
3183 glTexCoord2f(0.0f, 0.0f);
3184 glVertex2i(0, fbheight);
3186 /* bottom right */
3187 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
3188 glVertex2i(fbwidth, src_surface->currentDesc.Height);
3190 /* top right */
3191 glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3192 (float)fbheight / (float)src_surface->pow2Height);
3193 glVertex2i(fbwidth, 0);
3194 glEnd();
3196 glDisable(texture_target);
3197 checkGLcall("glDisable(texture_target)");
3199 /* Cleanup */
3200 if (src != src_surface->texture_name && src != backup)
3202 glDeleteTextures(1, &src);
3203 checkGLcall("glDeleteTextures(1, &src)");
3205 if(backup) {
3206 glDeleteTextures(1, &backup);
3207 checkGLcall("glDeleteTextures(1, &backup)");
3210 LEAVE_GL();
3212 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3214 context_release(context);
3216 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3217 * path is never entered
3219 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)dst_surface, SFLAG_INTEXTURE, TRUE);
3222 /* Until the blit_shader is ready, define some prototypes here. */
3223 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
3224 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
3225 const struct wined3d_format_desc *src_format_desc,
3226 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
3227 const struct wined3d_format_desc *dst_format_desc);
3229 /* Front buffer coordinates are always full screen coordinates, but our GL
3230 * drawable is limited to the window's client area. The sysmem and texture
3231 * copies do have the full screen size. Note that GL has a bottom-left
3232 * origin, while D3D has a top-left origin. */
3233 void surface_translate_frontbuffer_coords(IWineD3DSurfaceImpl *surface, HWND window, RECT *rect)
3235 POINT offset = {0, surface->currentDesc.Height};
3236 RECT windowsize;
3238 GetClientRect(window, &windowsize);
3239 offset.y -= windowsize.bottom - windowsize.top;
3240 ScreenToClient(window, &offset);
3241 OffsetRect(rect, offset.x, offset.y);
3244 /* Not called from the VTable */
3245 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *dst_surface, const RECT *DestRect,
3246 IWineD3DSurfaceImpl *src_surface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx,
3247 WINED3DTEXTUREFILTERTYPE Filter)
3249 IWineD3DDeviceImpl *device = dst_surface->resource.device;
3250 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3251 RECT dst_rect, src_rect;
3253 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3254 dst_surface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3255 Flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3257 /* Get the swapchain. One of the surfaces has to be a primary surface */
3258 if (dst_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3260 WARN("Destination is in sysmem, rejecting gl blt\n");
3261 return WINED3DERR_INVALIDCALL;
3263 IWineD3DSurface_GetContainer((IWineD3DSurface *)dst_surface, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
3264 if (dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *)dstSwapchain);
3265 if (src_surface)
3267 if (src_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3269 WARN("Src is in sysmem, rejecting gl blt\n");
3270 return WINED3DERR_INVALIDCALL;
3272 IWineD3DSurface_GetContainer((IWineD3DSurface *)src_surface, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3273 if (srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *)srcSwapchain);
3276 /* Early sort out of cases where no render target is used */
3277 if (!dstSwapchain && !srcSwapchain
3278 && src_surface != device->render_targets[0]
3279 && dst_surface != device->render_targets[0])
3281 TRACE("No surface is render target, not using hardware blit.\n");
3282 return WINED3DERR_INVALIDCALL;
3285 /* No destination color keying supported */
3286 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3287 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3288 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3289 return WINED3DERR_INVALIDCALL;
3292 surface_get_rect(dst_surface, DestRect, &dst_rect);
3293 if (src_surface) surface_get_rect(src_surface, SrcRect, &src_rect);
3295 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3296 if (dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->back_buffers
3297 && dst_surface == dstSwapchain->front_buffer
3298 && src_surface == dstSwapchain->back_buffers[0])
3300 /* Half-life does a Blt from the back buffer to the front buffer,
3301 * Full surface size, no flags... Use present instead
3303 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3306 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3307 while(1)
3309 TRACE("Looking if a Present can be done...\n");
3310 /* Source Rectangle must be full surface */
3311 if (src_rect.left || src_rect.top
3312 || src_rect.right != src_surface->currentDesc.Width
3313 || src_rect.bottom != src_surface->currentDesc.Height)
3315 TRACE("No, Source rectangle doesn't match\n");
3316 break;
3319 /* No stretching may occur */
3320 if(src_rect.right != dst_rect.right - dst_rect.left ||
3321 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3322 TRACE("No, stretching is done\n");
3323 break;
3326 /* Destination must be full surface or match the clipping rectangle */
3327 if (dst_surface->clipper && ((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd)
3329 RECT cliprect;
3330 POINT pos[2];
3331 GetClientRect(((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd, &cliprect);
3332 pos[0].x = dst_rect.left;
3333 pos[0].y = dst_rect.top;
3334 pos[1].x = dst_rect.right;
3335 pos[1].y = dst_rect.bottom;
3336 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *)dst_surface->clipper)->hWnd, pos, 2);
3338 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3339 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3341 TRACE("No, dest rectangle doesn't match(clipper)\n");
3342 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3343 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3344 break;
3347 else if (dst_rect.left || dst_rect.top
3348 || dst_rect.right != dst_surface->currentDesc.Width
3349 || dst_rect.bottom != dst_surface->currentDesc.Height)
3351 TRACE("No, dest rectangle doesn't match(surface size)\n");
3352 break;
3355 TRACE("Yes\n");
3357 /* These flags are unimportant for the flag check, remove them */
3358 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3359 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3361 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3362 * take very long, while a flip is fast.
3363 * This applies to Half-Life, which does such Blts every time it finished
3364 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3365 * menu. This is also used by all apps when they do windowed rendering
3367 * The problem is that flipping is not really the same as copying. After a
3368 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3369 * untouched. Therefore it's necessary to override the swap effect
3370 * and to set it back after the flip.
3372 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3373 * testcases.
3376 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3377 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3379 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3380 IWineD3DSwapChain_Present((IWineD3DSwapChain *)dstSwapchain,
3381 NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3383 dstSwapchain->presentParms.SwapEffect = orig_swap;
3385 return WINED3D_OK;
3387 break;
3390 TRACE("Unsupported blit between buffers on the same swapchain\n");
3391 return WINED3DERR_INVALIDCALL;
3392 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3393 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3394 return WINED3DERR_INVALIDCALL;
3395 } else if(dstSwapchain && srcSwapchain) {
3396 FIXME("Implement hardware blit between two different swapchains\n");
3397 return WINED3DERR_INVALIDCALL;
3399 else if (dstSwapchain)
3401 /* Handled with regular texture -> swapchain blit */
3402 if (src_surface == device->render_targets[0])
3403 TRACE("Blit from active render target to a swapchain\n");
3405 else if (srcSwapchain && dst_surface == device->render_targets[0])
3407 FIXME("Implement blit from a swapchain to the active render target\n");
3408 return WINED3DERR_INVALIDCALL;
3411 if ((srcSwapchain || src_surface == device->render_targets[0]) && !dstSwapchain)
3413 /* Blit from render target to texture */
3414 BOOL stretchx;
3416 /* P8 read back is not implemented */
3417 if (src_surface->resource.format_desc->format == WINED3DFMT_P8_UINT
3418 || dst_surface->resource.format_desc->format == WINED3DFMT_P8_UINT)
3420 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3421 return WINED3DERR_INVALIDCALL;
3424 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3425 TRACE("Color keying not supported by frame buffer to texture blit\n");
3426 return WINED3DERR_INVALIDCALL;
3427 /* Destination color key is checked above */
3430 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3431 stretchx = TRUE;
3432 } else {
3433 stretchx = FALSE;
3436 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3437 * flip the image nor scale it.
3439 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3440 * -> If the app wants a image width an unscaled width, copy it line per line
3441 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3442 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3443 * back buffer. This is slower than reading line per line, thus not used for flipping
3444 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3445 * pixel by pixel
3447 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3448 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3449 * backends.
3451 if (fbo_blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
3452 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format_desc,
3453 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format_desc))
3455 stretch_rect_fbo(device, src_surface, &src_rect, dst_surface, &dst_rect, Filter);
3457 else if (!stretchx || dst_rect.right - dst_rect.left > src_surface->currentDesc.Width
3458 || dst_rect.bottom - dst_rect.top > src_surface->currentDesc.Height)
3460 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3461 fb_copy_to_texture_direct(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3462 } else {
3463 TRACE("Using hardware stretching to flip / stretch the texture\n");
3464 fb_copy_to_texture_hwstretch(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3467 if (!(dst_surface->Flags & SFLAG_DONOTFREE))
3469 HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory);
3470 dst_surface->resource.allocatedMemory = NULL;
3471 dst_surface->resource.heapMemory = NULL;
3473 else
3475 dst_surface->Flags &= ~SFLAG_INSYSMEM;
3478 return WINED3D_OK;
3480 else if (src_surface)
3482 /* Blit from offscreen surface to render target */
3483 DWORD oldCKeyFlags = src_surface->CKeyFlags;
3484 WINEDDCOLORKEY oldBltCKey = src_surface->SrcBltCKey;
3485 struct wined3d_context *context;
3487 TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
3489 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3490 && fbo_blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
3491 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3492 src_surface->resource.format_desc,
3493 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3494 dst_surface->resource.format_desc))
3496 TRACE("Using stretch_rect_fbo\n");
3497 /* The source is always a texture, but never the currently active render target, and the texture
3498 * contents are never upside down. */
3499 stretch_rect_fbo(device, src_surface, &src_rect, dst_surface, &dst_rect, Filter);
3500 return WINED3D_OK;
3503 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3504 && arbfp_blit.blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
3505 &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3506 src_surface->resource.format_desc,
3507 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3508 dst_surface->resource.format_desc))
3510 return arbfp_blit_surface(device, src_surface, &src_rect, dst_surface, &dst_rect, BLIT_OP_BLIT, Filter);
3513 /* Color keying: Check if we have to do a color keyed blt,
3514 * and if not check if a color key is activated.
3516 * Just modify the color keying parameters in the surface and restore them afterwards
3517 * The surface keeps track of the color key last used to load the opengl surface.
3518 * PreLoad will catch the change to the flags and color key and reload if necessary.
3520 if(Flags & WINEDDBLT_KEYSRC) {
3521 /* Use color key from surface */
3522 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3523 /* Use color key from DDBltFx */
3524 src_surface->CKeyFlags |= WINEDDSD_CKSRCBLT;
3525 src_surface->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3526 } else {
3527 /* Do not use color key */
3528 src_surface->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3531 /* Now load the surface */
3532 surface_internal_preload(src_surface, SRGB_RGB);
3534 /* Activate the destination context, set it up for blitting */
3535 context = context_acquire(device, dst_surface);
3536 context_apply_blit_state(context, device);
3538 if (dstSwapchain && dst_surface == dstSwapchain->front_buffer)
3539 surface_translate_frontbuffer_coords(dst_surface, context->win_handle, &dst_rect);
3541 if (!device->blitter->blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
3542 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format_desc,
3543 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format_desc))
3545 FIXME("Unsupported blit operation falling back to software\n");
3546 return WINED3DERR_INVALIDCALL;
3549 device->blitter->set_shader((IWineD3DDevice *)device, src_surface);
3551 ENTER_GL();
3553 /* This is for color keying */
3554 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3555 glEnable(GL_ALPHA_TEST);
3556 checkGLcall("glEnable(GL_ALPHA_TEST)");
3558 /* When the primary render target uses P8, the alpha component contains the palette index.
3559 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3560 * should be masked away have alpha set to 0. */
3561 if (primary_render_target_is_p8(device))
3562 glAlphaFunc(GL_NOTEQUAL, (float)src_surface->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3563 else
3564 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3565 checkGLcall("glAlphaFunc");
3566 } else {
3567 glDisable(GL_ALPHA_TEST);
3568 checkGLcall("glDisable(GL_ALPHA_TEST)");
3571 /* Draw a textured quad
3573 draw_textured_quad(src_surface, &src_rect, &dst_rect, Filter);
3575 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3576 glDisable(GL_ALPHA_TEST);
3577 checkGLcall("glDisable(GL_ALPHA_TEST)");
3580 /* Restore the color key parameters */
3581 src_surface->CKeyFlags = oldCKeyFlags;
3582 src_surface->SrcBltCKey = oldBltCKey;
3584 LEAVE_GL();
3586 /* Leave the opengl state valid for blitting */
3587 device->blitter->unset_shader((IWineD3DDevice *)device);
3589 if (wined3d_settings.strict_draw_ordering || (dstSwapchain
3590 && (dst_surface == dstSwapchain->front_buffer
3591 || dstSwapchain->num_contexts > 1)))
3592 wglFlush(); /* Flush to ensure ordering across contexts. */
3594 context_release(context);
3596 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3597 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3598 * is outdated now
3600 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)dst_surface, SFLAG_INDRAWABLE, TRUE);
3602 return WINED3D_OK;
3603 } else {
3604 /* Source-Less Blit to render target */
3605 if (Flags & WINEDDBLT_COLORFILL) {
3606 DWORD color;
3608 TRACE("Colorfill\n");
3610 /* The color as given in the Blt function is in the format of the frame-buffer...
3611 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3613 if (!surface_convert_color_to_argb(dst_surface, DDBltFx->u5.dwFillColor, &color))
3615 /* The color conversion function already prints an error, so need to do it here */
3616 return WINED3DERR_INVALIDCALL;
3619 if (ffp_blit.blit_supported(&device->adapter->gl_info, BLIT_OP_COLOR_FILL,
3620 NULL, 0, 0, NULL,
3621 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3622 dst_surface->resource.format_desc))
3624 return ffp_blit.color_fill(device, dst_surface, &dst_rect, color);
3626 else if (cpu_blit.blit_supported(&device->adapter->gl_info, BLIT_OP_COLOR_FILL,
3627 NULL, 0, 0, NULL,
3628 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3629 dst_surface->resource.format_desc))
3631 return cpu_blit.color_fill(device, dst_surface, &dst_rect, color);
3633 return WINED3DERR_INVALIDCALL;
3637 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3638 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3639 return WINED3DERR_INVALIDCALL;
3642 static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3643 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx)
3645 IWineD3DDeviceImpl *device = This->resource.device;
3646 float depth;
3648 if (Flags & WINEDDBLT_DEPTHFILL) {
3649 switch(This->resource.format_desc->format)
3651 case WINED3DFMT_D16_UNORM:
3652 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3653 break;
3654 case WINED3DFMT_S1_UINT_D15_UNORM:
3655 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3656 break;
3657 case WINED3DFMT_D24_UNORM_S8_UINT:
3658 case WINED3DFMT_X8D24_UNORM:
3659 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3660 break;
3661 case WINED3DFMT_D32_UNORM:
3662 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3663 break;
3664 default:
3665 depth = 0.0f;
3666 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format_desc->format));
3669 return IWineD3DDevice_Clear((IWineD3DDevice *)device, DestRect ? 1 : 0, (const WINED3DRECT *)DestRect,
3670 WINED3DCLEAR_ZBUFFER, 0x00000000, depth, 0x00000000);
3673 FIXME("(%p): Unsupp depthstencil blit\n", This);
3674 return WINED3DERR_INVALIDCALL;
3677 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect, IWineD3DSurface *SrcSurface,
3678 const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3679 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3680 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3681 IWineD3DDeviceImpl *device = This->resource.device;
3683 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3684 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3686 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3688 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3689 return WINEDDERR_SURFACEBUSY;
3692 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3693 * except depth blits, which seem to work
3695 if (This == device->depth_stencil || (Src && Src == device->depth_stencil))
3697 if (device->inScene && !(Flags & WINEDDBLT_DEPTHFILL))
3699 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3700 return WINED3DERR_INVALIDCALL;
3701 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3702 TRACE("Z Blit override handled the blit\n");
3703 return WINED3D_OK;
3707 /* Special cases for RenderTargets */
3708 if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3709 || (Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET)))
3711 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This, DestRect, Src, SrcRect, Flags, DDBltFx, Filter)))
3712 return WINED3D_OK;
3715 /* For the rest call the X11 surface implementation.
3716 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3717 * other Blts are rather rare
3719 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3722 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3723 IWineD3DSurface *Source, const RECT *rsrc, DWORD trans)
3725 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3726 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3727 IWineD3DDeviceImpl *device = This->resource.device;
3729 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3731 if ( (This->Flags & SFLAG_LOCKED) || (srcImpl->Flags & SFLAG_LOCKED))
3733 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3734 return WINEDDERR_SURFACEBUSY;
3737 if (device->inScene && (This == device->depth_stencil || srcImpl == device->depth_stencil))
3739 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3740 return WINED3DERR_INVALIDCALL;
3743 /* Special cases for RenderTargets */
3744 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3745 (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) ) {
3747 RECT SrcRect, DstRect;
3748 DWORD Flags=0;
3750 surface_get_rect(srcImpl, rsrc, &SrcRect);
3752 DstRect.left = dstx;
3753 DstRect.top=dsty;
3754 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3755 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3757 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3758 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3759 Flags |= WINEDDBLT_KEYSRC;
3760 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3761 Flags |= WINEDDBLT_KEYDEST;
3762 if(trans & WINEDDBLTFAST_WAIT)
3763 Flags |= WINEDDBLT_WAIT;
3764 if(trans & WINEDDBLTFAST_DONOTWAIT)
3765 Flags |= WINEDDBLT_DONOTWAIT;
3767 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This,
3768 &DstRect, srcImpl, &SrcRect, Flags, NULL, WINED3DTEXF_POINT)))
3769 return WINED3D_OK;
3773 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3776 static HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface)
3778 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3779 RGBQUAD col[256];
3780 IWineD3DPaletteImpl *pal = This->palette;
3781 unsigned int n;
3782 TRACE("(%p)\n", This);
3784 if (!pal) return WINED3D_OK;
3786 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
3787 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
3789 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3791 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3792 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
3794 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3795 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
3796 } else {
3797 if(!(This->Flags & SFLAG_INSYSMEM)) {
3798 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3799 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3801 TRACE("Dirtifying surface\n");
3802 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3806 if(This->Flags & SFLAG_DIBSECTION) {
3807 TRACE("(%p): Updating the hdc's palette\n", This);
3808 for (n=0; n<256; n++) {
3809 col[n].rgbRed = pal->palents[n].peRed;
3810 col[n].rgbGreen = pal->palents[n].peGreen;
3811 col[n].rgbBlue = pal->palents[n].peBlue;
3812 col[n].rgbReserved = 0;
3814 SetDIBColorTable(This->hDC, 0, 256, col);
3817 /* Propagate the changes to the drawable when we have a palette. */
3818 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3819 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3821 return WINED3D_OK;
3824 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3825 /** Check against the maximum texture sizes supported by the video card **/
3826 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3827 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
3828 unsigned int pow2Width, pow2Height;
3830 This->texture_name = 0;
3831 This->texture_target = GL_TEXTURE_2D;
3833 /* Non-power2 support */
3834 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINE_NORMALIZED_TEXRECT])
3836 pow2Width = This->currentDesc.Width;
3837 pow2Height = This->currentDesc.Height;
3839 else
3841 /* Find the nearest pow2 match */
3842 pow2Width = pow2Height = 1;
3843 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3844 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3846 This->pow2Width = pow2Width;
3847 This->pow2Height = pow2Height;
3849 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3850 /** TODO: add support for non power two compressed textures **/
3851 if (This->resource.format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
3853 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3854 This, This->currentDesc.Width, This->currentDesc.Height);
3855 return WINED3DERR_NOTAVAILABLE;
3859 if(pow2Width != This->currentDesc.Width ||
3860 pow2Height != This->currentDesc.Height) {
3861 This->Flags |= SFLAG_NONPOW2;
3864 TRACE("%p\n", This);
3865 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
3866 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
3868 /* one of three options
3869 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)
3870 2: Set the texture to the maximum size (bad idea)
3871 3: WARN and return WINED3DERR_NOTAVAILABLE;
3872 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.
3874 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
3876 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
3877 return WINED3DERR_NOTAVAILABLE;
3880 /* We should never use this surface in combination with OpenGL! */
3881 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
3883 else
3885 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3886 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3887 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3889 if (This->Flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
3890 && !(This->resource.format_desc->format == WINED3DFMT_P8_UINT
3891 && gl_info->supported[EXT_PALETTED_TEXTURE]
3892 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
3894 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
3895 This->pow2Width = This->currentDesc.Width;
3896 This->pow2Height = This->currentDesc.Height;
3897 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
3901 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3902 switch(wined3d_settings.offscreen_rendering_mode) {
3903 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3904 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3908 This->Flags |= SFLAG_INSYSMEM;
3910 return WINED3D_OK;
3913 /* GL locking is done by the caller */
3914 static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
3915 GLuint texture, GLsizei w, GLsizei h, GLenum target)
3917 IWineD3DDeviceImpl *device = This->resource.device;
3918 GLint compare_mode = GL_NONE;
3919 struct blt_info info;
3920 GLint old_binding = 0;
3922 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
3924 glDisable(GL_CULL_FACE);
3925 glDisable(GL_BLEND);
3926 glDisable(GL_ALPHA_TEST);
3927 glDisable(GL_SCISSOR_TEST);
3928 glDisable(GL_STENCIL_TEST);
3929 glEnable(GL_DEPTH_TEST);
3930 glDepthFunc(GL_ALWAYS);
3931 glDepthMask(GL_TRUE);
3932 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
3933 glViewport(0, 0, w, h);
3935 surface_get_blt_info(target, NULL, w, h, &info);
3936 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
3937 glGetIntegerv(info.binding, &old_binding);
3938 glBindTexture(info.bind_target, texture);
3939 if (gl_info->supported[ARB_SHADOW])
3941 glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
3942 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
3945 device->shader_backend->shader_select_depth_blt((IWineD3DDevice *)device,
3946 info.tex_type, &This->ds_current_size);
3948 glBegin(GL_TRIANGLE_STRIP);
3949 glTexCoord3fv(info.coords[0]);
3950 glVertex2f(-1.0f, -1.0f);
3951 glTexCoord3fv(info.coords[1]);
3952 glVertex2f(1.0f, -1.0f);
3953 glTexCoord3fv(info.coords[2]);
3954 glVertex2f(-1.0f, 1.0f);
3955 glTexCoord3fv(info.coords[3]);
3956 glVertex2f(1.0f, 1.0f);
3957 glEnd();
3959 if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
3960 glBindTexture(info.bind_target, old_binding);
3962 glPopAttrib();
3964 device->shader_backend->shader_deselect_depth_blt((IWineD3DDevice *)device);
3967 void surface_modify_ds_location(IWineD3DSurfaceImpl *surface,
3968 DWORD location, UINT w, UINT h)
3970 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);
3972 if (location & ~SFLAG_DS_LOCATIONS)
3973 FIXME("Invalid location (%#x) specified.\n", location);
3975 surface->ds_current_size.cx = w;
3976 surface->ds_current_size.cy = h;
3977 surface->Flags &= ~SFLAG_DS_LOCATIONS;
3978 surface->Flags |= location;
3981 /* Context activation is done by the caller. */
3982 void surface_load_ds_location(IWineD3DSurfaceImpl *surface, struct wined3d_context *context, DWORD location)
3984 IWineD3DDeviceImpl *device = surface->resource.device;
3985 const struct wined3d_gl_info *gl_info = context->gl_info;
3987 TRACE("surface %p, new location %#x.\n", surface, location);
3989 /* TODO: Make this work for modes other than FBO */
3990 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
3992 if (!(surface->Flags & location))
3994 surface->ds_current_size.cx = 0;
3995 surface->ds_current_size.cy = 0;
3998 if (surface->ds_current_size.cx == surface->currentDesc.Width
3999 && surface->ds_current_size.cy == surface->currentDesc.Height)
4001 TRACE("Location (%#x) is already up to date.\n", location);
4002 return;
4005 if (surface->current_renderbuffer)
4007 FIXME("Not supported with fixed up depth stencil.\n");
4008 return;
4011 if (!(surface->Flags & SFLAG_LOCATIONS))
4013 FIXME("No up to date depth stencil location.\n");
4014 surface->Flags |= location;
4015 return;
4018 if (location == SFLAG_DS_OFFSCREEN)
4020 GLint old_binding = 0;
4021 GLenum bind_target;
4023 TRACE("Copying onscreen depth buffer to depth texture.\n");
4025 ENTER_GL();
4027 if (!device->depth_blt_texture)
4029 glGenTextures(1, &device->depth_blt_texture);
4032 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4033 * directly on the FBO texture. That's because we need to flip. */
4034 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4035 if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4037 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4038 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4040 else
4042 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4043 bind_target = GL_TEXTURE_2D;
4045 glBindTexture(bind_target, device->depth_blt_texture);
4046 glCopyTexImage2D(bind_target, surface->texture_level, surface->resource.format_desc->glInternal,
4047 0, 0, surface->currentDesc.Width, surface->currentDesc.Height, 0);
4048 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4049 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4050 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4051 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4052 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4053 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4054 glBindTexture(bind_target, old_binding);
4056 /* Setup the destination */
4057 if (!device->depth_blt_rb)
4059 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4060 checkGLcall("glGenRenderbuffersEXT");
4062 if (device->depth_blt_rb_w != surface->currentDesc.Width
4063 || device->depth_blt_rb_h != surface->currentDesc.Height)
4065 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4066 checkGLcall("glBindRenderbufferEXT");
4067 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
4068 surface->currentDesc.Width, surface->currentDesc.Height);
4069 checkGLcall("glRenderbufferStorageEXT");
4070 device->depth_blt_rb_w = surface->currentDesc.Width;
4071 device->depth_blt_rb_h = surface->currentDesc.Height;
4074 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4075 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4076 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4077 checkGLcall("glFramebufferRenderbufferEXT");
4078 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, surface, FALSE);
4080 /* Do the actual blit */
4081 surface_depth_blt(surface, gl_info, device->depth_blt_texture,
4082 surface->currentDesc.Width, surface->currentDesc.Height, bind_target);
4083 checkGLcall("depth_blt");
4085 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4086 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4088 LEAVE_GL();
4090 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4092 else if (location == SFLAG_DS_ONSCREEN)
4094 TRACE("Copying depth texture to onscreen depth buffer.\n");
4096 ENTER_GL();
4098 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4099 surface_depth_blt(surface, gl_info, surface->texture_name,
4100 surface->currentDesc.Width, surface->currentDesc.Height, surface->texture_target);
4101 checkGLcall("depth_blt");
4103 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4105 LEAVE_GL();
4107 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4109 else
4111 ERR("Invalid location (%#x) specified.\n", location);
4114 surface->Flags |= location;
4115 surface->ds_current_size.cx = surface->currentDesc.Width;
4116 surface->ds_current_size.cy = surface->currentDesc.Height;
4119 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
4120 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4121 IWineD3DBaseTexture *texture;
4122 IWineD3DSurfaceImpl *overlay;
4124 TRACE("(%p)->(%s, %s)\n", iface, debug_surflocation(flag),
4125 persistent ? "TRUE" : "FALSE");
4127 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4129 if (surface_is_offscreen(This))
4131 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4132 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4134 else
4136 TRACE("Surface %p is an onscreen surface\n", iface);
4140 if(persistent) {
4141 if(((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) ||
4142 ((This->Flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX))) {
4143 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4144 TRACE("Passing to container\n");
4145 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4146 IWineD3DBaseTexture_Release(texture);
4149 This->Flags &= ~SFLAG_LOCATIONS;
4150 This->Flags |= flag;
4152 /* Redraw emulated overlays, if any */
4153 if(flag & SFLAG_INDRAWABLE && !list_empty(&This->overlays)) {
4154 LIST_FOR_EACH_ENTRY(overlay, &This->overlays, IWineD3DSurfaceImpl, overlay_entry) {
4155 IWineD3DSurface_DrawOverlay((IWineD3DSurface *) overlay);
4158 } else {
4159 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))) {
4160 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4161 TRACE("Passing to container\n");
4162 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4163 IWineD3DBaseTexture_Release(texture);
4166 This->Flags &= ~flag;
4169 if(!(This->Flags & SFLAG_LOCATIONS)) {
4170 ERR("%p: Surface does not have any up to date location\n", This);
4174 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in)
4176 IWineD3DDeviceImpl *device = This->resource.device;
4177 IWineD3DSwapChainImpl *swapchain;
4178 struct wined3d_context *context;
4179 RECT src_rect, dst_rect;
4181 surface_get_rect(This, rect_in, &src_rect);
4183 context = context_acquire(device, This);
4184 context_apply_blit_state(context, device);
4185 if (context->render_offscreen)
4187 dst_rect.left = src_rect.left;
4188 dst_rect.right = src_rect.right;
4189 dst_rect.top = src_rect.bottom;
4190 dst_rect.bottom = src_rect.top;
4192 else
4194 dst_rect = src_rect;
4197 if ((This->Flags & SFLAG_SWAPCHAIN) && This == ((IWineD3DSwapChainImpl *)This->container)->front_buffer)
4198 surface_translate_frontbuffer_coords(This, context->win_handle, &dst_rect);
4200 device->blitter->set_shader((IWineD3DDevice *) device, This);
4202 ENTER_GL();
4203 draw_textured_quad(This, &src_rect, &dst_rect, WINED3DTEXF_POINT);
4204 LEAVE_GL();
4206 device->blitter->unset_shader((IWineD3DDevice *) device);
4208 swapchain = (This->Flags & SFLAG_SWAPCHAIN) ? (IWineD3DSwapChainImpl *)This->container : NULL;
4209 if (wined3d_settings.strict_draw_ordering || (swapchain
4210 && (This == swapchain->front_buffer || swapchain->num_contexts > 1)))
4211 wglFlush(); /* Flush to ensure ordering across contexts. */
4213 context_release(context);
4216 /*****************************************************************************
4217 * IWineD3DSurface::LoadLocation
4219 * Copies the current surface data from wherever it is to the requested
4220 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4221 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4222 * multiple locations, the gl texture is preferred over the drawable, which is
4223 * preferred over system memory. The PBO counts as system memory. If rect is
4224 * not NULL, only the specified rectangle is copied (only supported for
4225 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4226 * location is marked up to date after the copy.
4228 * Parameters:
4229 * flag: Surface location flag to be updated
4230 * rect: rectangle to be copied
4232 * Returns:
4233 * WINED3D_OK on success
4234 * WINED3DERR_DEVICELOST on an internal error
4236 *****************************************************************************/
4237 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4238 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4239 IWineD3DDeviceImpl *device = This->resource.device;
4240 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4241 struct wined3d_format_desc desc;
4242 CONVERT_TYPES convert;
4243 int width, pitch, outpitch;
4244 BYTE *mem;
4245 BOOL drawable_read_ok = TRUE;
4246 BOOL in_fbo = FALSE;
4248 if (This->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
4250 if (flag == SFLAG_INTEXTURE)
4252 struct wined3d_context *context = context_acquire(device, NULL);
4253 surface_load_ds_location(This, context, SFLAG_DS_OFFSCREEN);
4254 context_release(context);
4255 return WINED3D_OK;
4257 else
4259 FIXME("Unimplemented location %#x for depth/stencil buffers.\n", flag);
4260 return WINED3DERR_INVALIDCALL;
4264 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4266 if (surface_is_offscreen(This))
4268 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4269 * Prefer SFLAG_INTEXTURE. */
4270 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4271 drawable_read_ok = FALSE;
4272 in_fbo = TRUE;
4274 else
4276 TRACE("Surface %p is an onscreen surface\n", iface);
4280 TRACE("(%p)->(%s, %p)\n", iface, debug_surflocation(flag), rect);
4281 if(rect) {
4282 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4285 if(This->Flags & flag) {
4286 TRACE("Location already up to date\n");
4287 return WINED3D_OK;
4290 if(!(This->Flags & SFLAG_LOCATIONS)) {
4291 ERR("%p: Surface does not have any up to date location\n", This);
4292 This->Flags |= SFLAG_LOST;
4293 return WINED3DERR_DEVICELOST;
4296 if(flag == SFLAG_INSYSMEM) {
4297 surface_prepare_system_memory(This);
4299 /* Download the surface to system memory */
4300 if (This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4302 struct wined3d_context *context = NULL;
4304 if (!device->isInDraw) context = context_acquire(device, NULL);
4306 surface_bind_and_dirtify(This, !(This->Flags & SFLAG_INTEXTURE));
4307 surface_download_data(This, gl_info);
4309 if (context) context_release(context);
4311 else
4313 /* Note: It might be faster to download into a texture first. */
4314 read_from_framebuffer(This, rect,
4315 This->resource.allocatedMemory,
4316 IWineD3DSurface_GetPitch(iface));
4318 } else if(flag == SFLAG_INDRAWABLE) {
4319 if(This->Flags & SFLAG_INTEXTURE) {
4320 surface_blt_to_drawable(This, rect);
4321 } else {
4322 int byte_count;
4323 if((This->Flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX) {
4324 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4325 * values, otherwise we get incorrect values in the target. For now go the slow way
4326 * via a system memory copy
4328 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4331 d3dfmt_get_conv(This, FALSE /* We need color keying */, FALSE /* We won't use textures */, &desc, &convert);
4333 /* The width is in 'length' not in bytes */
4334 width = This->currentDesc.Width;
4335 pitch = IWineD3DSurface_GetPitch(iface);
4337 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4338 * but it isn't set (yet) in all cases it is getting called. */
4339 if ((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO))
4341 struct wined3d_context *context = NULL;
4343 TRACE("Removing the pbo attached to surface %p\n", This);
4345 if (!device->isInDraw) context = context_acquire(device, NULL);
4346 surface_remove_pbo(This, gl_info);
4347 if (context) context_release(context);
4350 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4351 int height = This->currentDesc.Height;
4352 byte_count = desc.conv_byte_count;
4354 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4355 outpitch = width * byte_count;
4356 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4358 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4359 if(!mem) {
4360 ERR("Out of memory %d, %d!\n", outpitch, height);
4361 return WINED3DERR_OUTOFVIDEOMEMORY;
4363 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4365 This->Flags |= SFLAG_CONVERTED;
4366 } else {
4367 This->Flags &= ~SFLAG_CONVERTED;
4368 mem = This->resource.allocatedMemory;
4369 byte_count = desc.byte_count;
4372 flush_to_framebuffer_drawpixels(This, desc.glFormat, desc.glType, byte_count, mem);
4374 /* Don't delete PBO memory */
4375 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4376 HeapFree(GetProcessHeap(), 0, mem);
4378 } else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */ {
4379 if (drawable_read_ok && (This->Flags & SFLAG_INDRAWABLE)) {
4380 read_from_framebuffer_texture(This, flag == SFLAG_INSRGBTEX);
4382 else
4384 /* Upload from system memory */
4385 BOOL srgb = flag == SFLAG_INSRGBTEX;
4386 struct wined3d_context *context = NULL;
4388 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */,
4389 &desc, &convert);
4391 if(srgb) {
4392 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE) {
4393 /* Performance warning ... */
4394 FIXME("%p: Downloading rgb texture to reload it as srgb\n", This);
4395 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4397 } else {
4398 if((This->Flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX) {
4399 /* Performance warning ... */
4400 FIXME("%p: Downloading srgb texture to reload it as rgb\n", This);
4401 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4404 if(!(This->Flags & SFLAG_INSYSMEM)) {
4405 /* Should not happen */
4406 ERR("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set\n");
4407 /* Lets hope we get it from somewhere... */
4408 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4411 if (!device->isInDraw) context = context_acquire(device, NULL);
4413 surface_prepare_texture(This, gl_info, srgb);
4414 surface_bind_and_dirtify(This, srgb);
4416 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4417 This->Flags |= SFLAG_GLCKEY;
4418 This->glCKey = This->SrcBltCKey;
4420 else This->Flags &= ~SFLAG_GLCKEY;
4422 /* The width is in 'length' not in bytes */
4423 width = This->currentDesc.Width;
4424 pitch = IWineD3DSurface_GetPitch(iface);
4426 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4427 * but it isn't set (yet) in all cases it is getting called. */
4428 if(((convert != NO_CONVERSION) || desc.convert) && (This->Flags & SFLAG_PBO)) {
4429 TRACE("Removing the pbo attached to surface %p\n", This);
4430 surface_remove_pbo(This, gl_info);
4433 if(desc.convert) {
4434 /* This code is entered for texture formats which need a fixup. */
4435 int height = This->currentDesc.Height;
4437 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4438 outpitch = width * desc.conv_byte_count;
4439 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4441 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4442 if(!mem) {
4443 ERR("Out of memory %d, %d!\n", outpitch, height);
4444 if (context) context_release(context);
4445 return WINED3DERR_OUTOFVIDEOMEMORY;
4447 desc.convert(This->resource.allocatedMemory, mem, pitch, width, height);
4448 } else if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4449 /* This code is only entered for color keying fixups */
4450 int height = This->currentDesc.Height;
4452 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4453 outpitch = width * desc.conv_byte_count;
4454 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4456 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4457 if(!mem) {
4458 ERR("Out of memory %d, %d!\n", outpitch, height);
4459 if (context) context_release(context);
4460 return WINED3DERR_OUTOFVIDEOMEMORY;
4462 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4463 } else {
4464 mem = This->resource.allocatedMemory;
4467 /* Make sure the correct pitch is used */
4468 ENTER_GL();
4469 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4470 LEAVE_GL();
4472 if (mem || (This->Flags & SFLAG_PBO))
4473 surface_upload_data(This, gl_info, &desc, srgb, mem);
4475 /* Restore the default pitch */
4476 ENTER_GL();
4477 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4478 LEAVE_GL();
4480 if (context) context_release(context);
4482 /* Don't delete PBO memory */
4483 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4484 HeapFree(GetProcessHeap(), 0, mem);
4488 if(rect == NULL) {
4489 This->Flags |= flag;
4492 if (in_fbo && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4493 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4494 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4497 return WINED3D_OK;
4500 static HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container)
4502 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4503 IWineD3DSwapChain *swapchain = NULL;
4505 /* Update the drawable size method */
4506 if(container) {
4507 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4509 if(swapchain) {
4510 This->get_drawable_size = get_drawable_size_swapchain;
4511 IWineD3DSwapChain_Release(swapchain);
4512 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4513 switch(wined3d_settings.offscreen_rendering_mode) {
4514 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4515 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4519 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4522 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4523 return SURFACE_OPENGL;
4526 static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4527 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4528 HRESULT hr;
4530 /* If there's no destination surface there is nothing to do */
4531 if(!This->overlay_dest) return WINED3D_OK;
4533 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4534 * update the overlay. Prevent an endless recursion
4536 if(This->overlay_dest->Flags & SFLAG_INOVERLAYDRAW) {
4537 return WINED3D_OK;
4539 This->overlay_dest->Flags |= SFLAG_INOVERLAYDRAW;
4540 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *) This->overlay_dest, &This->overlay_destrect,
4541 iface, &This->overlay_srcrect, WINEDDBLT_WAIT,
4542 NULL, WINED3DTEXF_LINEAR);
4543 This->overlay_dest->Flags &= ~SFLAG_INOVERLAYDRAW;
4545 return hr;
4548 BOOL surface_is_offscreen(IWineD3DSurfaceImpl *surface)
4550 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)surface->container;
4552 /* Not on a swapchain - must be offscreen */
4553 if (!(surface->Flags & SFLAG_SWAPCHAIN)) return TRUE;
4555 /* The front buffer is always onscreen */
4556 if (surface == swapchain->front_buffer) return FALSE;
4558 /* If the swapchain is rendered to an FBO, the backbuffer is
4559 * offscreen, otherwise onscreen */
4560 return swapchain->render_to_fbo;
4563 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4565 /* IUnknown */
4566 IWineD3DBaseSurfaceImpl_QueryInterface,
4567 IWineD3DBaseSurfaceImpl_AddRef,
4568 IWineD3DSurfaceImpl_Release,
4569 /* IWineD3DResource */
4570 IWineD3DBaseSurfaceImpl_GetParent,
4571 IWineD3DBaseSurfaceImpl_SetPrivateData,
4572 IWineD3DBaseSurfaceImpl_GetPrivateData,
4573 IWineD3DBaseSurfaceImpl_FreePrivateData,
4574 IWineD3DBaseSurfaceImpl_SetPriority,
4575 IWineD3DBaseSurfaceImpl_GetPriority,
4576 IWineD3DSurfaceImpl_PreLoad,
4577 IWineD3DSurfaceImpl_UnLoad,
4578 IWineD3DBaseSurfaceImpl_GetType,
4579 /* IWineD3DSurface */
4580 IWineD3DBaseSurfaceImpl_GetContainer,
4581 IWineD3DBaseSurfaceImpl_GetDesc,
4582 IWineD3DSurfaceImpl_LockRect,
4583 IWineD3DSurfaceImpl_UnlockRect,
4584 IWineD3DSurfaceImpl_GetDC,
4585 IWineD3DSurfaceImpl_ReleaseDC,
4586 IWineD3DSurfaceImpl_Flip,
4587 IWineD3DSurfaceImpl_Blt,
4588 IWineD3DBaseSurfaceImpl_GetBltStatus,
4589 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4590 IWineD3DBaseSurfaceImpl_IsLost,
4591 IWineD3DBaseSurfaceImpl_Restore,
4592 IWineD3DSurfaceImpl_BltFast,
4593 IWineD3DBaseSurfaceImpl_GetPalette,
4594 IWineD3DBaseSurfaceImpl_SetPalette,
4595 IWineD3DSurfaceImpl_RealizePalette,
4596 IWineD3DBaseSurfaceImpl_SetColorKey,
4597 IWineD3DBaseSurfaceImpl_GetPitch,
4598 IWineD3DSurfaceImpl_SetMem,
4599 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4600 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4601 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4602 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4603 IWineD3DBaseSurfaceImpl_SetClipper,
4604 IWineD3DBaseSurfaceImpl_GetClipper,
4605 /* Internal use: */
4606 IWineD3DSurfaceImpl_LoadTexture,
4607 IWineD3DSurfaceImpl_BindTexture,
4608 IWineD3DSurfaceImpl_SetContainer,
4609 IWineD3DBaseSurfaceImpl_GetData,
4610 IWineD3DSurfaceImpl_SetFormat,
4611 IWineD3DSurfaceImpl_PrivateSetup,
4612 IWineD3DSurfaceImpl_ModifyLocation,
4613 IWineD3DSurfaceImpl_LoadLocation,
4614 IWineD3DSurfaceImpl_GetImplType,
4615 IWineD3DSurfaceImpl_DrawOverlay
4618 static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4619 /* Context activation is done by the caller. */
4620 static void ffp_blit_free(IWineD3DDevice *iface) { }
4622 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4623 /* Context activation is done by the caller. */
4624 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4626 BYTE table[256][4];
4627 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4629 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4631 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4632 ENTER_GL();
4633 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4634 LEAVE_GL();
4637 /* Context activation is done by the caller. */
4638 static HRESULT ffp_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4640 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4641 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4642 enum complex_fixup fixup = get_complex_fixup(surface->resource.format_desc->color_fixup);
4644 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4645 * else the surface is converted in software at upload time in LoadLocation.
4647 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4648 ffp_blit_p8_upload_palette(surface, gl_info);
4650 ENTER_GL();
4651 glEnable(surface->texture_target);
4652 checkGLcall("glEnable(surface->texture_target)");
4653 LEAVE_GL();
4654 return WINED3D_OK;
4657 /* Context activation is done by the caller. */
4658 static void ffp_blit_unset(IWineD3DDevice *iface)
4660 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4661 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4663 ENTER_GL();
4664 glDisable(GL_TEXTURE_2D);
4665 checkGLcall("glDisable(GL_TEXTURE_2D)");
4666 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4668 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4669 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4671 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4673 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4674 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4676 LEAVE_GL();
4679 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4680 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4681 const struct wined3d_format_desc *src_format_desc,
4682 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4683 const struct wined3d_format_desc *dst_format_desc)
4685 enum complex_fixup src_fixup;
4687 if (blit_op == BLIT_OP_COLOR_FILL)
4689 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4691 TRACE("Color fill not supported\n");
4692 return FALSE;
4695 return TRUE;
4698 src_fixup = get_complex_fixup(src_format_desc->color_fixup);
4699 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4701 TRACE("Checking support for fixup:\n");
4702 dump_color_fixup_desc(src_format_desc->color_fixup);
4705 if (blit_op != BLIT_OP_BLIT)
4707 TRACE("Unsupported blit_op=%d\n", blit_op);
4708 return FALSE;
4711 if (!is_identity_fixup(dst_format_desc->color_fixup))
4713 TRACE("Destination fixups are not supported\n");
4714 return FALSE;
4717 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4719 TRACE("P8 fixup supported\n");
4720 return TRUE;
4723 /* We only support identity conversions. */
4724 if (is_identity_fixup(src_format_desc->color_fixup))
4726 TRACE("[OK]\n");
4727 return TRUE;
4730 TRACE("[FAILED]\n");
4731 return FALSE;
4734 static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect, DWORD fill_color)
4736 return IWineD3DDeviceImpl_ClearSurface(device, dst_surface, 1 /* Number of rectangles */,
4737 (const WINED3DRECT*)dst_rect, WINED3DCLEAR_TARGET, fill_color, 0.0f /* Z */, 0 /* Stencil */);
4740 const struct blit_shader ffp_blit = {
4741 ffp_blit_alloc,
4742 ffp_blit_free,
4743 ffp_blit_set,
4744 ffp_blit_unset,
4745 ffp_blit_supported,
4746 ffp_blit_color_fill
4749 static HRESULT cpu_blit_alloc(IWineD3DDevice *iface)
4751 return WINED3D_OK;
4754 /* Context activation is done by the caller. */
4755 static void cpu_blit_free(IWineD3DDevice *iface)
4759 /* Context activation is done by the caller. */
4760 static HRESULT cpu_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4762 return WINED3D_OK;
4765 /* Context activation is done by the caller. */
4766 static void cpu_blit_unset(IWineD3DDevice *iface)
4770 static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4771 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4772 const struct wined3d_format_desc *src_format_desc,
4773 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4774 const struct wined3d_format_desc *dst_format_desc)
4776 if (blit_op == BLIT_OP_COLOR_FILL)
4778 return TRUE;
4781 return FALSE;
4784 static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect, DWORD fill_color)
4786 WINEDDBLTFX BltFx;
4787 memset(&BltFx, 0, sizeof(BltFx));
4788 BltFx.dwSize = sizeof(BltFx);
4789 BltFx.u5.dwFillColor = color_convert_argb_to_fmt(fill_color, dst_surface->resource.format_desc->format);
4790 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect, NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
4793 const struct blit_shader cpu_blit = {
4794 cpu_blit_alloc,
4795 cpu_blit_free,
4796 cpu_blit_set,
4797 cpu_blit_unset,
4798 cpu_blit_supported,
4799 cpu_blit_color_fill
4802 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4803 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4804 const struct wined3d_format_desc *src_format_desc,
4805 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4806 const struct wined3d_format_desc *dst_format_desc)
4808 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
4809 return FALSE;
4811 /* We only support blitting. Things like color keying / color fill should
4812 * be handled by other blitters.
4814 if (blit_op != BLIT_OP_BLIT)
4815 return FALSE;
4817 /* Source and/or destination need to be on the GL side */
4818 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
4819 return FALSE;
4821 if(!((src_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET))
4822 && ((dst_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
4823 return FALSE;
4825 if (!is_identity_fixup(src_format_desc->color_fixup) ||
4826 !is_identity_fixup(dst_format_desc->color_fixup))
4827 return FALSE;
4829 if (!(src_format_desc->format == dst_format_desc->format
4830 || (is_identity_fixup(src_format_desc->color_fixup)
4831 && is_identity_fixup(dst_format_desc->color_fixup))))
4832 return FALSE;
4834 return TRUE;