VirtualBox

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

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

crOpenGL/wddm: enable hw blitting on nvidia cards for all rects

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