VirtualBox

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

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

wddm: VBOXWDDM->VBOX_WITH_WDDM

  • Property svn:eol-style set to native
File size: 193.5 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)
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 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3136 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
3137 src_rect->left, Src->currentDesc.Height - src_rect->bottom,
3138 dst_rect.right - dst_rect.left, -(dst_rect.bottom-dst_rect.top));
3139 }
3140 else
3141 {
3142 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
3143 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
3144 {
3145 /* Well, that stuff works, but it's very slow.
3146 * find a better way instead
3147 */
3148 UINT col;
3149
3150 for(col = dst_rect.left; col < dst_rect.right; col++) {
3151 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3152 dst_rect.left + col /* x offset */, row /* y offset */,
3153 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
3154 }
3155 } else {
3156 glCopyTexSubImage2D(This->texture_target, This->texture_level,
3157 dst_rect.left /* x offset */, row /* y offset */,
3158 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
3159 }
3160 }
3161 }
3162 }
3163 checkGLcall("glCopyTexSubImage2D");
3164
3165 LEAVE_GL();
3166 context_release(context);
3167
3168 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3169 * path is never entered
3170 */
3171 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3172}
3173
3174/* Uses the hardware to stretch and flip the image */
3175static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface,
3176 const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
3177{
3178 IWineD3DDeviceImpl *myDevice = This->resource.device;
3179 GLuint src, backup = 0;
3180 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3181 IWineD3DSwapChainImpl *src_swapchain = NULL;
3182 float left, right, top, bottom; /* Texture coordinates */
3183 UINT fbwidth = Src->currentDesc.Width;
3184 UINT fbheight = Src->currentDesc.Height;
3185 struct wined3d_context *context;
3186 GLenum drawBuffer = GL_BACK;
3187 GLenum texture_target;
3188 BOOL noBackBufferBackup;
3189 BOOL src_offscreen;
3190 BOOL upsidedown = FALSE;
3191 RECT dst_rect = *dst_rect_in;
3192
3193 TRACE("Using hwstretch blit\n");
3194 /* Activate the Proper context for reading from the source surface, set it up for blitting */
3195 context = context_acquire(myDevice, SrcSurface, CTXUSAGE_BLIT);
3196 surface_internal_preload((IWineD3DSurface *) This, SRGB_RGB);
3197
3198 src_offscreen = surface_is_offscreen(SrcSurface);
3199 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
3200 if (!noBackBufferBackup && !Src->texture_name)
3201 {
3202 /* Get it a description */
3203 surface_internal_preload(SrcSurface, SRGB_RGB);
3204 }
3205 ENTER_GL();
3206
3207 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
3208 * This way we don't have to wait for the 2nd readback to finish to leave this function.
3209 */
3210 if (context->aux_buffers >= 2)
3211 {
3212 /* Got more than one aux buffer? Use the 2nd aux buffer */
3213 drawBuffer = GL_AUX1;
3214 }
3215 else if ((!src_offscreen || myDevice->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
3216 {
3217 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
3218 drawBuffer = GL_AUX0;
3219 }
3220
3221 if(noBackBufferBackup) {
3222 glGenTextures(1, &backup);
3223 checkGLcall("glGenTextures");
3224 glBindTexture(GL_TEXTURE_2D, backup);
3225 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3226 texture_target = GL_TEXTURE_2D;
3227 } else {
3228 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
3229 * we are reading from the back buffer, the backup can be used as source texture
3230 */
3231 texture_target = Src->texture_target;
3232 glBindTexture(texture_target, Src->texture_name);
3233 checkGLcall("glBindTexture(texture_target, Src->texture_name)");
3234 glEnable(texture_target);
3235 checkGLcall("glEnable(texture_target)");
3236
3237 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3238 Src->Flags &= ~SFLAG_INTEXTURE;
3239 }
3240
3241 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3242 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3243 */
3244 if(dst_rect.top > dst_rect.bottom) {
3245 UINT tmp = dst_rect.bottom;
3246 dst_rect.bottom = dst_rect.top;
3247 dst_rect.top = tmp;
3248 upsidedown = TRUE;
3249 }
3250
3251 if (src_offscreen)
3252 {
3253 TRACE("Reading from an offscreen target\n");
3254 upsidedown = !upsidedown;
3255 glReadBuffer(myDevice->offscreenBuffer);
3256 }
3257 else
3258 {
3259 glReadBuffer(surface_get_gl_buffer(SrcSurface));
3260 }
3261
3262 /* TODO: Only back up the part that will be overwritten */
3263 glCopyTexSubImage2D(texture_target, 0,
3264 0, 0 /* read offsets */,
3265 0, 0,
3266 fbwidth,
3267 fbheight);
3268
3269 checkGLcall("glCopyTexSubImage2D");
3270
3271 /* No issue with overriding these - the sampler is dirty due to blit usage */
3272 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3273 wined3d_gl_mag_filter(magLookup, Filter));
3274 checkGLcall("glTexParameteri");
3275 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3276 wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3277 checkGLcall("glTexParameteri");
3278
3279 IWineD3DSurface_GetContainer((IWineD3DSurface *)SrcSurface, &IID_IWineD3DSwapChain, (void **)&src_swapchain);
3280 if (src_swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *)src_swapchain);
3281 if (!src_swapchain || (IWineD3DSurface *) Src == src_swapchain->backBuffer[0]) {
3282 src = backup ? backup : Src->texture_name;
3283 } else {
3284 glReadBuffer(GL_FRONT);
3285 checkGLcall("glReadBuffer(GL_FRONT)");
3286
3287 glGenTextures(1, &src);
3288 checkGLcall("glGenTextures(1, &src)");
3289 glBindTexture(GL_TEXTURE_2D, src);
3290 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3291
3292 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3293 * out for power of 2 sizes
3294 */
3295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
3296 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3297 checkGLcall("glTexImage2D");
3298 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3299 0, 0 /* read offsets */,
3300 0, 0,
3301 fbwidth,
3302 fbheight);
3303
3304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3305 checkGLcall("glTexParameteri");
3306 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3307 checkGLcall("glTexParameteri");
3308
3309 glReadBuffer(GL_BACK);
3310 checkGLcall("glReadBuffer(GL_BACK)");
3311
3312 if(texture_target != GL_TEXTURE_2D) {
3313 glDisable(texture_target);
3314 glEnable(GL_TEXTURE_2D);
3315 texture_target = GL_TEXTURE_2D;
3316 }
3317 }
3318 checkGLcall("glEnd and previous");
3319
3320 left = src_rect->left;
3321 right = src_rect->right;
3322
3323 if(upsidedown) {
3324 top = Src->currentDesc.Height - src_rect->top;
3325 bottom = Src->currentDesc.Height - src_rect->bottom;
3326 } else {
3327 top = Src->currentDesc.Height - src_rect->bottom;
3328 bottom = Src->currentDesc.Height - src_rect->top;
3329 }
3330
3331 if(Src->Flags & SFLAG_NORMCOORD) {
3332 left /= Src->pow2Width;
3333 right /= Src->pow2Width;
3334 top /= Src->pow2Height;
3335 bottom /= Src->pow2Height;
3336 }
3337
3338 /* draw the source texture stretched and upside down. The correct surface is bound already */
3339 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3340 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3341
3342 context_set_draw_buffer(context, drawBuffer);
3343 glReadBuffer(drawBuffer);
3344
3345 glBegin(GL_QUADS);
3346 /* bottom left */
3347 glTexCoord2f(left, bottom);
3348 glVertex2i(0, fbheight);
3349
3350 /* top left */
3351 glTexCoord2f(left, top);
3352 glVertex2i(0, fbheight - dst_rect.bottom - dst_rect.top);
3353
3354 /* top right */
3355 glTexCoord2f(right, top);
3356 glVertex2i(dst_rect.right - dst_rect.left, fbheight - dst_rect.bottom - dst_rect.top);
3357
3358 /* bottom right */
3359 glTexCoord2f(right, bottom);
3360 glVertex2i(dst_rect.right - dst_rect.left, fbheight);
3361 glEnd();
3362 checkGLcall("glEnd and previous");
3363
3364 if (texture_target != This->texture_target)
3365 {
3366 glDisable(texture_target);
3367 glEnable(This->texture_target);
3368 texture_target = This->texture_target;
3369 }
3370
3371 /* Now read the stretched and upside down image into the destination texture */
3372 glBindTexture(texture_target, This->texture_name);
3373 checkGLcall("glBindTexture");
3374 glCopyTexSubImage2D(texture_target,
3375 0,
3376 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3377 0, 0, /* We blitted the image to the origin */
3378 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3379 checkGLcall("glCopyTexSubImage2D");
3380
3381 if(drawBuffer == GL_BACK) {
3382 /* Write the back buffer backup back */
3383 if(backup) {
3384 if(texture_target != GL_TEXTURE_2D) {
3385 glDisable(texture_target);
3386 glEnable(GL_TEXTURE_2D);
3387 texture_target = GL_TEXTURE_2D;
3388 }
3389 glBindTexture(GL_TEXTURE_2D, backup);
3390 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3391 } else {
3392 if (texture_target != Src->texture_target)
3393 {
3394 glDisable(texture_target);
3395 glEnable(Src->texture_target);
3396 texture_target = Src->texture_target;
3397 }
3398 glBindTexture(Src->texture_target, Src->texture_name);
3399 checkGLcall("glBindTexture(Src->texture_target, Src->texture_name)");
3400 }
3401
3402 glBegin(GL_QUADS);
3403 /* top left */
3404 glTexCoord2f(0.0f, (float)fbheight / (float)Src->pow2Height);
3405 glVertex2i(0, 0);
3406
3407 /* bottom left */
3408 glTexCoord2f(0.0f, 0.0f);
3409 glVertex2i(0, fbheight);
3410
3411 /* bottom right */
3412 glTexCoord2f((float)fbwidth / (float)Src->pow2Width, 0.0f);
3413 glVertex2i(fbwidth, Src->currentDesc.Height);
3414
3415 /* top right */
3416 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
3417 glVertex2i(fbwidth, 0);
3418 glEnd();
3419 }
3420 glDisable(texture_target);
3421 checkGLcall("glDisable(texture_target)");
3422
3423 /* Cleanup */
3424 if (src != Src->texture_name && src != backup)
3425 {
3426 glDeleteTextures(1, &src);
3427 checkGLcall("glDeleteTextures(1, &src)");
3428 }
3429 if(backup) {
3430 glDeleteTextures(1, &backup);
3431 checkGLcall("glDeleteTextures(1, &backup)");
3432 }
3433
3434 LEAVE_GL();
3435
3436 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3437
3438 context_release(context);
3439
3440 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3441 * path is never entered
3442 */
3443 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3444}
3445
3446/* Until the blit_shader is ready, define some prototypes here. */
3447static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
3448 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
3449 const struct wined3d_format_desc *src_format_desc,
3450 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
3451 const struct wined3d_format_desc *dst_format_desc);
3452
3453/* Not called from the VTable */
3454static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3455 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx,
3456 WINED3DTEXTUREFILTERTYPE Filter)
3457{
3458 IWineD3DDeviceImpl *myDevice = This->resource.device;
3459 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3460 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3461 RECT dst_rect, src_rect;
3462
3463 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3464
3465 /* Get the swapchain. One of the surfaces has to be a primary surface */
3466 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3467 WARN("Destination is in sysmem, rejecting gl blt\n");
3468 return WINED3DERR_INVALIDCALL;
3469 }
3470 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
3471 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
3472 if(Src) {
3473 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3474 WARN("Src is in sysmem, rejecting gl blt\n");
3475 return WINED3DERR_INVALIDCALL;
3476 }
3477 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3478 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
3479 }
3480
3481 /* Early sort out of cases where no render target is used */
3482 if(!dstSwapchain && !srcSwapchain &&
3483 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3484 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
3485 return WINED3DERR_INVALIDCALL;
3486 }
3487
3488 /* No destination color keying supported */
3489 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3490 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3491 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3492 return WINED3DERR_INVALIDCALL;
3493 }
3494
3495 surface_get_rect(This, DestRect, &dst_rect);
3496 if(Src) surface_get_rect(Src, SrcRect, &src_rect);
3497
3498 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3499 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3500 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3501 /* Half-life does a Blt from the back buffer to the front buffer,
3502 * Full surface size, no flags... Use present instead
3503 *
3504 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3505 */
3506
3507 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3508 while(1)
3509 {
3510 TRACE("Looking if a Present can be done...\n");
3511 /* Source Rectangle must be full surface */
3512 if(src_rect.left != 0 || src_rect.top != 0 ||
3513 src_rect.right != Src->currentDesc.Width || src_rect.bottom != Src->currentDesc.Height) {
3514 TRACE("No, Source rectangle doesn't match\n");
3515 break;
3516 }
3517
3518 /* No stretching may occur */
3519 if(src_rect.right != dst_rect.right - dst_rect.left ||
3520 src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3521 TRACE("No, stretching is done\n");
3522 break;
3523 }
3524
3525 /* Destination must be full surface or match the clipping rectangle */
3526 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3527 {
3528 RECT cliprect;
3529 POINT pos[2];
3530 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3531 pos[0].x = dst_rect.left;
3532 pos[0].y = dst_rect.top;
3533 pos[1].x = dst_rect.right;
3534 pos[1].y = dst_rect.bottom;
3535 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3536 pos, 2);
3537
3538 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3539 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3540 {
3541 TRACE("No, dest rectangle doesn't match(clipper)\n");
3542 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3543 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3544 break;
3545 }
3546 }
3547 else
3548 {
3549 if(dst_rect.left != 0 || dst_rect.top != 0 ||
3550 dst_rect.right != This->currentDesc.Width || dst_rect.bottom != This->currentDesc.Height) {
3551 TRACE("No, dest rectangle doesn't match(surface size)\n");
3552 break;
3553 }
3554 }
3555
3556 TRACE("Yes\n");
3557
3558 /* These flags are unimportant for the flag check, remove them */
3559 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3560 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3561
3562 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3563 * take very long, while a flip is fast.
3564 * This applies to Half-Life, which does such Blts every time it finished
3565 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3566 * menu. This is also used by all apps when they do windowed rendering
3567 *
3568 * The problem is that flipping is not really the same as copying. After a
3569 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3570 * untouched. Therefore it's necessary to override the swap effect
3571 * and to set it back after the flip.
3572 *
3573 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3574 * testcases.
3575 */
3576
3577 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3578 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3579
3580 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3581 IWineD3DSwapChain_Present((IWineD3DSwapChain *)dstSwapchain,
3582 NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3583
3584 dstSwapchain->presentParms.SwapEffect = orig_swap;
3585
3586 return WINED3D_OK;
3587 }
3588 break;
3589 }
3590
3591 TRACE("Unsupported blit between buffers on the same swapchain\n");
3592 return WINED3DERR_INVALIDCALL;
3593 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3594 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3595 return WINED3DERR_INVALIDCALL;
3596 } else if(dstSwapchain && srcSwapchain) {
3597 FIXME("Implement hardware blit between two different swapchains\n");
3598 return WINED3DERR_INVALIDCALL;
3599 } else if(dstSwapchain) {
3600 if(SrcSurface == myDevice->render_targets[0]) {
3601 TRACE("Blit from active render target to a swapchain\n");
3602 /* Handled with regular texture -> swapchain blit */
3603 }
3604 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3605 FIXME("Implement blit from a swapchain to the active render target\n");
3606 return WINED3DERR_INVALIDCALL;
3607 }
3608
3609 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3610 /* Blit from render target to texture */
3611 BOOL stretchx;
3612
3613 /* P8 read back is not implemented */
3614 if (Src->resource.format_desc->format == WINED3DFMT_P8_UINT ||
3615 This->resource.format_desc->format == WINED3DFMT_P8_UINT)
3616 {
3617 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3618 return WINED3DERR_INVALIDCALL;
3619 }
3620
3621 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3622 TRACE("Color keying not supported by frame buffer to texture blit\n");
3623 return WINED3DERR_INVALIDCALL;
3624 /* Destination color key is checked above */
3625 }
3626
3627 if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3628 stretchx = TRUE;
3629 } else {
3630 stretchx = FALSE;
3631 }
3632
3633 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3634 * flip the image nor scale it.
3635 *
3636 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3637 * -> If the app wants a image width an unscaled width, copy it line per line
3638 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3639 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3640 * back buffer. This is slower than reading line per line, thus not used for flipping
3641 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3642 * pixel by pixel
3643 *
3644 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3645 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3646 * backends.
3647 */
3648 if (fbo_blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3649 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3650 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3651 {
3652 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &src_rect,
3653 (IWineD3DSurface *)This, &dst_rect, Filter);
3654 } else if((!stretchx) || dst_rect.right - dst_rect.left > Src->currentDesc.Width ||
3655 dst_rect.bottom - dst_rect.top > Src->currentDesc.Height) {
3656 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3657 fb_copy_to_texture_direct(This, SrcSurface, &src_rect, &dst_rect, Filter);
3658 } else {
3659 TRACE("Using hardware stretching to flip / stretch the texture\n");
3660 fb_copy_to_texture_hwstretch(This, SrcSurface, &src_rect, &dst_rect, Filter);
3661 }
3662
3663 if(!(This->Flags & SFLAG_DONOTFREE)) {
3664 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3665 This->resource.allocatedMemory = NULL;
3666 This->resource.heapMemory = NULL;
3667 } else {
3668 This->Flags &= ~SFLAG_INSYSMEM;
3669 }
3670
3671 return WINED3D_OK;
3672 } else if(Src) {
3673 /* Blit from offscreen surface to render target */
3674 DWORD oldCKeyFlags = Src->CKeyFlags;
3675 WINEDDCOLORKEY oldBltCKey = Src->SrcBltCKey;
3676 struct wined3d_context *context;
3677
3678 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3679
3680 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3681 && fbo_blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3682 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3683 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3684 {
3685 TRACE("Using stretch_rect_fbo\n");
3686 /* The source is always a texture, but never the currently active render target, and the texture
3687 * contents are never upside down
3688 */
3689 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &src_rect,
3690 (IWineD3DSurface *)This, &dst_rect, Filter);
3691 return WINED3D_OK;
3692 }
3693
3694 if (!(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3695 && arbfp_blit.blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3696 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3697 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3698 {
3699 return arbfp_blit_surface(myDevice, Src, &src_rect, This, &dst_rect, BLIT_OP_BLIT, Filter);
3700 }
3701
3702 /* Color keying: Check if we have to do a color keyed blt,
3703 * and if not check if a color key is activated.
3704 *
3705 * Just modify the color keying parameters in the surface and restore them afterwards
3706 * The surface keeps track of the color key last used to load the opengl surface.
3707 * PreLoad will catch the change to the flags and color key and reload if necessary.
3708 */
3709 if(Flags & WINEDDBLT_KEYSRC) {
3710 /* Use color key from surface */
3711 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3712 /* Use color key from DDBltFx */
3713 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3714 Src->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3715 } else {
3716 /* Do not use color key */
3717 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3718 }
3719
3720 /* Now load the surface */
3721 surface_internal_preload((IWineD3DSurface *) Src, SRGB_RGB);
3722
3723 /* Activate the destination context, set it up for blitting */
3724 context = context_acquire(myDevice, (IWineD3DSurface *)This, CTXUSAGE_BLIT);
3725
3726 /* The coordinates of the ddraw front buffer are always fullscreen ('screen coordinates',
3727 * while OpenGL coordinates are window relative.
3728 * Also beware of the origin difference(top left vs bottom left).
3729 * Also beware that the front buffer's surface size is screen width x screen height,
3730 * whereas the real gl drawable size is the size of the window.
3731 */
3732 if (dstSwapchain && (IWineD3DSurface *)This == dstSwapchain->frontBuffer) {
3733 RECT windowsize;
3734 POINT offset = {0,0};
3735 UINT h;
3736 ClientToScreen(context->win_handle, &offset);
3737 GetClientRect(context->win_handle, &windowsize);
3738 h = windowsize.bottom - windowsize.top;
3739 dst_rect.left -= offset.x; dst_rect.right -=offset.x;
3740 dst_rect.top -= offset.y; dst_rect.bottom -=offset.y;
3741 dst_rect.top += This->currentDesc.Height - h; dst_rect.bottom += This->currentDesc.Height - h;
3742 }
3743
3744 if (!myDevice->blitter->blit_supported(&myDevice->adapter->gl_info, BLIT_OP_BLIT,
3745 &src_rect, Src->resource.usage, Src->resource.pool, Src->resource.format_desc,
3746 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3747 {
3748 FIXME("Unsupported blit operation falling back to software\n");
3749 return WINED3DERR_INVALIDCALL;
3750 }
3751
3752 myDevice->blitter->set_shader((IWineD3DDevice *) myDevice, Src);
3753
3754 ENTER_GL();
3755
3756 /* This is for color keying */
3757 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3758 glEnable(GL_ALPHA_TEST);
3759 checkGLcall("glEnable(GL_ALPHA_TEST)");
3760
3761 /* When the primary render target uses P8, the alpha component contains the palette index.
3762 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3763 * should be masked away have alpha set to 0. */
3764 if(primary_render_target_is_p8(myDevice))
3765 glAlphaFunc(GL_NOTEQUAL, (float)Src->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3766 else
3767 glAlphaFunc(GL_NOTEQUAL, 0.0f);
3768 checkGLcall("glAlphaFunc");
3769 } else {
3770 glDisable(GL_ALPHA_TEST);
3771 checkGLcall("glDisable(GL_ALPHA_TEST)");
3772 }
3773
3774 /* Draw a textured quad
3775 */
3776 draw_textured_quad(Src, &src_rect, &dst_rect, Filter);
3777
3778 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3779 glDisable(GL_ALPHA_TEST);
3780 checkGLcall("glDisable(GL_ALPHA_TEST)");
3781 }
3782
3783 /* Restore the color key parameters */
3784 Src->CKeyFlags = oldCKeyFlags;
3785 Src->SrcBltCKey = oldBltCKey;
3786
3787 LEAVE_GL();
3788
3789 /* Leave the opengl state valid for blitting */
3790 myDevice->blitter->unset_shader((IWineD3DDevice *) myDevice);
3791
3792 if (wined3d_settings.strict_draw_ordering || (dstSwapchain
3793 && ((IWineD3DSurface *)This == dstSwapchain->frontBuffer
3794#ifdef VBOX_WITH_WDDM
3795 || dstSwapchain->device->numContexts > 1
3796#else
3797 || dstSwapchain->num_contexts > 1
3798#endif
3799 )))
3800 wglFlush(); /* Flush to ensure ordering across contexts. */
3801
3802 context_release(context);
3803
3804 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3805 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3806 * is outdated now
3807 */
3808 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3809
3810 return WINED3D_OK;
3811 } else {
3812 /* Source-Less Blit to render target */
3813 if (Flags & WINEDDBLT_COLORFILL) {
3814 DWORD color;
3815
3816 TRACE("Colorfill\n");
3817
3818 /* The color as given in the Blt function is in the format of the frame-buffer...
3819 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3820 */
3821 if (!surface_convert_color_to_argb(This, DDBltFx->u5.dwFillColor, &color))
3822 {
3823 /* The color conversion function already prints an error, so need to do it here */
3824 return WINED3DERR_INVALIDCALL;
3825 }
3826
3827 if (ffp_blit.blit_supported(&myDevice->adapter->gl_info, BLIT_OP_COLOR_FILL,
3828 NULL, 0, 0, NULL,
3829 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3830 {
3831 return ffp_blit.color_fill(myDevice, This, &dst_rect, color);
3832 }
3833 else if (cpu_blit.blit_supported(&myDevice->adapter->gl_info, BLIT_OP_COLOR_FILL,
3834 NULL, 0, 0, NULL,
3835 &dst_rect, This->resource.usage, This->resource.pool, This->resource.format_desc))
3836 {
3837 return cpu_blit.color_fill(myDevice, This, &dst_rect, color);
3838 }
3839 return WINED3DERR_INVALIDCALL;
3840 }
3841 }
3842
3843 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3844 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3845 return WINED3DERR_INVALIDCALL;
3846}
3847
3848static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3849 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx)
3850{
3851 IWineD3DDeviceImpl *myDevice = This->resource.device;
3852 float depth;
3853
3854 if (Flags & WINEDDBLT_DEPTHFILL) {
3855 switch(This->resource.format_desc->format)
3856 {
3857 case WINED3DFMT_D16_UNORM:
3858 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3859 break;
3860 case WINED3DFMT_S1_UINT_D15_UNORM:
3861 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3862 break;
3863 case WINED3DFMT_D24_UNORM_S8_UINT:
3864 case WINED3DFMT_X8D24_UNORM:
3865 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3866 break;
3867 case WINED3DFMT_D32_UNORM:
3868 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3869 break;
3870 default:
3871 depth = 0.0f;
3872 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format_desc->format));
3873 }
3874
3875 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3876 DestRect == NULL ? 0 : 1,
3877 (const WINED3DRECT *)DestRect,
3878 WINED3DCLEAR_ZBUFFER,
3879 0x00000000,
3880 depth,
3881 0x00000000);
3882 }
3883
3884 FIXME("(%p): Unsupp depthstencil blit\n", This);
3885 return WINED3DERR_INVALIDCALL;
3886}
3887
3888static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect, IWineD3DSurface *SrcSurface,
3889 const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3890 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3891 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3892 IWineD3DDeviceImpl *myDevice = This->resource.device;
3893
3894 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3895 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3896
3897 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3898 {
3899 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3900 return WINEDDERR_SURFACEBUSY;
3901 }
3902
3903 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3904 * except depth blits, which seem to work
3905 */
3906 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3907 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3908 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3909 return WINED3DERR_INVALIDCALL;
3910 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3911 TRACE("Z Blit override handled the blit\n");
3912 return WINED3D_OK;
3913 }
3914 }
3915
3916 /* Special cases for RenderTargets */
3917 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3918 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3919 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3920 }
3921
3922 /* For the rest call the X11 surface implementation.
3923 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3924 * other Blts are rather rare
3925 */
3926 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3927}
3928
3929static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3930 IWineD3DSurface *Source, const RECT *rsrc, DWORD trans)
3931{
3932 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3933 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3934 IWineD3DDeviceImpl *myDevice = This->resource.device;
3935
3936 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3937
3938 if ( (This->Flags & SFLAG_LOCKED) || (srcImpl->Flags & SFLAG_LOCKED))
3939 {
3940 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3941 return WINEDDERR_SURFACEBUSY;
3942 }
3943
3944 if(myDevice->inScene &&
3945 (iface == myDevice->stencilBufferTarget ||
3946 (Source == myDevice->stencilBufferTarget))) {
3947 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3948 return WINED3DERR_INVALIDCALL;
3949 }
3950
3951 /* Special cases for RenderTargets */
3952 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3953 (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) ) {
3954
3955 RECT SrcRect, DstRect;
3956 DWORD Flags=0;
3957
3958 surface_get_rect(srcImpl, rsrc, &SrcRect);
3959
3960 DstRect.left = dstx;
3961 DstRect.top=dsty;
3962 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3963 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3964
3965 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3966 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3967 Flags |= WINEDDBLT_KEYSRC;
3968 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3969 Flags |= WINEDDBLT_KEYDEST;
3970 if(trans & WINEDDBLTFAST_WAIT)
3971 Flags |= WINEDDBLT_WAIT;
3972 if(trans & WINEDDBLTFAST_DONOTWAIT)
3973 Flags |= WINEDDBLT_DONOTWAIT;
3974
3975 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3976 }
3977
3978
3979 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3980}
3981
3982static HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface)
3983{
3984 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3985 RGBQUAD col[256];
3986 IWineD3DPaletteImpl *pal = This->palette;
3987 unsigned int n;
3988 TRACE("(%p)\n", This);
3989
3990 if (!pal) return WINED3D_OK;
3991
3992 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
3993 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
3994 {
3995 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3996 {
3997 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3998 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
3999
4000 /* We want to force a palette refresh, so mark the drawable as not being up to date */
4001 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
4002 } else {
4003 if(!(This->Flags & SFLAG_INSYSMEM)) {
4004 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
4005 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
4006 }
4007 TRACE("Dirtifying surface\n");
4008 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
4009 }
4010 }
4011
4012 if(This->Flags & SFLAG_DIBSECTION) {
4013 TRACE("(%p): Updating the hdc's palette\n", This);
4014 for (n=0; n<256; n++) {
4015 col[n].rgbRed = pal->palents[n].peRed;
4016 col[n].rgbGreen = pal->palents[n].peGreen;
4017 col[n].rgbBlue = pal->palents[n].peBlue;
4018 col[n].rgbReserved = 0;
4019 }
4020 SetDIBColorTable(This->hDC, 0, 256, col);
4021 }
4022
4023 /* Propagate the changes to the drawable when we have a palette. */
4024 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4025 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
4026
4027 return WINED3D_OK;
4028}
4029
4030#ifdef VBOX_WITH_WDDM
4031static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect);
4032#endif
4033
4034static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
4035 /** Check against the maximum texture sizes supported by the video card **/
4036 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4037 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
4038 unsigned int pow2Width, pow2Height;
4039
4040 This->texture_name = 0;
4041 This->texture_target = GL_TEXTURE_2D;
4042
4043 /* Non-power2 support */
4044 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINE_NORMALIZED_TEXRECT])
4045 {
4046 pow2Width = This->currentDesc.Width;
4047 pow2Height = This->currentDesc.Height;
4048 }
4049 else
4050 {
4051 /* Find the nearest pow2 match */
4052 pow2Width = pow2Height = 1;
4053 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
4054 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
4055 }
4056 This->pow2Width = pow2Width;
4057 This->pow2Height = pow2Height;
4058
4059 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
4060 /** TODO: add support for non power two compressed textures **/
4061 if (This->resource.format_desc->Flags & WINED3DFMT_FLAG_COMPRESSED)
4062 {
4063 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
4064 This, This->currentDesc.Width, This->currentDesc.Height);
4065 return WINED3DERR_NOTAVAILABLE;
4066 }
4067 }
4068
4069 if(pow2Width != This->currentDesc.Width ||
4070 pow2Height != This->currentDesc.Height) {
4071 This->Flags |= SFLAG_NONPOW2;
4072 }
4073
4074 TRACE("%p\n", This);
4075 if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
4076 && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
4077 {
4078 /* one of three options
4079 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)
4080 2: Set the texture to the maximum size (bad idea)
4081 3: WARN and return WINED3DERR_NOTAVAILABLE;
4082 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.
4083 */
4084 if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4085 {
4086 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4087 return WINED3DERR_NOTAVAILABLE;
4088 }
4089
4090 /* We should never use this surface in combination with OpenGL! */
4091 TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4092 }
4093 else
4094 {
4095 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4096 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4097 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4098 */
4099 if (This->Flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4100 && !(This->resource.format_desc->format == WINED3DFMT_P8_UINT
4101 && gl_info->supported[EXT_PALETTED_TEXTURE]
4102 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4103 {
4104 This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4105 This->pow2Width = This->currentDesc.Width;
4106 This->pow2Height = This->currentDesc.Height;
4107 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4108 }
4109 }
4110
4111 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4112 switch(wined3d_settings.offscreen_rendering_mode) {
4113 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4114 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4115 }
4116 }
4117
4118 This->Flags |= SFLAG_INSYSMEM;
4119
4120 return WINED3D_OK;
4121}
4122
4123/* GL locking is done by the caller */
4124static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4125 GLuint texture, GLsizei w, GLsizei h, GLenum target)
4126{
4127 IWineD3DDeviceImpl *device = This->resource.device;
4128 struct blt_info info;
4129 GLint old_binding = 0;
4130
4131 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4132
4133 glDisable(GL_CULL_FACE);
4134 glDisable(GL_BLEND);
4135 glDisable(GL_ALPHA_TEST);
4136 glDisable(GL_SCISSOR_TEST);
4137 glDisable(GL_STENCIL_TEST);
4138 glEnable(GL_DEPTH_TEST);
4139 glDepthFunc(GL_ALWAYS);
4140 glDepthMask(GL_TRUE);
4141 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4142 glViewport(0, 0, w, h);
4143
4144 surface_get_blt_info(target, NULL, w, h, &info);
4145 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4146 glGetIntegerv(info.binding, &old_binding);
4147 glBindTexture(info.bind_target, texture);
4148
4149 device->shader_backend->shader_select_depth_blt((IWineD3DDevice *)device, info.tex_type);
4150
4151 glBegin(GL_TRIANGLE_STRIP);
4152 glTexCoord3fv(info.coords[0]);
4153 glVertex2f(-1.0f, -1.0f);
4154 glTexCoord3fv(info.coords[1]);
4155 glVertex2f(1.0f, -1.0f);
4156 glTexCoord3fv(info.coords[2]);
4157 glVertex2f(-1.0f, 1.0f);
4158 glTexCoord3fv(info.coords[3]);
4159 glVertex2f(1.0f, 1.0f);
4160 glEnd();
4161
4162 glBindTexture(info.bind_target, old_binding);
4163
4164 glPopAttrib();
4165
4166 device->shader_backend->shader_deselect_depth_blt((IWineD3DDevice *)device);
4167}
4168
4169void surface_modify_ds_location(IWineD3DSurface *iface, DWORD location) {
4170 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4171
4172 TRACE("(%p) New location %#x\n", This, location);
4173
4174 if (location & ~SFLAG_DS_LOCATIONS) {
4175 FIXME("(%p) Invalid location (%#x) specified\n", This, location);
4176 }
4177
4178 This->Flags &= ~SFLAG_DS_LOCATIONS;
4179 This->Flags |= location;
4180}
4181
4182/* Context activation is done by the caller. */
4183void surface_load_ds_location(IWineD3DSurface *iface, struct wined3d_context *context, DWORD location)
4184{
4185 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4186 IWineD3DDeviceImpl *device = This->resource.device;
4187 const struct wined3d_gl_info *gl_info = context->gl_info;
4188
4189 TRACE("(%p) New location %#x\n", This, location);
4190
4191 /* TODO: Make this work for modes other than FBO */
4192 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4193
4194 if (This->Flags & location) {
4195 TRACE("(%p) Location (%#x) is already up to date\n", This, location);
4196 return;
4197 }
4198
4199 if (This->current_renderbuffer) {
4200 FIXME("(%p) Not supported with fixed up depth stencil\n", This);
4201 return;
4202 }
4203
4204 if (location == SFLAG_DS_OFFSCREEN) {
4205 if (This->Flags & SFLAG_DS_ONSCREEN) {
4206 GLint old_binding = 0;
4207 GLenum bind_target;
4208
4209 TRACE("(%p) Copying onscreen depth buffer to depth texture\n", This);
4210
4211 ENTER_GL();
4212
4213 if (!device->depth_blt_texture) {
4214 glGenTextures(1, &device->depth_blt_texture);
4215 }
4216
4217 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4218 * directly on the FBO texture. That's because we need to flip. */
4219 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4220 if (This->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4221 {
4222 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4223 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4224 } else {
4225 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4226 bind_target = GL_TEXTURE_2D;
4227 }
4228 glBindTexture(bind_target, device->depth_blt_texture);
4229 glCopyTexImage2D(bind_target, This->texture_level, This->resource.format_desc->glInternal,
4230 0, 0, This->currentDesc.Width, This->currentDesc.Height, 0);
4231 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4232 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4233 glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4234 glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4235 glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4236 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4237 glBindTexture(bind_target, old_binding);
4238
4239 /* Setup the destination */
4240 if (!device->depth_blt_rb) {
4241 gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4242 checkGLcall("glGenRenderbuffersEXT");
4243 }
4244 if (device->depth_blt_rb_w != This->currentDesc.Width
4245 || device->depth_blt_rb_h != This->currentDesc.Height) {
4246 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4247 checkGLcall("glBindRenderbufferEXT");
4248 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
4249 This->currentDesc.Width, This->currentDesc.Height);
4250 checkGLcall("glRenderbufferStorageEXT");
4251 device->depth_blt_rb_w = This->currentDesc.Width;
4252 device->depth_blt_rb_h = This->currentDesc.Height;
4253 }
4254
4255 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4256 gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4257 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4258 checkGLcall("glFramebufferRenderbufferEXT");
4259 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, This, FALSE);
4260
4261 /* Do the actual blit */
4262 surface_depth_blt(This, gl_info, device->depth_blt_texture,
4263 This->currentDesc.Width, This->currentDesc.Height, bind_target);
4264 checkGLcall("depth_blt");
4265
4266 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4267 else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4268
4269 LEAVE_GL();
4270
4271 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4272 }
4273 else
4274 {
4275 FIXME("No up to date depth stencil location\n");
4276 }
4277 } else if (location == SFLAG_DS_ONSCREEN) {
4278 if (This->Flags & SFLAG_DS_OFFSCREEN) {
4279 TRACE("(%p) Copying depth texture to onscreen depth buffer\n", This);
4280
4281 ENTER_GL();
4282
4283 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4284 surface_depth_blt(This, gl_info, This->texture_name,
4285 This->currentDesc.Width, This->currentDesc.Height, This->texture_target);
4286 checkGLcall("depth_blt");
4287
4288 if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4289
4290 LEAVE_GL();
4291
4292 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4293 }
4294 else
4295 {
4296 FIXME("No up to date depth stencil location\n");
4297 }
4298 } else {
4299 ERR("(%p) Invalid location (%#x) specified\n", This, location);
4300 }
4301
4302 This->Flags |= location;
4303}
4304
4305static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
4306 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4307 IWineD3DBaseTexture *texture;
4308 IWineD3DSurfaceImpl *overlay;
4309
4310 TRACE("(%p)->(%s, %s)\n", iface, debug_surflocation(flag),
4311 persistent ? "TRUE" : "FALSE");
4312
4313 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4314 if (surface_is_offscreen(iface))
4315 {
4316 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4317 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4318 }
4319 else
4320 {
4321 TRACE("Surface %p is an onscreen surface\n", iface);
4322 }
4323 }
4324
4325 if(persistent) {
4326 if(((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) ||
4327 ((This->Flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX))) {
4328 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4329 TRACE("Passing to container\n");
4330 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4331 IWineD3DBaseTexture_Release(texture);
4332 }
4333 }
4334
4335 This->Flags &= ~SFLAG_LOCATIONS;
4336 This->Flags |= flag;
4337
4338 /* Redraw emulated overlays, if any */
4339 if(flag & SFLAG_INDRAWABLE && !list_empty(&This->overlays)) {
4340 LIST_FOR_EACH_ENTRY(overlay, &This->overlays, IWineD3DSurfaceImpl, overlay_entry) {
4341 IWineD3DSurface_DrawOverlay((IWineD3DSurface *) overlay);
4342 }
4343 }
4344 } else {
4345 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))) {
4346 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4347 TRACE("Passing to container\n");
4348 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4349 IWineD3DBaseTexture_Release(texture);
4350 }
4351 }
4352
4353 This->Flags &= ~flag;
4354 }
4355
4356#ifdef VBOX_WITH_WDDM
4357 if(VBOXSHRC_IS_INITIALIZED(This)) {
4358 /* with the shared resource only texture can be considered valid
4359 * to make sure changes done to the resource in the other device context are visible
4360 * because the resource contents is shared via texture.
4361 * This is why we ensure texture location is the one and only which is always valid */
4362 if(!(This->Flags & SFLAG_INTEXTURE)) {
4363 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
4364 } else {
4365 This->Flags &= ~SFLAG_LOCATIONS;
4366 This->Flags |= SFLAG_INTEXTURE;
4367 }
4368 }
4369#endif
4370
4371 if(!(This->Flags & SFLAG_LOCATIONS)) {
4372 ERR("%p: Surface does not have any up to date location\n", This);
4373 }
4374}
4375
4376static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in)
4377{
4378 IWineD3DDeviceImpl *device = This->resource.device;
4379 IWineD3DSwapChainImpl *swapchain;
4380 struct wined3d_context *context;
4381 RECT src_rect, dst_rect;
4382
4383 surface_get_rect(This, rect_in, &src_rect);
4384
4385 context = context_acquire(device, (IWineD3DSurface*)This, CTXUSAGE_BLIT);
4386 if (context->render_offscreen)
4387 {
4388 dst_rect.left = src_rect.left;
4389 dst_rect.right = src_rect.right;
4390 dst_rect.top = src_rect.bottom;
4391 dst_rect.bottom = src_rect.top;
4392 }
4393 else
4394 {
4395 dst_rect = src_rect;
4396 }
4397
4398 device->blitter->set_shader((IWineD3DDevice *) device, This);
4399
4400 ENTER_GL();
4401 draw_textured_quad(This, &src_rect, &dst_rect, WINED3DTEXF_POINT);
4402 LEAVE_GL();
4403
4404 device->blitter->set_shader((IWineD3DDevice *) device, This);
4405
4406 swapchain = (This->Flags & SFLAG_SWAPCHAIN) ? (IWineD3DSwapChainImpl *)This->container : NULL;
4407 if (wined3d_settings.strict_draw_ordering || (swapchain
4408 && ((IWineD3DSurface *)This == swapchain->frontBuffer
4409#ifdef VBOX_WITH_WDDM
4410 || swapchain->device->numContexts > 1
4411#else
4412 || swapchain->num_contexts > 1
4413#endif
4414 )))
4415 wglFlush(); /* Flush to ensure ordering across contexts. */
4416
4417 context_release(context);
4418}
4419
4420/*****************************************************************************
4421 * IWineD3DSurface::LoadLocation
4422 *
4423 * Copies the current surface data from wherever it is to the requested
4424 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4425 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4426 * multiple locations, the gl texture is preferred over the drawable, which is
4427 * preferred over system memory. The PBO counts as system memory. If rect is
4428 * not NULL, only the specified rectangle is copied (only supported for
4429 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4430 * location is marked up to date after the copy.
4431 *
4432 * Parameters:
4433 * flag: Surface location flag to be updated
4434 * rect: rectangle to be copied
4435 *
4436 * Returns:
4437 * WINED3D_OK on success
4438 * WINED3DERR_DEVICELOST on an internal error
4439 *
4440 *****************************************************************************/
4441static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4442 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4443 IWineD3DDeviceImpl *device = This->resource.device;
4444 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4445 struct wined3d_format_desc desc;
4446 CONVERT_TYPES convert;
4447 int width, pitch, outpitch;
4448 BYTE *mem;
4449 BOOL drawable_read_ok = TRUE;
4450 BOOL in_fbo = FALSE;
4451
4452 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4453 if (surface_is_offscreen(iface))
4454 {
4455 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4456 * Prefer SFLAG_INTEXTURE. */
4457 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4458 drawable_read_ok = FALSE;
4459 in_fbo = TRUE;
4460 }
4461 else
4462 {
4463 TRACE("Surface %p is an onscreen surface\n", iface);
4464 }
4465 }
4466
4467 TRACE("(%p)->(%s, %p)\n", iface, debug_surflocation(flag), rect);
4468 if(rect) {
4469 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4470 }
4471
4472 if(This->Flags & flag) {
4473 TRACE("Location already up to date\n");
4474 return WINED3D_OK;
4475 }
4476
4477 if(!(This->Flags & SFLAG_LOCATIONS)) {
4478 ERR("%p: Surface does not have any up to date location\n", This);
4479 This->Flags |= SFLAG_LOST;
4480 return WINED3DERR_DEVICELOST;
4481 }
4482
4483 if(flag == SFLAG_INSYSMEM) {
4484 surface_prepare_system_memory(This);
4485
4486 /* Download the surface to system memory */
4487 if (This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4488 {
4489 struct wined3d_context *context = NULL;
4490
4491 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4492
4493 surface_bind_and_dirtify(This, !(This->Flags & SFLAG_INTEXTURE));
4494 surface_download_data(This, gl_info);
4495
4496 if (context) context_release(context);
4497 }
4498 else
4499 {
4500 /* Note: It might be faster to download into a texture first. */
4501 read_from_framebuffer(This, rect,
4502 This->resource.allocatedMemory,
4503 IWineD3DSurface_GetPitch(iface));
4504 }
4505 } else if(flag == SFLAG_INDRAWABLE) {
4506 if(This->Flags & SFLAG_INTEXTURE) {
4507 surface_blt_to_drawable(This, rect);
4508 } else {
4509 int byte_count;
4510 if((This->Flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX) {
4511 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4512 * values, otherwise we get incorrect values in the target. For now go the slow way
4513 * via a system memory copy
4514 */
4515 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4516 }
4517
4518 d3dfmt_get_conv(This, FALSE /* We need color keying */, FALSE /* We won't use textures */, &desc, &convert);
4519
4520 /* The width is in 'length' not in bytes */
4521 width = This->currentDesc.Width;
4522 pitch = IWineD3DSurface_GetPitch(iface);
4523
4524 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4525 * but it isn't set (yet) in all cases it is getting called. */
4526 if ((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO))
4527 {
4528 struct wined3d_context *context = NULL;
4529
4530 TRACE("Removing the pbo attached to surface %p\n", This);
4531
4532 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4533 surface_remove_pbo(This, gl_info);
4534 if (context) context_release(context);
4535 }
4536
4537 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4538 int height = This->currentDesc.Height;
4539 byte_count = desc.conv_byte_count;
4540
4541 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4542 outpitch = width * byte_count;
4543 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4544
4545 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4546 if(!mem) {
4547 ERR("Out of memory %d, %d!\n", outpitch, height);
4548 return WINED3DERR_OUTOFVIDEOMEMORY;
4549 }
4550 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4551
4552 This->Flags |= SFLAG_CONVERTED;
4553 } else {
4554 This->Flags &= ~SFLAG_CONVERTED;
4555 mem = This->resource.allocatedMemory;
4556 byte_count = desc.byte_count;
4557 }
4558
4559 flush_to_framebuffer_drawpixels(This, desc.glFormat, desc.glType, byte_count, mem);
4560
4561 /* Don't delete PBO memory */
4562 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4563 HeapFree(GetProcessHeap(), 0, mem);
4564 }
4565 } else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */ {
4566 if (drawable_read_ok && (This->Flags & SFLAG_INDRAWABLE)) {
4567 read_from_framebuffer_texture(This, flag == SFLAG_INSRGBTEX);
4568 }
4569 else
4570 {
4571 /* Upload from system memory */
4572 BOOL srgb = flag == SFLAG_INSRGBTEX;
4573 struct wined3d_context *context = NULL;
4574
4575 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */,
4576 &desc, &convert);
4577
4578 if(srgb) {
4579 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE) {
4580 /* Performance warning ... */
4581 FIXME("%p: Downloading rgb texture to reload it as srgb\n", This);
4582 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4583 }
4584 } else {
4585 if((This->Flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX) {
4586 /* Performance warning ... */
4587 FIXME("%p: Downloading srgb texture to reload it as rgb\n", This);
4588 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4589 }
4590 }
4591 if(!(This->Flags & SFLAG_INSYSMEM)) {
4592 /* Should not happen */
4593 ERR("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set\n");
4594 /* Lets hope we get it from somewhere... */
4595 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4596 }
4597
4598 if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4599
4600 surface_prepare_texture(This, gl_info, srgb);
4601 surface_bind_and_dirtify(This, srgb);
4602
4603 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4604 This->Flags |= SFLAG_GLCKEY;
4605 This->glCKey = This->SrcBltCKey;
4606 }
4607 else This->Flags &= ~SFLAG_GLCKEY;
4608
4609 /* The width is in 'length' not in bytes */
4610 width = This->currentDesc.Width;
4611 pitch = IWineD3DSurface_GetPitch(iface);
4612
4613 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4614 * but it isn't set (yet) in all cases it is getting called. */
4615 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4616 TRACE("Removing the pbo attached to surface %p\n", This);
4617 surface_remove_pbo(This, gl_info);
4618 }
4619
4620 if(desc.convert) {
4621 /* This code is entered for texture formats which need a fixup. */
4622 int height = This->currentDesc.Height;
4623
4624 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4625 outpitch = width * desc.conv_byte_count;
4626 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4627
4628 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4629 if(!mem) {
4630 ERR("Out of memory %d, %d!\n", outpitch, height);
4631 if (context) context_release(context);
4632 return WINED3DERR_OUTOFVIDEOMEMORY;
4633 }
4634 desc.convert(This->resource.allocatedMemory, mem, pitch, width, height);
4635 } else if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4636 /* This code is only entered for color keying fixups */
4637 int height = This->currentDesc.Height;
4638
4639 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4640 outpitch = width * desc.conv_byte_count;
4641 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4642
4643 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4644 if(!mem) {
4645 ERR("Out of memory %d, %d!\n", outpitch, height);
4646 if (context) context_release(context);
4647 return WINED3DERR_OUTOFVIDEOMEMORY;
4648 }
4649 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4650 } else {
4651 mem = This->resource.allocatedMemory;
4652 }
4653
4654 /* Make sure the correct pitch is used */
4655 ENTER_GL();
4656 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4657 LEAVE_GL();
4658
4659 if (mem || (This->Flags & SFLAG_PBO))
4660 surface_upload_data(This, gl_info, &desc, srgb, mem);
4661
4662 /* Restore the default pitch */
4663 ENTER_GL();
4664 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4665 LEAVE_GL();
4666
4667 if (context) context_release(context);
4668
4669 /* Don't delete PBO memory */
4670 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4671 HeapFree(GetProcessHeap(), 0, mem);
4672 }
4673 }
4674
4675#ifdef VBOX_WITH_WDDM
4676 if (VBOXSHRC_IS_INITIALIZED(This))
4677 {
4678 /* with the shared resource only texture can be considered valid
4679 * to make sure changes done to the resource in the other device context are visible
4680 * because the resource contents is shared via texture.
4681 * One can load and use other locations as needed,
4682 * but they should be reloaded each time on each usage */
4683 Assert(!!(This->Flags & SFLAG_INTEXTURE) || !!(flag & SFLAG_INTEXTURE));
4684 This->Flags &= ~SFLAG_LOCATIONS;
4685 This->Flags |= SFLAG_INTEXTURE;
4686 /* @todo: SFLAG_INSRGBTEX ?? */
4687// if (in_fbo)
4688// {
4689// This->Flags |= SFLAG_INDRAWABLE;
4690// }
4691 }
4692 else
4693#endif
4694 {
4695 if(rect == NULL) {
4696 This->Flags |= flag;
4697 }
4698
4699 if (in_fbo && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4700 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4701 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4702 }
4703 }
4704
4705 return WINED3D_OK;
4706}
4707
4708static HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container)
4709{
4710 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4711 IWineD3DSwapChain *swapchain = NULL;
4712
4713 /* Update the drawable size method */
4714 if(container) {
4715 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4716 }
4717 if(swapchain) {
4718 This->get_drawable_size = get_drawable_size_swapchain;
4719 IWineD3DSwapChain_Release(swapchain);
4720 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4721 switch(wined3d_settings.offscreen_rendering_mode) {
4722 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4723 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4724 }
4725 }
4726
4727 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4728}
4729
4730static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4731 return SURFACE_OPENGL;
4732}
4733
4734static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4735 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4736 HRESULT hr;
4737
4738 /* If there's no destination surface there is nothing to do */
4739 if(!This->overlay_dest) return WINED3D_OK;
4740
4741 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4742 * update the overlay. Prevent an endless recursion
4743 */
4744 if(This->overlay_dest->Flags & SFLAG_INOVERLAYDRAW) {
4745 return WINED3D_OK;
4746 }
4747 This->overlay_dest->Flags |= SFLAG_INOVERLAYDRAW;
4748 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *) This->overlay_dest, &This->overlay_destrect,
4749 iface, &This->overlay_srcrect, WINEDDBLT_WAIT,
4750 NULL, WINED3DTEXF_LINEAR);
4751 This->overlay_dest->Flags &= ~SFLAG_INOVERLAYDRAW;
4752
4753 return hr;
4754}
4755
4756BOOL surface_is_offscreen(IWineD3DSurface *iface)
4757{
4758 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4759 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) This->container;
4760
4761 /* Not on a swapchain - must be offscreen */
4762 if (!(This->Flags & SFLAG_SWAPCHAIN)) return TRUE;
4763
4764 /* The front buffer is always onscreen */
4765 if(iface == swapchain->frontBuffer) return FALSE;
4766
4767 /* If the swapchain is rendered to an FBO, the backbuffer is
4768 * offscreen, otherwise onscreen */
4769 return swapchain->render_to_fbo;
4770}
4771
4772const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4773{
4774 /* IUnknown */
4775 IWineD3DBaseSurfaceImpl_QueryInterface,
4776 IWineD3DBaseSurfaceImpl_AddRef,
4777 IWineD3DSurfaceImpl_Release,
4778 /* IWineD3DResource */
4779 IWineD3DBaseSurfaceImpl_GetParent,
4780 IWineD3DBaseSurfaceImpl_SetPrivateData,
4781 IWineD3DBaseSurfaceImpl_GetPrivateData,
4782 IWineD3DBaseSurfaceImpl_FreePrivateData,
4783 IWineD3DBaseSurfaceImpl_SetPriority,
4784 IWineD3DBaseSurfaceImpl_GetPriority,
4785 IWineD3DSurfaceImpl_PreLoad,
4786 IWineD3DSurfaceImpl_UnLoad,
4787 IWineD3DBaseSurfaceImpl_GetType,
4788 /* IWineD3DSurface */
4789 IWineD3DBaseSurfaceImpl_GetContainer,
4790 IWineD3DBaseSurfaceImpl_GetDesc,
4791 IWineD3DSurfaceImpl_LockRect,
4792 IWineD3DSurfaceImpl_UnlockRect,
4793 IWineD3DSurfaceImpl_GetDC,
4794 IWineD3DSurfaceImpl_ReleaseDC,
4795 IWineD3DSurfaceImpl_Flip,
4796 IWineD3DSurfaceImpl_Blt,
4797 IWineD3DBaseSurfaceImpl_GetBltStatus,
4798 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4799 IWineD3DBaseSurfaceImpl_IsLost,
4800 IWineD3DBaseSurfaceImpl_Restore,
4801 IWineD3DSurfaceImpl_BltFast,
4802 IWineD3DBaseSurfaceImpl_GetPalette,
4803 IWineD3DBaseSurfaceImpl_SetPalette,
4804 IWineD3DSurfaceImpl_RealizePalette,
4805 IWineD3DBaseSurfaceImpl_SetColorKey,
4806 IWineD3DBaseSurfaceImpl_GetPitch,
4807 IWineD3DSurfaceImpl_SetMem,
4808 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4809 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4810 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4811 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4812 IWineD3DBaseSurfaceImpl_SetClipper,
4813 IWineD3DBaseSurfaceImpl_GetClipper,
4814 /* Internal use: */
4815 IWineD3DSurfaceImpl_LoadTexture,
4816 IWineD3DSurfaceImpl_BindTexture,
4817 IWineD3DSurfaceImpl_SaveSnapshot,
4818 IWineD3DSurfaceImpl_SetContainer,
4819 IWineD3DBaseSurfaceImpl_GetData,
4820 IWineD3DSurfaceImpl_SetFormat,
4821 IWineD3DSurfaceImpl_PrivateSetup,
4822 IWineD3DSurfaceImpl_ModifyLocation,
4823 IWineD3DSurfaceImpl_LoadLocation,
4824 IWineD3DSurfaceImpl_GetImplType,
4825 IWineD3DSurfaceImpl_DrawOverlay
4826};
4827
4828static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4829/* Context activation is done by the caller. */
4830static void ffp_blit_free(IWineD3DDevice *iface) { }
4831
4832/* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4833/* Context activation is done by the caller. */
4834static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4835{
4836 BYTE table[256][4];
4837 BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4838
4839 d3dfmt_p8_init_palette(surface, table, colorkey_active);
4840
4841 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4842 ENTER_GL();
4843 GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4844 LEAVE_GL();
4845}
4846
4847/* Context activation is done by the caller. */
4848static HRESULT ffp_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4849{
4850 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4851 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4852 enum complex_fixup fixup = get_complex_fixup(surface->resource.format_desc->color_fixup);
4853
4854 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4855 * else the surface is converted in software at upload time in LoadLocation.
4856 */
4857 if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4858 ffp_blit_p8_upload_palette(surface, gl_info);
4859
4860 ENTER_GL();
4861 glEnable(surface->texture_target);
4862 checkGLcall("glEnable(surface->texture_target)");
4863 LEAVE_GL();
4864 return WINED3D_OK;
4865}
4866
4867/* Context activation is done by the caller. */
4868static void ffp_blit_unset(IWineD3DDevice *iface)
4869{
4870 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4871 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4872
4873 ENTER_GL();
4874 glDisable(GL_TEXTURE_2D);
4875 checkGLcall("glDisable(GL_TEXTURE_2D)");
4876 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4877 {
4878 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4879 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4880 }
4881 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4882 {
4883 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4884 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4885 }
4886 LEAVE_GL();
4887}
4888
4889static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4890 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4891 const struct wined3d_format_desc *src_format_desc,
4892 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4893 const struct wined3d_format_desc *dst_format_desc)
4894{
4895 enum complex_fixup src_fixup;
4896
4897 if (blit_op == BLIT_OP_COLOR_FILL)
4898 {
4899 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4900 {
4901 TRACE("Color fill not supported\n");
4902 return FALSE;
4903 }
4904
4905 return TRUE;
4906 }
4907
4908 src_fixup = get_complex_fixup(src_format_desc->color_fixup);
4909 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4910 {
4911 TRACE("Checking support for fixup:\n");
4912 dump_color_fixup_desc(src_format_desc->color_fixup);
4913 }
4914
4915 if (blit_op != BLIT_OP_BLIT)
4916 {
4917 TRACE("Unsupported blit_op=%d\n", blit_op);
4918 return FALSE;
4919 }
4920
4921 if (!is_identity_fixup(dst_format_desc->color_fixup))
4922 {
4923 TRACE("Destination fixups are not supported\n");
4924 return FALSE;
4925 }
4926
4927 if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4928 {
4929 TRACE("P8 fixup supported\n");
4930 return TRUE;
4931 }
4932
4933 /* We only support identity conversions. */
4934 if (is_identity_fixup(src_format_desc->color_fixup))
4935 {
4936 TRACE("[OK]\n");
4937 return TRUE;
4938 }
4939
4940 TRACE("[FAILED]\n");
4941 return FALSE;
4942}
4943
4944static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect, DWORD fill_color)
4945{
4946 return IWineD3DDeviceImpl_ClearSurface(device, dst_surface, 1 /* Number of rectangles */,
4947 (const WINED3DRECT*)dst_rect, WINED3DCLEAR_TARGET, fill_color, 0.0f /* Z */, 0 /* Stencil */);
4948}
4949
4950const struct blit_shader ffp_blit = {
4951 ffp_blit_alloc,
4952 ffp_blit_free,
4953 ffp_blit_set,
4954 ffp_blit_unset,
4955 ffp_blit_supported,
4956 ffp_blit_color_fill
4957};
4958
4959static HRESULT cpu_blit_alloc(IWineD3DDevice *iface)
4960{
4961 return WINED3D_OK;
4962}
4963
4964/* Context activation is done by the caller. */
4965static void cpu_blit_free(IWineD3DDevice *iface)
4966{
4967}
4968
4969/* Context activation is done by the caller. */
4970static HRESULT cpu_blit_set(IWineD3DDevice *iface, IWineD3DSurfaceImpl *surface)
4971{
4972 return WINED3D_OK;
4973}
4974
4975/* Context activation is done by the caller. */
4976static void cpu_blit_unset(IWineD3DDevice *iface)
4977{
4978}
4979
4980static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
4981 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
4982 const struct wined3d_format_desc *src_format_desc,
4983 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
4984 const struct wined3d_format_desc *dst_format_desc)
4985{
4986 if (blit_op == BLIT_OP_COLOR_FILL)
4987 {
4988 return TRUE;
4989 }
4990
4991 return FALSE;
4992}
4993
4994static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect, DWORD fill_color)
4995{
4996 WINEDDBLTFX BltFx;
4997 memset(&BltFx, 0, sizeof(BltFx));
4998 BltFx.dwSize = sizeof(BltFx);
4999 BltFx.u5.dwFillColor = color_convert_argb_to_fmt(fill_color, dst_surface->resource.format_desc->format);
5000 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect, NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
5001}
5002
5003const struct blit_shader cpu_blit = {
5004 cpu_blit_alloc,
5005 cpu_blit_free,
5006 cpu_blit_set,
5007 cpu_blit_unset,
5008 cpu_blit_supported,
5009 cpu_blit_color_fill
5010};
5011
5012static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum blit_operation blit_op,
5013 const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool,
5014 const struct wined3d_format_desc *src_format_desc,
5015 const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool,
5016 const struct wined3d_format_desc *dst_format_desc)
5017{
5018 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5019 return FALSE;
5020
5021 /* We only support blitting. Things like color keying / color fill should
5022 * be handled by other blitters.
5023 */
5024 if (blit_op != BLIT_OP_BLIT)
5025 return FALSE;
5026
5027 /* Source and/or destination need to be on the GL side */
5028 if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
5029 return FALSE;
5030
5031 if(!((src_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET))
5032 && ((dst_format_desc->Flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
5033 return FALSE;
5034
5035 if (!is_identity_fixup(src_format_desc->color_fixup) ||
5036 !is_identity_fixup(dst_format_desc->color_fixup))
5037 return FALSE;
5038
5039 if (!(src_format_desc->format == dst_format_desc->format
5040 || (is_identity_fixup(src_format_desc->color_fixup)
5041 && is_identity_fixup(dst_format_desc->color_fixup))))
5042 return FALSE;
5043
5044 return TRUE;
5045}
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