VirtualBox

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

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

wine: shared rc support more impl + fixes

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

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