VirtualBox

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

Last change on this file since 41028 was 40962, checked in by vboxsync, 13 years ago

wined3d: more work-arounds for intel graphics rendering issues

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