VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c@ 33282

Last change on this file since 33282 was 33282, checked in by vboxsync, 14 years ago

crOpenGL/wddm: enable more blits

  • Property svn:eol-style set to native
File size: 194.2 KB
Line 
1/*
2 * IWineD3DSurface Implementation
3 *
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
14 *
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.
19 *
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.
24 *
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
28 */
29
30/*
31 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
32 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
33 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
34 * a choice of LGPL license versions is made available with the language indicating
35 * that LGPLv2 or any later version may be used, or where a choice of which version
36 * of the LGPL is applied is otherwise unspecified.
37 */
38
39#include "config.h"
40#include "wine/port.h"
41#include "wined3d_private.h"
42
43WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
44WINE_DECLARE_DEBUG_CHANNEL(d3d);
45
46#define GLINFO_LOCATION (*gl_info)
47
48static void surface_cleanup(IWineD3DSurfaceImpl *This)
49{
50 IWineD3DDeviceImpl *device = This->resource.device;
51 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
52 struct wined3d_context *context = NULL;
53 renderbuffer_entry_t *entry, *entry2;
54
55 TRACE("(%p) : Cleaning up.\n", This);
56
57 /* Need a context to destroy the texture. Use the currently active render
58 * target, but only if the primary render target exists. Otherwise
59 * lastActiveRenderTarget is garbage. When destroying the primary render
60 * target, Uninit3D() will activate a context before doing anything. */
61 if (device->render_targets && device->render_targets[0])
62 {
63 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
64 }
65
66 ENTER_GL();
67
68 if (This->texture_name)
69 {
70 /* Release the OpenGL texture. */
71 TRACE("Deleting texture %u.\n", This->texture_name);
72 glDeleteTextures(1, &This->texture_name);
73 }
74
75 if (This->Flags & SFLAG_PBO)
76 {
77 /* Delete the PBO. */
78 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
79 }
80
81 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry)
82 {
83 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
84 HeapFree(GetProcessHeap(), 0, entry);
85 }
86
87 LEAVE_GL();
88
89 if (This->Flags & SFLAG_DIBSECTION)
90 {
91 /* Release the DC. */
92 SelectObject(This->hDC, This->dib.holdbitmap);
93 DeleteDC(This->hDC);
94 /* Release the DIB section. */
95 DeleteObject(This->dib.DIBsection);
96 This->dib.bitmap_data = NULL;
97 This->resource.allocatedMemory = NULL;
98 }
99
100 if (This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem((IWineD3DSurface *)This, NULL);
101 if (This->overlay_dest) list_remove(&This->overlay_entry);
102
103 HeapFree(GetProcessHeap(), 0, This->palette9);
104
105 resource_cleanup((IWineD3DResource *)This);
106
107 if (context) context_release(context);
108}
109
110UINT surface_calculate_size(const struct wined3d_format_desc *format_desc, UINT alignment, UINT width, UINT height)
111{
112 UINT size;
113
114 if (format_desc->format == WINED3DFMT_UNKNOWN)
115 {
116 size = 0;
117 }
118 else if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
119 {
120 UINT row_block_count = (width + format_desc->block_width - 1) / format_desc->block_width;
121 UINT row_count = (height + format_desc->block_height - 1) / format_desc->block_height;
122 size = row_count * row_block_count * format_desc->block_byte_count;
123 }
124 else
125 {
126 /* The pitch is a multiple of 4 bytes. */
127 size = height * (((width * format_desc->byte_count) + alignment - 1) & ~(alignment - 1));
128 }
129
130 if (format_desc->heightscale != 0.0f) size *= format_desc->heightscale;
131
132 return size;
133}
134
135struct blt_info
136{
137 GLenum binding;
138 GLenum bind_target;
139 enum tex_types tex_type;
140 GLfloat coords[4][3];
141};
142
143struct float_rect
144{
145 float l;
146 float t;
147 float r;
148 float b;
149};
150
151static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
152{
153 f->l = ((r->left * 2.0f) / w) - 1.0f;
154 f->t = ((r->top * 2.0f) / h) - 1.0f;
155 f->r = ((r->right * 2.0f) / w) - 1.0f;
156 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
157}
158
159static void surface_get_blt_info(GLenum target, const RECT *rect_in, GLsizei w, GLsizei h, struct blt_info *info)
160{
161 GLfloat (*coords)[3] = info->coords;
162 RECT rect;
163 struct float_rect f;
164
165 if (rect_in)
166 rect = *rect_in;
167 else
168 {
169 rect.left = 0;
170 rect.top = h;
171 rect.right = w;
172 rect.bottom = 0;
173 }
174
175 switch (target)
176 {
177 default:
178 FIXME("Unsupported texture target %#x\n", target);
179 /* Fall back to GL_TEXTURE_2D */
180 case GL_TEXTURE_2D:
181 info->binding = GL_TEXTURE_BINDING_2D;
182 info->bind_target = GL_TEXTURE_2D;
183 info->tex_type = tex_2d;
184 coords[0][0] = (float)rect.left / w;
185 coords[0][1] = (float)rect.top / h;
186 coords[0][2] = 0.0f;
187
188 coords[1][0] = (float)rect.right / w;
189 coords[1][1] = (float)rect.top / h;
190 coords[1][2] = 0.0f;
191
192 coords[2][0] = (float)rect.left / w;
193 coords[2][1] = (float)rect.bottom / h;
194 coords[2][2] = 0.0f;
195
196 coords[3][0] = (float)rect.right / w;
197 coords[3][1] = (float)rect.bottom / h;
198 coords[3][2] = 0.0f;
199 break;
200
201 case GL_TEXTURE_RECTANGLE_ARB:
202 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
203 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
204 info->tex_type = tex_rect;
205 coords[0][0] = rect.left; coords[0][1] = rect.top; coords[0][2] = 0.0f;
206 coords[1][0] = rect.right; coords[1][1] = rect.top; coords[1][2] = 0.0f;
207 coords[2][0] = rect.left; coords[2][1] = rect.bottom; coords[2][2] = 0.0f;
208 coords[3][0] = rect.right; coords[3][1] = rect.bottom; coords[3][2] = 0.0f;
209 break;
210
211 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
212 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
213 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
214 info->tex_type = tex_cube;
215 cube_coords_float(&rect, w, h, &f);
216
217 coords[0][0] = 1.0f; coords[0][1] = -f.t; coords[0][2] = -f.l;
218 coords[1][0] = 1.0f; coords[1][1] = -f.t; coords[1][2] = -f.r;
219 coords[2][0] = 1.0f; coords[2][1] = -f.b; coords[2][2] = -f.l;
220 coords[3][0] = 1.0f; coords[3][1] = -f.b; coords[3][2] = -f.r;
221 break;
222
223 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
224 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
225 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
226 info->tex_type = tex_cube;
227 cube_coords_float(&rect, w, h, &f);
228
229 coords[0][0] = -1.0f; coords[0][1] = -f.t; coords[0][2] = f.l;
230 coords[1][0] = -1.0f; coords[1][1] = -f.t; coords[1][2] = f.r;
231 coords[2][0] = -1.0f; coords[2][1] = -f.b; coords[2][2] = f.l;
232 coords[3][0] = -1.0f; coords[3][1] = -f.b; coords[3][2] = f.r;
233 break;
234
235 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
236 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
237 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
238 info->tex_type = tex_cube;
239 cube_coords_float(&rect, w, h, &f);
240
241 coords[0][0] = f.l; coords[0][1] = 1.0f; coords[0][2] = f.t;
242 coords[1][0] = f.r; coords[1][1] = 1.0f; coords[1][2] = f.t;
243 coords[2][0] = f.l; coords[2][1] = 1.0f; coords[2][2] = f.b;
244 coords[3][0] = f.r; coords[3][1] = 1.0f; coords[3][2] = f.b;
245 break;
246
247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
248 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
249 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
250 info->tex_type = tex_cube;
251 cube_coords_float(&rect, w, h, &f);
252
253 coords[0][0] = f.l; coords[0][1] = -1.0f; coords[0][2] = -f.t;
254 coords[1][0] = f.r; coords[1][1] = -1.0f; coords[1][2] = -f.t;
255 coords[2][0] = f.l; coords[2][1] = -1.0f; coords[2][2] = -f.b;
256 coords[3][0] = f.r; coords[3][1] = -1.0f; coords[3][2] = -f.b;
257 break;
258
259 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
260 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
261 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
262 info->tex_type = tex_cube;
263 cube_coords_float(&rect, w, h, &f);
264
265 coords[0][0] = f.l; coords[0][1] = -f.t; coords[0][2] = 1.0f;
266 coords[1][0] = f.r; coords[1][1] = -f.t; coords[1][2] = 1.0f;
267 coords[2][0] = f.l; coords[2][1] = -f.b; coords[2][2] = 1.0f;
268 coords[3][0] = f.r; coords[3][1] = -f.b; coords[3][2] = 1.0f;
269 break;
270
271 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
272 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
273 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
274 info->tex_type = tex_cube;
275 cube_coords_float(&rect, w, h, &f);
276
277 coords[0][0] = -f.l; coords[0][1] = -f.t; coords[0][2] = -1.0f;
278 coords[1][0] = -f.r; coords[1][1] = -f.t; coords[1][2] = -1.0f;
279 coords[2][0] = -f.l; coords[2][1] = -f.b; coords[2][2] = -1.0f;
280 coords[3][0] = -f.r; coords[3][1] = -f.b; coords[3][2] = -1.0f;
281 break;
282 }
283}
284
285static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
286{
287 if (rect_in)
288 *rect_out = *rect_in;
289 else
290 {
291 rect_out->left = 0;
292 rect_out->top = 0;
293 rect_out->right = This->currentDesc.Width;
294 rect_out->bottom = This->currentDesc.Height;
295 }
296}
297
298/* GL locking and context activation is done by the caller */
299void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
300{
301 IWineD3DBaseTextureImpl *texture;
302 struct blt_info info;
303
304 surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
305
306 glEnable(info.bind_target);
307 checkGLcall("glEnable(bind_target)");
308
309 /* Bind the texture */
310 glBindTexture(info.bind_target, src_surface->texture_name);
311 checkGLcall("glBindTexture");
312
313 /* Filtering for StretchRect */
314 glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
315 wined3d_gl_mag_filter(magLookup, Filter));
316 checkGLcall("glTexParameteri");
317 glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
318 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
319 checkGLcall("glTexParameteri");
320 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
321 glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
322 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
323 checkGLcall("glTexEnvi");
324
325 /* Draw a quad */
326 glBegin(GL_TRIANGLE_STRIP);
327 glTexCoord3fv(info.coords[0]);
328 glVertex2i(dst_rect->left, dst_rect->top);
329
330 glTexCoord3fv(info.coords[1]);
331 glVertex2i(dst_rect->right, dst_rect->top);
332
333 glTexCoord3fv(info.coords[2]);
334 glVertex2i(dst_rect->left, dst_rect->bottom);
335
336 glTexCoord3fv(info.coords[3]);
337 glVertex2i(dst_rect->right, dst_rect->bottom);
338 glEnd();
339
340 /* Unbind the texture */
341 glBindTexture(info.bind_target, 0);
342 checkGLcall("glBindTexture(info->bind_target, 0)");
343
344 /* We changed the filtering settings on the texture. Inform the
345 * container about this to get the filters reset properly next draw. */
346 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)src_surface, &IID_IWineD3DBaseTexture, (void **)&texture)))
347 {
348 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
349 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
350 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
351 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture);
352 }
353}
354
355HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
356 UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
357 UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format,
358 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops
359#ifdef VBOX_WITH_WDDM
360 , HANDLE *shared_handle
361 , void *pvClientMem
362#endif
363 )
364{
365 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
366 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
367 void (*cleanup)(IWineD3DSurfaceImpl *This);
368 unsigned int resource_size;
369 HRESULT hr;
370
371 if (multisample_quality > 0)
372 {
373 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality);
374 multisample_quality = 0;
375 }
376
377 /* FIXME: Check that the format is supported by the device. */
378
379 resource_size = surface_calculate_size(format_desc, alignment, width, height);
380
381 /* Look at the implementation and set the correct Vtable. */
382 switch (surface_type)
383 {
384 case SURFACE_OPENGL:
385 surface->lpVtbl = &IWineD3DSurface_Vtbl;
386 cleanup = surface_cleanup;
387 break;
388
389 case SURFACE_GDI:
390 surface->lpVtbl = &IWineGDISurface_Vtbl;
391 cleanup = surface_gdi_cleanup;
392 break;
393
394 default:
395 ERR("Requested unknown surface implementation %#x.\n", surface_type);
396 return WINED3DERR_INVALIDCALL;
397 }
398
399 hr = resource_init((IWineD3DResource *)surface, WINED3DRTYPE_SURFACE,
400 device, resource_size, usage, format_desc, pool, parent, parent_ops
401#ifdef VBOX_WITH_WDDM
402 , shared_handle
403 , pvClientMem
404#endif
405 );
406 if (FAILED(hr))
407 {
408 WARN("Failed to initialize resource, returning %#x.\n", hr);
409 return hr;
410 }
411
412 /* "Standalone" surface. */
413 IWineD3DSurface_SetContainer((IWineD3DSurface *)surface, NULL);
414
415 surface->currentDesc.Width = width;
416 surface->currentDesc.Height = height;
417 surface->currentDesc.MultiSampleType = multisample_type;
418 surface->currentDesc.MultiSampleQuality = multisample_quality;
419 surface->texture_level = level;
420 list_init(&surface->overlays);
421
422 /* Flags */
423 surface->Flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
424#ifdef VBOX_WITH_WDDM
425 if (pool == WINED3DPOOL_SYSTEMMEM && pvClientMem) surface->Flags |= SFLAG_CLIENTMEM;
426#endif
427 if (discard) surface->Flags |= SFLAG_DISCARD;
428 if (lockable || format == WINED3DFMT_D16_LOCKABLE) surface->Flags |= SFLAG_LOCKABLE;
429
430 /* Quick lockable sanity check.
431 * TODO: remove this after surfaces, usage and lockability have been debugged properly
432 * this function is too deep to need to care about things like this.
433 * Levels need to be checked too, since they all affect what can be done. */
434 switch (pool)
435 {
436 case WINED3DPOOL_SCRATCH:
437 if(!lockable)
438 {
439 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
440 "which are mutually exclusive, setting lockable to TRUE.\n");
441 lockable = TRUE;
442 }
443 break;
444
445 case WINED3DPOOL_SYSTEMMEM:
446 if (!lockable)
447 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
448 break;
449
450 case WINED3DPOOL_MANAGED:
451 if (usage & WINED3DUSAGE_DYNAMIC)
452 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
453 break;
454
455 case WINED3DPOOL_DEFAULT:
456 if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
457 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
458 break;
459
460 default:
461 FIXME("Unknown pool %#x.\n", pool);
462 break;
463 };
464
465 if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
466 {
467 FIXME("Trying to create a render target that isn't in the default pool.\n");
468 }
469
470 /* Mark the texture as dirty so that it gets loaded first time around. */
471 surface_add_dirty_rect((IWineD3DSurface *)surface, NULL);
472 list_init(&surface->renderbuffers);
473
474 TRACE("surface %p, memory %p, size %u\n", surface, surface->resource.allocatedMemory, surface->resource.size);
475
476 /* Call the private setup routine */
477 hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
478 if (FAILED(hr))
479 {
480 ERR("Private setup failed, returning %#x\n", hr);
481 cleanup(surface);
482 return hr;
483 }
484
485#ifdef VBOX_WITH_WDDM
486 if (VBOXSHRC_IS_SHARED(surface))
487 {
488 Assert(shared_handle);
489 VBOXSHRC_SET_INITIALIZED(surface);
490 IWineD3DSurface_LoadLocation((IWineD3DSurface*)surface, SFLAG_INTEXTURE, NULL);
491 if (!VBOXSHRC_IS_SHARED_OPENED(surface))
492 {
493 Assert(!(*shared_handle));
494 *shared_handle = VBOXSHRC_GET_SHAREHANDLE(surface);
495 }
496 else
497 {
498 Assert(*shared_handle);
499 Assert(*shared_handle == VBOXSHRC_GET_SHAREHANDLE(surface));
500 }
501 }
502 else
503 {
504 Assert(!shared_handle);
505 }
506#endif
507
508 return hr;
509}
510
511static void surface_force_reload(IWineD3DSurface *iface)
512{
513 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
514
515#if defined(DEBUG_misha) && defined (VBOX_WITH_WDDM)
516 if (VBOXSHRC_IS_INITIALIZED(This))
517 {
518 Assert(0);
519 }
520#endif
521
522 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
523}
524
525void surface_set_texture_name(IWineD3DSurface *iface, GLuint new_name, BOOL srgb)
526{
527 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
528 GLuint *name;
529 DWORD flag;
530
531 if(srgb)
532 {
533 name = &This->texture_name_srgb;
534 flag = SFLAG_INSRGBTEX;
535 }
536 else
537 {
538 name = &This->texture_name;
539 flag = SFLAG_INTEXTURE;
540 }
541
542 TRACE("(%p) : setting texture name %u\n", This, new_name);
543
544 if (!*name && new_name)
545 {
546 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
547 * surface has no texture name yet. See if we can get rid of this. */
548 if (This->Flags & flag)
549 ERR("Surface has SFLAG_INTEXTURE set, but no texture name\n");
550 IWineD3DSurface_ModifyLocation(iface, flag, FALSE);
551 }
552
553#ifdef VBOX_WITH_WDDM
554 if (VBOXSHRC_IS_SHARED(This))
555 {
556 VBOXSHRC_SET_SHAREHANDLE(This, new_name);
557 }
558#endif
559 *name = new_name;
560 surface_force_reload(iface);
561}
562
563void surface_set_texture_target(IWineD3DSurface *iface, GLenum target)
564{
565 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
566
567 TRACE("(%p) : setting target %#x\n", This, target);
568
569 if (This->texture_target != target)
570 {
571 if (target == GL_TEXTURE_RECTANGLE_ARB)
572 {
573 This->Flags &= ~SFLAG_NORMCOORD;
574 }
575 else if (This->texture_target == GL_TEXTURE_RECTANGLE_ARB)
576 {
577 This->Flags |= SFLAG_NORMCOORD;
578 }
579 }
580 This->texture_target = target;
581 surface_force_reload(iface);
582}
583
584/* Context activation is done by the caller. */
585static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This, BOOL srgb) {
586 DWORD active_sampler;
587
588 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
589 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
590 * gl states. The current texture unit should always be a valid one.
591 *
592 * To be more specific, this is tricky because we can implicitly be called
593 * from sampler() in state.c. This means we can't touch anything other than
594 * whatever happens to be the currently active texture, or we would risk
595 * marking already applied sampler states dirty again.
596 *
597 * TODO: Track the current active texture per GL context instead of using glGet
598 */
599 GLint active_texture=GL_TEXTURE0_ARB;
600 ENTER_GL();
601 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
602 LEAVE_GL();
603 active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
604
605 if (active_sampler != WINED3D_UNMAPPED_STAGE)
606 {
607 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
608 }
609 IWineD3DSurface_BindTexture((IWineD3DSurface *)This, srgb);
610}
611
612/* This function checks if the primary render target uses the 8bit paletted format. */
613static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
614{
615 if (device->render_targets && device->render_targets[0]) {
616 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
617 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
618 && (render_target->resource.format_desc->format == WINED3DFMT_P8_UINT))
619 return TRUE;
620 }
621 return FALSE;
622}
623
624/* This call just downloads data, the caller is responsible for binding the
625 * correct texture. */
626/* Context activation is done by the caller. */
627static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
628{
629 const struct wined3d_format_desc *format_desc = This->resource.format_desc;
630
631 /* Only support read back of converted P8 surfaces */
632 if (This->Flags & SFLAG_CONVERTED && format_desc->format != WINED3DFMT_P8_UINT)
633 {
634 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(format_desc->format));
635 return;
636 }
637
638 ENTER_GL();
639
640 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
641 {
642 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
643 This, This->texture_level, format_desc->glFormat, format_desc->glType,
644 This->resource.allocatedMemory);
645
646 if (This->Flags & SFLAG_PBO)
647 {
648 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
649 checkGLcall("glBindBufferARB");
650 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
651 checkGLcall("glGetCompressedTexImageARB");
652 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
653 checkGLcall("glBindBufferARB");
654 }
655 else
656 {
657 GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
658 This->texture_level, This->resource.allocatedMemory));
659 checkGLcall("glGetCompressedTexImageARB");
660 }
661
662 LEAVE_GL();
663 } else {
664 void *mem;
665 GLenum format = format_desc->glFormat;
666 GLenum type = format_desc->glType;
667 int src_pitch = 0;
668 int dst_pitch = 0;
669
670 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
671 if (format_desc->format == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
672 {
673 format = GL_ALPHA;
674 type = GL_UNSIGNED_BYTE;
675 }
676
677 if (This->Flags & SFLAG_NONPOW2) {
678 unsigned char alignment = This->resource.device->surface_alignment;
679 src_pitch = format_desc->byte_count * This->pow2Width;
680 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
681 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
682 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
683 } else {
684 mem = This->resource.allocatedMemory;
685 }
686
687 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
688 This, This->texture_level, format, type, mem);
689
690 if(This->Flags & SFLAG_PBO) {
691 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
692 checkGLcall("glBindBufferARB");
693
694 glGetTexImage(This->texture_target, This->texture_level, format, type, NULL);
695 checkGLcall("glGetTexImage");
696
697 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
698 checkGLcall("glBindBufferARB");
699 } else {
700 glGetTexImage(This->texture_target, This->texture_level, format, type, mem);
701 checkGLcall("glGetTexImage");
702 }
703 LEAVE_GL();
704
705 if (This->Flags & SFLAG_NONPOW2) {
706 const BYTE *src_data;
707 BYTE *dst_data;
708 UINT y;
709 /*
710 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
711 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
712 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
713 *
714 * We're doing this...
715 *
716 * instead of boxing the texture :
717 * |<-texture width ->| -->pow2width| /\
718 * |111111111111111111| | |
719 * |222 Texture 222222| boxed empty | texture height
720 * |3333 Data 33333333| | |
721 * |444444444444444444| | \/
722 * ----------------------------------- |
723 * | boxed empty | boxed empty | pow2height
724 * | | | \/
725 * -----------------------------------
726 *
727 *
728 * we're repacking the data to the expected texture width
729 *
730 * |<-texture width ->| -->pow2width| /\
731 * |111111111111111111222222222222222| |
732 * |222333333333333333333444444444444| texture height
733 * |444444 | |
734 * | | \/
735 * | | |
736 * | empty | pow2height
737 * | | \/
738 * -----------------------------------
739 *
740 * == is the same as
741 *
742 * |<-texture width ->| /\
743 * |111111111111111111|
744 * |222222222222222222|texture height
745 * |333333333333333333|
746 * |444444444444444444| \/
747 * --------------------
748 *
749 * this also means that any references to allocatedMemory should work with the data as if were a
750 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
751 *
752 * internally the texture is still stored in a boxed format so any references to textureName will
753 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
754 *
755 * Performance should not be an issue, because applications normally do not lock the surfaces when
756 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
757 * and doesn't have to be re-read.
758 */
759 src_data = mem;
760 dst_data = This->resource.allocatedMemory;
761 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
762 for (y = 1 ; y < This->currentDesc.Height; y++) {
763 /* skip the first row */
764 src_data += src_pitch;
765 dst_data += dst_pitch;
766 memcpy(dst_data, src_data, dst_pitch);
767 }
768
769 HeapFree(GetProcessHeap(), 0, mem);
770 }
771 }
772
773 /* Surface has now been downloaded */
774 This->Flags |= SFLAG_INSYSMEM;
775}
776
777/* This call just uploads data, the caller is responsible for binding the
778 * correct texture. */
779/* Context activation is done by the caller. */
780static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
781 const struct wined3d_format_desc *format_desc, BOOL srgb, const GLvoid *data)
782{
783 GLsizei width = This->currentDesc.Width;
784 GLsizei height = This->currentDesc.Height;
785 GLenum internal;
786
787 if (srgb)
788 {
789 internal = format_desc->glGammaInternal;
790 }
791 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET
792 && surface_is_offscreen((IWineD3DSurface *)This))
793 {
794 internal = format_desc->rtInternal;
795 }
796 else
797 {
798 internal = format_desc->glInternal;
799 }
800
801 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
802 This, internal, width, height, format_desc->glFormat, format_desc->glType, data);
803 TRACE("target %#x, level %u, resource size %u.\n",
804 This->texture_target, This->texture_level, This->resource.size);
805
806 if (format_desc->heightscale != 1.0f && format_desc->heightscale != 0.0f) height *= format_desc->heightscale;
807
808 ENTER_GL();
809
810 if (This->Flags & SFLAG_PBO)
811 {
812 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
813 checkGLcall("glBindBufferARB");
814
815 TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
816 data = NULL;
817 }
818
819 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
820 {
821 TRACE("Calling glCompressedTexSubImage2DARB.\n");
822
823 GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
824 0, 0, width, height, internal, This->resource.size, data));
825 checkGLcall("glCompressedTexSubImage2DARB");
826 }
827 else
828 {
829 TRACE("Calling glTexSubImage2D.\n");
830
831 glTexSubImage2D(This->texture_target, This->texture_level,
832 0, 0, width, height, format_desc->glFormat, format_desc->glType, data);
833 checkGLcall("glTexSubImage2D");
834 }
835
836 if (This->Flags & SFLAG_PBO)
837 {
838 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
839 checkGLcall("glBindBufferARB");
840 }
841
842 LEAVE_GL();
843
844 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
845 {
846 IWineD3DDeviceImpl *device = This->resource.device;
847 unsigned int i;
848
849 for (i = 0; i < device->numContexts; ++i)
850 {
851 context_surface_update(device->contexts[i], This);
852 }
853 }
854}
855
856/* This call just allocates the texture, the caller is responsible for binding
857 * the correct texture. */
858/* Context activation is done by the caller. */
859static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
860 const struct wined3d_format_desc *format_desc, BOOL srgb)
861{
862 BOOL enable_client_storage = FALSE;
863 GLsizei width = This->pow2Width;
864 GLsizei height = This->pow2Height;
865 const BYTE *mem = NULL;
866 GLenum internal;
867
868 if (srgb)
869 {
870 internal = format_desc->glGammaInternal;
871 }
872 else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET
873 && surface_is_offscreen((IWineD3DSurface *)This))
874 {
875 internal = format_desc->rtInternal;
876 }
877 else
878 {
879 internal = format_desc->glInternal;
880 }
881
882 if (format_desc->heightscale != 1.0f && format_desc->heightscale != 0.0f) height *= format_desc->heightscale;
883
884 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",
885 This, This->texture_target, This->texture_level, debug_d3dformat(format_desc->format),
886 internal, width, height, format_desc->glFormat, format_desc->glType);
887
888 ENTER_GL();
889
890 if (gl_info->supported[APPLE_CLIENT_STORAGE])
891 {
892 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
893 /* In some cases we want to disable client storage.
894 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
895 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
896 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
897 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
898 */
899 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
900 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
901 This->Flags &= ~SFLAG_CLIENT;
902 enable_client_storage = TRUE;
903 } else {
904 This->Flags |= SFLAG_CLIENT;
905
906 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
907 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
908 */
909 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
910 }
911 }
912
913#ifdef VBOX_WITH_WDDM
914 if (!VBOXSHRC_IS_SHARED_OPENED(This))
915#endif
916 {
917 if (format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED && mem)
918 {
919 GL_EXTCALL(glCompressedTexImage2DARB(This->texture_target, This->texture_level,
920 internal, width, height, 0, This->resource.size, mem));
921 }
922 else
923 {
924 glTexImage2D(This->texture_target, This->texture_level,
925 internal, width, height, 0, format_desc->glFormat, format_desc->glType, mem);
926 checkGLcall("glTexImage2D");
927 }
928 }
929
930 if(enable_client_storage) {
931 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
932 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
933 }
934 LEAVE_GL();
935}
936
937/* In D3D the depth stencil dimensions have to be greater than or equal to the
938 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
939/* TODO: We should synchronize the renderbuffer's content with the texture's content. */
940/* GL locking is done by the caller */
941void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
942 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
943 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
944 renderbuffer_entry_t *entry;
945 GLuint renderbuffer = 0;
946 unsigned int src_width, src_height;
947
948 src_width = This->pow2Width;
949 src_height = This->pow2Height;
950
951 /* A depth stencil smaller than the render target is not valid */
952 if (width > src_width || height > src_height) return;
953
954 /* Remove any renderbuffer set if the sizes match */
955 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
956 || (width == src_width && height == src_height))
957 {
958 This->current_renderbuffer = NULL;
959 return;
960 }
961
962 /* Look if we've already got a renderbuffer of the correct dimensions */
963 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
964 if (entry->width == width && entry->height == height) {
965 renderbuffer = entry->id;
966 This->current_renderbuffer = entry;
967 break;
968 }
969 }
970
971 if (!renderbuffer) {
972 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
973 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
974 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
975 This->resource.format_desc->glInternal, width, height);
976
977 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
978 entry->width = width;
979 entry->height = height;
980 entry->id = renderbuffer;
981 list_add_head(&This->renderbuffers, &entry->entry);
982
983 This->current_renderbuffer = entry;
984 }
985
986 checkGLcall("set_compatible_renderbuffer");
987}
988
989GLenum surface_get_gl_buffer(IWineD3DSurface *iface)
990{
991 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
992 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->container;
993
994 TRACE("iface %p.\n", iface);
995
996 if (!(This->Flags & SFLAG_SWAPCHAIN))
997 {
998 ERR("Surface %p is not on a swapchain.\n", iface);
999 return GL_NONE;
1000 }
1001
1002 if (swapchain->backBuffer && swapchain->backBuffer[0] == iface)
1003 {
1004 if (swapchain->render_to_fbo)
1005 {
1006 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
1007 return GL_COLOR_ATTACHMENT0;
1008 }
1009 TRACE("Returning GL_BACK\n");
1010 return GL_BACK;
1011 }
1012 else if (swapchain->frontBuffer == iface)
1013 {
1014 TRACE("Returning GL_FRONT\n");
1015 return GL_FRONT;
1016 }
1017
1018 FIXME("Higher back buffer, returning GL_BACK\n");
1019 return GL_BACK;
1020}
1021
1022#ifdef VBOX_WITH_WDDM
1023static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect);
1024#endif
1025
1026/* Slightly inefficient way to handle multiple dirty rects but it works :) */
1027void surface_add_dirty_rect(IWineD3DSurface *iface, const RECT *dirty_rect)
1028{
1029 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1030 IWineD3DBaseTexture *baseTexture = NULL;
1031
1032 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
1033 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1034
1035 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
1036
1037 if (dirty_rect)
1038 {
1039 This->dirtyRect.left = min(This->dirtyRect.left, dirty_rect->left);
1040 This->dirtyRect.top = min(This->dirtyRect.top, dirty_rect->top);
1041 This->dirtyRect.right = max(This->dirtyRect.right, dirty_rect->right);
1042 This->dirtyRect.bottom = max(This->dirtyRect.bottom, dirty_rect->bottom);
1043 }
1044 else
1045 {
1046 This->dirtyRect.left = 0;
1047 This->dirtyRect.top = 0;
1048 This->dirtyRect.right = This->currentDesc.Width;
1049 This->dirtyRect.bottom = This->currentDesc.Height;
1050 }
1051
1052 TRACE("(%p) : Dirty: yes, Rect:(%d, %d, %d, %d)\n", This, This->dirtyRect.left,
1053 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
1054
1055 /* if the container is a basetexture then mark it dirty. */
1056 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture)))
1057 {
1058 TRACE("Passing to container\n");
1059 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
1060 IWineD3DBaseTexture_Release(baseTexture);
1061 }
1062}
1063
1064static BOOL surface_convert_color_to_argb(IWineD3DSurfaceImpl *This, DWORD color, DWORD *argb_color)
1065{
1066 IWineD3DDeviceImpl *device = This->resource.device;
1067
1068 switch(This->resource.format_desc->format)
1069 {
1070 case WINED3DFMT_P8_UINT:
1071 {
1072 DWORD alpha;
1073
1074 if (primary_render_target_is_p8(device))
1075 alpha = color << 24;
1076 else
1077 alpha = 0xFF000000;
1078
1079 if (This->palette) {
1080 *argb_color = (alpha |
1081 (This->palette->palents[color].peRed << 16) |
1082 (This->palette->palents[color].peGreen << 8) |
1083 (This->palette->palents[color].peBlue));
1084 } else {
1085 *argb_color = alpha;
1086 }
1087 }
1088 break;
1089
1090 case WINED3DFMT_B5G6R5_UNORM:
1091 {
1092 if (color == 0xFFFF) {
1093 *argb_color = 0xFFFFFFFF;
1094 } else {
1095 *argb_color = ((0xFF000000) |
1096 ((color & 0xF800) << 8) |
1097 ((color & 0x07E0) << 5) |
1098 ((color & 0x001F) << 3));
1099 }
1100 }
1101 break;
1102
1103 case WINED3DFMT_B8G8R8_UNORM:
1104 case WINED3DFMT_B8G8R8X8_UNORM:
1105 *argb_color = 0xFF000000 | color;
1106 break;
1107
1108 case WINED3DFMT_B8G8R8A8_UNORM:
1109 *argb_color = color;
1110 break;
1111
1112 default:
1113 ERR("Unhandled conversion from %s to ARGB!\n", debug_d3dformat(This->resource.format_desc->format));
1114 return FALSE;
1115 }
1116 return TRUE;
1117}
1118
1119static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1120{
1121 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1122 ULONG ref = InterlockedDecrement(&This->resource.ref);
1123 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
1124
1125 if (!ref)
1126 {
1127 surface_cleanup(This);
1128 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
1129
1130 TRACE("(%p) Released.\n", This);
1131 HeapFree(GetProcessHeap(), 0, This);
1132 }
1133
1134 return ref;
1135}
1136
1137/* ****************************************************
1138 IWineD3DSurface IWineD3DResource parts follow
1139 **************************************************** */
1140
1141void surface_internal_preload(IWineD3DSurface *iface, enum WINED3DSRGB srgb)
1142{
1143 /* TODO: check for locks */
1144 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1145 IWineD3DDeviceImpl *device = This->resource.device;
1146 IWineD3DBaseTexture *baseTexture = NULL;
1147
1148 TRACE("(%p)Checking to see if the container is a base texture\n", This);
1149 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
1150 IWineD3DBaseTextureImpl *tex_impl = (IWineD3DBaseTextureImpl *) baseTexture;
1151 TRACE("Passing to container\n");
1152 tex_impl->baseTexture.internal_preload(baseTexture, srgb);
1153 IWineD3DBaseTexture_Release(baseTexture);
1154 } else {
1155 struct wined3d_context *context = NULL;
1156
1157 TRACE("(%p) : About to load surface\n", This);
1158
1159 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
1160
1161 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
1162 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
1163 {
1164 if(palette9_changed(This)) {
1165 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1166 /* TODO: This is not necessarily needed with hw palettized texture support */
1167 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
1168 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1169 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
1170 }
1171 }
1172
1173 IWineD3DSurface_LoadTexture(iface, srgb == SRGB_SRGB ? TRUE : FALSE);
1174
1175 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
1176 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1177 GLclampf tmp;
1178 tmp = 0.9f;
1179 ENTER_GL();
1180 glPrioritizeTextures(1, &This->texture_name, &tmp);
1181 LEAVE_GL();
1182 }
1183
1184 if (context) context_release(context);
1185 }
1186}
1187
1188static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
1189 surface_internal_preload(iface, SRGB_ANY);
1190}
1191
1192/* Context activation is done by the caller. */
1193static void surface_remove_pbo(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
1194{
1195 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1196 This->resource.allocatedMemory =
1197 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1198
1199 ENTER_GL();
1200 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1201 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1202 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
1203 checkGLcall("glGetBufferSubDataARB");
1204 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
1205 checkGLcall("glDeleteBuffersARB");
1206 LEAVE_GL();
1207
1208 This->pbo = 0;
1209 This->Flags &= ~SFLAG_PBO;
1210}
1211
1212BOOL surface_init_sysmem(IWineD3DSurface *iface)
1213{
1214 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1215
1216 if(!This->resource.allocatedMemory)
1217 {
1218 This->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + RESOURCE_ALIGNMENT);
1219 if(!This->resource.heapMemory)
1220 {
1221 ERR("Out of memory\n");
1222 return FALSE;
1223 }
1224 This->resource.allocatedMemory =
1225 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1226 }
1227 else
1228 {
1229 memset(This->resource.allocatedMemory, 0, This->resource.size);
1230 }
1231
1232 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
1233 return TRUE;
1234}
1235
1236static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
1237 IWineD3DBaseTexture *texture = NULL;
1238 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
1239 IWineD3DDeviceImpl *device = This->resource.device;
1240 const struct wined3d_gl_info *gl_info;
1241 renderbuffer_entry_t *entry, *entry2;
1242 struct wined3d_context *context;
1243
1244 TRACE("(%p)\n", iface);
1245
1246 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
1247 /* Default pool resources are supposed to be destroyed before Reset is called.
1248 * Implicit resources stay however. So this means we have an implicit render target
1249 * or depth stencil. The content may be destroyed, but we still have to tear down
1250 * opengl resources, so we cannot leave early.
1251 *
1252 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1253 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1254 * or the depth stencil into an FBO the texture or render buffer will be removed
1255 * and all flags get lost
1256 */
1257 surface_init_sysmem(iface);
1258 } else {
1259 /* Load the surface into system memory */
1260 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
1261 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
1262 }
1263 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
1264 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSRGBTEX, FALSE);
1265 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
1266
1267 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
1268 gl_info = context->gl_info;
1269
1270 /* Destroy PBOs, but load them into real sysmem before */
1271 if (This->Flags & SFLAG_PBO)
1272 surface_remove_pbo(This, gl_info);
1273
1274 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1275 * all application-created targets the application has to release the surface
1276 * before calling _Reset
1277 */
1278 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
1279 ENTER_GL();
1280 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1281 LEAVE_GL();
1282 list_remove(&entry->entry);
1283 HeapFree(GetProcessHeap(), 0, entry);
1284 }
1285 list_init(&This->renderbuffers);
1286 This->current_renderbuffer = NULL;
1287
1288 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
1289 * destroy it
1290 */
1291 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
1292 if(!texture) {
1293 ENTER_GL();
1294 glDeleteTextures(1, &This->texture_name);
1295 This->texture_name = 0;
1296 glDeleteTextures(1, &This->texture_name_srgb);
1297 This->texture_name_srgb = 0;
1298 LEAVE_GL();
1299 } else {
1300 IWineD3DBaseTexture_Release(texture);
1301 }
1302
1303 context_release(context);
1304}
1305
1306/* ******************************************************
1307 IWineD3DSurface IWineD3DSurface parts follow
1308 ****************************************************** */
1309
1310/* Read the framebuffer back into the surface */
1311static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1312{
1313 IWineD3DDeviceImpl *myDevice = This->resource.device;
1314 const struct wined3d_gl_info *gl_info;
1315 struct wined3d_context *context;
1316 BYTE *mem;
1317 GLint fmt;
1318 GLint type;
1319 BYTE *row, *top, *bottom;
1320 int i;
1321 BOOL bpp;
1322 RECT local_rect;
1323 BOOL srcIsUpsideDown;
1324 GLint rowLen = 0;
1325 GLint skipPix = 0;
1326 GLint skipRow = 0;
1327
1328 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1329 static BOOL warned = FALSE;
1330 if(!warned) {
1331 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1332 warned = TRUE;
1333 }
1334 return;
1335 }
1336
1337 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
1338 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
1339 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
1340 * context->last_was_blit set on the unlock.
1341 */
1342 context = context_acquire(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1343 gl_info = context->gl_info;
1344
1345 ENTER_GL();
1346
1347 /* Select the correct read buffer, and give some debug output.
1348 * There is no need to keep track of the current read buffer or reset it, every part of the code
1349 * that reads sets the read buffer as desired.
1350 */
1351 if (surface_is_offscreen((IWineD3DSurface *) This))
1352 {
1353 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1354 * Read from the back buffer
1355 */
1356 TRACE("Locking offscreen render target\n");
1357 glReadBuffer(myDevice->offscreenBuffer);
1358 srcIsUpsideDown = TRUE;
1359 }
1360 else
1361 {
1362 /* Onscreen surfaces are always part of a swapchain */
1363 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This);
1364 TRACE("Locking %#x buffer\n", buffer);
1365 glReadBuffer(buffer);
1366 checkGLcall("glReadBuffer");
1367 srcIsUpsideDown = FALSE;
1368 }
1369
1370 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1371 if(!rect) {
1372 local_rect.left = 0;
1373 local_rect.top = 0;
1374 local_rect.right = This->currentDesc.Width;
1375 local_rect.bottom = This->currentDesc.Height;
1376 } else {
1377 local_rect = *rect;
1378 }
1379 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1380
1381 switch(This->resource.format_desc->format)
1382 {
1383 case WINED3DFMT_P8_UINT:
1384 {
1385 if(primary_render_target_is_p8(myDevice)) {
1386 /* In case of P8 render targets the index is stored in the alpha component */
1387 fmt = GL_ALPHA;
1388 type = GL_UNSIGNED_BYTE;
1389 mem = dest;
1390 bpp = This->resource.format_desc->byte_count;
1391 } else {
1392 /* GL can't return palettized data, so read ARGB pixels into a
1393 * separate block of memory and convert them into palettized format
1394 * in software. Slow, but if the app means to use palettized render
1395 * targets and locks it...
1396 *
1397 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1398 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1399 * for the color channels when palettizing the colors.
1400 */
1401 fmt = GL_RGB;
1402 type = GL_UNSIGNED_BYTE;
1403 pitch *= 3;
1404 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1405 if(!mem) {
1406 ERR("Out of memory\n");
1407 LEAVE_GL();
1408 return;
1409 }
1410 bpp = This->resource.format_desc->byte_count * 3;
1411 }
1412 }
1413 break;
1414
1415 default:
1416 mem = dest;
1417 fmt = This->resource.format_desc->glFormat;
1418 type = This->resource.format_desc->glType;
1419 bpp = This->resource.format_desc->byte_count;
1420 }
1421
1422 if(This->Flags & SFLAG_PBO) {
1423 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1424 checkGLcall("glBindBufferARB");
1425 if(mem != NULL) {
1426 ERR("mem not null for pbo -- unexpected\n");
1427 mem = NULL;
1428 }
1429 }
1430
1431 /* Save old pixel store pack state */
1432 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1433 checkGLcall("glGetIntegerv");
1434 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1435 checkGLcall("glGetIntegerv");
1436 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1437 checkGLcall("glGetIntegerv");
1438
1439 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1440 glPixelStorei(GL_PACK_ROW_LENGTH, This->currentDesc.Width);
1441 checkGLcall("glPixelStorei");
1442 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1443 checkGLcall("glPixelStorei");
1444 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1445 checkGLcall("glPixelStorei");
1446
1447 glReadPixels(local_rect.left, (!srcIsUpsideDown) ? (This->currentDesc.Height - local_rect.bottom) : local_rect.top ,
1448 local_rect.right - local_rect.left,
1449 local_rect.bottom - local_rect.top,
1450 fmt, type, mem);
1451 checkGLcall("glReadPixels");
1452
1453 /* Reset previous pixel store pack state */
1454 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1455 checkGLcall("glPixelStorei");
1456 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1457 checkGLcall("glPixelStorei");
1458 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1459 checkGLcall("glPixelStorei");
1460
1461 if(This->Flags & SFLAG_PBO) {
1462 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
1463 checkGLcall("glBindBufferARB");
1464
1465 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1466 * to get a pointer to it and perform the flipping in software. This is a lot
1467 * faster than calling glReadPixels for each line. In case we want more speed
1468 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1469 if(!srcIsUpsideDown) {
1470 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1471 checkGLcall("glBindBufferARB");
1472
1473 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1474 checkGLcall("glMapBufferARB");
1475 }
1476 }
1477
1478 /* TODO: Merge this with the palettization loop below for P8 targets */
1479 if(!srcIsUpsideDown) {
1480 UINT len, off;
1481 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1482 Flip the lines in software */
1483 len = (local_rect.right - local_rect.left) * bpp;
1484 off = local_rect.left * bpp;
1485
1486 row = HeapAlloc(GetProcessHeap(), 0, len);
1487 if(!row) {
1488 ERR("Out of memory\n");
1489 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT) HeapFree(GetProcessHeap(), 0, mem);
1490 LEAVE_GL();
1491 return;
1492 }
1493
1494 top = mem + pitch * local_rect.top;
1495 bottom = mem + pitch * (local_rect.bottom - 1);
1496 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
1497 memcpy(row, top + off, len);
1498 memcpy(top + off, bottom + off, len);
1499 memcpy(bottom + off, row, len);
1500 top += pitch;
1501 bottom -= pitch;
1502 }
1503 HeapFree(GetProcessHeap(), 0, row);
1504
1505 /* Unmap the temp PBO buffer */
1506 if(This->Flags & SFLAG_PBO) {
1507 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1508 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1509 }
1510 }
1511
1512 LEAVE_GL();
1513 context_release(context);
1514
1515 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1516 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1517 * the same color but we have no choice.
1518 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1519 */
1520 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(myDevice))
1521 {
1522 const PALETTEENTRY *pal = NULL;
1523 DWORD width = pitch / 3;
1524 int x, y, c;
1525
1526 if(This->palette) {
1527 pal = This->palette->palents;
1528 } else {
1529 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1530 HeapFree(GetProcessHeap(), 0, mem);
1531 return ;
1532 }
1533
1534 for(y = local_rect.top; y < local_rect.bottom; y++) {
1535 for(x = local_rect.left; x < local_rect.right; x++) {
1536 /* start lines pixels */
1537 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1538 const BYTE *green = blue + 1;
1539 const BYTE *red = green + 1;
1540
1541 for(c = 0; c < 256; c++) {
1542 if(*red == pal[c].peRed &&
1543 *green == pal[c].peGreen &&
1544 *blue == pal[c].peBlue)
1545 {
1546 *((BYTE *) dest + y * width + x) = c;
1547 break;
1548 }
1549 }
1550 }
1551 }
1552 HeapFree(GetProcessHeap(), 0, mem);
1553 }
1554}
1555
1556/* Read the framebuffer contents into a texture */
1557static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1558{
1559 IWineD3DDeviceImpl *device = This->resource.device;
1560 const struct wined3d_gl_info *gl_info;
1561 struct wined3d_context *context;
1562 GLint prevRead;
1563 BOOL alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1564
1565 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1566 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1567 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1568 */
1569 context = context_acquire(device, (IWineD3DSurface *) This, CTXUSAGE_RESOURCELOAD);
1570 gl_info = context->gl_info;
1571
1572 surface_bind_and_dirtify(This, srgb);
1573
1574 ENTER_GL();
1575 glGetIntegerv(GL_READ_BUFFER, &prevRead);
1576 LEAVE_GL();
1577
1578 /* Select the correct read buffer, and give some debug output.
1579 * There is no need to keep track of the current read buffer or reset it, every part of the code
1580 * that reads sets the read buffer as desired.
1581 */
1582 if (!surface_is_offscreen((IWineD3DSurface *)This))
1583 {
1584 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This);
1585 TRACE("Locking %#x buffer\n", buffer);
1586
1587 ENTER_GL();
1588 glReadBuffer(buffer);
1589 checkGLcall("glReadBuffer");
1590 LEAVE_GL();
1591 }
1592 else
1593 {
1594 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1595 * Read from the back buffer
1596 */
1597 TRACE("Locking offscreen render target\n");
1598 ENTER_GL();
1599 glReadBuffer(device->offscreenBuffer);
1600 checkGLcall("glReadBuffer");
1601 LEAVE_GL();
1602 }
1603
1604 if (!(This->Flags & alloc_flag))
1605 {
1606 surface_allocate_surface(This, gl_info, This->resource.format_desc, srgb);
1607 This->Flags |= alloc_flag;
1608 }
1609
1610 ENTER_GL();
1611 /* If !SrcIsUpsideDown we should flip the surface.
1612 * This can be done using glCopyTexSubImage2D but this
1613 * is VERY slow, so don't do that. We should prevent
1614 * this code from getting called in such cases or perhaps
1615 * we can use FBOs */
1616
1617 glCopyTexSubImage2D(This->texture_target, This->texture_level,
1618 0, 0, 0, 0, This->currentDesc.Width, This->currentDesc.Height);
1619 checkGLcall("glCopyTexSubImage2D");
1620
1621 glReadBuffer(prevRead);
1622 checkGLcall("glReadBuffer");
1623
1624 LEAVE_GL();
1625
1626 context_release(context);
1627
1628 TRACE("Updated target %d\n", This->texture_target);
1629}
1630
1631/* Context activation is done by the caller. */
1632void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1633{
1634 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1635 CONVERT_TYPES convert;
1636 struct wined3d_format_desc desc;
1637
1638 if (surface->Flags & alloc_flag) return;
1639
1640 d3dfmt_get_conv(surface, TRUE, TRUE, &desc, &convert);
1641 if(convert != NO_CONVERSION) surface->Flags |= SFLAG_CONVERTED;
1642 else surface->Flags &= ~SFLAG_CONVERTED;
1643
1644 surface_bind_and_dirtify(surface, srgb);
1645 surface_allocate_surface(surface, gl_info, &desc, srgb);
1646 surface->Flags |= alloc_flag;
1647}
1648
1649static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1650{
1651 IWineD3DDeviceImpl *device = This->resource.device;
1652 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1653
1654 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1655 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1656 * changed
1657 */
1658 if(!(This->Flags & SFLAG_DYNLOCK)) {
1659 This->lockCount++;
1660 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1661 if(This->lockCount > MAXLOCKCOUNT) {
1662 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1663 This->Flags |= SFLAG_DYNLOCK;
1664 }
1665 }
1666
1667 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1668 * Also don't create a PBO for systemmem surfaces.
1669 */
1670 if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->Flags & SFLAG_DYNLOCK)
1671 && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1672 && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1673 {
1674 GLenum error;
1675 struct wined3d_context *context;
1676
1677 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
1678 ENTER_GL();
1679
1680 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1681 error = glGetError();
1682 if(This->pbo == 0 || error != GL_NO_ERROR) {
1683 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1684 }
1685
1686 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1687
1688 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1689 checkGLcall("glBindBufferARB");
1690
1691 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1692 checkGLcall("glBufferDataARB");
1693
1694 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1695 checkGLcall("glBindBufferARB");
1696
1697 /* We don't need the system memory anymore and we can't even use it for PBOs */
1698 if(!(This->Flags & SFLAG_CLIENT)) {
1699 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1700 This->resource.heapMemory = NULL;
1701 }
1702 This->resource.allocatedMemory = NULL;
1703 This->Flags |= SFLAG_PBO;
1704 LEAVE_GL();
1705 context_release(context);
1706 }
1707 else if (!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO))
1708 {
1709 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1710 * or a pbo to map
1711 */
1712 if(!This->resource.heapMemory) {
1713 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1714 }
1715 This->resource.allocatedMemory =
1716 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1717 if(This->Flags & SFLAG_INSYSMEM) {
1718 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1719 }
1720 }
1721}
1722
1723static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
1724 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1725 IWineD3DDeviceImpl *myDevice = This->resource.device;
1726 const RECT *pass_rect = pRect;
1727
1728 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1729
1730 /* This is also done in the base class, but we have to verify this before loading any data from
1731 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1732 * may interfere, and all other bad things may happen
1733 */
1734 if (This->Flags & SFLAG_LOCKED) {
1735 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1736 return WINED3DERR_INVALIDCALL;
1737 }
1738 This->Flags |= SFLAG_LOCKED;
1739
1740 if (!(This->Flags & SFLAG_LOCKABLE))
1741 {
1742 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1743 }
1744
1745 if (Flags & WINED3DLOCK_DISCARD) {
1746 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1747 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1748 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1749 This->Flags |= SFLAG_INSYSMEM;
1750 goto lock_end;
1751 }
1752
1753 if (This->Flags & SFLAG_INSYSMEM) {
1754 TRACE("Local copy is up to date, not downloading data\n");
1755 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1756 goto lock_end;
1757 }
1758
1759 /* IWineD3DSurface_LoadLocation() does not check if the rectangle specifies
1760 * the full surface. Most callers don't need that, so do it here. */
1761 if (pRect && pRect->top == 0 && pRect->left == 0
1762 && pRect->right == This->currentDesc.Width
1763 && pRect->bottom == This->currentDesc.Height)
1764 {
1765 pass_rect = NULL;
1766 }
1767
1768 if (!(wined3d_settings.rendertargetlock_mode == RTL_DISABLE
1769 && ((This->Flags & SFLAG_SWAPCHAIN) || iface == myDevice->render_targets[0])))
1770 {
1771 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pass_rect);
1772 }
1773
1774lock_end:
1775 if (This->Flags & SFLAG_PBO)
1776 {
1777 const struct wined3d_gl_info *gl_info;
1778 struct wined3d_context *context;
1779
1780 context = context_acquire(myDevice, NULL, CTXUSAGE_RESOURCELOAD);
1781 gl_info = context->gl_info;
1782
1783 ENTER_GL();
1784 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1785 checkGLcall("glBindBufferARB");
1786
1787 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1788 if(This->resource.allocatedMemory) {
1789 ERR("The surface already has PBO memory allocated!\n");
1790 }
1791
1792 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1793 checkGLcall("glMapBufferARB");
1794
1795 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1796 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1797 checkGLcall("glBindBufferARB");
1798
1799 LEAVE_GL();
1800 context_release(context);
1801 }
1802
1803 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1804 /* Don't dirtify */
1805 } else {
1806 IWineD3DBaseTexture *pBaseTexture;
1807 /**
1808 * Dirtify on lock
1809 * as seen in msdn docs
1810 */
1811 surface_add_dirty_rect(iface, pRect);
1812
1813 /** Dirtify Container if needed */
1814 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture))) {
1815 TRACE("Making container dirty\n");
1816 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1817 IWineD3DBaseTexture_Release(pBaseTexture);
1818 } else {
1819 TRACE("Surface is standalone, no need to dirty the container\n");
1820 }
1821 }
1822
1823 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1824}
1825
1826static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1827 GLint prev_store;
1828 GLint prev_rasterpos[4];
1829 GLint skipBytes = 0;
1830 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1831 IWineD3DDeviceImpl *myDevice = This->resource.device;
1832 const struct wined3d_gl_info *gl_info;
1833 struct wined3d_context *context;
1834
1835 /* Activate the correct context for the render target */
1836 context = context_acquire(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1837 gl_info = context->gl_info;
1838
1839 ENTER_GL();
1840
1841 if (!surface_is_offscreen((IWineD3DSurface *)This))
1842 {
1843 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This);
1844 TRACE("Unlocking %#x buffer.\n", buffer);
1845 context_set_draw_buffer(context, buffer);
1846 }
1847 else
1848 {
1849 /* Primary offscreen render target */
1850 TRACE("Offscreen render target.\n");
1851 context_set_draw_buffer(context, myDevice->offscreenBuffer);
1852 }
1853
1854 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1855 checkGLcall("glGetIntegerv");
1856 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1857 checkGLcall("glGetIntegerv");
1858 glPixelZoom(1.0f, -1.0f);
1859 checkGLcall("glPixelZoom");
1860
1861 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1862 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1863 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1864
1865 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1866 checkGLcall("glRasterPos3i");
1867
1868 /* Some drivers(radeon dri, others?) don't like exceptions during
1869 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1870 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1871 * catch to put the dib section in InSync mode, which leads to a crash
1872 * and a blocked x server on my radeon card.
1873 *
1874 * The following lines read the dib section so it is put in InSync mode
1875 * before glDrawPixels is called and the crash is prevented. There won't
1876 * be any interfering gdi accesses, because UnlockRect is called from
1877 * ReleaseDC, and the app won't use the dc any more afterwards.
1878 */
1879 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1880 volatile BYTE read;
1881 read = This->resource.allocatedMemory[0];
1882 }
1883
1884 if(This->Flags & SFLAG_PBO) {
1885 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1886 checkGLcall("glBindBufferARB");
1887 }
1888
1889 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1890 if(This->Flags & SFLAG_LOCKED) {
1891 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1892 (This->lockedRect.bottom - This->lockedRect.top)-1,
1893 fmt, type,
1894 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1895 checkGLcall("glDrawPixels");
1896 } else {
1897 glDrawPixels(This->currentDesc.Width,
1898 This->currentDesc.Height,
1899 fmt, type, mem);
1900 checkGLcall("glDrawPixels");
1901 }
1902
1903 if(This->Flags & SFLAG_PBO) {
1904 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1905 checkGLcall("glBindBufferARB");
1906 }
1907
1908 glPixelZoom(1.0f, 1.0f);
1909 checkGLcall("glPixelZoom");
1910
1911 glRasterPos3iv(&prev_rasterpos[0]);
1912 checkGLcall("glRasterPos3iv");
1913
1914 /* Reset to previous pack row length */
1915 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1916 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH)");
1917
1918 LEAVE_GL();
1919 context_release(context);
1920}
1921
1922static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1923 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1924 IWineD3DDeviceImpl *myDevice = This->resource.device;
1925 BOOL fullsurface;
1926
1927 if (!(This->Flags & SFLAG_LOCKED)) {
1928 WARN("trying to Unlock an unlocked surf@%p\n", This);
1929 return WINEDDERR_NOTLOCKED;
1930 }
1931
1932 if (This->Flags & SFLAG_PBO)
1933 {
1934 const struct wined3d_gl_info *gl_info;
1935 struct wined3d_context *context;
1936
1937 TRACE("Freeing PBO memory\n");
1938
1939 context = context_acquire(myDevice, NULL, CTXUSAGE_RESOURCELOAD);
1940 gl_info = context->gl_info;
1941
1942 ENTER_GL();
1943 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1944 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1945 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1946 checkGLcall("glUnmapBufferARB");
1947 LEAVE_GL();
1948 context_release(context);
1949
1950 This->resource.allocatedMemory = NULL;
1951 }
1952
1953 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1954
1955 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1956 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1957 goto unlock_end;
1958 }
1959
1960 if ((This->Flags & SFLAG_SWAPCHAIN) || (myDevice->render_targets && iface == myDevice->render_targets[0]))
1961 {
1962 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1963 static BOOL warned = FALSE;
1964 if(!warned) {
1965 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1966 warned = TRUE;
1967 }
1968 goto unlock_end;
1969 }
1970
1971 if(This->dirtyRect.left == 0 &&
1972 This->dirtyRect.top == 0 &&
1973 This->dirtyRect.right == This->currentDesc.Width &&
1974 This->dirtyRect.bottom == This->currentDesc.Height) {
1975 fullsurface = TRUE;
1976 } else {
1977 /* TODO: Proper partial rectangle tracking */
1978 fullsurface = FALSE;
1979 This->Flags |= SFLAG_INSYSMEM;
1980 }
1981
1982 switch(wined3d_settings.rendertargetlock_mode) {
1983 case RTL_READTEX:
1984 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1985 /* drop through */
1986
1987 case RTL_READDRAW:
1988 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1989 break;
1990 }
1991
1992 if(!fullsurface) {
1993 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1994 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1995 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1996 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1997 * not fully up to date because only a subrectangle was read in LockRect.
1998 */
1999 This->Flags &= ~SFLAG_INSYSMEM;
2000 This->Flags |= SFLAG_INDRAWABLE;
2001 }
2002
2003 This->dirtyRect.left = This->currentDesc.Width;
2004 This->dirtyRect.top = This->currentDesc.Height;
2005 This->dirtyRect.right = 0;
2006 This->dirtyRect.bottom = 0;
2007 } else if(iface == myDevice->stencilBufferTarget) {
2008 FIXME("Depth Stencil buffer locking is not implemented\n");
2009 } else {
2010 /* The rest should be a normal texture */
2011 IWineD3DBaseTextureImpl *impl;
2012 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
2013 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
2014 * states need resetting
2015 */
2016 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
2017 if(impl->baseTexture.bindCount) {
2018 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
2019 }
2020 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
2021 }
2022 }
2023
2024 unlock_end:
2025 This->Flags &= ~SFLAG_LOCKED;
2026 memset(&This->lockedRect, 0, sizeof(RECT));
2027
2028 /* Overlays have to be redrawn manually after changes with the GL implementation */
2029 if(This->overlay_dest) {
2030 IWineD3DSurface_DrawOverlay(iface);
2031 }
2032 return WINED3D_OK;
2033}
2034
2035static void surface_release_client_storage(IWineD3DSurface *iface)
2036{
2037 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2038 struct wined3d_context *context;
2039
2040 context = context_acquire(This->resource.device, NULL, CTXUSAGE_RESOURCELOAD);
2041
2042 ENTER_GL();
2043 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
2044#ifdef VBOX_WITH_WDDM
2045 if (!VBOXSHRC_IS_SHARED_OPENED(This))
2046#endif
2047 {
2048 if(This->texture_name)
2049 {
2050 surface_bind_and_dirtify(This, FALSE);
2051 glTexImage2D(This->texture_target, This->texture_level,
2052 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2053 }
2054 if(This->texture_name_srgb)
2055 {
2056 surface_bind_and_dirtify(This, TRUE);
2057 glTexImage2D(This->texture_target, This->texture_level,
2058 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2059 }
2060 }
2061 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
2062
2063 LEAVE_GL();
2064 context_release(context);
2065
2066 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSRGBTEX, FALSE);
2067 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
2068 surface_force_reload(iface);
2069}
2070
2071static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
2072{
2073 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2074 WINED3DLOCKED_RECT lock;
2075 HRESULT hr;
2076 RGBQUAD col[256];
2077
2078 TRACE("(%p)->(%p)\n",This,pHDC);
2079
2080 if(This->Flags & SFLAG_USERPTR) {
2081 ERR("Not supported on surfaces with an application-provided surfaces\n");
2082 return WINEDDERR_NODC;
2083 }
2084
2085 /* Give more detailed info for ddraw */
2086 if (This->Flags & SFLAG_DCINUSE)
2087 return WINEDDERR_DCALREADYCREATED;
2088
2089 /* Can't GetDC if the surface is locked */
2090 if (This->Flags & SFLAG_LOCKED)
2091 return WINED3DERR_INVALIDCALL;
2092
2093 memset(&lock, 0, sizeof(lock)); /* To be sure */
2094
2095 /* Create a DIB section if there isn't a hdc yet */
2096 if(!This->hDC) {
2097 if(This->Flags & SFLAG_CLIENT) {
2098 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2099 surface_release_client_storage(iface);
2100 }
2101 hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
2102 if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2103
2104 /* Use the dib section from now on if we are not using a PBO */
2105 if(!(This->Flags & SFLAG_PBO))
2106 This->resource.allocatedMemory = This->dib.bitmap_data;
2107 }
2108
2109 /* Lock the surface */
2110 hr = IWineD3DSurface_LockRect(iface,
2111 &lock,
2112 NULL,
2113 0);
2114
2115 if(This->Flags & SFLAG_PBO) {
2116 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
2117 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2118 }
2119
2120 if(FAILED(hr)) {
2121 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
2122 /* keep the dib section */
2123 return hr;
2124 }
2125
2126 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
2127 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
2128 {
2129 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2130 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2131 unsigned int n;
2132 const PALETTEENTRY *pal = NULL;
2133
2134 if(This->palette) {
2135 pal = This->palette->palents;
2136 } else {
2137 IWineD3DSurfaceImpl *dds_primary;
2138 IWineD3DSwapChainImpl *swapchain;
2139#ifdef VBOX_WITH_WDDM
2140 /* tmp work-around */
2141 swapchain = (IWineD3DSwapChainImpl *)This->resource.device->swapchains[This->resource.device->NumberOfSwapChains-1];
2142#else
2143 swapchain = (IWineD3DSwapChainImpl *)This->resource.device->swapchains[0];
2144#endif
2145 dds_primary = (IWineD3DSurfaceImpl *)swapchain->frontBuffer;
2146 if (dds_primary && dds_primary->palette)
2147 pal = dds_primary->palette->palents;
2148 }
2149
2150 if (pal) {
2151 for (n=0; n<256; n++) {
2152 col[n].rgbRed = pal[n].peRed;
2153 col[n].rgbGreen = pal[n].peGreen;
2154 col[n].rgbBlue = pal[n].peBlue;
2155 col[n].rgbReserved = 0;
2156 }
2157 SetDIBColorTable(This->hDC, 0, 256, col);
2158 }
2159 }
2160
2161 *pHDC = This->hDC;
2162 TRACE("returning %p\n",*pHDC);
2163 This->Flags |= SFLAG_DCINUSE;
2164
2165 return WINED3D_OK;
2166}
2167
2168static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2169{
2170 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2171
2172 TRACE("(%p)->(%p)\n",This,hDC);
2173
2174 if (!(This->Flags & SFLAG_DCINUSE))
2175 return WINEDDERR_NODC;
2176
2177 if (This->hDC !=hDC) {
2178 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2179 return WINEDDERR_NODC;
2180 }
2181
2182 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
2183 /* Copy the contents of the DIB over to the PBO */
2184 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2185 }
2186
2187 /* we locked first, so unlock now */
2188 IWineD3DSurface_UnlockRect(iface);
2189
2190 This->Flags &= ~SFLAG_DCINUSE;
2191
2192 return WINED3D_OK;
2193}
2194
2195/* ******************************************************
2196 IWineD3DSurface Internal (No mapping to directx api) parts follow
2197 ****************************************************** */
2198
2199HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, struct wined3d_format_desc *desc, CONVERT_TYPES *convert)
2200{
2201 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2202 IWineD3DDeviceImpl *device = This->resource.device;
2203 BOOL blit_supported = FALSE;
2204 RECT rect = {0, 0, This->pow2Width, This->pow2Height};
2205
2206 /* Copy the default values from the surface. Below we might perform fixups */
2207 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2208 *desc = *This->resource.format_desc;
2209 *convert = NO_CONVERSION;
2210
2211 /* Ok, now look if we have to do any conversion */
2212 switch(This->resource.format_desc->format)
2213 {
2214 case WINED3DFMT_P8_UINT:
2215 /* ****************
2216 Paletted Texture
2217 **************** */
2218
2219 blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, BLIT_OP_BLIT,
2220 &rect, This->resource.usage, This->resource.pool,
2221 This->resource.format_desc, &rect, This->resource.usage,
2222 This->resource.pool, This->resource.format_desc);
2223
2224 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2225 * texturing. Further also use conversion in case of color keying.
2226 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2227 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2228 * conflicts with this.
2229 */
2230 if (!((blit_supported && device->render_targets && This == (IWineD3DSurfaceImpl*)device->render_targets[0]))
2231 || colorkey_active || !use_texturing)
2232 {
2233 desc->glFormat = GL_RGBA;
2234 desc->glInternal = GL_RGBA;
2235 desc->glType = GL_UNSIGNED_BYTE;
2236 desc->conv_byte_count = 4;
2237 if(colorkey_active) {
2238 *convert = CONVERT_PALETTED_CK;
2239 } else {
2240 *convert = CONVERT_PALETTED;
2241 }
2242 }
2243 break;
2244
2245 case WINED3DFMT_B2G3R3_UNORM:
2246 /* **********************
2247 GL_UNSIGNED_BYTE_3_3_2
2248 ********************** */
2249 if (colorkey_active) {
2250 /* This texture format will never be used.. So do not care about color keying
2251 up until the point in time it will be needed :-) */
2252 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2253 }
2254 break;
2255
2256 case WINED3DFMT_B5G6R5_UNORM:
2257 if (colorkey_active) {
2258 *convert = CONVERT_CK_565;
2259 desc->glFormat = GL_RGBA;
2260 desc->glInternal = GL_RGB5_A1;
2261 desc->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2262 desc->conv_byte_count = 2;
2263 }
2264 break;
2265
2266 case WINED3DFMT_B5G5R5X1_UNORM:
2267 if (colorkey_active) {
2268 *convert = CONVERT_CK_5551;
2269 desc->glFormat = GL_BGRA;
2270 desc->glInternal = GL_RGB5_A1;
2271 desc->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2272 desc->conv_byte_count = 2;
2273 }
2274 break;
2275
2276 case WINED3DFMT_B8G8R8_UNORM:
2277 if (colorkey_active) {
2278 *convert = CONVERT_CK_RGB24;
2279 desc->glFormat = GL_RGBA;
2280 desc->glInternal = GL_RGBA8;
2281 desc->glType = GL_UNSIGNED_INT_8_8_8_8;
2282 desc->conv_byte_count = 4;
2283 }
2284 break;
2285
2286 case WINED3DFMT_B8G8R8X8_UNORM:
2287 if (colorkey_active) {
2288 *convert = CONVERT_RGB32_888;
2289 desc->glFormat = GL_RGBA;
2290 desc->glInternal = GL_RGBA8;
2291 desc->glType = GL_UNSIGNED_INT_8_8_8_8;
2292 desc->conv_byte_count = 4;
2293 }
2294 break;
2295
2296 default:
2297 break;
2298 }
2299
2300 return WINED3D_OK;
2301}
2302
2303void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2304{
2305 IWineD3DDeviceImpl *device = This->resource.device;
2306 IWineD3DPaletteImpl *pal = This->palette;
2307 BOOL index_in_alpha = FALSE;
2308 unsigned int i;
2309
2310 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2311 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2312 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2313 * duplicate entries. Store the color key in the unused alpha component to speed the
2314 * download up and to make conversion unneeded. */
2315 index_in_alpha = primary_render_target_is_p8(device);
2316
2317 if (!pal)
2318 {
2319 UINT dxVersion = ((IWineD3DImpl *)device->wined3d)->dxVersion;
2320
2321 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2322 if (dxVersion <= 7)
2323 {
2324 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2325 if (index_in_alpha)
2326 {
2327 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2328 * there's no palette at this time. */
2329 for (i = 0; i < 256; i++) table[i][3] = i;
2330 }
2331 }
2332 else
2333 {
2334 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2335 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2336 * capability flag is present (wine does advertise this capability) */
2337 for (i = 0; i < 256; ++i)
2338 {
2339 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2340 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2341 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2342 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2343 }
2344 }
2345 }
2346 else
2347 {
2348 TRACE("Using surface palette %p\n", pal);
2349 /* Get the surface's palette */
2350 for (i = 0; i < 256; ++i)
2351 {
2352 table[i][0] = pal->palents[i].peRed;
2353 table[i][1] = pal->palents[i].peGreen;
2354 table[i][2] = pal->palents[i].peBlue;
2355
2356 /* When index_in_alpha is set the palette index is stored in the
2357 * alpha component. In case of a readback we can then read
2358 * GL_ALPHA. Color keying is handled in BltOverride using a
2359 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2360 * color key itself is passed to glAlphaFunc in other cases the
2361 * alpha component of pixels that should be masked away is set to 0. */
2362 if (index_in_alpha)
2363 {
2364 table[i][3] = i;
2365 }
2366 else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2367 && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2368 {
2369 table[i][3] = 0x00;
2370 }
2371 else if(pal->Flags & WINEDDPCAPS_ALPHA)
2372 {
2373 table[i][3] = pal->palents[i].peFlags;
2374 }
2375 else
2376 {
2377 table[i][3] = 0xFF;
2378 }
2379 }
2380 }
2381}
2382
2383static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2384 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2385{
2386 const BYTE *source;
2387 BYTE *dest;
2388 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2389
2390 switch (convert) {
2391 case NO_CONVERSION:
2392 {
2393 memcpy(dst, src, pitch * height);
2394 break;
2395 }
2396 case CONVERT_PALETTED:
2397 case CONVERT_PALETTED_CK:
2398 {
2399 IWineD3DPaletteImpl* pal = This->palette;
2400 BYTE table[256][4];
2401 unsigned int x, y;
2402
2403 if( pal == NULL) {
2404 /* TODO: If we are a sublevel, try to get the palette from level 0 */
2405 }
2406
2407 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2408
2409 for (y = 0; y < height; y++)
2410 {
2411 source = src + pitch * y;
2412 dest = dst + outpitch * y;
2413 /* This is an 1 bpp format, using the width here is fine */
2414 for (x = 0; x < width; x++) {
2415 BYTE color = *source++;
2416 *dest++ = table[color][0];
2417 *dest++ = table[color][1];
2418 *dest++ = table[color][2];
2419 *dest++ = table[color][3];
2420 }
2421 }
2422 }
2423 break;
2424
2425 case CONVERT_CK_565:
2426 {
2427 /* Converting the 565 format in 5551 packed to emulate color-keying.
2428
2429 Note : in all these conversion, it would be best to average the averaging
2430 pixels to get the color of the pixel that will be color-keyed to
2431 prevent 'color bleeding'. This will be done later on if ever it is
2432 too visible.
2433
2434 Note2: Nvidia documents say that their driver does not support alpha + color keying
2435 on the same surface and disables color keying in such a case
2436 */
2437 unsigned int x, y;
2438 const WORD *Source;
2439 WORD *Dest;
2440
2441 TRACE("Color keyed 565\n");
2442
2443 for (y = 0; y < height; y++) {
2444 Source = (const WORD *)(src + y * pitch);
2445 Dest = (WORD *) (dst + y * outpitch);
2446 for (x = 0; x < width; x++ ) {
2447 WORD color = *Source++;
2448 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2449 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2450 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2451 *Dest |= 0x0001;
2452 }
2453 Dest++;
2454 }
2455 }
2456 }
2457 break;
2458
2459 case CONVERT_CK_5551:
2460 {
2461 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2462 unsigned int x, y;
2463 const WORD *Source;
2464 WORD *Dest;
2465 TRACE("Color keyed 5551\n");
2466 for (y = 0; y < height; y++) {
2467 Source = (const WORD *)(src + y * pitch);
2468 Dest = (WORD *) (dst + y * outpitch);
2469 for (x = 0; x < width; x++ ) {
2470 WORD color = *Source++;
2471 *Dest = color;
2472 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2473 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2474 *Dest |= (1 << 15);
2475 }
2476 else {
2477 *Dest &= ~(1 << 15);
2478 }
2479 Dest++;
2480 }
2481 }
2482 }
2483 break;
2484
2485 case CONVERT_CK_RGB24:
2486 {
2487 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2488 unsigned int x, y;
2489 for (y = 0; y < height; y++)
2490 {
2491 source = src + pitch * y;
2492 dest = dst + outpitch * y;
2493 for (x = 0; x < width; x++) {
2494 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2495 DWORD dstcolor = color << 8;
2496 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2497 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2498 dstcolor |= 0xff;
2499 }
2500 *(DWORD*)dest = dstcolor;
2501 source += 3;
2502 dest += 4;
2503 }
2504 }
2505 }
2506 break;
2507
2508 case CONVERT_RGB32_888:
2509 {
2510 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2511 unsigned int x, y;
2512 for (y = 0; y < height; y++)
2513 {
2514 source = src + pitch * y;
2515 dest = dst + outpitch * y;
2516 for (x = 0; x < width; x++) {
2517 DWORD color = 0xffffff & *(const DWORD*)source;
2518 DWORD dstcolor = color << 8;
2519 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2520 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2521 dstcolor |= 0xff;
2522 }
2523 *(DWORD*)dest = dstcolor;
2524 source += 4;
2525 dest += 4;
2526 }
2527 }
2528 }
2529 break;
2530
2531 default:
2532 ERR("Unsupported conversion type %#x.\n", convert);
2533 }
2534 return WINED3D_OK;
2535}
2536
2537BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2538{
2539 IWineD3DDeviceImpl *device = This->resource.device;
2540
2541 if (This->palette || (This->resource.format_desc->format != WINED3DFMT_P8_UINT
2542 && This->resource.format_desc->format != WINED3DFMT_P8_UINT_A8_UNORM))
2543 {
2544 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2545 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2546 */
2547 return FALSE;
2548 }
2549
2550 if (This->palette9)
2551 {
2552 if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2553 {
2554 return FALSE;
2555 }
2556 } else {
2557 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2558 }
2559 memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2560 return TRUE;
2561}
2562
2563static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2564 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2565 DWORD flag = srgb_mode ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
2566
2567 if (!(This->Flags & flag)) {
2568 TRACE("Reloading because surface is dirty\n");
2569 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2570 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2571 /* Reload: vice versa OR */
2572 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2573 /* Also reload: Color key is active AND the color key has changed */
2574 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2575 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2576 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2577 TRACE("Reloading because of color keying\n");
2578 /* To perform the color key conversion we need a sysmem copy of
2579 * the surface. Make sure we have it
2580 */
2581
2582 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2583 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2584 /* TODO: This is not necessarily needed with hw palettized texture support */
2585 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2586 } else {
2587 TRACE("surface is already in texture\n");
2588 return WINED3D_OK;
2589 }
2590
2591 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2592 * These resources are not bound by device size or format restrictions. Because of this,
2593 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2594 * However, these resources can always be created, locked, and copied.
2595 */
2596 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2597 {
2598 FIXME("(%p) Operation not supported for scratch textures\n",This);
2599 return WINED3DERR_INVALIDCALL;
2600 }
2601
2602 IWineD3DSurface_LoadLocation(iface, flag, NULL /* no partial locking for textures yet */);
2603
2604#if 0
2605 {
2606 static unsigned int gen = 0;
2607 char buffer[4096];
2608 ++gen;
2609 if ((gen % 10) == 0) {
2610 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm",
2611 This, This->texture_target, This->texture_level, gen);
2612 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2613 }
2614 /*
2615 * debugging crash code
2616 if (gen == 250) {
2617 void** test = NULL;
2618 *test = 0;
2619 }
2620 */
2621 }
2622#endif
2623
2624 if (!(This->Flags & SFLAG_DONOTFREE)) {
2625 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2626 This->resource.allocatedMemory = NULL;
2627 This->resource.heapMemory = NULL;
2628 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2629 }
2630
2631 return WINED3D_OK;
2632}
2633
2634/* Context activation is done by the caller. */
2635static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface, BOOL srgb) {
2636 /* TODO: check for locks */
2637 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2638 IWineD3DBaseTexture *baseTexture = NULL;
2639
2640 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2641 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2642 TRACE("Passing to container\n");
2643 IWineD3DBaseTexture_BindTexture(baseTexture, srgb);
2644 IWineD3DBaseTexture_Release(baseTexture);
2645 }
2646 else
2647 {
2648 GLuint *name;
2649
2650 TRACE("(%p) : Binding surface\n", This);
2651
2652 name = srgb ? &This->texture_name_srgb : &This->texture_name;
2653
2654 ENTER_GL();
2655
2656 if (!This->texture_level)
2657 {
2658 if (!*name) {
2659#ifdef VBOX_WITH_WDDM
2660 if (VBOXSHRC_IS_SHARED_OPENED(This))
2661 {
2662 *name = VBOXSHRC_GET_SHAREHANDLE(This);
2663 }
2664 else
2665#endif
2666 {
2667 glGenTextures(1, name);
2668#ifdef VBOX_WITH_WDDM
2669 if (VBOXSHRC_IS_SHARED(This))
2670 {
2671 VBOXSHRC_SET_SHAREHANDLE(This, *name);
2672 }
2673#endif
2674 }
2675 checkGLcall("glGenTextures");
2676 TRACE("Surface %p given name %d\n", This, *name);
2677
2678 glBindTexture(This->texture_target, *name);
2679 checkGLcall("glBindTexture");
2680 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2681 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2682 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2683 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2684 glTexParameteri(This->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2685 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2686 glTexParameteri(This->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2687 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2688 glTexParameteri(This->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2689 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2690 }
2691 /* This is where we should be reducing the amount of GLMemoryUsed */
2692 } else if (*name) {
2693 /* Mipmap surfaces should have a base texture container */
2694 ERR("Mipmap surface has a glTexture bound to it!\n");
2695 }
2696
2697 glBindTexture(This->texture_target, *name);
2698 checkGLcall("glBindTexture");
2699
2700 LEAVE_GL();
2701 }
2702}
2703
2704#include <errno.h>
2705#include <stdio.h>
2706static HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename)
2707{
2708 FILE* f = NULL;
2709 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2710 char *allocatedMemory;
2711 const char *textureRow;
2712 IWineD3DSwapChain *swapChain = NULL;
2713 int width, height, i, y;
2714 GLuint tmpTexture = 0;
2715 DWORD color;
2716 /*FIXME:
2717 Textures may not be stored in ->allocatedgMemory and a GlTexture
2718 so we should lock the surface before saving a snapshot, or at least check that
2719 */
2720 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2721 by calling GetTexImage and in compressed form by calling
2722 GetCompressedTexImageARB. Queried compressed images can be saved and
2723 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2724 texture images do not need to be processed by the GL and should
2725 significantly improve texture loading performance relative to uncompressed
2726 images. */
2727
2728/* Setup the width and height to be the internal texture width and height. */
2729 width = This->pow2Width;
2730 height = This->pow2Height;
2731/* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2732 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2733
2734 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2735 /* if were not a real texture then read the back buffer into a real texture */
2736 /* we don't want to interfere with the back buffer so read the data into a temporary
2737 * texture and then save the data out of the temporary texture
2738 */
2739 GLint prevRead;
2740 ENTER_GL();
2741 TRACE("(%p) Reading render target into texture\n", This);
2742
2743 glGenTextures(1, &tmpTexture);
2744 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2745
2746 glTexImage2D(GL_TEXTURE_2D,
2747 0,
2748 GL_RGBA,
2749 width,
2750 height,
2751 0/*border*/,
2752 GL_RGBA,
2753 GL_UNSIGNED_INT_8_8_8_8_REV,
2754 NULL);
2755
2756 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2757 checkGLcall("glGetIntegerv");
2758 glReadBuffer(swapChain ? GL_BACK : This->resource.device->offscreenBuffer);
2759 checkGLcall("glReadBuffer");
2760 glCopyTexImage2D(GL_TEXTURE_2D,
2761 0,
2762 GL_RGBA,
2763 0,
2764 0,
2765 width,
2766 height,
2767 0);
2768
2769 checkGLcall("glCopyTexImage2D");
2770 glReadBuffer(prevRead);
2771 LEAVE_GL();
2772
2773 } else { /* bind the real texture, and make sure it up to date */
2774 surface_internal_preload(iface, SRGB_RGB);
2775 surface_bind_and_dirtify(This, FALSE);
2776 }
2777 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2778 ENTER_GL();
2779 FIXME("Saving texture level %d width %d height %d\n", This->texture_level, width, height);
2780 glGetTexImage(GL_TEXTURE_2D, This->texture_level, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, allocatedMemory);
2781 checkGLcall("glGetTexImage");
2782 if (tmpTexture) {
2783 glBindTexture(GL_TEXTURE_2D, 0);
2784 glDeleteTextures(1, &tmpTexture);
2785 }
2786 LEAVE_GL();
2787
2788 f = fopen(filename, "w+");
2789 if (NULL == f) {
2790 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2791 return WINED3DERR_INVALIDCALL;
2792 }
2793/* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2794 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format_desc->format));
2795/* TGA header */
2796 fputc(0,f);
2797 fputc(0,f);
2798 fputc(2,f);
2799 fputc(0,f);
2800 fputc(0,f);
2801 fputc(0,f);
2802 fputc(0,f);
2803 fputc(0,f);
2804 fputc(0,f);
2805 fputc(0,f);
2806 fputc(0,f);
2807 fputc(0,f);
2808/* short width*/
2809 fwrite(&width,2,1,f);
2810/* short height */
2811 fwrite(&height,2,1,f);
2812/* format rgba */
2813 fputc(0x20,f);
2814 fputc(0x28,f);
2815/* raw data */
2816 /* if the data is upside down if we've fetched it from a back buffer, so it needs flipping again to make it the correct way up */
2817 if(swapChain)
2818 textureRow = allocatedMemory + (width * (height - 1) *4);
2819 else
2820 textureRow = allocatedMemory;
2821 for (y = 0 ; y < height; y++) {
2822 for (i = 0; i < width; i++) {
2823 color = *((const DWORD*)textureRow);
2824 fputc((color >> 16) & 0xFF, f); /* B */
2825 fputc((color >> 8) & 0xFF, f); /* G */
2826 fputc((color >> 0) & 0xFF, f); /* R */
2827 fputc((color >> 24) & 0xFF, f); /* A */
2828 textureRow += 4;
2829 }
2830 /* take two rows of the pointer to the texture memory */
2831 if(swapChain)
2832 (textureRow-= width << 3);
2833
2834 }
2835 TRACE("Closing file\n");
2836 fclose(f);
2837
2838 if(swapChain) {
2839 IWineD3DSwapChain_Release(swapChain);
2840 }
2841 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2842 return WINED3D_OK;
2843}
2844
2845static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2846 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2847 HRESULT hr;
2848
2849 TRACE("(%p) : Calling base function first\n", This);
2850 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2851 if(SUCCEEDED(hr)) {
2852 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2853 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format_desc->glFormat,
2854 This->resource.format_desc->glInternal, This->resource.format_desc->glType);
2855 }
2856 return hr;
2857}
2858
2859static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2860 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2861
2862 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2863 WARN("Surface is locked or the HDC is in use\n");
2864 return WINED3DERR_INVALIDCALL;
2865 }
2866
2867 if(Mem && Mem != This->resource.allocatedMemory) {
2868 void *release = NULL;
2869
2870 /* Do I have to copy the old surface content? */
2871 if(This->Flags & SFLAG_DIBSECTION) {
2872 /* Release the DC. No need to hold the critical section for the update
2873 * Thread because this thread runs only on front buffers, but this method
2874 * fails for render targets in the check above.
2875 */
2876 SelectObject(This->hDC, This->dib.holdbitmap);
2877 DeleteDC(This->hDC);
2878 /* Release the DIB section */
2879 DeleteObject(This->dib.DIBsection);
2880 This->dib.bitmap_data = NULL;
2881 This->resource.allocatedMemory = NULL;
2882 This->hDC = NULL;
2883 This->Flags &= ~SFLAG_DIBSECTION;
2884 } else if(!(This->Flags & SFLAG_USERPTR)) {
2885 release = This->resource.heapMemory;
2886 This->resource.heapMemory = NULL;
2887 }
2888 This->resource.allocatedMemory = Mem;
2889 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2890
2891 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2892 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2893
2894 /* For client textures opengl has to be notified */
2895 if(This->Flags & SFLAG_CLIENT) {
2896 surface_release_client_storage(iface);
2897 }
2898
2899 /* Now free the old memory if any */
2900 HeapFree(GetProcessHeap(), 0, release);
2901 } else if(This->Flags & SFLAG_USERPTR) {
2902 /* LockRect and GetDC will re-create the dib section and allocated memory */
2903 This->resource.allocatedMemory = NULL;
2904 /* HeapMemory should be NULL already */
2905 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2906 This->Flags &= ~SFLAG_USERPTR;
2907
2908 if(This->Flags & SFLAG_CLIENT) {
2909 surface_release_client_storage(iface);
2910 }
2911 }
2912 return WINED3D_OK;
2913}
2914
2915void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2916
2917 /* Flip the surface contents */
2918 /* Flip the DC */
2919 {
2920 HDC tmp;
2921 tmp = front->hDC;
2922 front->hDC = back->hDC;
2923 back->hDC = tmp;
2924 }
2925
2926 /* Flip the DIBsection */
2927 {
2928 HBITMAP tmp;
2929 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
2930 tmp = front->dib.DIBsection;
2931 front->dib.DIBsection = back->dib.DIBsection;
2932 back->dib.DIBsection = tmp;
2933
2934 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
2935 else front->Flags &= ~SFLAG_DIBSECTION;
2936 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
2937 else back->Flags &= ~SFLAG_DIBSECTION;
2938 }
2939
2940 /* Flip the surface data */
2941 {
2942 void* tmp;
2943
2944 tmp = front->dib.bitmap_data;
2945 front->dib.bitmap_data = back->dib.bitmap_data;
2946 back->dib.bitmap_data = tmp;
2947
2948 tmp = front->resource.allocatedMemory;
2949 front->resource.allocatedMemory = back->resource.allocatedMemory;
2950 back->resource.allocatedMemory = tmp;
2951
2952 tmp = front->resource.heapMemory;
2953 front->resource.heapMemory = back->resource.heapMemory;
2954 back->resource.heapMemory = tmp;
2955 }
2956
2957 /* Flip the PBO */
2958 {
2959 GLuint tmp_pbo = front->pbo;
2960 front->pbo = back->pbo;
2961 back->pbo = tmp_pbo;
2962 }
2963
2964 /* client_memory should not be different, but just in case */
2965 {
2966 BOOL tmp;
2967 tmp = front->dib.client_memory;
2968 front->dib.client_memory = back->dib.client_memory;
2969 back->dib.client_memory = tmp;
2970 }
2971
2972 /* Flip the opengl texture */
2973 {
2974 GLuint tmp;
2975
2976 tmp = back->texture_name;
2977 back->texture_name = front->texture_name;
2978 front->texture_name = tmp;
2979
2980 tmp = back->texture_name_srgb;
2981 back->texture_name_srgb = front->texture_name_srgb;
2982 front->texture_name_srgb = tmp;
2983 }
2984
2985 {
2986 DWORD tmp_flags = back->Flags;
2987 back->Flags = front->Flags;
2988 front->Flags = tmp_flags;
2989 }
2990}
2991
2992static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2993 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2994 IWineD3DSwapChainImpl *swapchain = NULL;
2995 HRESULT hr;
2996 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2997
2998 /* Flipping is only supported on RenderTargets and overlays*/
2999 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
3000 WARN("Tried to flip a non-render target, non-overlay surface\n");
3001 return WINEDDERR_NOTFLIPPABLE;
3002 }
3003
3004 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
3005 flip_surface(This, (IWineD3DSurfaceImpl *) override);
3006
3007 /* Update the overlay if it is visible */
3008 if(This->overlay_dest) {
3009 return IWineD3DSurface_DrawOverlay((IWineD3DSurface *) This);
3010 } else {
3011 return WINED3D_OK;
3012 }
3013 }
3014
3015 if(override) {
3016 /* DDraw sets this for the X11 surfaces, so don't confuse the user
3017 * FIXME("(%p) Target override is not supported by now\n", This);
3018 * Additionally, it isn't really possible to support triple-buffering
3019 * properly on opengl at all
3020 */
3021 }
3022
3023 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
3024 if(!swapchain) {
3025 ERR("Flipped surface is not on a swapchain\n");
3026 return WINEDDERR_NOTFLIPPABLE;
3027 }
3028
3029 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
3030 * and only d3d8 and d3d9 apps specify the presentation interval
3031 */
3032 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
3033 /* Most common case first to avoid wasting time on all the other cases */
3034 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
3035 } else if(Flags & WINEDDFLIP_NOVSYNC) {
3036 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3037 } else if(Flags & WINEDDFLIP_INTERVAL2) {
3038 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
3039 } else if(Flags & WINEDDFLIP_INTERVAL3) {
3040 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
3041 } else {
3042 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
3043 }
3044
3045 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
3046 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *)swapchain,
3047 NULL, NULL, swapchain->win_handle, NULL, 0);
3048 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
3049 return hr;
3050}
3051
3052/* Does a direct frame buffer -> texture copy. Stretching is done
3053 * with single pixel copy calls
3054 */
3055static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface,
3056 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter, BOOL doit)
3057{
3058 IWineD3DDeviceImpl *myDevice = This->resource.device;
3059 float xrel, yrel;
3060 UINT row;
3061 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3062 struct wined3d_context *context;
3063 BOOL upsidedown = FALSE;
3064 RECT dst_rect = *dst_rect_in;
3065
3066 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3067 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3068 */
3069 if(dst_rect.top > dst_rect.bottom) {
3070 UINT tmp = dst_rect.bottom;
3071 dst_rect.bottom = dst_rect.top;
3072 dst_rect.top = tmp;
3073 upsidedown = TRUE;
3074 }
3075
3076 context = context_acquire(myDevice, SrcSurface, CTXUSAGE_BLIT);
3077 surface_internal_preload((IWineD3DSurface *) This, SRGB_RGB);
3078 ENTER_GL();
3079
3080 /* Bind the target texture */
3081 glBindTexture(This->texture_target, This->texture_name);
3082 checkGLcall("glBindTexture");
3083 if(surface_is_offscreen(SrcSurface)) {
3084 TRACE("Reading from an offscreen target\n");
3085 upsidedown = !upsidedown;
3086 glReadBuffer(myDevice->offscreenBuffer);
3087 }
3088 else
3089 {
3090 glReadBuffer(surface_get_gl_buffer(SrcSurface));
3091 }
3092 checkGLcall("glReadBuffer");
3093
3094 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
3095 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
3096
3097 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
3098 {
3099 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
3100
3101 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
3102 ERR("Texture filtering not supported in direct blit\n");
3103 }
3104 }
3105 else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
3106 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
3107 {
3108 ERR("Texture filtering not supported in direct blit\n");
3109 }
3110
3111 if (upsidedown
3112 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
3113 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
3114 {
3115 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
3116
3117 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3118 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
3119 src_rect->left, Src->currentDesc.Height - src_rect->bottom,
3120 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3121 } else {
3122 UINT yoffset = Src->currentDesc.Height - src_rect->top + dst_rect.top - 1;
3123 /* I have to process this row by row to swap the image,
3124 * otherwise it would be upside down, so stretching in y direction
3125 * doesn't cost extra time
3126 *
3127 * However, stretching in x direction can be avoided if not necessary
3128 */
3129
3130 if (!((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
3131 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
3132 {
3133 /* No stretching involved, so just pass negative height and let host side take care of inverting */
3134
3135 if (doit)
3136 {
3137 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3138 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
3139 src_rect->left, Src->currentDesc.Height - src_rect->bottom,
3140 dst_rect.right - dst_rect.left, -(dst_rect.bottom-dst_rect.top));
3141 }
3142 }
3143 else
3144 {
3145 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
3146 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
3147 {
3148 /* Well, that stuff works, but it's very slow.
3149 * find a better way instead
3150 */
3151 UINT col;
3152
3153 for(col = dst_rect.left; col < dst_rect.right; col++) {
3154 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3155 dst_rect.left + col /* x offset */, row /* y offset */,
3156 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
3157 }
3158 } else {
3159 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3160 dst_rect.left /* x offset */, row /* y offset */,
3161 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
3162 }
3163 }
3164 }
3165 }
3166 checkGLcall("glCopyTexSubImage2D");
3167
3168 LEAVE_GL();
3169 context_release(context);
3170
3171 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3172 * path is never entered
3173 */
3174 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3175}
3176
3177/* Uses the hardware to stretch and flip the image */
3178static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface,
3179 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
3180{
3181 IWineD3DDeviceImpl *myDevice = This->resource.device;
3182 GLuint src, backup = 0;
3183 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3184 IWineD3DSwapChainImpl *src_swapchain = NULL;
3185 float left, right, top, bottom; /* Texture coordinates */
3186 UINT fbwidth = Src->currentDesc.Width;
3187 UINT fbheight = Src->currentDesc.Height;
3188 struct wined3d_context *context;
3189 GLenum drawBuffer = GL_BACK;
3190 GLenum texture_target;
3191 BOOL noBackBufferBackup;
3192 BOOL src_offscreen;
3193 BOOL upsidedown = FALSE;
3194 RECT dst_rect = *dst_rect_in;
3195
3196 TRACE("Using hwstretch blit\n");
3197 /* Activate the Proper context for reading from the source surface, set it up for blitting */
3198 context = context_acquire(myDevice, SrcSurface, CTXUSAGE_BLIT);
3199 surface_internal_preload((IWineD3DSurface *) This, SRGB_RGB);
3200
3201 src_offscreen = surface_is_offscreen(SrcSurface);
3202 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
3203 if (!noBackBufferBackup && !Src->texture_name)
3204 {
3205 /* Get it a description */
3206 surface_internal_preload(SrcSurface, SRGB_RGB);
3207 }
3208 ENTER_GL();
3209
3210 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
3211 * This way we don't have to wait for the 2nd readback to finish to leave this function.
3212 */
3213 if (context->aux_buffers >= 2)
3214 {
3215 /* Got more than one aux buffer? Use the 2nd aux buffer */
3216 drawBuffer = GL_AUX1;
3217 }
3218 else if ((!src_offscreen || myDevice->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
3219 {
3220 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
3221 drawBuffer = GL_AUX0;
3222 }
3223
3224 if(noBackBufferBackup) {
3225 glGenTextures(1, &backup);
3226 checkGLcall("glGenTextures");
3227 glBindTexture(GL_TEXTURE_2D, backup);
3228 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3229 texture_target = GL_TEXTURE_2D;
3230 } else {
3231 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
3232 * we are reading from the back buffer, the backup can be used as source texture
3233 */
3234 texture_target = Src->texture_target;
3235 glBindTexture(texture_target, Src->texture_name);
3236 checkGLcall("glBindTexture(texture_target, Src->texture_name)");
3237 glEnable(texture_target);
3238 checkGLcall("glEnable(texture_target)");
3239
3240 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3241 Src->Flags &= ~SFLAG_INTEXTURE;
3242 }
3243
3244 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3245 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3246 */
3247 if(dst_rect.top > dst_rect.bottom) {
3248 UINT tmp = dst_rect.bottom;
3249 dst_rect.bottom = dst_rect.top;
3250 dst_rect.top = tmp;
3251 upsidedown = TRUE;
3252 }
3253
3254 if (src_offscreen)
3255 {
3256 TRACE("Reading from an offscreen target\n");
3257 upsidedown = !upsidedown;
3258 glReadBuffer(myDevice->offscreenBuffer);
3259 }
3260 else
3261 {
3262 glReadBuffer(surface_get_gl_buffer(SrcSurface));
3263 }
3264
3265 /* TODO: Only back up the part that will be overwritten */
3266 glCopyTexSubImage2D(texture_target, 0,
3267 0, 0 /* read offsets */,
3268 0, 0,
3269 fbwidth,
3270 fbheight);
3271
3272 checkGLcall("glCopyTexSubImage2D");
3273
3274 /* No issue with overriding these - the sampler is dirty due to blit usage */
3275 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3276 wined3d_gl_mag_filter(magLookup, Filter));
3277 checkGLcall("glTexParameteri");
3278 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3279 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3280 checkGLcall("glTexParameteri");
3281
3282 IWineD3DSurface_GetContainer((IWineD3DSurface *)SrcSurface, &IID_IWineD3DSwapChain, (void **)&src_swapchain);
3283 if (src_swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *)src_swapchain);
3284 if (!src_swapchain || (IWineD3DSurface *) Src == src_swapchain->backBuffer[0]) {
3285 src = backup ? backup : Src->texture_name;
3286 } else {
3287 glReadBuffer(GL_FRONT);
3288 checkGLcall("glReadBuffer(GL_FRONT)");
3289
3290 glGenTextures(1, &src);
3291 checkGLcall("glGenTextures(1, &src)");
3292 glBindTexture(GL_TEXTURE_2D, src);
3293 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3294
3295 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3296 * out for power of 2 sizes
3297 */
3298 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
3299 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3300 checkGLcall("glTexImage2D");
3301 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3302 0, 0 /* read offsets */,
3303 0, 0,
3304 fbwidth,
3305 fbheight);
3306
3307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3308 checkGLcall("glTexParameteri");
3309 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3310 checkGLcall("glTexParameteri");
3311
3312 glReadBuffer(GL_BACK);
3313 checkGLcall("glReadBuffer(GL_BACK)");
3314
3315 if(texture_target != GL_TEXTURE_2D) {
3316 glDisable(texture_target);
3317 glEnable(GL_TEXTURE_2D);
3318 texture_target = GL_TEXTURE_2D;
3319 }
3320 }
3321 checkGLcall("glEnd and previous");
3322
3323 left = src_rect->left;
3324 right = src_rect->right;
3325
3326 if(upsidedown) {
3327 top = Src->currentDesc.Height - src_rect->top;
3328 bottom = Src->currentDesc.Height - src_rect->bottom;
3329 } else {
3330 top = Src->currentDesc.Height - src_rect->bottom;
3331 bottom = Src->currentDesc.Height - src_rect->top;
3332 }
3333
3334 if(Src->Flags & SFLAG_NORMCOORD) {
3335 left /= Src->pow2Width;
3336 right /= Src->pow2Width;
3337 top /= Src->pow2Height;
3338 bottom /= Src->pow2Height;
3339 }
3340
3341 /* draw the source texture stretched and upside down. The correct surface is bound already */
3342 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3343 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3344
3345 context_set_draw_buffer(context, drawBuffer);
3346 glReadBuffer(drawBuffer);
3347
3348 glBegin(GL_QUADS);
3349 /* bottom left */
3350 glTexCoord2f(left, bottom);
3351 glVertex2i(0, fbheight);
3352
3353 /* top left */
3354 glTexCoord2f(left, top);
3355 glVertex2i(0, fbheight - dst_rect.bottom - dst_rect.top);
3356
3357 /* top right */
3358 glTexCoord2f(right, top);
3359 glVertex2i(dst_rect.right - dst_rect.left, fbheight - dst_rect.bottom - dst_rect.top);
3360
3361 /* bottom right */
3362 glTexCoord2f(right, bottom);
3363 glVertex2i(dst_rect.right - dst_rect.left, fbheight);
3364 glEnd();
3365 checkGLcall("glEnd and previous");
3366
3367 if (texture_target != This->texture_target)
3368 {
3369 glDisable(texture_target);
3370 glEnable(This->texture_target);
3371 texture_target = This->texture_target;
3372 }
3373
3374 /* Now read the stretched and upside down image into the destination texture */
3375 glBindTexture(texture_target, This->texture_name);
3376 checkGLcall("glBindTexture");
3377 glCopyTexSubImage2D(texture_target,
3378 0,
3379 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3380 0, 0, /* We blitted the image to the origin */
3381 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3382 checkGLcall("glCopyTexSubImage2D");
3383
3384 if(drawBuffer == GL_BACK) {
3385 /* Write the back buffer backup back */
3386 if(backup) {
3387 if(texture_target != GL_TEXTURE_2D) {
3388 glDisable(texture_target);
3389 glEnable(GL_TEXTURE_2D);
3390 texture_target = GL_TEXTURE_2D;
3391 }
3392 glBindTexture(GL_TEXTURE_2D, backup);
3393 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3394 } else {
3395 if (texture_target != Src->texture_target)
3396 {
3397 glDisable(texture_target);
3398 glEnable(Src->texture_target);
3399 texture_target = Src->texture_target;
3400 }
3401 glBindTexture(Src->texture_target, Src->texture_name);
3402 checkGLcall("glBindTexture(Src->texture_target, Src->texture_name)");
3403 }
3404
3405 glBegin(GL_QUADS);
3406 /* top left */
3407 glTexCoord2f(0.0f, (float)fbheight / (float)Src->pow2Height);
3408 glVertex2i(0, 0);
3409
3410 /* bottom left */
3411 glTexCoord2f(0.0f, 0.0f);
3412 glVertex2i(0, fbheight);
3413
3414 /* bottom right */
3415 glTexCoord2f((float)fbwidth / (float)Src->pow2Width, 0.0f);
3416 glVertex2i(fbwidth, Src->currentDesc.Height);
3417
3418 /* top right */
3419 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
3420 glVertex2i(fbwidth, 0);
3421 glEnd();
3422 }
3423 glDisable(texture_target);
3424 checkGLcall("glDisable(texture_target)");
3425
3426 /* Cleanup */
3427 if (src != Src->texture_name && src != backup)
3428 {
3429 glDeleteTextures(1, &src);
3430 checkGLcall("glDeleteTextures(1, &src)");
3431 }
3432 if(backup) {
3433 glDeleteTextures(1, &backup);
3434 checkGLcall("glDeleteTextures(1, &backup)");
3435 }
3436
3437 LEAVE_GL();
3438
3439 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3440
3441 context_release(context);
3442
3443 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3444 * path is never entered
3445 */
3446 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3447}
3448
3449/* Until the blit_shader is ready, define some prototypes here. */
3450static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
3451 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
3452 const struct wined3d_format_desc *src_format_desc,
3453 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
3454 const struct wined3d_format_desc *dst_format_desc);
3455
3456/* Not called from the VTable */
3457static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3458 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx,
3459 WINED3DTEXTUREFILTERTYPE Filter)
3460{
3461 IWineD3DDeviceImpl *myDevice = This->resource.device;
3462 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3463 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3464 RECT dst_rect, src_rect;
3465
3466 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3467
3468 /* Get the swapchain. One of the surfaces has to be a primary surface */
3469 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3470 WARN("Destination is in sysmem, rejecting gl blt\n");
3471 return WINED3DERR_INVALIDCALL;
3472 }
3473 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
3474 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
3475 if(Src) {
3476 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3477 WARN("Src is in sysmem, rejecting gl blt\n");
3478 return WINED3DERR_INVALIDCALL;
3479 }
3480 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3481 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
3482 }
3483
3484 /* Early sort out of cases where no render target is used */
3485 if(!dstSwapchain && !srcSwapchain &&
3486 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3487 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
3488 return WINED3DERR_INVALIDCALL;
3489 }
3490
3491 /* No destination color keying supported */
3492 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3493 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3494 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3495 return WINED3DERR_INVALIDCALL;
3496 }
3497
3498 surface_get_rect(This, DestRect, &dst_rect);
3499 if(Src) surface_get_rect(Src, SrcRect, &src_rect);
3500
3501 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3502 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3503 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3504 /* Half-life does a Blt from the back buffer to the front buffer,
3505 * Full surface size, no flags... Use present instead
3506 *
3507 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3508 */
3509
3510 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3511 while(1)
3512 {
3513 TRACE("Looking if a Present can be done...\n");
3514 /* Source Rectangle must be full surface */
3515 if(src_rect.left != 0 || src_rect.top != 0 ||
3516 src_rect.right != Src->currentDesc.Width || src_rect.bottom != Src->currentDesc.Height) {
3517 TRACE("No, Source rectangle doesn't match\n");
3518 break;
3519 }
3520
3521 /* No stretching may occur */
3522 if(src_rect.right != dst_rect.right - dst_rect.left ||
3523 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3524 TRACE("No, stretching is done\n");
3525 break;
3526 }
3527
3528 /* Destination must be full surface or match the clipping rectangle */
3529 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3530 {
3531 RECT cliprect;
3532 POINT pos[2];
3533 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3534 pos[0].x = dst_rect.left;
3535 pos[0].y = dst_rect.top;
3536 pos[1].x = dst_rect.right;
3537 pos[1].y = dst_rect.bottom;
3538 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3539 pos, 2);
3540
3541 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3542 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3543 {
3544 TRACE("No, dest rectangle doesn't match(clipper)\n");
3545 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3546 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3547 break;
3548 }
3549 }
3550 else
3551 {
3552 if(dst_rect.left != 0 || dst_rect.top != 0 ||
3553 dst_rect.right != This->currentDesc.Width || dst_rect.bottom != This->currentDesc.Height) {
3554 TRACE("No, dest rectangle doesn't match(surface size)\n");
3555 break;
3556 }
3557 }
3558
3559 TRACE("Yes\n");
3560
3561 /* These flags are unimportant for the flag check, remove them */
3562 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3563 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3564
3565 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3566 * take very long, while a flip is fast.
3567 * This applies to Half-Life, which does such Blts every time it finished
3568 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3569 * menu. This is also used by all apps when they do windowed rendering
3570 *
3571 * The problem is that flipping is not really the same as copying. After a
3572 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3573 * untouched. Therefore it's necessary to override the swap effect
3574 * and to set it back after the flip.
3575 *
3576 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3577 * testcases.
3578 */
3579
3580 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3581 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3582
3583 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3584 IWineD3DSwapChain_Present((IWineD3DSwapChain *)dstSwapchain,
3585 NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3586
3587 dstSwapchain->presentParms.SwapEffect = orig_swap;
3588
3589 return WINED3D_OK;
3590 }
3591 break;
3592 }
3593
3594 TRACE("Unsupported blit between buffers on the same swapchain\n");
3595 return WINED3DERR_INVALIDCALL;
3596 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3597 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3598 return WINED3DERR_INVALIDCALL;
3599 } else if(dstSwapchain && srcSwapchain) {
3600 FIXME("Implement hardware blit between two different swapchains\n");
3601 return WINED3DERR_INVALIDCALL;
3602 } else if(dstSwapchain) {
3603 if(SrcSurface == myDevice->render_targets[0]) {
3604 TRACE("Blit from active render target to a swapchain\n");
3605 /* Handled with regular texture -> swapchain blit */
3606 }
3607 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3608 FIXME("Implement blit from a swapchain to the active render target\n");
3609 return WINED3DERR_INVALIDCALL;
3610 }
3611
3612 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3613 /* Blit from render target to texture */
3614 BOOL stretchx;
3615
3616 /* P8 read back is not implemented */
3617 if (Src->resource.format_desc->format == WINED3DFMT_P8_UINT ||
3618 This->resource.format_desc->format == WINED3DFMT_P8_UINT)
3619 {
3620 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3621 return WINED3DERR_INVALIDCALL;
3622 }
3623
3624 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3625 TRACE("Color keying not supported by frame buffer to texture blit\n");
3626 return WINED3DERR_INVALIDCALL;
3627 /* Destination color key is checked above */
3628 }
3629
3630 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3631 stretchx = TRUE;
3632 } else {
3633 stretchx = FALSE;
3634 }
3635
3636 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3637 * flip the image nor scale it.
3638 *
3639 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3640 * -> If the app wants a image width an unscaled width, copy it line per line
3641 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3642 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3643 * back buffer. This is slower than reading line per line, thus not used for flipping
3644 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3645 * pixel by pixel
3646 *
3647 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3648 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3649 * backends.
3650 */
3651 if (fbo_blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3652 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3653 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc)
3654 && (!(myDevice->adapter->gl_info.quirks & WINED3D_QUIRK_FULLSIZE_BLIT)
3655 || (dst_rect.right==This->currentDesc.Width && dst_rect.bottom==This->currentDesc.Height
3656 && dst_rect.left==0 && dst_rect.top==0)
3657 )
3658 )
3659 {
3660 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &src_rect,
3661 (IWineD3DSurface *)This, &dst_rect, Filter);
3662 } else if((!stretchx) || dst_rect.right - dst_rect.left > Src->currentDesc.Width ||
3663 dst_rect.bottom - dst_rect.top > Src->currentDesc.Height) {
3664 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3665 fb_copy_to_texture_direct(This, SrcSurface, &src_rect, &dst_rect, Filter, TRUE);
3666 } else {
3667 TRACE("Using hardware stretching to flip / stretch the texture\n");
3668 fb_copy_to_texture_hwstretch(This, SrcSurface, &src_rect, &dst_rect, Filter);
3669 }
3670
3671 if(!(This->Flags & SFLAG_DONOTFREE)) {
3672 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3673 This->resource.allocatedMemory = NULL;
3674 This->resource.heapMemory = NULL;
3675 } else {
3676 This->Flags &= ~SFLAG_INSYSMEM;
3677 }
3678
3679 return WINED3D_OK;
3680 } else if(Src) {
3681 /* Blit from offscreen surface to render target */
3682 DWORD oldCKeyFlags = Src->CKeyFlags;
3683 WINEDDCOLORKEY oldBltCKey = Src->SrcBltCKey;
3684 struct wined3d_context *context;
3685
3686 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3687
3688 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3689 && fbo_blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3690 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3691 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc)
3692 && (!(myDevice->adapter->gl_info.quirks & WINED3D_QUIRK_FULLSIZE_BLIT)
3693 || (dst_rect.right==This->currentDesc.Width && dst_rect.bottom==This->currentDesc.Height
3694 && dst_rect.left==0 && dst_rect.top==0)
3695 )
3696 )
3697 {
3698 TRACE("Using stretch_rect_fbo\n");
3699 /* The source is always a texture, but never the currently active render target, and the texture
3700 * contents are never upside down
3701 */
3702 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &src_rect,
3703 (IWineD3DSurface *)This, &dst_rect, Filter);
3704 return WINED3D_OK;
3705 }
3706
3707 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3708 && arbfp_blit.blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3709 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3710 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3711 {
3712 return arbfp_blit_surface(myDevice, Src, &src_rect, This, &dst_rect, BLIT_OP_BLIT, Filter);
3713 }
3714
3715 /* Color keying: Check if we have to do a color keyed blt,
3716 * and if not check if a color key is activated.
3717 *
3718 * Just modify the color keying parameters in the surface and restore them afterwards
3719 * The surface keeps track of the color key last used to load the opengl surface.
3720 * PreLoad will catch the change to the flags and color key and reload if necessary.
3721 */
3722 if(Flags & WINEDDBLT_KEYSRC) {
3723 /* Use color key from surface */
3724 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3725 /* Use color key from DDBltFx */
3726 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3727 Src->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3728 } else {
3729 /* Do not use color key */
3730 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3731 }
3732
3733 /* Now load the surface */
3734 surface_internal_preload((IWineD3DSurface *) Src, SRGB_RGB);
3735
3736 /* Activate the destination context, set it up for blitting */
3737 context = context_acquire(myDevice, (IWineD3DSurface *)This, CTXUSAGE_BLIT);
3738
3739 /* The coordinates of the ddraw front buffer are always fullscreen ('screen coordinates',
3740 * while OpenGL coordinates are window relative.
3741 * Also beware of the origin difference(top left vs bottom left).
3742 * Also beware that the front buffer's surface size is screen width x screen height,
3743 * whereas the real gl drawable size is the size of the window.
3744 */
3745 if (dstSwapchain && (IWineD3DSurface *)This == dstSwapchain->frontBuffer) {
3746 RECT windowsize;
3747 POINT offset = {0,0};
3748 UINT h;
3749 ClientToScreen(context->win_handle, &offset);
3750 GetClientRect(context->win_handle, &windowsize);
3751 h = windowsize.bottom - windowsize.top;
3752 dst_rect.left -= offset.x; dst_rect.right -=offset.x;
3753 dst_rect.top -= offset.y; dst_rect.bottom -=offset.y;
3754 dst_rect.top += This->currentDesc.Height - h; dst_rect.bottom += This->currentDesc.Height - h;
3755 }
3756
3757 if (!myDevice->blitter->blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3758 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3759 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3760 {
3761 FIXME("Unsupported blit operation falling back to software\n");
3762 return WINED3DERR_INVALIDCALL;
3763 }
3764
3765 myDevice->blitter->set_shader((IWineD3DDevice *) myDevice, Src);
3766
3767 ENTER_GL();
3768
3769 /* This is for color keying */
3770 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3771 glEnable(GL_ALPHA_TEST);
3772 checkGLcall("glEnable(GL_ALPHA_TEST)");
3773
3774 /* When the primary render target uses P8, the alpha component contains the palette index.
3775 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3776 * should be masked away have alpha set to 0. */
3777 if(primary_render_target_is_p8(myDevice))
3778 glAlphaFunc(GL_NOTEQUAL, (float)Src->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3779 else
3780 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3781 checkGLcall("glAlphaFunc");
3782 } else {
3783 glDisable(GL_ALPHA_TEST);
3784 checkGLcall("glDisable(GL_ALPHA_TEST)");
3785 }
3786
3787 /* Draw a textured quad
3788 */
3789 draw_textured_quad(Src, &src_rect, &dst_rect, Filter);
3790
3791 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3792 glDisable(GL_ALPHA_TEST);
3793 checkGLcall("glDisable(GL_ALPHA_TEST)");
3794 }
3795
3796 /* Restore the color key parameters */
3797 Src->CKeyFlags = oldCKeyFlags;
3798 Src->SrcBltCKey = oldBltCKey;
3799
3800 LEAVE_GL();
3801
3802 /* Leave the opengl state valid for blitting */
3803 myDevice->blitter->unset_shader((IWineD3DDevice *) myDevice);
3804
3805 if (wined3d_settings.strict_draw_ordering || (dstSwapchain
3806 && ((IWineD3DSurface *)This == dstSwapchain->frontBuffer
3807#ifdef VBOX_WITH_WDDM
3808 || dstSwapchain->device->numContexts > 1
3809#else
3810 || dstSwapchain->num_contexts > 1
3811#endif
3812 )))
3813 wglFlush(); /* Flush to ensure ordering across contexts. */
3814
3815 context_release(context);
3816
3817 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3818 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3819 * is outdated now
3820 */
3821 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3822
3823 return WINED3D_OK;
3824 } else {
3825 /* Source-Less Blit to render target */
3826 if (Flags & WINEDDBLT_COLORFILL) {
3827 DWORD color;
3828
3829 TRACE("Colorfill\n");
3830
3831 /* The color as given in the Blt function is in the format of the frame-buffer...
3832 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3833 */
3834 if (!surface_convert_color_to_argb(This, DDBltFx->u5.dwFillColor, &color))
3835 {
3836 /* The color conversion function already prints an error, so need to do it here */
3837 return WINED3DERR_INVALIDCALL;
3838 }
3839
3840 if (ffp_blit.blit_supported(&myDevice->adapter->gl_info, BLIT_OP_COLOR_FILL,
3841 NULL, 0, 0, NULL,
3842 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3843 {
3844 return ffp_blit.color_fill(myDevice, This, &dst_rect, color);
3845 }
3846 else if (cpu_blit.blit_supported(&myDevice->adapter->gl_info, BLIT_OP_COLOR_FILL,
3847 NULL, 0, 0, NULL,
3848 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3849 {
3850 return cpu_blit.color_fill(myDevice, This, &dst_rect, color);
3851 }
3852 return WINED3DERR_INVALIDCALL;
3853 }
3854 }
3855
3856 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3857 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3858 return WINED3DERR_INVALIDCALL;
3859}
3860
3861static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3862 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx)
3863{
3864 IWineD3DDeviceImpl *myDevice = This->resource.device;
3865 float depth;
3866
3867 if (Flags & WINEDDBLT_DEPTHFILL) {
3868 switch(This->resource.format_desc->format)
3869 {
3870 case WINED3DFMT_D16_UNORM:
3871 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3872 break;
3873 case WINED3DFMT_S1_UINT_D15_UNORM:
3874 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3875 break;
3876 case WINED3DFMT_D24_UNORM_S8_UINT:
3877 case WINED3DFMT_X8D24_UNORM:
3878 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3879 break;
3880 case WINED3DFMT_D32_UNORM:
3881 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3882 break;
3883 default:
3884 depth = 0.0f;
3885 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format_desc->format));
3886 }
3887
3888 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3889 DestRect == NULL ? 0 : 1,
3890 (const WINED3DRECT *)DestRect,
3891 WINED3DCLEAR_ZBUFFER,
3892 0x00000000,
3893 depth,
3894 0x00000000);
3895 }
3896
3897 FIXME("(%p): Unsupp depthstencil blit\n", This);
3898 return WINED3DERR_INVALIDCALL;
3899}
3900
3901static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect, IWineD3DSurface *SrcSurface,
3902 const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3903 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3904 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3905 IWineD3DDeviceImpl *myDevice = This->resource.device;
3906
3907 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3908 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3909
3910 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3911 {
3912 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3913 return WINEDDERR_SURFACEBUSY;
3914 }
3915
3916 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3917 * except depth blits, which seem to work
3918 */
3919 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3920 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3921 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3922 return WINED3DERR_INVALIDCALL;
3923 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3924 TRACE("Z Blit override handled the blit\n");
3925 return WINED3D_OK;
3926 }
3927 }
3928
3929 /* Special cases for RenderTargets */
3930 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3931 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3932 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3933 }
3934
3935 /* For the rest call the X11 surface implementation.
3936 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3937 * other Blts are rather rare
3938 */
3939 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3940}
3941
3942static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3943 IWineD3DSurface *Source, const RECT *rsrc, DWORD trans)
3944{
3945 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3946 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3947 IWineD3DDeviceImpl *myDevice = This->resource.device;
3948
3949 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3950
3951 if ( (This->Flags & SFLAG_LOCKED) || (srcImpl->Flags & SFLAG_LOCKED))
3952 {
3953 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3954 return WINEDDERR_SURFACEBUSY;
3955 }
3956
3957 if(myDevice->inScene &&
3958 (iface == myDevice->stencilBufferTarget ||
3959 (Source == myDevice->stencilBufferTarget))) {
3960 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3961 return WINED3DERR_INVALIDCALL;
3962 }
3963
3964 /* Special cases for RenderTargets */
3965 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3966 (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) ) {
3967
3968 RECT SrcRect, DstRect;
3969 DWORD Flags=0;
3970
3971 surface_get_rect(srcImpl, rsrc, &SrcRect);
3972
3973 DstRect.left = dstx;
3974 DstRect.top=dsty;
3975 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3976 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3977
3978 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3979 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3980 Flags |= WINEDDBLT_KEYSRC;
3981 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3982 Flags |= WINEDDBLT_KEYDEST;
3983 if(trans & WINEDDBLTFAST_WAIT)
3984 Flags |= WINEDDBLT_WAIT;
3985 if(trans & WINEDDBLTFAST_DONOTWAIT)
3986 Flags |= WINEDDBLT_DONOTWAIT;
3987
3988 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3989 }
3990
3991
3992 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3993}
3994
3995static HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface)
3996{
3997 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3998 RGBQUAD col[256];
3999 IWineD3DPaletteImpl *pal = This->palette;
4000 unsigned int n;
4001 TRACE("(%p)\n", This);
4002
4003 if (!pal) return WINED3D_OK;
4004
4005 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
4006 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
4007 {
4008 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4009 {
4010 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
4011 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
4012
4013 /* We want to force a palette refresh, so mark the drawable as not being up to date */
4014 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
4015 } else {
4016 if(!(This->Flags & SFLAG_INSYSMEM)) {
4017 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
4018 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
4019 }
4020 TRACE("Dirtifying surface\n");
4021 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
4022 }
4023 }
4024
4025 if(This->Flags & SFLAG_DIBSECTION) {
4026 TRACE("(%p): Updating the hdc's palette\n", This);
4027 for (n=0; n<256; n++) {
4028 col[n].rgbRed = pal->palents[n].peRed;
4029 col[n].rgbGreen = pal->palents[n].peGreen;
4030 col[n].rgbBlue = pal->palents[n].peBlue;
4031 col[n].rgbReserved = 0;
4032 }
4033 SetDIBColorTable(This->hDC, 0, 256, col);
4034 }
4035
4036 /* Propagate the changes to the drawable when we have a palette. */
4037 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4038 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
4039
4040 return WINED3D_OK;
4041}
4042
4043#ifdef VBOX_WITH_WDDM
4044static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect);
4045#endif
4046
4047static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
4048 /** Check against the maximum texture sizes supported by the video card **/
4049 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4050 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
4051 unsigned int pow2Width, pow2Height;
4052
4053 This->texture_name = 0;
4054 This->texture_target = GL_TEXTURE_2D;
4055
4056 /* Non-power2 support */
4057 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINE_NORMALIZED_TEXRECT])
4058 {
4059 pow2Width = This->currentDesc.Width;
4060 pow2Height = This->currentDesc.Height;
4061 }
4062 else
4063 {
4064 /* Find the nearest pow2 match */
4065 pow2Width = pow2Height = 1;
4066 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
4067 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
4068 }
4069 This->pow2Width = pow2Width;
4070 This->pow2Height = pow2Height;
4071
4072 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
4073 /** TODO: add support for non power two compressed textures **/
4074 if (This->resource.format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
4075 {
4076 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
4077 This, This->currentDesc.Width, This->currentDesc.Height);
4078 return WINED3DERR_NOTAVAILABLE;
4079 }
4080 }
4081
4082 if(pow2Width != This->currentDesc.Width ||
4083 pow2Height != This->currentDesc.Height) {
4084 This->Flags |= SFLAG_NONPOW2;
4085 }
4086
4087 TRACE("%p\n", This);
4088 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
4089 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
4090 {
4091 /* one of three options
4092 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)
4093 2: Set the texture to the maximum size (bad idea)
4094 3: WARN and return WINED3DERR_NOTAVAILABLE;
4095 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.
4096 */
4097 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4098 {
4099 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4100 return WINED3DERR_NOTAVAILABLE;
4101 }
4102
4103 /* We should never use this surface in combination with OpenGL! */
4104 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4105 }
4106 else
4107 {
4108 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4109 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4110 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4111 */
4112 if (This->Flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4113 && !(This->resource.format_desc->format == WINED3DFMT_P8_UINT
4114 && gl_info->supported[EXT_PALETTED_TEXTURE]
4115 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4116 {
4117 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4118 This->pow2Width = This->currentDesc.Width;
4119 This->pow2Height = This->currentDesc.Height;
4120 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4121 }
4122 }
4123
4124 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4125 switch(wined3d_settings.offscreen_rendering_mode) {
4126 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4127 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4128 }
4129 }
4130
4131 This->Flags |= SFLAG_INSYSMEM;
4132
4133 return WINED3D_OK;
4134}
4135
4136/* GL locking is done by the caller */
4137static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4138 GLuint texture, GLsizei w, GLsizei h, GLenum target)
4139{
4140 IWineD3DDeviceImpl *device = This->resource.device;
4141 struct blt_info info;
4142 GLint old_binding = 0;
4143
4144 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4145
4146 glDisable(GL_CULL_FACE);
4147 glDisable(GL_BLEND);
4148 glDisable(GL_ALPHA_TEST);
4149 glDisable(GL_SCISSOR_TEST);
4150 glDisable(GL_STENCIL_TEST);
4151 glEnable(GL_DEPTH_TEST);
4152 glDepthFunc(GL_ALWAYS);
4153 glDepthMask(GL_TRUE);
4154 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4155 glViewport(0, 0, w, h);
4156
4157 surface_get_blt_info(target, NULL, w, h, &info);
4158 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4159 glGetIntegerv(info.binding, &old_binding);
4160 glBindTexture(info.bind_target, texture);
4161
4162 device->shader_backend->shader_select_depth_blt((IWineD3DDevice *)device, info.tex_type);
4163
4164 glBegin(GL_TRIANGLE_STRIP);
4165 glTexCoord3fv(info.coords[0]);
4166 glVertex2f(-1.0f, -1.0f);
4167 glTexCoord3fv(info.coords[1]);
4168 glVertex2f(1.0f, -1.0f);
4169 glTexCoord3fv(info.coords[2]);
4170 glVertex2f(-1.0f, 1.0f);
4171 glTexCoord3fv(info.coords[3]);
4172 glVertex2f(1.0f, 1.0f);
4173 glEnd();
4174
4175 glBindTexture(info.bind_target, old_binding);
4176
4177 glPopAttrib();
4178
4179 device->shader_backend->shader_deselect_depth_blt((IWineD3DDevice *)device);
4180}
4181
4182void surface_modify_ds_location(IWineD3DSurface *iface, DWORD location) {
4183 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4184
4185 TRACE("(%p) New location %#x\n", This, location);
4186
4187 if (location & ~SFLAG_DS_LOCATIONS) {
4188 FIXME("(%p) Invalid location (%#x) specified\n", This, location);
4189 }
4190
4191 This->Flags &= ~SFLAG_DS_LOCATIONS;
4192 This->Flags |= location;
4193}
4194
4195/* Context activation is done by the caller. */
4196void surface_load_ds_location(IWineD3DSurface *iface, struct wined3d_context *context, DWORD location)
4197{
4198 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4199 IWineD3DDeviceImpl *device = This->resource.device;
4200 const struct wined3d_gl_info *gl_info = context->gl_info;
4201
4202 TRACE("(%p) New location %#x\n", This, location);
4203
4204 /* TODO: Make this work for modes other than FBO */
4205 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4206
4207 if (This->Flags & location) {
4208 TRACE("(%p) Location (%#x) is already up to date\n", This, location);
4209 return;
4210 }
4211
4212 if (This->current_renderbuffer) {
4213 FIXME("(%p) Not supported with fixed up depth stencil\n", This);
4214 return;
4215 }
4216
4217 if (location == SFLAG_DS_OFFSCREEN) {
4218 if (This->Flags & SFLAG_DS_ONSCREEN) {
4219 GLint old_binding = 0;
4220 GLenum bind_target;
4221
4222 TRACE("(%p) Copying onscreen depth buffer to depth texture\n", This);
4223
4224 ENTER_GL();
4225
4226 if (!device->depth_blt_texture) {
4227 glGenTextures(1, &device->depth_blt_texture);
4228 }
4229
4230 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4231 * directly on the FBO texture. That's because we need to flip. */
4232 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4233 if (This->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4234 {
4235 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4236 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4237 } else {
4238 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4239 bind_target = GL_TEXTURE_2D;
4240 }
4241 glBindTexture(bind_target, device->depth_blt_texture);
4242 glCopyTexImage2D(bind_target, This->texture_level, This->resource.format_desc->glInternal,
4243 0, 0, This->currentDesc.Width, This->currentDesc.Height, 0);
4244 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4245 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4246 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4247 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4248 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4249 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4250 glBindTexture(bind_target, old_binding);
4251
4252 /* Setup the destination */
4253 if (!device->depth_blt_rb) {
4254 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4255 checkGLcall("glGenRenderbuffersEXT");
4256 }
4257 if (device->depth_blt_rb_w != This->currentDesc.Width
4258 || device->depth_blt_rb_h != This->currentDesc.Height) {
4259 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4260 checkGLcall("glBindRenderbufferEXT");
4261 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
4262 This->currentDesc.Width, This->currentDesc.Height);
4263 checkGLcall("glRenderbufferStorageEXT");
4264 device->depth_blt_rb_w = This->currentDesc.Width;
4265 device->depth_blt_rb_h = This->currentDesc.Height;
4266 }
4267
4268 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4269 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4270 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4271 checkGLcall("glFramebufferRenderbufferEXT");
4272 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, This, FALSE);
4273
4274 /* Do the actual blit */
4275 surface_depth_blt(This, gl_info, device->depth_blt_texture,
4276 This->currentDesc.Width, This->currentDesc.Height, bind_target);
4277 checkGLcall("depth_blt");
4278
4279 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4280 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4281
4282 LEAVE_GL();
4283
4284 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4285 }
4286 else
4287 {
4288 FIXME("No up to date depth stencil location\n");
4289 }
4290 } else if (location == SFLAG_DS_ONSCREEN) {
4291 if (This->Flags & SFLAG_DS_OFFSCREEN) {
4292 TRACE("(%p) Copying depth texture to onscreen depth buffer\n", This);
4293
4294 ENTER_GL();
4295
4296 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4297 surface_depth_blt(This, gl_info, This->texture_name,
4298 This->currentDesc.Width, This->currentDesc.Height, This->texture_target);
4299 checkGLcall("depth_blt");
4300
4301 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4302
4303 LEAVE_GL();
4304
4305 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4306 }
4307 else
4308 {
4309 FIXME("No up to date depth stencil location\n");
4310 }
4311 } else {
4312 ERR("(%p) Invalid location (%#x) specified\n", This, location);
4313 }
4314
4315 This->Flags |= location;
4316}
4317
4318static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
4319 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4320 IWineD3DBaseTexture *texture;
4321 IWineD3DSurfaceImpl *overlay;
4322
4323 TRACE("(%p)->(%s, %s)\n", iface, debug_surflocation(flag),
4324 persistent ? "TRUE" : "FALSE");
4325
4326 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4327 if (surface_is_offscreen(iface))
4328 {
4329 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4330 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4331 }
4332 else
4333 {
4334 TRACE("Surface %p is an onscreen surface\n", iface);
4335 }
4336 }
4337
4338 if(persistent) {
4339 if(((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) ||
4340 ((This->Flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX))) {
4341 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4342 TRACE("Passing to container\n");
4343 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4344 IWineD3DBaseTexture_Release(texture);
4345 }
4346 }
4347
4348 This->Flags &= ~SFLAG_LOCATIONS;
4349 This->Flags |= flag;
4350
4351 /* Redraw emulated overlays, if any */
4352 if(flag & SFLAG_INDRAWABLE && !list_empty(&This->overlays)) {
4353 LIST_FOR_EACH_ENTRY(overlay, &This->overlays, IWineD3DSurfaceImpl, overlay_entry) {
4354 IWineD3DSurface_DrawOverlay((IWineD3DSurface *) overlay);
4355 }
4356 }
4357 } else {
4358 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))) {
4359 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4360 TRACE("Passing to container\n");
4361 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4362 IWineD3DBaseTexture_Release(texture);
4363 }
4364 }
4365
4366 This->Flags &= ~flag;
4367 }
4368
4369#ifdef VBOX_WITH_WDDM
4370 if(VBOXSHRC_IS_INITIALIZED(This)) {
4371 /* with the shared resource only texture can be considered valid
4372 * to make sure changes done to the resource in the other device context are visible
4373 * because the resource contents is shared via texture.
4374 * This is why we ensure texture location is the one and only which is always valid */
4375 if(!(This->Flags & SFLAG_INTEXTURE)) {
4376 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
4377 } else {
4378 This->Flags &= ~SFLAG_LOCATIONS;
4379 This->Flags |= SFLAG_INTEXTURE;
4380 }
4381 }
4382#endif
4383
4384 if(!(This->Flags & SFLAG_LOCATIONS)) {
4385 ERR("%p: Surface does not have any up to date location\n", This);
4386 }
4387}
4388
4389static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in)
4390{
4391 IWineD3DDeviceImpl *device = This->resource.device;
4392 IWineD3DSwapChainImpl *swapchain;
4393 struct wined3d_context *context;
4394 RECT src_rect, dst_rect;
4395
4396 surface_get_rect(This, rect_in, &src_rect);
4397
4398 context = context_acquire(device, (IWineD3DSurface*)This, CTXUSAGE_BLIT);
4399 if (context->render_offscreen)
4400 {
4401 dst_rect.left = src_rect.left;
4402 dst_rect.right = src_rect.right;
4403 dst_rect.top = src_rect.bottom;
4404 dst_rect.bottom = src_rect.top;
4405 }
4406 else
4407 {
4408 dst_rect = src_rect;
4409 }
4410
4411 device->blitter->set_shader((IWineD3DDevice *) device, This);
4412
4413 ENTER_GL();
4414 draw_textured_quad(This, &src_rect, &dst_rect, WINED3DTEXF_POINT);
4415 LEAVE_GL();
4416
4417 device->blitter->set_shader((IWineD3DDevice *) device, This);
4418
4419 swapchain = (This->Flags & SFLAG_SWAPCHAIN) ? (IWineD3DSwapChainImpl *)This->container : NULL;
4420 if (wined3d_settings.strict_draw_ordering || (swapchain
4421 && ((IWineD3DSurface *)This == swapchain->frontBuffer
4422#ifdef VBOX_WITH_WDDM
4423 || swapchain->device->numContexts > 1
4424#else
4425 || swapchain->num_contexts > 1
4426#endif
4427 )))
4428 wglFlush(); /* Flush to ensure ordering across contexts. */
4429
4430 context_release(context);
4431}
4432
4433/*****************************************************************************
4434 * IWineD3DSurface::LoadLocation
4435 *
4436 * Copies the current surface data from wherever it is to the requested
4437 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4438 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4439 * multiple locations, the gl texture is preferred over the drawable, which is
4440 * preferred over system memory. The PBO counts as system memory. If rect is
4441 * not NULL, only the specified rectangle is copied (only supported for
4442 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4443 * location is marked up to date after the copy.
4444 *
4445 * Parameters:
4446 * flag: Surface location flag to be updated
4447 * rect: rectangle to be copied
4448 *
4449 * Returns:
4450 * WINED3D_OK on success
4451 * WINED3DERR_DEVICELOST on an internal error
4452 *
4453 *****************************************************************************/
4454static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4455 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4456 IWineD3DDeviceImpl *device = This->resource.device;
4457 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4458 struct wined3d_format_desc desc;
4459 CONVERT_TYPES convert;
4460 int width, pitch, outpitch;
4461 BYTE *mem;
4462 BOOL drawable_read_ok = TRUE;
4463 BOOL in_fbo = FALSE;
4464
4465 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4466 if (surface_is_offscreen(iface))
4467 {
4468 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4469 * Prefer SFLAG_INTEXTURE. */
4470 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4471 drawable_read_ok = FALSE;
4472 in_fbo = TRUE;
4473 }
4474 else
4475 {
4476 TRACE("Surface %p is an onscreen surface\n", iface);
4477 }
4478 }
4479
4480 TRACE("(%p)->(%s, %p)\n", iface, debug_surflocation(flag), rect);
4481 if(rect) {
4482 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4483 }
4484
4485 if(This->Flags & flag) {
4486 TRACE("Location already up to date\n");
4487 return WINED3D_OK;
4488 }
4489
4490 if(!(This->Flags & SFLAG_LOCATIONS)) {
4491 ERR("%p: Surface does not have any up to date location\n", This);
4492 This->Flags |= SFLAG_LOST;
4493 return WINED3DERR_DEVICELOST;
4494 }
4495
4496 if(flag == SFLAG_INSYSMEM) {
4497 surface_prepare_system_memory(This);
4498
4499 /* Download the surface to system memory */
4500 if (This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4501 {
4502 struct wined3d_context *context = NULL;
4503
4504 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4505
4506 surface_bind_and_dirtify(This, !(This->Flags & SFLAG_INTEXTURE));
4507 surface_download_data(This, gl_info);
4508
4509 if (context) context_release(context);
4510 }
4511 else
4512 {
4513 /* Note: It might be faster to download into a texture first. */
4514 read_from_framebuffer(This, rect,
4515 This->resource.allocatedMemory,
4516 IWineD3DSurface_GetPitch(iface));
4517 }
4518 } else if(flag == SFLAG_INDRAWABLE) {
4519 if(This->Flags & SFLAG_INTEXTURE) {
4520 surface_blt_to_drawable(This, rect);
4521 } else {
4522 int byte_count;
4523 if((This->Flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX) {
4524 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4525 * values, otherwise we get incorrect values in the target. For now go the slow way
4526 * via a system memory copy
4527 */
4528 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4529 }
4530
4531 d3dfmt_get_conv(This, FALSE /* We need color keying */, FALSE /* We won't use textures */, &desc, &convert);
4532
4533 /* The width is in 'length' not in bytes */
4534 width = This->currentDesc.Width;
4535 pitch = IWineD3DSurface_GetPitch(iface);
4536
4537 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4538 * but it isn't set (yet) in all cases it is getting called. */
4539 if ((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO))
4540 {
4541 struct wined3d_context *context = NULL;
4542
4543 TRACE("Removing the pbo attached to surface %p\n", This);
4544
4545 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4546 surface_remove_pbo(This, gl_info);
4547 if (context) context_release(context);
4548 }
4549
4550 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4551 int height = This->currentDesc.Height;
4552 byte_count = desc.conv_byte_count;
4553
4554 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4555 outpitch = width * byte_count;
4556 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4557
4558 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4559 if(!mem) {
4560 ERR("Out of memory %d, %d!\n", outpitch, height);
4561 return WINED3DERR_OUTOFVIDEOMEMORY;
4562 }
4563 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4564
4565 This->Flags |= SFLAG_CONVERTED;
4566 } else {
4567 This->Flags &= ~SFLAG_CONVERTED;
4568 mem = This->resource.allocatedMemory;
4569 byte_count = desc.byte_count;
4570 }
4571
4572 flush_to_framebuffer_drawpixels(This, desc.glFormat, desc.glType, byte_count, mem);
4573
4574 /* Don't delete PBO memory */
4575 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4576 HeapFree(GetProcessHeap(), 0, mem);
4577 }
4578 } else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */ {
4579 if (drawable_read_ok && (This->Flags & SFLAG_INDRAWABLE)) {
4580 read_from_framebuffer_texture(This, flag == SFLAG_INSRGBTEX);
4581 }
4582 else
4583 {
4584 /* Upload from system memory */
4585 BOOL srgb = flag == SFLAG_INSRGBTEX;
4586 struct wined3d_context *context = NULL;
4587
4588 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */,
4589 &desc, &convert);
4590
4591 if(srgb) {
4592 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE) {
4593 /* Performance warning ... */
4594 FIXME("%p: Downloading rgb texture to reload it as srgb\n", This);
4595 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4596 }
4597 } else {
4598 if((This->Flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX) {
4599 /* Performance warning ... */
4600 FIXME("%p: Downloading srgb texture to reload it as rgb\n", This);
4601 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4602 }
4603 }
4604 if(!(This->Flags & SFLAG_INSYSMEM)) {
4605 /* Should not happen */
4606 ERR("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set\n");
4607 /* Lets hope we get it from somewhere... */
4608 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4609 }
4610
4611 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4612
4613 surface_prepare_texture(This, gl_info, srgb);
4614 surface_bind_and_dirtify(This, srgb);
4615
4616 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4617 This->Flags |= SFLAG_GLCKEY;
4618 This->glCKey = This->SrcBltCKey;
4619 }
4620 else This->Flags &= ~SFLAG_GLCKEY;
4621
4622 /* The width is in 'length' not in bytes */
4623 width = This->currentDesc.Width;
4624 pitch = IWineD3DSurface_GetPitch(iface);
4625
4626 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4627 * but it isn't set (yet) in all cases it is getting called. */
4628 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4629 TRACE("Removing the pbo attached to surface %p\n", This);
4630 surface_remove_pbo(This, gl_info);
4631 }
4632
4633 if(desc.convert) {
4634 /* This code is entered for texture formats which need a fixup. */
4635 int height = This->currentDesc.Height;
4636
4637 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4638 outpitch = width * desc.conv_byte_count;
4639 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4640
4641 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4642 if(!mem) {
4643 ERR("Out of memory %d, %d!\n", outpitch, height);
4644 if (context) context_release(context);
4645 return WINED3DERR_OUTOFVIDEOMEMORY;
4646 }
4647 desc.convert(This->resource.allocatedMemory, mem, pitch, width, height);
4648 } else if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4649 /* This code is only entered for color keying fixups */
4650 int height = This->currentDesc.Height;
4651
4652 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4653 outpitch = width * desc.conv_byte_count;
4654 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4655
4656 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4657 if(!mem) {
4658 ERR("Out of memory %d, %d!\n", outpitch, height);
4659 if (context) context_release(context);
4660 return WINED3DERR_OUTOFVIDEOMEMORY;
4661 }
4662 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4663 } else {
4664 mem = This->resource.allocatedMemory;
4665 }
4666
4667 /* Make sure the correct pitch is used */
4668 ENTER_GL();
4669 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4670 LEAVE_GL();
4671
4672 if (mem || (This->Flags & SFLAG_PBO))
4673 surface_upload_data(This, gl_info, &desc, srgb, mem);
4674
4675 /* Restore the default pitch */
4676 ENTER_GL();
4677 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4678 LEAVE_GL();
4679
4680 if (context) context_release(context);
4681
4682 /* Don't delete PBO memory */
4683 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4684 HeapFree(GetProcessHeap(), 0, mem);
4685 }
4686 }
4687
4688#ifdef VBOX_WITH_WDDM
4689 if (VBOXSHRC_IS_INITIALIZED(This))
4690 {
4691 /* with the shared resource only texture can be considered valid
4692 * to make sure changes done to the resource in the other device context are visible
4693 * because the resource contents is shared via texture.
4694 * One can load and use other locations as needed,
4695 * but they should be reloaded each time on each usage */
4696 Assert(!!(This->Flags & SFLAG_INTEXTURE) || !!(flag & SFLAG_INTEXTURE));
4697 This->Flags &= ~SFLAG_LOCATIONS;
4698 This->Flags |= SFLAG_INTEXTURE;
4699 /* @todo: SFLAG_INSRGBTEX ?? */
4700// if (in_fbo)
4701// {
4702// This->Flags |= SFLAG_INDRAWABLE;
4703// }
4704 }
4705 else
4706#endif
4707 {
4708 if(rect == NULL) {
4709 This->Flags |= flag;
4710 }
4711
4712 if (in_fbo && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4713 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4714 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4715 }
4716 }
4717
4718 return WINED3D_OK;
4719}
4720
4721static HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container)
4722{
4723 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4724 IWineD3DSwapChain *swapchain = NULL;
4725
4726 /* Update the drawable size method */
4727 if(container) {
4728 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4729 }
4730 if(swapchain) {
4731 This->get_drawable_size = get_drawable_size_swapchain;
4732 IWineD3DSwapChain_Release(swapchain);
4733 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4734 switch(wined3d_settings.offscreen_rendering_mode) {
4735 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4736 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4737 }
4738 }
4739
4740 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4741}
4742
4743static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4744 return SURFACE_OPENGL;
4745}
4746
4747static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4748 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4749 HRESULT hr;
4750
4751 /* If there's no destination surface there is nothing to do */
4752 if(!This->overlay_dest) return WINED3D_OK;
4753
4754 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4755 * update the overlay. Prevent an endless recursion
4756 */
4757 if(This->overlay_dest->Flags & SFLAG_INOVERLAYDRAW) {
4758 return WINED3D_OK;
4759 }
4760 This->overlay_dest->Flags |= SFLAG_INOVERLAYDRAW;
4761 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *) This->overlay_dest, &This->overlay_destrect,
4762 iface, &This->overlay_srcrect, WINEDDBLT_WAIT,
4763 NULL, WINED3DTEXF_LINEAR);
4764 This->overlay_dest->Flags &= ~SFLAG_INOVERLAYDRAW;
4765
4766 return hr;
4767}
4768
4769BOOL surface_is_offscreen(IWineD3DSurface *iface)
4770{
4771 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4772 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) This->container;
4773
4774 /* Not on a swapchain - must be offscreen */
4775 if (!(This->Flags & SFLAG_SWAPCHAIN)) return TRUE;
4776
4777 /* The front buffer is always onscreen */
4778 if(iface == swapchain->frontBuffer) return FALSE;
4779
4780 /* If the swapchain is rendered to an FBO, the backbuffer is
4781 * offscreen, otherwise onscreen */
4782 return swapchain->render_to_fbo;
4783}
4784
4785const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4786{
4787 /* IUnknown */
4788 IWineD3DBaseSurfaceImpl_QueryInterface,
4789 IWineD3DBaseSurfaceImpl_AddRef,
4790 IWineD3DSurfaceImpl_Release,
4791 /* IWineD3DResource */
4792 IWineD3DBaseSurfaceImpl_GetParent,
4793 IWineD3DBaseSurfaceImpl_SetPrivateData,
4794 IWineD3DBaseSurfaceImpl_GetPrivateData,
4795 IWineD3DBaseSurfaceImpl_FreePrivateData,
4796 IWineD3DBaseSurfaceImpl_SetPriority,
4797 IWineD3DBaseSurfaceImpl_GetPriority,
4798 IWineD3DSurfaceImpl_PreLoad,
4799 IWineD3DSurfaceImpl_UnLoad,
4800 IWineD3DBaseSurfaceImpl_GetType,
4801 /* IWineD3DSurface */
4802 IWineD3DBaseSurfaceImpl_GetContainer,
4803 IWineD3DBaseSurfaceImpl_GetDesc,
4804 IWineD3DSurfaceImpl_LockRect,
4805 IWineD3DSurfaceImpl_UnlockRect,
4806 IWineD3DSurfaceImpl_GetDC,
4807 IWineD3DSurfaceImpl_ReleaseDC,
4808 IWineD3DSurfaceImpl_Flip,
4809 IWineD3DSurfaceImpl_Blt,
4810 IWineD3DBaseSurfaceImpl_GetBltStatus,
4811 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4812 IWineD3DBaseSurfaceImpl_IsLost,
4813 IWineD3DBaseSurfaceImpl_Restore,
4814 IWineD3DSurfaceImpl_BltFast,
4815 IWineD3DBaseSurfaceImpl_GetPalette,
4816 IWineD3DBaseSurfaceImpl_SetPalette,
4817 IWineD3DSurfaceImpl_RealizePalette,
4818 IWineD3DBaseSurfaceImpl_SetColorKey,
4819 IWineD3DBaseSurfaceImpl_GetPitch,
4820 IWineD3DSurfaceImpl_SetMem,
4821 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4822 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4823 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4824 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4825 IWineD3DBaseSurfaceImpl_SetClipper,
4826 IWineD3DBaseSurfaceImpl_GetClipper,
4827 /* Internal use: */
4828 IWineD3DSurfaceImpl_LoadTexture,
4829 IWineD3DSurfaceImpl_BindTexture,
4830 IWineD3DSurfaceImpl_SaveSnapshot,
4831 IWineD3DSurfaceImpl_SetContainer,
4832 IWineD3DBaseSurfaceImpl_GetData,
4833 IWineD3DSurfaceImpl_SetFormat,
4834 IWineD3DSurfaceImpl_PrivateSetup,
4835 IWineD3DSurfaceImpl_ModifyLocation,
4836 IWineD3DSurfaceImpl_LoadLocation,
4837 IWineD3DSurfaceImpl_GetImplType,
4838 IWineD3DSurfaceImpl_DrawOverlay
4839};
4840
4841static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4842/* Context activation is done by the caller. */
4843static void ffp_blit_free(IWineD3DDevice *iface) { }
4844
4845/* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4846/* Context activation is done by the caller. */
4847static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4848{
4849 BYTE table[256][4];
4850 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4851
4852 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4853
4854 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4855 ENTER_GL();
4856 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4857 LEAVE_GL();
4858}
4859
4860/* Context activation is done by the caller. */
4861static HRESULT ffp_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4862{
4863 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4864 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4865 enum complex_fixup fixup = get_complex_fixup(surface->resource.format_desc->color_fixup);
4866
4867 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4868 * else the surface is converted in software at upload time in LoadLocation.
4869 */
4870 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4871 ffp_blit_p8_upload_palette(surface, gl_info);
4872
4873 ENTER_GL();
4874 glEnable(surface->texture_target);
4875 checkGLcall("glEnable(surface->texture_target)");
4876 LEAVE_GL();
4877 return WINED3D_OK;
4878}
4879
4880/* Context activation is done by the caller. */
4881static void ffp_blit_unset(IWineD3DDevice *iface)
4882{
4883 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4884 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4885
4886 ENTER_GL();
4887 glDisable(GL_TEXTURE_2D);
4888 checkGLcall("glDisable(GL_TEXTURE_2D)");
4889 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4890 {
4891 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4892 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4893 }
4894 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4895 {
4896 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4897 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4898 }
4899 LEAVE_GL();
4900}
4901
4902static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4903 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4904 const struct wined3d_format_desc *src_format_desc,
4905 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4906 const struct wined3d_format_desc *dst_format_desc)
4907{
4908 enum complex_fixup src_fixup;
4909
4910 if (blit_op == BLIT_OP_COLOR_FILL)
4911 {
4912 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4913 {
4914 TRACE("Color fill not supported\n");
4915 return FALSE;
4916 }
4917
4918 return TRUE;
4919 }
4920
4921 src_fixup = get_complex_fixup(src_format_desc->color_fixup);
4922 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4923 {
4924 TRACE("Checking support for fixup:\n");
4925 dump_color_fixup_desc(src_format_desc->color_fixup);
4926 }
4927
4928 if (blit_op != BLIT_OP_BLIT)
4929 {
4930 TRACE("Unsupported blit_op=%d\n", blit_op);
4931 return FALSE;
4932 }
4933
4934 if (!is_identity_fixup(dst_format_desc->color_fixup))
4935 {
4936 TRACE("Destination fixups are not supported\n");
4937 return FALSE;
4938 }
4939
4940 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4941 {
4942 TRACE("P8 fixup supported\n");
4943 return TRUE;
4944 }
4945
4946 /* We only support identity conversions. */
4947 if (is_identity_fixup(src_format_desc->color_fixup))
4948 {
4949 TRACE("[OK]\n");
4950 return TRUE;
4951 }
4952
4953 TRACE("[FAILED]\n");
4954 return FALSE;
4955}
4956
4957static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect, DWORD fill_color)
4958{
4959 return IWineD3DDeviceImpl_ClearSurface(device, dst_surface, 1 /* Number of rectangles */,
4960 (const WINED3DRECT*)dst_rect, WINED3DCLEAR_TARGET, fill_color, 0.0f /* Z */, 0 /* Stencil */);
4961}
4962
4963const struct blit_shader ffp_blit = {
4964 ffp_blit_alloc,
4965 ffp_blit_free,
4966 ffp_blit_set,
4967 ffp_blit_unset,
4968 ffp_blit_supported,
4969 ffp_blit_color_fill
4970};
4971
4972static HRESULT cpu_blit_alloc(IWineD3DDevice *iface)
4973{
4974 return WINED3D_OK;
4975}
4976
4977/* Context activation is done by the caller. */
4978static void cpu_blit_free(IWineD3DDevice *iface)
4979{
4980}
4981
4982/* Context activation is done by the caller. */
4983static HRESULT cpu_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4984{
4985 return WINED3D_OK;
4986}
4987
4988/* Context activation is done by the caller. */
4989static void cpu_blit_unset(IWineD3DDevice *iface)
4990{
4991}
4992
4993static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4994 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4995 const struct wined3d_format_desc *src_format_desc,
4996 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4997 const struct wined3d_format_desc *dst_format_desc)
4998{
4999 if (blit_op == BLIT_OP_COLOR_FILL)
5000 {
5001 return TRUE;
5002 }
5003
5004 return FALSE;
5005}
5006
5007static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect, DWORD fill_color)
5008{
5009 WINEDDBLTFX BltFx;
5010 memset(&BltFx, 0, sizeof(BltFx));
5011 BltFx.dwSize = sizeof(BltFx);
5012 BltFx.u5.dwFillColor = color_convert_argb_to_fmt(fill_color, dst_surface->resource.format_desc->format);
5013 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect, NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
5014}
5015
5016const struct blit_shader cpu_blit = {
5017 cpu_blit_alloc,
5018 cpu_blit_free,
5019 cpu_blit_set,
5020 cpu_blit_unset,
5021 cpu_blit_supported,
5022 cpu_blit_color_fill
5023};
5024
5025static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
5026 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
5027 const struct wined3d_format_desc *src_format_desc,
5028 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
5029 const struct wined3d_format_desc *dst_format_desc)
5030{
5031 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5032 return FALSE;
5033
5034 /* We only support blitting. Things like color keying / color fill should
5035 * be handled by other blitters.
5036 */
5037 if (blit_op != BLIT_OP_BLIT)
5038 return FALSE;
5039
5040 /* Source and/or destination need to be on the GL side */
5041 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
5042 return FALSE;
5043
5044 if(!((src_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET))
5045 && ((dst_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
5046 return FALSE;
5047
5048 if (!is_identity_fixup(src_format_desc->color_fixup) ||
5049 !is_identity_fixup(dst_format_desc->color_fixup))
5050 return FALSE;
5051
5052 if (!(src_format_desc->format == dst_format_desc->format
5053 || (is_identity_fixup(src_format_desc->color_fixup)
5054 && is_identity_fixup(dst_format_desc->color_fixup))))
5055 return FALSE;
5056
5057 return TRUE;
5058}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette