VirtualBox

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

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

*: rebrand Sun (L)GPL disclaimers

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