VirtualBox

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

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

Wddm/3D: fixed random screen corruption when running 3d apps under aero on win7

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