VirtualBox

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

Last change on this file since 32501 was 32240, checked in by vboxsync, 15 years ago

crOpenGL/wddm: aero +20% speed up

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette