2 * IWineD3DSurface Implementation
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
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.
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.
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
31 #include "wine/port.h"
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface
);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
37 #define GLINFO_LOCATION (*gl_info)
39 static void surface_cleanup(IWineD3DSurfaceImpl
*This
)
41 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
42 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
43 struct wined3d_context
*context
= NULL
;
44 renderbuffer_entry_t
*entry
, *entry2
;
46 TRACE("(%p) : Cleaning up.\n", This
);
48 /* Need a context to destroy the texture. Use the currently active render
49 * target, but only if the primary render target exists. Otherwise
50 * lastActiveRenderTarget is garbage. When destroying the primary render
51 * target, Uninit3D() will activate a context before doing anything. */
52 if (device
->render_targets
&& device
->render_targets
[0])
54 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
59 if (This
->texture_name
)
61 /* Release the OpenGL texture. */
62 TRACE("Deleting texture %u.\n", This
->texture_name
);
63 glDeleteTextures(1, &This
->texture_name
);
66 if (This
->Flags
& SFLAG_PBO
)
69 GL_EXTCALL(glDeleteBuffersARB(1, &This
->pbo
));
72 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &This
->renderbuffers
, renderbuffer_entry_t
, entry
)
74 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
75 HeapFree(GetProcessHeap(), 0, entry
);
80 if (This
->Flags
& SFLAG_DIBSECTION
)
83 SelectObject(This
->hDC
, This
->dib
.holdbitmap
);
85 /* Release the DIB section. */
86 DeleteObject(This
->dib
.DIBsection
);
87 This
->dib
.bitmap_data
= NULL
;
88 This
->resource
.allocatedMemory
= NULL
;
91 if (This
->Flags
& SFLAG_USERPTR
) IWineD3DSurface_SetMem((IWineD3DSurface
*)This
, NULL
);
92 if (This
->overlay_dest
) list_remove(&This
->overlay_entry
);
94 HeapFree(GetProcessHeap(), 0, This
->palette9
);
96 resource_cleanup((IWineD3DResource
*)This
);
98 if (context
) context_release(context
);
101 UINT
surface_calculate_size(const struct wined3d_format_desc
*format_desc
, UINT alignment
, UINT width
, UINT height
)
105 if (format_desc
->format
== WINED3DFMT_UNKNOWN
)
109 else if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
111 UINT row_block_count
= (width
+ format_desc
->block_width
- 1) / format_desc
->block_width
;
112 UINT row_count
= (height
+ format_desc
->block_height
- 1) / format_desc
->block_height
;
113 size
= row_count
* row_block_count
* format_desc
->block_byte_count
;
117 /* The pitch is a multiple of 4 bytes. */
118 size
= height
* (((width
* format_desc
->byte_count
) + alignment
- 1) & ~(alignment
- 1));
121 if (format_desc
->heightscale
!= 0.0f
) size
*= format_desc
->heightscale
;
130 enum tex_types tex_type
;
131 GLfloat coords
[4][3];
142 static inline void cube_coords_float(const RECT
*r
, UINT w
, UINT h
, struct float_rect
*f
)
144 f
->l
= ((r
->left
* 2.0f
) / w
) - 1.0f
;
145 f
->t
= ((r
->top
* 2.0f
) / h
) - 1.0f
;
146 f
->r
= ((r
->right
* 2.0f
) / w
) - 1.0f
;
147 f
->b
= ((r
->bottom
* 2.0f
) / h
) - 1.0f
;
150 static void surface_get_blt_info(GLenum target
, const RECT
*rect_in
, GLsizei w
, GLsizei h
, struct blt_info
*info
)
152 GLfloat (*coords
)[3] = info
->coords
;
169 FIXME("Unsupported texture target %#x\n", target
);
170 /* Fall back to GL_TEXTURE_2D */
172 info
->binding
= GL_TEXTURE_BINDING_2D
;
173 info
->bind_target
= GL_TEXTURE_2D
;
174 info
->tex_type
= tex_2d
;
175 coords
[0][0] = (float)rect
.left
/ w
;
176 coords
[0][1] = (float)rect
.top
/ h
;
179 coords
[1][0] = (float)rect
.right
/ w
;
180 coords
[1][1] = (float)rect
.top
/ h
;
183 coords
[2][0] = (float)rect
.left
/ w
;
184 coords
[2][1] = (float)rect
.bottom
/ h
;
187 coords
[3][0] = (float)rect
.right
/ w
;
188 coords
[3][1] = (float)rect
.bottom
/ h
;
192 case GL_TEXTURE_RECTANGLE_ARB
:
193 info
->binding
= GL_TEXTURE_BINDING_RECTANGLE_ARB
;
194 info
->bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
195 info
->tex_type
= tex_rect
;
196 coords
[0][0] = rect
.left
; coords
[0][1] = rect
.top
; coords
[0][2] = 0.0f
;
197 coords
[1][0] = rect
.right
; coords
[1][1] = rect
.top
; coords
[1][2] = 0.0f
;
198 coords
[2][0] = rect
.left
; coords
[2][1] = rect
.bottom
; coords
[2][2] = 0.0f
;
199 coords
[3][0] = rect
.right
; coords
[3][1] = rect
.bottom
; coords
[3][2] = 0.0f
;
202 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
203 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
204 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
205 info
->tex_type
= tex_cube
;
206 cube_coords_float(&rect
, w
, h
, &f
);
208 coords
[0][0] = 1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = -f
.l
;
209 coords
[1][0] = 1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = -f
.r
;
210 coords
[2][0] = 1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = -f
.l
;
211 coords
[3][0] = 1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = -f
.r
;
214 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
215 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
216 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
217 info
->tex_type
= tex_cube
;
218 cube_coords_float(&rect
, w
, h
, &f
);
220 coords
[0][0] = -1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = f
.l
;
221 coords
[1][0] = -1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = f
.r
;
222 coords
[2][0] = -1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = f
.l
;
223 coords
[3][0] = -1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = f
.r
;
226 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
227 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
228 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
229 info
->tex_type
= tex_cube
;
230 cube_coords_float(&rect
, w
, h
, &f
);
232 coords
[0][0] = f
.l
; coords
[0][1] = 1.0f
; coords
[0][2] = f
.t
;
233 coords
[1][0] = f
.r
; coords
[1][1] = 1.0f
; coords
[1][2] = f
.t
;
234 coords
[2][0] = f
.l
; coords
[2][1] = 1.0f
; coords
[2][2] = f
.b
;
235 coords
[3][0] = f
.r
; coords
[3][1] = 1.0f
; coords
[3][2] = f
.b
;
238 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
239 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
240 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
241 info
->tex_type
= tex_cube
;
242 cube_coords_float(&rect
, w
, h
, &f
);
244 coords
[0][0] = f
.l
; coords
[0][1] = -1.0f
; coords
[0][2] = -f
.t
;
245 coords
[1][0] = f
.r
; coords
[1][1] = -1.0f
; coords
[1][2] = -f
.t
;
246 coords
[2][0] = f
.l
; coords
[2][1] = -1.0f
; coords
[2][2] = -f
.b
;
247 coords
[3][0] = f
.r
; coords
[3][1] = -1.0f
; coords
[3][2] = -f
.b
;
250 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
251 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
252 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
253 info
->tex_type
= tex_cube
;
254 cube_coords_float(&rect
, w
, h
, &f
);
256 coords
[0][0] = f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = 1.0f
;
257 coords
[1][0] = f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = 1.0f
;
258 coords
[2][0] = f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = 1.0f
;
259 coords
[3][0] = f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = 1.0f
;
262 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
263 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
264 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
265 info
->tex_type
= tex_cube
;
266 cube_coords_float(&rect
, w
, h
, &f
);
268 coords
[0][0] = -f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = -1.0f
;
269 coords
[1][0] = -f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = -1.0f
;
270 coords
[2][0] = -f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = -1.0f
;
271 coords
[3][0] = -f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = -1.0f
;
276 static inline void surface_get_rect(IWineD3DSurfaceImpl
*This
, const RECT
*rect_in
, RECT
*rect_out
)
279 *rect_out
= *rect_in
;
284 rect_out
->right
= This
->currentDesc
.Width
;
285 rect_out
->bottom
= This
->currentDesc
.Height
;
289 /* GL locking and context activation is done by the caller */
290 void draw_textured_quad(IWineD3DSurfaceImpl
*src_surface
, const RECT
*src_rect
, const RECT
*dst_rect
, WINED3DTEXTUREFILTERTYPE Filter
)
292 IWineD3DBaseTextureImpl
*texture
;
293 struct blt_info info
;
295 surface_get_blt_info(src_surface
->texture_target
, src_rect
, src_surface
->pow2Width
, src_surface
->pow2Height
, &info
);
297 glEnable(info
.bind_target
);
298 checkGLcall("glEnable(bind_target)");
300 /* Bind the texture */
301 glBindTexture(info
.bind_target
, src_surface
->texture_name
);
302 checkGLcall("glBindTexture");
304 /* Filtering for StretchRect */
305 glTexParameteri(info
.bind_target
, GL_TEXTURE_MAG_FILTER
,
306 wined3d_gl_mag_filter(magLookup
, Filter
));
307 checkGLcall("glTexParameteri");
308 glTexParameteri(info
.bind_target
, GL_TEXTURE_MIN_FILTER
,
309 wined3d_gl_min_mip_filter(minMipLookup
, Filter
, WINED3DTEXF_NONE
));
310 checkGLcall("glTexParameteri");
311 glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
312 glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
313 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
314 checkGLcall("glTexEnvi");
317 glBegin(GL_TRIANGLE_STRIP
);
318 glTexCoord3fv(info
.coords
[0]);
319 glVertex2i(dst_rect
->left
, dst_rect
->top
);
321 glTexCoord3fv(info
.coords
[1]);
322 glVertex2i(dst_rect
->right
, dst_rect
->top
);
324 glTexCoord3fv(info
.coords
[2]);
325 glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
327 glTexCoord3fv(info
.coords
[3]);
328 glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
331 /* Unbind the texture */
332 glBindTexture(info
.bind_target
, 0);
333 checkGLcall("glBindTexture(info->bind_target, 0)");
335 /* We changed the filtering settings on the texture. Inform the
336 * container about this to get the filters reset properly next draw. */
337 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)src_surface
, &IID_IWineD3DBaseTexture
, (void **)&texture
)))
339 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
340 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
341 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
342 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture
);
346 HRESULT
surface_init(IWineD3DSurfaceImpl
*surface
, WINED3DSURFTYPE surface_type
, UINT alignment
,
347 UINT width
, UINT height
, UINT level
, BOOL lockable
, BOOL discard
, WINED3DMULTISAMPLE_TYPE multisample_type
,
348 UINT multisample_quality
, IWineD3DDeviceImpl
*device
, DWORD usage
, WINED3DFORMAT format
,
349 WINED3DPOOL pool
, IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
351 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
352 const struct wined3d_format_desc
*format_desc
= getFormatDescEntry(format
, gl_info
);
353 void (*cleanup
)(IWineD3DSurfaceImpl
*This
);
354 unsigned int resource_size
;
357 if (multisample_quality
> 0)
359 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality
);
360 multisample_quality
= 0;
363 /* FIXME: Check that the format is supported by the device. */
365 resource_size
= surface_calculate_size(format_desc
, alignment
, width
, height
);
367 /* Look at the implementation and set the correct Vtable. */
368 switch (surface_type
)
371 surface
->lpVtbl
= &IWineD3DSurface_Vtbl
;
372 cleanup
= surface_cleanup
;
376 surface
->lpVtbl
= &IWineGDISurface_Vtbl
;
377 cleanup
= surface_gdi_cleanup
;
381 ERR("Requested unknown surface implementation %#x.\n", surface_type
);
382 return WINED3DERR_INVALIDCALL
;
385 hr
= resource_init((IWineD3DResource
*)surface
, WINED3DRTYPE_SURFACE
,
386 device
, resource_size
, usage
, format_desc
, pool
, parent
, parent_ops
);
389 WARN("Failed to initialize resource, returning %#x.\n", hr
);
393 /* "Standalone" surface. */
394 IWineD3DSurface_SetContainer((IWineD3DSurface
*)surface
, NULL
);
396 surface
->currentDesc
.Width
= width
;
397 surface
->currentDesc
.Height
= height
;
398 surface
->currentDesc
.MultiSampleType
= multisample_type
;
399 surface
->currentDesc
.MultiSampleQuality
= multisample_quality
;
400 surface
->texture_level
= level
;
401 list_init(&surface
->overlays
);
404 surface
->Flags
= SFLAG_NORMCOORD
; /* Default to normalized coords. */
405 if (discard
) surface
->Flags
|= SFLAG_DISCARD
;
406 if (lockable
|| format
== WINED3DFMT_D16_LOCKABLE
) surface
->Flags
|= SFLAG_LOCKABLE
;
408 /* Quick lockable sanity check.
409 * TODO: remove this after surfaces, usage and lockability have been debugged properly
410 * this function is too deep to need to care about things like this.
411 * Levels need to be checked too, since they all affect what can be done. */
414 case WINED3DPOOL_SCRATCH
:
417 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
418 "which are mutually exclusive, setting lockable to TRUE.\n");
423 case WINED3DPOOL_SYSTEMMEM
:
425 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
428 case WINED3DPOOL_MANAGED
:
429 if (usage
& WINED3DUSAGE_DYNAMIC
)
430 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
433 case WINED3DPOOL_DEFAULT
:
434 if (lockable
&& !(usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
)))
435 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
439 FIXME("Unknown pool %#x.\n", pool
);
443 if (usage
& WINED3DUSAGE_RENDERTARGET
&& pool
!= WINED3DPOOL_DEFAULT
)
445 FIXME("Trying to create a render target that isn't in the default pool.\n");
448 /* Mark the texture as dirty so that it gets loaded first time around. */
449 surface_add_dirty_rect((IWineD3DSurface
*)surface
, NULL
);
450 list_init(&surface
->renderbuffers
);
452 TRACE("surface %p, memory %p, size %u\n", surface
, surface
->resource
.allocatedMemory
, surface
->resource
.size
);
454 /* Call the private setup routine */
455 hr
= IWineD3DSurface_PrivateSetup((IWineD3DSurface
*)surface
);
458 ERR("Private setup failed, returning %#x\n", hr
);
466 static void surface_force_reload(IWineD3DSurface
*iface
)
468 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
470 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
473 void surface_set_texture_name(IWineD3DSurface
*iface
, GLuint new_name
, BOOL srgb
)
475 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
481 name
= &This
->texture_name_srgb
;
482 flag
= SFLAG_INSRGBTEX
;
486 name
= &This
->texture_name
;
487 flag
= SFLAG_INTEXTURE
;
490 TRACE("(%p) : setting texture name %u\n", This
, new_name
);
492 if (!*name
&& new_name
)
494 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
495 * surface has no texture name yet. See if we can get rid of this. */
496 if (This
->Flags
& flag
)
497 ERR("Surface has SFLAG_INTEXTURE set, but no texture name\n");
498 IWineD3DSurface_ModifyLocation(iface
, flag
, FALSE
);
502 surface_force_reload(iface
);
505 void surface_set_texture_target(IWineD3DSurface
*iface
, GLenum target
)
507 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
509 TRACE("(%p) : setting target %#x\n", This
, target
);
511 if (This
->texture_target
!= target
)
513 if (target
== GL_TEXTURE_RECTANGLE_ARB
)
515 This
->Flags
&= ~SFLAG_NORMCOORD
;
517 else if (This
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
519 This
->Flags
|= SFLAG_NORMCOORD
;
522 This
->texture_target
= target
;
523 surface_force_reload(iface
);
526 /* Context activation is done by the caller. */
527 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl
*This
, BOOL srgb
) {
528 DWORD active_sampler
;
530 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
531 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
532 * gl states. The current texture unit should always be a valid one.
534 * To be more specific, this is tricky because we can implicitly be called
535 * from sampler() in state.c. This means we can't touch anything other than
536 * whatever happens to be the currently active texture, or we would risk
537 * marking already applied sampler states dirty again.
539 * TODO: Track the current active texture per GL context instead of using glGet
541 GLint active_texture
;
543 glGetIntegerv(GL_ACTIVE_TEXTURE
, &active_texture
);
545 active_sampler
= This
->resource
.device
->rev_tex_unit_map
[active_texture
- GL_TEXTURE0_ARB
];
547 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
549 IWineD3DDeviceImpl_MarkStateDirty(This
->resource
.device
, STATE_SAMPLER(active_sampler
));
551 IWineD3DSurface_BindTexture((IWineD3DSurface
*)This
, srgb
);
554 /* This function checks if the primary render target uses the 8bit paletted format. */
555 static BOOL
primary_render_target_is_p8(IWineD3DDeviceImpl
*device
)
557 if (device
->render_targets
&& device
->render_targets
[0]) {
558 IWineD3DSurfaceImpl
* render_target
= (IWineD3DSurfaceImpl
*)device
->render_targets
[0];
559 if ((render_target
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
560 && (render_target
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
))
566 /* This call just downloads data, the caller is responsible for binding the
567 * correct texture. */
568 /* Context activation is done by the caller. */
569 static void surface_download_data(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
)
571 const struct wined3d_format_desc
*format_desc
= This
->resource
.format_desc
;
573 /* Only support read back of converted P8 surfaces */
574 if (This
->Flags
& SFLAG_CONVERTED
&& format_desc
->format
!= WINED3DFMT_P8_UINT
)
576 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(format_desc
->format
));
582 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
584 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
585 This
, This
->texture_level
, format_desc
->glFormat
, format_desc
->glType
,
586 This
->resource
.allocatedMemory
);
588 if (This
->Flags
& SFLAG_PBO
)
590 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
591 checkGLcall("glBindBufferARB");
592 GL_EXTCALL(glGetCompressedTexImageARB(This
->texture_target
, This
->texture_level
, NULL
));
593 checkGLcall("glGetCompressedTexImageARB");
594 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
595 checkGLcall("glBindBufferARB");
599 GL_EXTCALL(glGetCompressedTexImageARB(This
->texture_target
,
600 This
->texture_level
, This
->resource
.allocatedMemory
));
601 checkGLcall("glGetCompressedTexImageARB");
607 GLenum format
= format_desc
->glFormat
;
608 GLenum type
= format_desc
->glType
;
612 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
613 if (format_desc
->format
== WINED3DFMT_P8_UINT
&& primary_render_target_is_p8(This
->resource
.device
))
616 type
= GL_UNSIGNED_BYTE
;
619 if (This
->Flags
& SFLAG_NONPOW2
) {
620 unsigned char alignment
= This
->resource
.device
->surface_alignment
;
621 src_pitch
= format_desc
->byte_count
* This
->pow2Width
;
622 dst_pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*) This
);
623 src_pitch
= (src_pitch
+ alignment
- 1) & ~(alignment
- 1);
624 mem
= HeapAlloc(GetProcessHeap(), 0, src_pitch
* This
->pow2Height
);
626 mem
= This
->resource
.allocatedMemory
;
629 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
630 This
, This
->texture_level
, format
, type
, mem
);
632 if(This
->Flags
& SFLAG_PBO
) {
633 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
634 checkGLcall("glBindBufferARB");
636 glGetTexImage(This
->texture_target
, This
->texture_level
, format
, type
, NULL
);
637 checkGLcall("glGetTexImage");
639 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
640 checkGLcall("glBindBufferARB");
642 glGetTexImage(This
->texture_target
, This
->texture_level
, format
, type
, mem
);
643 checkGLcall("glGetTexImage");
647 if (This
->Flags
& SFLAG_NONPOW2
) {
648 const BYTE
*src_data
;
652 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
653 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
654 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
656 * We're doing this...
658 * instead of boxing the texture :
659 * |<-texture width ->| -->pow2width| /\
660 * |111111111111111111| | |
661 * |222 Texture 222222| boxed empty | texture height
662 * |3333 Data 33333333| | |
663 * |444444444444444444| | \/
664 * ----------------------------------- |
665 * | boxed empty | boxed empty | pow2height
667 * -----------------------------------
670 * we're repacking the data to the expected texture width
672 * |<-texture width ->| -->pow2width| /\
673 * |111111111111111111222222222222222| |
674 * |222333333333333333333444444444444| texture height
678 * | empty | pow2height
680 * -----------------------------------
684 * |<-texture width ->| /\
685 * |111111111111111111|
686 * |222222222222222222|texture height
687 * |333333333333333333|
688 * |444444444444444444| \/
689 * --------------------
691 * this also means that any references to allocatedMemory should work with the data as if were a
692 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
694 * internally the texture is still stored in a boxed format so any references to textureName will
695 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
697 * Performance should not be an issue, because applications normally do not lock the surfaces when
698 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
699 * and doesn't have to be re-read.
702 dst_data
= This
->resource
.allocatedMemory
;
703 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This
, src_pitch
, dst_pitch
);
704 for (y
= 1 ; y
< This
->currentDesc
.Height
; y
++) {
705 /* skip the first row */
706 src_data
+= src_pitch
;
707 dst_data
+= dst_pitch
;
708 memcpy(dst_data
, src_data
, dst_pitch
);
711 HeapFree(GetProcessHeap(), 0, mem
);
715 /* Surface has now been downloaded */
716 This
->Flags
|= SFLAG_INSYSMEM
;
719 /* This call just uploads data, the caller is responsible for binding the
720 * correct texture. */
721 /* Context activation is done by the caller. */
722 static void surface_upload_data(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
723 const struct wined3d_format_desc
*format_desc
, BOOL srgb
, const GLvoid
*data
)
725 GLsizei width
= This
->currentDesc
.Width
;
726 GLsizei height
= This
->currentDesc
.Height
;
731 internal
= format_desc
->glGammaInternal
;
733 else if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
&& surface_is_offscreen(This
))
735 internal
= format_desc
->rtInternal
;
739 internal
= format_desc
->glInternal
;
742 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
743 This
, internal
, width
, height
, format_desc
->glFormat
, format_desc
->glType
, data
);
744 TRACE("target %#x, level %u, resource size %u.\n",
745 This
->texture_target
, This
->texture_level
, This
->resource
.size
);
747 if (format_desc
->heightscale
!= 1.0f
&& format_desc
->heightscale
!= 0.0f
) height
*= format_desc
->heightscale
;
751 if (This
->Flags
& SFLAG_PBO
)
753 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
754 checkGLcall("glBindBufferARB");
756 TRACE("(%p) pbo: %#x, data: %p.\n", This
, This
->pbo
, data
);
760 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
762 TRACE("Calling glCompressedTexSubImage2DARB.\n");
764 GL_EXTCALL(glCompressedTexSubImage2DARB(This
->texture_target
, This
->texture_level
,
765 0, 0, width
, height
, internal
, This
->resource
.size
, data
));
766 checkGLcall("glCompressedTexSubImage2DARB");
770 TRACE("Calling glTexSubImage2D.\n");
772 glTexSubImage2D(This
->texture_target
, This
->texture_level
,
773 0, 0, width
, height
, format_desc
->glFormat
, format_desc
->glType
, data
);
774 checkGLcall("glTexSubImage2D");
777 if (This
->Flags
& SFLAG_PBO
)
779 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
780 checkGLcall("glBindBufferARB");
785 if (gl_info
->quirks
& WINED3D_QUIRK_FBO_TEX_UPDATE
)
787 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
790 for (i
= 0; i
< device
->numContexts
; ++i
)
792 context_surface_update(device
->contexts
[i
], This
);
797 /* This call just allocates the texture, the caller is responsible for binding
798 * the correct texture. */
799 /* Context activation is done by the caller. */
800 static void surface_allocate_surface(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
801 const struct wined3d_format_desc
*format_desc
, BOOL srgb
)
803 BOOL enable_client_storage
= FALSE
;
804 GLsizei width
= This
->pow2Width
;
805 GLsizei height
= This
->pow2Height
;
806 const BYTE
*mem
= NULL
;
811 internal
= format_desc
->glGammaInternal
;
813 else if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
&& surface_is_offscreen(This
))
815 internal
= format_desc
->rtInternal
;
819 internal
= format_desc
->glInternal
;
822 if (format_desc
->heightscale
!= 1.0f
&& format_desc
->heightscale
!= 0.0f
) height
*= format_desc
->heightscale
;
824 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",
825 This
, This
->texture_target
, This
->texture_level
, debug_d3dformat(format_desc
->format
),
826 internal
, width
, height
, format_desc
->glFormat
, format_desc
->glType
);
830 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
832 if(This
->Flags
& (SFLAG_NONPOW2
| SFLAG_DIBSECTION
| SFLAG_CONVERTED
) || This
->resource
.allocatedMemory
== NULL
) {
833 /* In some cases we want to disable client storage.
834 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
835 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
836 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
837 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
839 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
840 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
841 This
->Flags
&= ~SFLAG_CLIENT
;
842 enable_client_storage
= TRUE
;
844 This
->Flags
|= SFLAG_CLIENT
;
846 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
847 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
849 mem
= (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
853 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
&& mem
)
855 GL_EXTCALL(glCompressedTexImage2DARB(This
->texture_target
, This
->texture_level
,
856 internal
, width
, height
, 0, This
->resource
.size
, mem
));
860 glTexImage2D(This
->texture_target
, This
->texture_level
,
861 internal
, width
, height
, 0, format_desc
->glFormat
, format_desc
->glType
, mem
);
862 checkGLcall("glTexImage2D");
865 if(enable_client_storage
) {
866 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
867 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
872 /* In D3D the depth stencil dimensions have to be greater than or equal to the
873 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
874 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
875 /* GL locking is done by the caller */
876 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl
*surface
, unsigned int width
, unsigned int height
)
878 const struct wined3d_gl_info
*gl_info
= &surface
->resource
.device
->adapter
->gl_info
;
879 renderbuffer_entry_t
*entry
;
880 GLuint renderbuffer
= 0;
881 unsigned int src_width
, src_height
;
883 src_width
= surface
->pow2Width
;
884 src_height
= surface
->pow2Height
;
886 /* A depth stencil smaller than the render target is not valid */
887 if (width
> src_width
|| height
> src_height
) return;
889 /* Remove any renderbuffer set if the sizes match */
890 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
]
891 || (width
== src_width
&& height
== src_height
))
893 surface
->current_renderbuffer
= NULL
;
897 /* Look if we've already got a renderbuffer of the correct dimensions */
898 LIST_FOR_EACH_ENTRY(entry
, &surface
->renderbuffers
, renderbuffer_entry_t
, entry
)
900 if (entry
->width
== width
&& entry
->height
== height
)
902 renderbuffer
= entry
->id
;
903 surface
->current_renderbuffer
= entry
;
910 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
911 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
912 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
,
913 surface
->resource
.format_desc
->glInternal
, width
, height
);
915 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t
));
916 entry
->width
= width
;
917 entry
->height
= height
;
918 entry
->id
= renderbuffer
;
919 list_add_head(&surface
->renderbuffers
, &entry
->entry
);
921 surface
->current_renderbuffer
= entry
;
924 checkGLcall("set_compatible_renderbuffer");
927 GLenum
surface_get_gl_buffer(IWineD3DSurface
*iface
)
929 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
930 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)This
->container
;
932 TRACE("iface %p.\n", iface
);
934 if (!(This
->Flags
& SFLAG_SWAPCHAIN
))
936 ERR("Surface %p is not on a swapchain.\n", iface
);
940 if (swapchain
->backBuffer
&& swapchain
->backBuffer
[0] == iface
)
942 if (swapchain
->render_to_fbo
)
944 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
945 return GL_COLOR_ATTACHMENT0
;
947 TRACE("Returning GL_BACK\n");
950 else if (swapchain
->frontBuffer
== iface
)
952 TRACE("Returning GL_FRONT\n");
956 FIXME("Higher back buffer, returning GL_BACK\n");
960 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
961 void surface_add_dirty_rect(IWineD3DSurface
*iface
, const RECT
*dirty_rect
)
963 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
964 IWineD3DBaseTexture
*baseTexture
= NULL
;
966 if (!(This
->Flags
& SFLAG_INSYSMEM
) && (This
->Flags
& SFLAG_INTEXTURE
))
967 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, NULL
/* no partial locking for textures yet */);
969 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSYSMEM
, TRUE
);
972 This
->dirtyRect
.left
= min(This
->dirtyRect
.left
, dirty_rect
->left
);
973 This
->dirtyRect
.top
= min(This
->dirtyRect
.top
, dirty_rect
->top
);
974 This
->dirtyRect
.right
= max(This
->dirtyRect
.right
, dirty_rect
->right
);
975 This
->dirtyRect
.bottom
= max(This
->dirtyRect
.bottom
, dirty_rect
->bottom
);
979 This
->dirtyRect
.left
= 0;
980 This
->dirtyRect
.top
= 0;
981 This
->dirtyRect
.right
= This
->currentDesc
.Width
;
982 This
->dirtyRect
.bottom
= This
->currentDesc
.Height
;
985 TRACE("(%p) : Dirty: yes, Rect:(%d, %d, %d, %d)\n", This
, This
->dirtyRect
.left
,
986 This
->dirtyRect
.top
, This
->dirtyRect
.right
, This
->dirtyRect
.bottom
);
988 /* if the container is a basetexture then mark it dirty. */
989 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&baseTexture
)))
991 TRACE("Passing to container\n");
992 IWineD3DBaseTexture_SetDirty(baseTexture
, TRUE
);
993 IWineD3DBaseTexture_Release(baseTexture
);
997 static BOOL
surface_convert_color_to_argb(IWineD3DSurfaceImpl
*This
, DWORD color
, DWORD
*argb_color
)
999 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1001 switch(This
->resource
.format_desc
->format
)
1003 case WINED3DFMT_P8_UINT
:
1007 if (primary_render_target_is_p8(device
))
1008 alpha
= color
<< 24;
1012 if (This
->palette
) {
1013 *argb_color
= (alpha
|
1014 (This
->palette
->palents
[color
].peRed
<< 16) |
1015 (This
->palette
->palents
[color
].peGreen
<< 8) |
1016 (This
->palette
->palents
[color
].peBlue
));
1018 *argb_color
= alpha
;
1023 case WINED3DFMT_B5G6R5_UNORM
:
1025 if (color
== 0xFFFF) {
1026 *argb_color
= 0xFFFFFFFF;
1028 *argb_color
= ((0xFF000000) |
1029 ((color
& 0xF800) << 8) |
1030 ((color
& 0x07E0) << 5) |
1031 ((color
& 0x001F) << 3));
1036 case WINED3DFMT_B8G8R8_UNORM
:
1037 case WINED3DFMT_B8G8R8X8_UNORM
:
1038 *argb_color
= 0xFF000000 | color
;
1041 case WINED3DFMT_B8G8R8A8_UNORM
:
1042 *argb_color
= color
;
1046 ERR("Unhandled conversion from %s to ARGB!\n", debug_d3dformat(This
->resource
.format_desc
->format
));
1052 static ULONG WINAPI
IWineD3DSurfaceImpl_Release(IWineD3DSurface
*iface
)
1054 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1055 ULONG ref
= InterlockedDecrement(&This
->resource
.ref
);
1056 TRACE("(%p) : Releasing from %d\n", This
, ref
+ 1);
1060 surface_cleanup(This
);
1061 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
1063 TRACE("(%p) Released.\n", This
);
1064 HeapFree(GetProcessHeap(), 0, This
);
1070 /* ****************************************************
1071 IWineD3DSurface IWineD3DResource parts follow
1072 **************************************************** */
1074 void surface_internal_preload(IWineD3DSurface
*iface
, enum WINED3DSRGB srgb
)
1076 /* TODO: check for locks */
1077 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1078 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1079 IWineD3DBaseTexture
*baseTexture
= NULL
;
1081 TRACE("(%p)Checking to see if the container is a base texture\n", This
);
1082 if (IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&baseTexture
) == WINED3D_OK
) {
1083 IWineD3DBaseTextureImpl
*tex_impl
= (IWineD3DBaseTextureImpl
*) baseTexture
;
1084 TRACE("Passing to container\n");
1085 tex_impl
->baseTexture
.internal_preload(baseTexture
, srgb
);
1086 IWineD3DBaseTexture_Release(baseTexture
);
1088 struct wined3d_context
*context
= NULL
;
1090 TRACE("(%p) : About to load surface\n", This
);
1092 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
1094 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
1095 || This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT_A8_UNORM
)
1097 if(palette9_changed(This
)) {
1098 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1099 /* TODO: This is not necessarily needed with hw palettized texture support */
1100 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, NULL
);
1101 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1102 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INTEXTURE
, FALSE
);
1106 IWineD3DSurface_LoadTexture(iface
, srgb
== SRGB_SRGB
? TRUE
: FALSE
);
1108 if (This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
1109 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1113 glPrioritizeTextures(1, &This
->texture_name
, &tmp
);
1117 if (context
) context_release(context
);
1121 static void WINAPI
IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface
*iface
) {
1122 surface_internal_preload(iface
, SRGB_ANY
);
1125 /* Context activation is done by the caller. */
1126 static void surface_remove_pbo(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
)
1128 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap() ,0 , This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1129 This
->resource
.allocatedMemory
=
1130 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1133 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1134 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1135 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0, This
->resource
.size
, This
->resource
.allocatedMemory
));
1136 checkGLcall("glGetBufferSubDataARB");
1137 GL_EXTCALL(glDeleteBuffersARB(1, &This
->pbo
));
1138 checkGLcall("glDeleteBuffersARB");
1142 This
->Flags
&= ~SFLAG_PBO
;
1145 BOOL
surface_init_sysmem(IWineD3DSurface
*iface
)
1147 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
1149 if(!This
->resource
.allocatedMemory
)
1151 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1152 if(!This
->resource
.heapMemory
)
1154 ERR("Out of memory\n");
1157 This
->resource
.allocatedMemory
=
1158 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1162 memset(This
->resource
.allocatedMemory
, 0, This
->resource
.size
);
1165 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSYSMEM
, TRUE
);
1169 static void WINAPI
IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface
*iface
) {
1170 IWineD3DBaseTexture
*texture
= NULL
;
1171 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
1172 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1173 const struct wined3d_gl_info
*gl_info
;
1174 renderbuffer_entry_t
*entry
, *entry2
;
1175 struct wined3d_context
*context
;
1177 TRACE("(%p)\n", iface
);
1179 if(This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
1180 /* Default pool resources are supposed to be destroyed before Reset is called.
1181 * Implicit resources stay however. So this means we have an implicit render target
1182 * or depth stencil. The content may be destroyed, but we still have to tear down
1183 * opengl resources, so we cannot leave early.
1185 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1186 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1187 * or the depth stencil into an FBO the texture or render buffer will be removed
1188 * and all flags get lost
1190 surface_init_sysmem(iface
);
1192 /* Load the surface into system memory */
1193 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, NULL
);
1194 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INDRAWABLE
, FALSE
);
1196 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INTEXTURE
, FALSE
);
1197 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSRGBTEX
, FALSE
);
1198 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
1200 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
1201 gl_info
= context
->gl_info
;
1203 /* Destroy PBOs, but load them into real sysmem before */
1204 if (This
->Flags
& SFLAG_PBO
)
1205 surface_remove_pbo(This
, gl_info
);
1207 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1208 * all application-created targets the application has to release the surface
1209 * before calling _Reset
1211 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &This
->renderbuffers
, renderbuffer_entry_t
, entry
) {
1213 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1215 list_remove(&entry
->entry
);
1216 HeapFree(GetProcessHeap(), 0, entry
);
1218 list_init(&This
->renderbuffers
);
1219 This
->current_renderbuffer
= NULL
;
1221 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
1224 IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **) &texture
);
1227 glDeleteTextures(1, &This
->texture_name
);
1228 This
->texture_name
= 0;
1229 glDeleteTextures(1, &This
->texture_name_srgb
);
1230 This
->texture_name_srgb
= 0;
1233 IWineD3DBaseTexture_Release(texture
);
1236 context_release(context
);
1239 /* ******************************************************
1240 IWineD3DSurface IWineD3DSurface parts follow
1241 ****************************************************** */
1243 /* Read the framebuffer back into the surface */
1244 static void read_from_framebuffer(IWineD3DSurfaceImpl
*This
, const RECT
*rect
, void *dest
, UINT pitch
)
1246 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1247 const struct wined3d_gl_info
*gl_info
;
1248 struct wined3d_context
*context
;
1252 BYTE
*row
, *top
, *bottom
;
1256 BOOL srcIsUpsideDown
;
1261 if(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
) {
1262 static BOOL warned
= FALSE
;
1264 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1270 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
1271 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
1272 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
1273 * context->last_was_blit set on the unlock.
1275 context
= context_acquire(device
, (IWineD3DSurface
*)This
, CTXUSAGE_BLIT
);
1276 gl_info
= context
->gl_info
;
1280 /* Select the correct read buffer, and give some debug output.
1281 * There is no need to keep track of the current read buffer or reset it, every part of the code
1282 * that reads sets the read buffer as desired.
1284 if (surface_is_offscreen(This
))
1286 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1287 * Read from the back buffer
1289 TRACE("Locking offscreen render target\n");
1290 glReadBuffer(device
->offscreenBuffer
);
1291 srcIsUpsideDown
= TRUE
;
1295 /* Onscreen surfaces are always part of a swapchain */
1296 GLenum buffer
= surface_get_gl_buffer((IWineD3DSurface
*)This
);
1297 TRACE("Locking %#x buffer\n", buffer
);
1298 glReadBuffer(buffer
);
1299 checkGLcall("glReadBuffer");
1300 srcIsUpsideDown
= FALSE
;
1303 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1305 local_rect
.left
= 0;
1307 local_rect
.right
= This
->currentDesc
.Width
;
1308 local_rect
.bottom
= This
->currentDesc
.Height
;
1312 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1314 switch(This
->resource
.format_desc
->format
)
1316 case WINED3DFMT_P8_UINT
:
1318 if (primary_render_target_is_p8(device
))
1320 /* In case of P8 render targets the index is stored in the alpha component */
1322 type
= GL_UNSIGNED_BYTE
;
1324 bpp
= This
->resource
.format_desc
->byte_count
;
1326 /* GL can't return palettized data, so read ARGB pixels into a
1327 * separate block of memory and convert them into palettized format
1328 * in software. Slow, but if the app means to use palettized render
1329 * targets and locks it...
1331 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1332 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1333 * for the color channels when palettizing the colors.
1336 type
= GL_UNSIGNED_BYTE
;
1338 mem
= HeapAlloc(GetProcessHeap(), 0, This
->resource
.size
* 3);
1340 ERR("Out of memory\n");
1344 bpp
= This
->resource
.format_desc
->byte_count
* 3;
1351 fmt
= This
->resource
.format_desc
->glFormat
;
1352 type
= This
->resource
.format_desc
->glType
;
1353 bpp
= This
->resource
.format_desc
->byte_count
;
1356 if(This
->Flags
& SFLAG_PBO
) {
1357 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
1358 checkGLcall("glBindBufferARB");
1360 ERR("mem not null for pbo -- unexpected\n");
1365 /* Save old pixel store pack state */
1366 glGetIntegerv(GL_PACK_ROW_LENGTH
, &rowLen
);
1367 checkGLcall("glGetIntegerv");
1368 glGetIntegerv(GL_PACK_SKIP_PIXELS
, &skipPix
);
1369 checkGLcall("glGetIntegerv");
1370 glGetIntegerv(GL_PACK_SKIP_ROWS
, &skipRow
);
1371 checkGLcall("glGetIntegerv");
1373 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1374 glPixelStorei(GL_PACK_ROW_LENGTH
, This
->currentDesc
.Width
);
1375 checkGLcall("glPixelStorei");
1376 glPixelStorei(GL_PACK_SKIP_PIXELS
, local_rect
.left
);
1377 checkGLcall("glPixelStorei");
1378 glPixelStorei(GL_PACK_SKIP_ROWS
, local_rect
.top
);
1379 checkGLcall("glPixelStorei");
1381 glReadPixels(local_rect
.left
, (!srcIsUpsideDown
) ? (This
->currentDesc
.Height
- local_rect
.bottom
) : local_rect
.top
,
1382 local_rect
.right
- local_rect
.left
,
1383 local_rect
.bottom
- local_rect
.top
,
1385 checkGLcall("glReadPixels");
1387 /* Reset previous pixel store pack state */
1388 glPixelStorei(GL_PACK_ROW_LENGTH
, rowLen
);
1389 checkGLcall("glPixelStorei");
1390 glPixelStorei(GL_PACK_SKIP_PIXELS
, skipPix
);
1391 checkGLcall("glPixelStorei");
1392 glPixelStorei(GL_PACK_SKIP_ROWS
, skipRow
);
1393 checkGLcall("glPixelStorei");
1395 if(This
->Flags
& SFLAG_PBO
) {
1396 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
1397 checkGLcall("glBindBufferARB");
1399 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1400 * to get a pointer to it and perform the flipping in software. This is a lot
1401 * faster than calling glReadPixels for each line. In case we want more speed
1402 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1403 if(!srcIsUpsideDown
) {
1404 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1405 checkGLcall("glBindBufferARB");
1407 mem
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, GL_READ_WRITE_ARB
));
1408 checkGLcall("glMapBufferARB");
1412 /* TODO: Merge this with the palettization loop below for P8 targets */
1413 if(!srcIsUpsideDown
) {
1415 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1416 Flip the lines in software */
1417 len
= (local_rect
.right
- local_rect
.left
) * bpp
;
1418 off
= local_rect
.left
* bpp
;
1420 row
= HeapAlloc(GetProcessHeap(), 0, len
);
1422 ERR("Out of memory\n");
1423 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
) HeapFree(GetProcessHeap(), 0, mem
);
1428 top
= mem
+ pitch
* local_rect
.top
;
1429 bottom
= mem
+ pitch
* (local_rect
.bottom
- 1);
1430 for(i
= 0; i
< (local_rect
.bottom
- local_rect
.top
) / 2; i
++) {
1431 memcpy(row
, top
+ off
, len
);
1432 memcpy(top
+ off
, bottom
+ off
, len
);
1433 memcpy(bottom
+ off
, row
, len
);
1437 HeapFree(GetProcessHeap(), 0, row
);
1439 /* Unmap the temp PBO buffer */
1440 if(This
->Flags
& SFLAG_PBO
) {
1441 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
1442 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1447 context_release(context
);
1449 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1450 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1451 * the same color but we have no choice.
1452 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1454 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
&& !primary_render_target_is_p8(device
))
1456 const PALETTEENTRY
*pal
= NULL
;
1457 DWORD width
= pitch
/ 3;
1461 pal
= This
->palette
->palents
;
1463 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1464 HeapFree(GetProcessHeap(), 0, mem
);
1468 for(y
= local_rect
.top
; y
< local_rect
.bottom
; y
++) {
1469 for(x
= local_rect
.left
; x
< local_rect
.right
; x
++) {
1470 /* start lines pixels */
1471 const BYTE
*blue
= mem
+ y
* pitch
+ x
* (sizeof(BYTE
) * 3);
1472 const BYTE
*green
= blue
+ 1;
1473 const BYTE
*red
= green
+ 1;
1475 for(c
= 0; c
< 256; c
++) {
1476 if(*red
== pal
[c
].peRed
&&
1477 *green
== pal
[c
].peGreen
&&
1478 *blue
== pal
[c
].peBlue
)
1480 *((BYTE
*) dest
+ y
* width
+ x
) = c
;
1486 HeapFree(GetProcessHeap(), 0, mem
);
1490 /* Read the framebuffer contents into a texture */
1491 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl
*This
, BOOL srgb
)
1493 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1494 const struct wined3d_gl_info
*gl_info
;
1495 struct wined3d_context
*context
;
1497 BOOL alloc_flag
= srgb
? SFLAG_SRGBALLOCATED
: SFLAG_ALLOCATED
;
1499 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1500 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1501 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1503 context
= context_acquire(device
, (IWineD3DSurface
*) This
, CTXUSAGE_RESOURCELOAD
);
1504 gl_info
= context
->gl_info
;
1506 surface_bind_and_dirtify(This
, srgb
);
1509 glGetIntegerv(GL_READ_BUFFER
, &prevRead
);
1512 /* Select the correct read buffer, and give some debug output.
1513 * There is no need to keep track of the current read buffer or reset it, every part of the code
1514 * that reads sets the read buffer as desired.
1516 if (!surface_is_offscreen(This
))
1518 GLenum buffer
= surface_get_gl_buffer((IWineD3DSurface
*)This
);
1519 TRACE("Locking %#x buffer\n", buffer
);
1522 glReadBuffer(buffer
);
1523 checkGLcall("glReadBuffer");
1528 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1529 * Read from the back buffer
1531 TRACE("Locking offscreen render target\n");
1533 glReadBuffer(device
->offscreenBuffer
);
1534 checkGLcall("glReadBuffer");
1538 if (!(This
->Flags
& alloc_flag
))
1540 surface_allocate_surface(This
, gl_info
, This
->resource
.format_desc
, srgb
);
1541 This
->Flags
|= alloc_flag
;
1545 /* If !SrcIsUpsideDown we should flip the surface.
1546 * This can be done using glCopyTexSubImage2D but this
1547 * is VERY slow, so don't do that. We should prevent
1548 * this code from getting called in such cases or perhaps
1549 * we can use FBOs */
1551 glCopyTexSubImage2D(This
->texture_target
, This
->texture_level
,
1552 0, 0, 0, 0, This
->currentDesc
.Width
, This
->currentDesc
.Height
);
1553 checkGLcall("glCopyTexSubImage2D");
1555 glReadBuffer(prevRead
);
1556 checkGLcall("glReadBuffer");
1560 context_release(context
);
1562 TRACE("Updated target %d\n", This
->texture_target
);
1565 /* Context activation is done by the caller. */
1566 void surface_prepare_texture(IWineD3DSurfaceImpl
*surface
, const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
1568 DWORD alloc_flag
= srgb
? SFLAG_SRGBALLOCATED
: SFLAG_ALLOCATED
;
1569 CONVERT_TYPES convert
;
1570 struct wined3d_format_desc desc
;
1572 if (surface
->Flags
& alloc_flag
) return;
1574 d3dfmt_get_conv(surface
, TRUE
, TRUE
, &desc
, &convert
);
1575 if(convert
!= NO_CONVERSION
) surface
->Flags
|= SFLAG_CONVERTED
;
1576 else surface
->Flags
&= ~SFLAG_CONVERTED
;
1578 surface_bind_and_dirtify(surface
, srgb
);
1579 surface_allocate_surface(surface
, gl_info
, &desc
, srgb
);
1580 surface
->Flags
|= alloc_flag
;
1583 static void surface_prepare_system_memory(IWineD3DSurfaceImpl
*This
)
1585 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1586 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1588 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1589 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1592 if(!(This
->Flags
& SFLAG_DYNLOCK
)) {
1594 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1595 if(This
->lockCount
> MAXLOCKCOUNT
) {
1596 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1597 This
->Flags
|= SFLAG_DYNLOCK
;
1601 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1602 * Also don't create a PBO for systemmem surfaces.
1604 if (gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
] && (This
->Flags
& SFLAG_DYNLOCK
)
1605 && !(This
->Flags
& (SFLAG_PBO
| SFLAG_CONVERTED
| SFLAG_NONPOW2
))
1606 && (This
->resource
.pool
!= WINED3DPOOL_SYSTEMMEM
))
1609 struct wined3d_context
*context
;
1611 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
1614 GL_EXTCALL(glGenBuffersARB(1, &This
->pbo
));
1615 error
= glGetError();
1616 if(This
->pbo
== 0 || error
!= GL_NO_ERROR
) {
1617 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error
), error
);
1620 TRACE("Attaching pbo=%#x to (%p)\n", This
->pbo
, This
);
1622 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1623 checkGLcall("glBindBufferARB");
1625 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->resource
.size
+ 4, This
->resource
.allocatedMemory
, GL_STREAM_DRAW_ARB
));
1626 checkGLcall("glBufferDataARB");
1628 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1629 checkGLcall("glBindBufferARB");
1631 /* We don't need the system memory anymore and we can't even use it for PBOs */
1632 if(!(This
->Flags
& SFLAG_CLIENT
)) {
1633 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
1634 This
->resource
.heapMemory
= NULL
;
1636 This
->resource
.allocatedMemory
= NULL
;
1637 This
->Flags
|= SFLAG_PBO
;
1639 context_release(context
);
1641 else if (!(This
->resource
.allocatedMemory
|| This
->Flags
& SFLAG_PBO
))
1643 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1646 if(!This
->resource
.heapMemory
) {
1647 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap() ,0 , This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1649 This
->resource
.allocatedMemory
=
1650 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1651 if(This
->Flags
& SFLAG_INSYSMEM
) {
1652 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1657 static HRESULT WINAPI
IWineD3DSurfaceImpl_LockRect(IWineD3DSurface
*iface
, WINED3DLOCKED_RECT
* pLockedRect
, CONST RECT
* pRect
, DWORD Flags
) {
1658 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1659 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1660 const RECT
*pass_rect
= pRect
;
1662 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This
, pRect
, Flags
, pLockedRect
, This
->resource
.allocatedMemory
);
1664 /* This is also done in the base class, but we have to verify this before loading any data from
1665 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1666 * may interfere, and all other bad things may happen
1668 if (This
->Flags
& SFLAG_LOCKED
) {
1669 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1670 return WINED3DERR_INVALIDCALL
;
1672 This
->Flags
|= SFLAG_LOCKED
;
1674 if (!(This
->Flags
& SFLAG_LOCKABLE
))
1676 TRACE("Warning: trying to lock unlockable surf@%p\n", This
);
1679 if (Flags
& WINED3DLOCK_DISCARD
) {
1680 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1681 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1682 surface_prepare_system_memory(This
); /* Makes sure memory is allocated */
1683 This
->Flags
|= SFLAG_INSYSMEM
;
1687 if (This
->Flags
& SFLAG_INSYSMEM
) {
1688 TRACE("Local copy is up to date, not downloading data\n");
1689 surface_prepare_system_memory(This
); /* Makes sure memory is allocated */
1693 /* IWineD3DSurface_LoadLocation() does not check if the rectangle specifies
1694 * the full surface. Most callers don't need that, so do it here. */
1695 if (pRect
&& pRect
->top
== 0 && pRect
->left
== 0
1696 && pRect
->right
== This
->currentDesc
.Width
1697 && pRect
->bottom
== This
->currentDesc
.Height
)
1702 if (!(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
1703 && ((This
->Flags
& SFLAG_SWAPCHAIN
) || iface
== device
->render_targets
[0])))
1705 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, pass_rect
);
1709 if (This
->Flags
& SFLAG_PBO
)
1711 const struct wined3d_gl_info
*gl_info
;
1712 struct wined3d_context
*context
;
1714 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
1715 gl_info
= context
->gl_info
;
1718 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1719 checkGLcall("glBindBufferARB");
1721 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1722 if(This
->resource
.allocatedMemory
) {
1723 ERR("The surface already has PBO memory allocated!\n");
1726 This
->resource
.allocatedMemory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, GL_READ_WRITE_ARB
));
1727 checkGLcall("glMapBufferARB");
1729 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1730 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1731 checkGLcall("glBindBufferARB");
1734 context_release(context
);
1737 if (Flags
& (WINED3DLOCK_NO_DIRTY_UPDATE
| WINED3DLOCK_READONLY
)) {
1740 IWineD3DBaseTexture
*pBaseTexture
;
1743 * as seen in msdn docs
1745 surface_add_dirty_rect(iface
, pRect
);
1747 /** Dirtify Container if needed */
1748 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&pBaseTexture
))) {
1749 TRACE("Making container dirty\n");
1750 IWineD3DBaseTexture_SetDirty(pBaseTexture
, TRUE
);
1751 IWineD3DBaseTexture_Release(pBaseTexture
);
1753 TRACE("Surface is standalone, no need to dirty the container\n");
1757 return IWineD3DBaseSurfaceImpl_LockRect(iface
, pLockedRect
, pRect
, Flags
);
1760 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl
*This
, GLenum fmt
, GLenum type
, UINT bpp
, const BYTE
*mem
) {
1762 GLint prev_rasterpos
[4];
1763 GLint skipBytes
= 0;
1764 UINT pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*) This
); /* target is argb, 4 byte */
1765 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1766 const struct wined3d_gl_info
*gl_info
;
1767 struct wined3d_context
*context
;
1769 /* Activate the correct context for the render target */
1770 context
= context_acquire(device
, (IWineD3DSurface
*)This
, CTXUSAGE_BLIT
);
1771 gl_info
= context
->gl_info
;
1775 if (!surface_is_offscreen(This
))
1777 GLenum buffer
= surface_get_gl_buffer((IWineD3DSurface
*)This
);
1778 TRACE("Unlocking %#x buffer.\n", buffer
);
1779 context_set_draw_buffer(context
, buffer
);
1783 /* Primary offscreen render target */
1784 TRACE("Offscreen render target.\n");
1785 context_set_draw_buffer(context
, device
->offscreenBuffer
);
1788 glGetIntegerv(GL_PACK_SWAP_BYTES
, &prev_store
);
1789 checkGLcall("glGetIntegerv");
1790 glGetIntegerv(GL_CURRENT_RASTER_POSITION
, &prev_rasterpos
[0]);
1791 checkGLcall("glGetIntegerv");
1792 glPixelZoom(1.0f
, -1.0f
);
1793 checkGLcall("glPixelZoom");
1795 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1796 glGetIntegerv(GL_UNPACK_ROW_LENGTH
, &skipBytes
);
1797 glPixelStorei(GL_UNPACK_ROW_LENGTH
, This
->currentDesc
.Width
);
1799 glRasterPos3i(This
->lockedRect
.left
, This
->lockedRect
.top
, 1);
1800 checkGLcall("glRasterPos3i");
1802 /* Some drivers(radeon dri, others?) don't like exceptions during
1803 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1804 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1805 * catch to put the dib section in InSync mode, which leads to a crash
1806 * and a blocked x server on my radeon card.
1808 * The following lines read the dib section so it is put in InSync mode
1809 * before glDrawPixels is called and the crash is prevented. There won't
1810 * be any interfering gdi accesses, because UnlockRect is called from
1811 * ReleaseDC, and the app won't use the dc any more afterwards.
1813 if((This
->Flags
& SFLAG_DIBSECTION
) && !(This
->Flags
& SFLAG_PBO
)) {
1815 read
= This
->resource
.allocatedMemory
[0];
1818 if(This
->Flags
& SFLAG_PBO
) {
1819 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1820 checkGLcall("glBindBufferARB");
1823 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1824 if(This
->Flags
& SFLAG_LOCKED
) {
1825 glDrawPixels(This
->lockedRect
.right
- This
->lockedRect
.left
,
1826 (This
->lockedRect
.bottom
- This
->lockedRect
.top
)-1,
1828 mem
+ bpp
* This
->lockedRect
.left
+ pitch
* This
->lockedRect
.top
);
1829 checkGLcall("glDrawPixels");
1831 glDrawPixels(This
->currentDesc
.Width
,
1832 This
->currentDesc
.Height
,
1834 checkGLcall("glDrawPixels");
1837 if(This
->Flags
& SFLAG_PBO
) {
1838 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1839 checkGLcall("glBindBufferARB");
1842 glPixelZoom(1.0f
, 1.0f
);
1843 checkGLcall("glPixelZoom");
1845 glRasterPos3iv(&prev_rasterpos
[0]);
1846 checkGLcall("glRasterPos3iv");
1848 /* Reset to previous pack row length */
1849 glPixelStorei(GL_UNPACK_ROW_LENGTH
, skipBytes
);
1850 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH)");
1853 context_release(context
);
1856 static HRESULT WINAPI
IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface
*iface
) {
1857 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1858 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1861 if (!(This
->Flags
& SFLAG_LOCKED
)) {
1862 WARN("trying to Unlock an unlocked surf@%p\n", This
);
1863 return WINEDDERR_NOTLOCKED
;
1866 if (This
->Flags
& SFLAG_PBO
)
1868 const struct wined3d_gl_info
*gl_info
;
1869 struct wined3d_context
*context
;
1871 TRACE("Freeing PBO memory\n");
1873 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
1874 gl_info
= context
->gl_info
;
1877 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1878 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
1879 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1880 checkGLcall("glUnmapBufferARB");
1882 context_release(context
);
1884 This
->resource
.allocatedMemory
= NULL
;
1887 TRACE("(%p) : dirtyfied(%d)\n", This
, This
->Flags
& (SFLAG_INDRAWABLE
| SFLAG_INTEXTURE
) ? 0 : 1);
1889 if (This
->Flags
& (SFLAG_INDRAWABLE
| SFLAG_INTEXTURE
)) {
1890 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This
);
1894 if ((This
->Flags
& SFLAG_SWAPCHAIN
) || (device
->render_targets
&& iface
== device
->render_targets
[0]))
1896 if(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
) {
1897 static BOOL warned
= FALSE
;
1899 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1905 if(This
->dirtyRect
.left
== 0 &&
1906 This
->dirtyRect
.top
== 0 &&
1907 This
->dirtyRect
.right
== This
->currentDesc
.Width
&&
1908 This
->dirtyRect
.bottom
== This
->currentDesc
.Height
) {
1911 /* TODO: Proper partial rectangle tracking */
1912 fullsurface
= FALSE
;
1913 This
->Flags
|= SFLAG_INSYSMEM
;
1916 switch(wined3d_settings
.rendertargetlock_mode
) {
1918 IWineD3DSurface_LoadLocation(iface
, SFLAG_INTEXTURE
, NULL
/* partial texture loading not supported yet */);
1922 IWineD3DSurface_LoadLocation(iface
, SFLAG_INDRAWABLE
, fullsurface
? NULL
: &This
->dirtyRect
);
1927 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1928 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1929 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1930 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1931 * not fully up to date because only a subrectangle was read in LockRect.
1933 This
->Flags
&= ~SFLAG_INSYSMEM
;
1934 This
->Flags
|= SFLAG_INDRAWABLE
;
1937 This
->dirtyRect
.left
= This
->currentDesc
.Width
;
1938 This
->dirtyRect
.top
= This
->currentDesc
.Height
;
1939 This
->dirtyRect
.right
= 0;
1940 This
->dirtyRect
.bottom
= 0;
1942 else if (iface
== device
->stencilBufferTarget
)
1944 FIXME("Depth Stencil buffer locking is not implemented\n");
1946 /* The rest should be a normal texture */
1947 IWineD3DBaseTextureImpl
*impl
;
1948 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1949 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1950 * states need resetting
1952 if(IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&impl
) == WINED3D_OK
) {
1953 if (impl
->baseTexture
.bindCount
)
1954 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(impl
->baseTexture
.sampler
));
1955 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*) impl
);
1960 This
->Flags
&= ~SFLAG_LOCKED
;
1961 memset(&This
->lockedRect
, 0, sizeof(RECT
));
1963 /* Overlays have to be redrawn manually after changes with the GL implementation */
1964 if(This
->overlay_dest
) {
1965 IWineD3DSurface_DrawOverlay(iface
);
1970 static void surface_release_client_storage(IWineD3DSurface
*iface
)
1972 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
1973 struct wined3d_context
*context
;
1975 context
= context_acquire(This
->resource
.device
, NULL
, CTXUSAGE_RESOURCELOAD
);
1978 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
1979 if(This
->texture_name
)
1981 surface_bind_and_dirtify(This
, FALSE
);
1982 glTexImage2D(This
->texture_target
, This
->texture_level
,
1983 GL_RGB
, 1, 1, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
1985 if(This
->texture_name_srgb
)
1987 surface_bind_and_dirtify(This
, TRUE
);
1988 glTexImage2D(This
->texture_target
, This
->texture_level
,
1989 GL_RGB
, 1, 1, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
1991 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1994 context_release(context
);
1996 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSRGBTEX
, FALSE
);
1997 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INTEXTURE
, FALSE
);
1998 surface_force_reload(iface
);
2001 static HRESULT WINAPI
IWineD3DSurfaceImpl_GetDC(IWineD3DSurface
*iface
, HDC
*pHDC
)
2003 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2004 WINED3DLOCKED_RECT lock
;
2008 TRACE("(%p)->(%p)\n",This
,pHDC
);
2010 if(This
->Flags
& SFLAG_USERPTR
) {
2011 ERR("Not supported on surfaces with an application-provided surfaces\n");
2012 return WINEDDERR_NODC
;
2015 /* Give more detailed info for ddraw */
2016 if (This
->Flags
& SFLAG_DCINUSE
)
2017 return WINEDDERR_DCALREADYCREATED
;
2019 /* Can't GetDC if the surface is locked */
2020 if (This
->Flags
& SFLAG_LOCKED
)
2021 return WINED3DERR_INVALIDCALL
;
2023 memset(&lock
, 0, sizeof(lock
)); /* To be sure */
2025 /* Create a DIB section if there isn't a hdc yet */
2027 if(This
->Flags
& SFLAG_CLIENT
) {
2028 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, NULL
);
2029 surface_release_client_storage(iface
);
2031 hr
= IWineD3DBaseSurfaceImpl_CreateDIBSection(iface
);
2032 if(FAILED(hr
)) return WINED3DERR_INVALIDCALL
;
2034 /* Use the dib section from now on if we are not using a PBO */
2035 if(!(This
->Flags
& SFLAG_PBO
))
2036 This
->resource
.allocatedMemory
= This
->dib
.bitmap_data
;
2039 /* Lock the surface */
2040 hr
= IWineD3DSurface_LockRect(iface
,
2045 if(This
->Flags
& SFLAG_PBO
) {
2046 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
2047 memcpy(This
->dib
.bitmap_data
, This
->resource
.allocatedMemory
, This
->dib
.bitmap_size
);
2051 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr
);
2052 /* keep the dib section */
2056 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
2057 || This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT_A8_UNORM
)
2059 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2060 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2062 const PALETTEENTRY
*pal
= NULL
;
2065 pal
= This
->palette
->palents
;
2067 IWineD3DSurfaceImpl
*dds_primary
;
2068 IWineD3DSwapChainImpl
*swapchain
;
2069 swapchain
= (IWineD3DSwapChainImpl
*)This
->resource
.device
->swapchains
[0];
2070 dds_primary
= (IWineD3DSurfaceImpl
*)swapchain
->frontBuffer
;
2071 if (dds_primary
&& dds_primary
->palette
)
2072 pal
= dds_primary
->palette
->palents
;
2076 for (n
=0; n
<256; n
++) {
2077 col
[n
].rgbRed
= pal
[n
].peRed
;
2078 col
[n
].rgbGreen
= pal
[n
].peGreen
;
2079 col
[n
].rgbBlue
= pal
[n
].peBlue
;
2080 col
[n
].rgbReserved
= 0;
2082 SetDIBColorTable(This
->hDC
, 0, 256, col
);
2087 TRACE("returning %p\n",*pHDC
);
2088 This
->Flags
|= SFLAG_DCINUSE
;
2093 static HRESULT WINAPI
IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface
*iface
, HDC hDC
)
2095 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2097 TRACE("(%p)->(%p)\n",This
,hDC
);
2099 if (!(This
->Flags
& SFLAG_DCINUSE
))
2100 return WINEDDERR_NODC
;
2102 if (This
->hDC
!=hDC
) {
2103 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC
, This
->hDC
);
2104 return WINEDDERR_NODC
;
2107 if((This
->Flags
& SFLAG_PBO
) && This
->resource
.allocatedMemory
) {
2108 /* Copy the contents of the DIB over to the PBO */
2109 memcpy(This
->resource
.allocatedMemory
, This
->dib
.bitmap_data
, This
->dib
.bitmap_size
);
2112 /* we locked first, so unlock now */
2113 IWineD3DSurface_UnlockRect(iface
);
2115 This
->Flags
&= ~SFLAG_DCINUSE
;
2120 /* ******************************************************
2121 IWineD3DSurface Internal (No mapping to directx api) parts follow
2122 ****************************************************** */
2124 HRESULT
d3dfmt_get_conv(IWineD3DSurfaceImpl
*This
, BOOL need_alpha_ck
, BOOL use_texturing
, struct wined3d_format_desc
*desc
, CONVERT_TYPES
*convert
)
2126 BOOL colorkey_active
= need_alpha_ck
&& (This
->CKeyFlags
& WINEDDSD_CKSRCBLT
);
2127 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2128 BOOL blit_supported
= FALSE
;
2129 RECT rect
= {0, 0, This
->pow2Width
, This
->pow2Height
};
2131 /* Copy the default values from the surface. Below we might perform fixups */
2132 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2133 *desc
= *This
->resource
.format_desc
;
2134 *convert
= NO_CONVERSION
;
2136 /* Ok, now look if we have to do any conversion */
2137 switch(This
->resource
.format_desc
->format
)
2139 case WINED3DFMT_P8_UINT
:
2144 blit_supported
= device
->blitter
->blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
2145 &rect
, This
->resource
.usage
, This
->resource
.pool
,
2146 This
->resource
.format_desc
, &rect
, This
->resource
.usage
,
2147 This
->resource
.pool
, This
->resource
.format_desc
);
2149 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2150 * texturing. Further also use conversion in case of color keying.
2151 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2152 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2153 * conflicts with this.
2155 if (!((blit_supported
&& device
->render_targets
&& This
== (IWineD3DSurfaceImpl
*)device
->render_targets
[0]))
2156 || colorkey_active
|| !use_texturing
)
2158 desc
->glFormat
= GL_RGBA
;
2159 desc
->glInternal
= GL_RGBA
;
2160 desc
->glType
= GL_UNSIGNED_BYTE
;
2161 desc
->conv_byte_count
= 4;
2162 if(colorkey_active
) {
2163 *convert
= CONVERT_PALETTED_CK
;
2165 *convert
= CONVERT_PALETTED
;
2170 case WINED3DFMT_B2G3R3_UNORM
:
2171 /* **********************
2172 GL_UNSIGNED_BYTE_3_3_2
2173 ********************** */
2174 if (colorkey_active
) {
2175 /* This texture format will never be used.. So do not care about color keying
2176 up until the point in time it will be needed :-) */
2177 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2181 case WINED3DFMT_B5G6R5_UNORM
:
2182 if (colorkey_active
) {
2183 *convert
= CONVERT_CK_565
;
2184 desc
->glFormat
= GL_RGBA
;
2185 desc
->glInternal
= GL_RGB5_A1
;
2186 desc
->glType
= GL_UNSIGNED_SHORT_5_5_5_1
;
2187 desc
->conv_byte_count
= 2;
2191 case WINED3DFMT_B5G5R5X1_UNORM
:
2192 if (colorkey_active
) {
2193 *convert
= CONVERT_CK_5551
;
2194 desc
->glFormat
= GL_BGRA
;
2195 desc
->glInternal
= GL_RGB5_A1
;
2196 desc
->glType
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
2197 desc
->conv_byte_count
= 2;
2201 case WINED3DFMT_B8G8R8_UNORM
:
2202 if (colorkey_active
) {
2203 *convert
= CONVERT_CK_RGB24
;
2204 desc
->glFormat
= GL_RGBA
;
2205 desc
->glInternal
= GL_RGBA8
;
2206 desc
->glType
= GL_UNSIGNED_INT_8_8_8_8
;
2207 desc
->conv_byte_count
= 4;
2211 case WINED3DFMT_B8G8R8X8_UNORM
:
2212 if (colorkey_active
) {
2213 *convert
= CONVERT_RGB32_888
;
2214 desc
->glFormat
= GL_RGBA
;
2215 desc
->glInternal
= GL_RGBA8
;
2216 desc
->glType
= GL_UNSIGNED_INT_8_8_8_8
;
2217 desc
->conv_byte_count
= 4;
2228 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl
*This
, BYTE table
[256][4], BOOL colorkey
)
2230 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2231 IWineD3DPaletteImpl
*pal
= This
->palette
;
2232 BOOL index_in_alpha
= FALSE
;
2235 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2236 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2237 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2238 * duplicate entries. Store the color key in the unused alpha component to speed the
2239 * download up and to make conversion unneeded. */
2240 index_in_alpha
= primary_render_target_is_p8(device
);
2244 UINT dxVersion
= ((IWineD3DImpl
*)device
->wined3d
)->dxVersion
;
2246 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2249 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2252 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2253 * there's no palette at this time. */
2254 for (i
= 0; i
< 256; i
++) table
[i
][3] = i
;
2259 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2260 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2261 * capability flag is present (wine does advertise this capability) */
2262 for (i
= 0; i
< 256; ++i
)
2264 table
[i
][0] = device
->palettes
[device
->currentPalette
][i
].peRed
;
2265 table
[i
][1] = device
->palettes
[device
->currentPalette
][i
].peGreen
;
2266 table
[i
][2] = device
->palettes
[device
->currentPalette
][i
].peBlue
;
2267 table
[i
][3] = device
->palettes
[device
->currentPalette
][i
].peFlags
;
2273 TRACE("Using surface palette %p\n", pal
);
2274 /* Get the surface's palette */
2275 for (i
= 0; i
< 256; ++i
)
2277 table
[i
][0] = pal
->palents
[i
].peRed
;
2278 table
[i
][1] = pal
->palents
[i
].peGreen
;
2279 table
[i
][2] = pal
->palents
[i
].peBlue
;
2281 /* When index_in_alpha is set the palette index is stored in the
2282 * alpha component. In case of a readback we can then read
2283 * GL_ALPHA. Color keying is handled in BltOverride using a
2284 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2285 * color key itself is passed to glAlphaFunc in other cases the
2286 * alpha component of pixels that should be masked away is set to 0. */
2291 else if (colorkey
&& (i
>= This
->SrcBltCKey
.dwColorSpaceLowValue
)
2292 && (i
<= This
->SrcBltCKey
.dwColorSpaceHighValue
))
2296 else if(pal
->Flags
& WINEDDPCAPS_ALPHA
)
2298 table
[i
][3] = pal
->palents
[i
].peFlags
;
2308 static HRESULT
d3dfmt_convert_surface(const BYTE
*src
, BYTE
*dst
, UINT pitch
, UINT width
,
2309 UINT height
, UINT outpitch
, CONVERT_TYPES convert
, IWineD3DSurfaceImpl
*This
)
2313 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src
, dst
, pitch
, height
, outpitch
, convert
,This
);
2318 memcpy(dst
, src
, pitch
* height
);
2321 case CONVERT_PALETTED
:
2322 case CONVERT_PALETTED_CK
:
2324 IWineD3DPaletteImpl
* pal
= This
->palette
;
2329 /* TODO: If we are a sublevel, try to get the palette from level 0 */
2332 d3dfmt_p8_init_palette(This
, table
, (convert
== CONVERT_PALETTED_CK
));
2334 for (y
= 0; y
< height
; y
++)
2336 source
= src
+ pitch
* y
;
2337 dest
= dst
+ outpitch
* y
;
2338 /* This is an 1 bpp format, using the width here is fine */
2339 for (x
= 0; x
< width
; x
++) {
2340 BYTE color
= *source
++;
2341 *dest
++ = table
[color
][0];
2342 *dest
++ = table
[color
][1];
2343 *dest
++ = table
[color
][2];
2344 *dest
++ = table
[color
][3];
2350 case CONVERT_CK_565
:
2352 /* Converting the 565 format in 5551 packed to emulate color-keying.
2354 Note : in all these conversion, it would be best to average the averaging
2355 pixels to get the color of the pixel that will be color-keyed to
2356 prevent 'color bleeding'. This will be done later on if ever it is
2359 Note2: Nvidia documents say that their driver does not support alpha + color keying
2360 on the same surface and disables color keying in such a case
2366 TRACE("Color keyed 565\n");
2368 for (y
= 0; y
< height
; y
++) {
2369 Source
= (const WORD
*)(src
+ y
* pitch
);
2370 Dest
= (WORD
*) (dst
+ y
* outpitch
);
2371 for (x
= 0; x
< width
; x
++ ) {
2372 WORD color
= *Source
++;
2373 *Dest
= ((color
& 0xFFC0) | ((color
& 0x1F) << 1));
2374 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2375 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2384 case CONVERT_CK_5551
:
2386 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2390 TRACE("Color keyed 5551\n");
2391 for (y
= 0; y
< height
; y
++) {
2392 Source
= (const WORD
*)(src
+ y
* pitch
);
2393 Dest
= (WORD
*) (dst
+ y
* outpitch
);
2394 for (x
= 0; x
< width
; x
++ ) {
2395 WORD color
= *Source
++;
2397 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2398 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2402 *Dest
&= ~(1 << 15);
2410 case CONVERT_CK_RGB24
:
2412 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2414 for (y
= 0; y
< height
; y
++)
2416 source
= src
+ pitch
* y
;
2417 dest
= dst
+ outpitch
* y
;
2418 for (x
= 0; x
< width
; x
++) {
2419 DWORD color
= ((DWORD
)source
[0] << 16) + ((DWORD
)source
[1] << 8) + (DWORD
)source
[2] ;
2420 DWORD dstcolor
= color
<< 8;
2421 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2422 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2425 *(DWORD
*)dest
= dstcolor
;
2433 case CONVERT_RGB32_888
:
2435 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2437 for (y
= 0; y
< height
; y
++)
2439 source
= src
+ pitch
* y
;
2440 dest
= dst
+ outpitch
* y
;
2441 for (x
= 0; x
< width
; x
++) {
2442 DWORD color
= 0xffffff & *(const DWORD
*)source
;
2443 DWORD dstcolor
= color
<< 8;
2444 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2445 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2448 *(DWORD
*)dest
= dstcolor
;
2457 ERR("Unsupported conversion type %#x.\n", convert
);
2462 BOOL
palette9_changed(IWineD3DSurfaceImpl
*This
)
2464 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2466 if (This
->palette
|| (This
->resource
.format_desc
->format
!= WINED3DFMT_P8_UINT
2467 && This
->resource
.format_desc
->format
!= WINED3DFMT_P8_UINT_A8_UNORM
))
2469 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2470 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2477 if (!memcmp(This
->palette9
, device
->palettes
[device
->currentPalette
], sizeof(PALETTEENTRY
) * 256))
2482 This
->palette9
= HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
) * 256);
2484 memcpy(This
->palette9
, device
->palettes
[device
->currentPalette
], sizeof(PALETTEENTRY
) * 256);
2488 static HRESULT WINAPI
IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface
*iface
, BOOL srgb_mode
) {
2489 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2490 DWORD flag
= srgb_mode
? SFLAG_INSRGBTEX
: SFLAG_INTEXTURE
;
2492 if (!(This
->Flags
& flag
)) {
2493 TRACE("Reloading because surface is dirty\n");
2494 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2495 ((This
->Flags
& SFLAG_GLCKEY
) && (!(This
->CKeyFlags
& WINEDDSD_CKSRCBLT
))) ||
2496 /* Reload: vice versa OR */
2497 ((!(This
->Flags
& SFLAG_GLCKEY
)) && (This
->CKeyFlags
& WINEDDSD_CKSRCBLT
)) ||
2498 /* Also reload: Color key is active AND the color key has changed */
2499 ((This
->CKeyFlags
& WINEDDSD_CKSRCBLT
) && (
2500 (This
->glCKey
.dwColorSpaceLowValue
!= This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2501 (This
->glCKey
.dwColorSpaceHighValue
!= This
->SrcBltCKey
.dwColorSpaceHighValue
)))) {
2502 TRACE("Reloading because of color keying\n");
2503 /* To perform the color key conversion we need a sysmem copy of
2504 * the surface. Make sure we have it
2507 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, NULL
);
2508 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2509 /* TODO: This is not necessarily needed with hw palettized texture support */
2510 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSYSMEM
, TRUE
);
2512 TRACE("surface is already in texture\n");
2516 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2517 * These resources are not bound by device size or format restrictions. Because of this,
2518 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2519 * However, these resources can always be created, locked, and copied.
2521 if (This
->resource
.pool
== WINED3DPOOL_SCRATCH
)
2523 FIXME("(%p) Operation not supported for scratch textures\n",This
);
2524 return WINED3DERR_INVALIDCALL
;
2527 IWineD3DSurface_LoadLocation(iface
, flag
, NULL
/* no partial locking for textures yet */);
2531 static unsigned int gen
= 0;
2534 if ((gen
% 10) == 0) {
2535 snprintf(buffer
, sizeof(buffer
), "/tmp/surface%p_type%u_level%u_%u.ppm",
2536 This
, This
->texture_target
, This
->texture_level
, gen
);
2537 IWineD3DSurfaceImpl_SaveSnapshot(iface
, buffer
);
2540 * debugging crash code
2549 if (!(This
->Flags
& SFLAG_DONOTFREE
)) {
2550 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
2551 This
->resource
.allocatedMemory
= NULL
;
2552 This
->resource
.heapMemory
= NULL
;
2553 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSYSMEM
, FALSE
);
2559 /* Context activation is done by the caller. */
2560 static void WINAPI
IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface
*iface
, BOOL srgb
) {
2561 /* TODO: check for locks */
2562 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2563 IWineD3DBaseTexture
*baseTexture
= NULL
;
2565 TRACE("(%p)Checking to see if the container is a base texture\n", This
);
2566 if (IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&baseTexture
) == WINED3D_OK
) {
2567 TRACE("Passing to container\n");
2568 IWineD3DBaseTexture_BindTexture(baseTexture
, srgb
);
2569 IWineD3DBaseTexture_Release(baseTexture
);
2575 TRACE("(%p) : Binding surface\n", This
);
2577 name
= srgb
? &This
->texture_name_srgb
: &This
->texture_name
;
2581 if (!This
->texture_level
)
2584 glGenTextures(1, name
);
2585 checkGLcall("glGenTextures");
2586 TRACE("Surface %p given name %d\n", This
, *name
);
2588 glBindTexture(This
->texture_target
, *name
);
2589 checkGLcall("glBindTexture");
2590 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2591 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2592 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2593 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2594 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2595 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2596 glTexParameteri(This
->texture_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2597 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2598 glTexParameteri(This
->texture_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2599 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2601 /* This is where we should be reducing the amount of GLMemoryUsed */
2603 /* Mipmap surfaces should have a base texture container */
2604 ERR("Mipmap surface has a glTexture bound to it!\n");
2607 glBindTexture(This
->texture_target
, *name
);
2608 checkGLcall("glBindTexture");
2616 static HRESULT WINAPI
IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface
*iface
, const char* filename
)
2619 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2620 char *allocatedMemory
;
2621 const char *textureRow
;
2622 IWineD3DSwapChain
*swapChain
= NULL
;
2623 int width
, height
, i
, y
;
2624 GLuint tmpTexture
= 0;
2627 Textures may not be stored in ->allocatedgMemory and a GlTexture
2628 so we should lock the surface before saving a snapshot, or at least check that
2630 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2631 by calling GetTexImage and in compressed form by calling
2632 GetCompressedTexImageARB. Queried compressed images can be saved and
2633 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2634 texture images do not need to be processed by the GL and should
2635 significantly improve texture loading performance relative to uncompressed
2638 /* Setup the width and height to be the internal texture width and height. */
2639 width
= This
->pow2Width
;
2640 height
= This
->pow2Height
;
2641 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2642 IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DSwapChain
, (void **)&swapChain
);
2644 if (This
->Flags
& SFLAG_INDRAWABLE
&& !(This
->Flags
& SFLAG_INTEXTURE
)) {
2645 /* if were not a real texture then read the back buffer into a real texture */
2646 /* we don't want to interfere with the back buffer so read the data into a temporary
2647 * texture and then save the data out of the temporary texture
2651 TRACE("(%p) Reading render target into texture\n", This
);
2653 glGenTextures(1, &tmpTexture
);
2654 glBindTexture(GL_TEXTURE_2D
, tmpTexture
);
2656 glTexImage2D(GL_TEXTURE_2D
,
2663 GL_UNSIGNED_INT_8_8_8_8_REV
,
2666 glGetIntegerv(GL_READ_BUFFER
, &prevRead
);
2667 checkGLcall("glGetIntegerv");
2668 glReadBuffer(swapChain
? GL_BACK
: This
->resource
.device
->offscreenBuffer
);
2669 checkGLcall("glReadBuffer");
2670 glCopyTexImage2D(GL_TEXTURE_2D
,
2679 checkGLcall("glCopyTexImage2D");
2680 glReadBuffer(prevRead
);
2683 } else { /* bind the real texture, and make sure it up to date */
2684 surface_internal_preload(iface
, SRGB_RGB
);
2685 surface_bind_and_dirtify(This
, FALSE
);
2687 allocatedMemory
= HeapAlloc(GetProcessHeap(), 0, width
* height
* 4);
2689 FIXME("Saving texture level %d width %d height %d\n", This
->texture_level
, width
, height
);
2690 glGetTexImage(GL_TEXTURE_2D
, This
->texture_level
, GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8_REV
, allocatedMemory
);
2691 checkGLcall("glGetTexImage");
2693 glBindTexture(GL_TEXTURE_2D
, 0);
2694 glDeleteTextures(1, &tmpTexture
);
2698 f
= fopen(filename
, "w+");
2700 ERR("opening of %s failed with: %s\n", filename
, strerror(errno
));
2701 return WINED3DERR_INVALIDCALL
;
2703 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2704 TRACE("(%p) opened %s with format %s\n", This
, filename
, debug_d3dformat(This
->resource
.format_desc
->format
));
2719 fwrite(&width
,2,1,f
);
2721 fwrite(&height
,2,1,f
);
2726 /* 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 */
2728 textureRow
= allocatedMemory
+ (width
* (height
- 1) *4);
2730 textureRow
= allocatedMemory
;
2731 for (y
= 0 ; y
< height
; y
++) {
2732 for (i
= 0; i
< width
; i
++) {
2733 color
= *((const DWORD
*)textureRow
);
2734 fputc((color
>> 16) & 0xFF, f
); /* B */
2735 fputc((color
>> 8) & 0xFF, f
); /* G */
2736 fputc((color
>> 0) & 0xFF, f
); /* R */
2737 fputc((color
>> 24) & 0xFF, f
); /* A */
2740 /* take two rows of the pointer to the texture memory */
2742 (textureRow
-= width
<< 3);
2745 TRACE("Closing file\n");
2749 IWineD3DSwapChain_Release(swapChain
);
2751 HeapFree(GetProcessHeap(), 0, allocatedMemory
);
2755 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface
*iface
, WINED3DFORMAT format
) {
2756 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2759 TRACE("(%p) : Calling base function first\n", This
);
2760 hr
= IWineD3DBaseSurfaceImpl_SetFormat(iface
, format
);
2762 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
2763 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This
, This
->resource
.format_desc
->glFormat
,
2764 This
->resource
.format_desc
->glInternal
, This
->resource
.format_desc
->glType
);
2769 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetMem(IWineD3DSurface
*iface
, void *Mem
) {
2770 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
2772 if(This
->Flags
& (SFLAG_LOCKED
| SFLAG_DCINUSE
)) {
2773 WARN("Surface is locked or the HDC is in use\n");
2774 return WINED3DERR_INVALIDCALL
;
2777 if(Mem
&& Mem
!= This
->resource
.allocatedMemory
) {
2778 void *release
= NULL
;
2780 /* Do I have to copy the old surface content? */
2781 if(This
->Flags
& SFLAG_DIBSECTION
) {
2782 /* Release the DC. No need to hold the critical section for the update
2783 * Thread because this thread runs only on front buffers, but this method
2784 * fails for render targets in the check above.
2786 SelectObject(This
->hDC
, This
->dib
.holdbitmap
);
2787 DeleteDC(This
->hDC
);
2788 /* Release the DIB section */
2789 DeleteObject(This
->dib
.DIBsection
);
2790 This
->dib
.bitmap_data
= NULL
;
2791 This
->resource
.allocatedMemory
= NULL
;
2793 This
->Flags
&= ~SFLAG_DIBSECTION
;
2794 } else if(!(This
->Flags
& SFLAG_USERPTR
)) {
2795 release
= This
->resource
.heapMemory
;
2796 This
->resource
.heapMemory
= NULL
;
2798 This
->resource
.allocatedMemory
= Mem
;
2799 This
->Flags
|= SFLAG_USERPTR
| SFLAG_INSYSMEM
;
2801 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2802 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSYSMEM
, TRUE
);
2804 /* For client textures opengl has to be notified */
2805 if(This
->Flags
& SFLAG_CLIENT
) {
2806 surface_release_client_storage(iface
);
2809 /* Now free the old memory if any */
2810 HeapFree(GetProcessHeap(), 0, release
);
2811 } else if(This
->Flags
& SFLAG_USERPTR
) {
2812 /* LockRect and GetDC will re-create the dib section and allocated memory */
2813 This
->resource
.allocatedMemory
= NULL
;
2814 /* HeapMemory should be NULL already */
2815 if(This
->resource
.heapMemory
!= NULL
) ERR("User pointer surface has heap memory allocated\n");
2816 This
->Flags
&= ~SFLAG_USERPTR
;
2818 if(This
->Flags
& SFLAG_CLIENT
) {
2819 surface_release_client_storage(iface
);
2825 void flip_surface(IWineD3DSurfaceImpl
*front
, IWineD3DSurfaceImpl
*back
) {
2827 /* Flip the surface contents */
2832 front
->hDC
= back
->hDC
;
2836 /* Flip the DIBsection */
2839 BOOL hasDib
= front
->Flags
& SFLAG_DIBSECTION
;
2840 tmp
= front
->dib
.DIBsection
;
2841 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
2842 back
->dib
.DIBsection
= tmp
;
2844 if(back
->Flags
& SFLAG_DIBSECTION
) front
->Flags
|= SFLAG_DIBSECTION
;
2845 else front
->Flags
&= ~SFLAG_DIBSECTION
;
2846 if(hasDib
) back
->Flags
|= SFLAG_DIBSECTION
;
2847 else back
->Flags
&= ~SFLAG_DIBSECTION
;
2850 /* Flip the surface data */
2854 tmp
= front
->dib
.bitmap_data
;
2855 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
2856 back
->dib
.bitmap_data
= tmp
;
2858 tmp
= front
->resource
.allocatedMemory
;
2859 front
->resource
.allocatedMemory
= back
->resource
.allocatedMemory
;
2860 back
->resource
.allocatedMemory
= tmp
;
2862 tmp
= front
->resource
.heapMemory
;
2863 front
->resource
.heapMemory
= back
->resource
.heapMemory
;
2864 back
->resource
.heapMemory
= tmp
;
2869 GLuint tmp_pbo
= front
->pbo
;
2870 front
->pbo
= back
->pbo
;
2871 back
->pbo
= tmp_pbo
;
2874 /* client_memory should not be different, but just in case */
2877 tmp
= front
->dib
.client_memory
;
2878 front
->dib
.client_memory
= back
->dib
.client_memory
;
2879 back
->dib
.client_memory
= tmp
;
2882 /* Flip the opengl texture */
2886 tmp
= back
->texture_name
;
2887 back
->texture_name
= front
->texture_name
;
2888 front
->texture_name
= tmp
;
2890 tmp
= back
->texture_name_srgb
;
2891 back
->texture_name_srgb
= front
->texture_name_srgb
;
2892 front
->texture_name_srgb
= tmp
;
2896 DWORD tmp_flags
= back
->Flags
;
2897 back
->Flags
= front
->Flags
;
2898 front
->Flags
= tmp_flags
;
2902 static HRESULT WINAPI
IWineD3DSurfaceImpl_Flip(IWineD3DSurface
*iface
, IWineD3DSurface
*override
, DWORD Flags
) {
2903 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2904 IWineD3DSwapChainImpl
*swapchain
= NULL
;
2906 TRACE("(%p)->(%p,%x)\n", This
, override
, Flags
);
2908 /* Flipping is only supported on RenderTargets and overlays*/
2909 if( !(This
->resource
.usage
& (WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_OVERLAY
)) ) {
2910 WARN("Tried to flip a non-render target, non-overlay surface\n");
2911 return WINEDDERR_NOTFLIPPABLE
;
2914 if(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
) {
2915 flip_surface(This
, (IWineD3DSurfaceImpl
*) override
);
2917 /* Update the overlay if it is visible */
2918 if(This
->overlay_dest
) {
2919 return IWineD3DSurface_DrawOverlay((IWineD3DSurface
*) This
);
2926 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2927 * FIXME("(%p) Target override is not supported by now\n", This);
2928 * Additionally, it isn't really possible to support triple-buffering
2929 * properly on opengl at all
2933 IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
2935 ERR("Flipped surface is not on a swapchain\n");
2936 return WINEDDERR_NOTFLIPPABLE
;
2939 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2940 * and only d3d8 and d3d9 apps specify the presentation interval
2942 if((Flags
& (WINEDDFLIP_NOVSYNC
| WINEDDFLIP_INTERVAL2
| WINEDDFLIP_INTERVAL3
| WINEDDFLIP_INTERVAL4
)) == 0) {
2943 /* Most common case first to avoid wasting time on all the other cases */
2944 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_ONE
;
2945 } else if(Flags
& WINEDDFLIP_NOVSYNC
) {
2946 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_IMMEDIATE
;
2947 } else if(Flags
& WINEDDFLIP_INTERVAL2
) {
2948 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_TWO
;
2949 } else if(Flags
& WINEDDFLIP_INTERVAL3
) {
2950 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_THREE
;
2952 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_FOUR
;
2955 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2956 hr
= IWineD3DSwapChain_Present((IWineD3DSwapChain
*)swapchain
,
2957 NULL
, NULL
, swapchain
->win_handle
, NULL
, 0);
2958 IWineD3DSwapChain_Release((IWineD3DSwapChain
*) swapchain
);
2962 /* Does a direct frame buffer -> texture copy. Stretching is done
2963 * with single pixel copy calls
2965 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl
*This
, IWineD3DSurface
*SrcSurface
,
2966 const RECT
*src_rect
, const RECT
*dst_rect_in
, WINED3DTEXTUREFILTERTYPE Filter
)
2968 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2971 IWineD3DSurfaceImpl
*Src
= (IWineD3DSurfaceImpl
*) SrcSurface
;
2972 struct wined3d_context
*context
;
2973 BOOL upsidedown
= FALSE
;
2974 RECT dst_rect
= *dst_rect_in
;
2976 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2977 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2979 if(dst_rect
.top
> dst_rect
.bottom
) {
2980 UINT tmp
= dst_rect
.bottom
;
2981 dst_rect
.bottom
= dst_rect
.top
;
2986 context
= context_acquire(device
, SrcSurface
, CTXUSAGE_BLIT
);
2987 surface_internal_preload((IWineD3DSurface
*) This
, SRGB_RGB
);
2990 /* Bind the target texture */
2991 glBindTexture(This
->texture_target
, This
->texture_name
);
2992 checkGLcall("glBindTexture");
2993 if (surface_is_offscreen(Src
))
2995 TRACE("Reading from an offscreen target\n");
2996 upsidedown
= !upsidedown
;
2997 glReadBuffer(device
->offscreenBuffer
);
3001 glReadBuffer(surface_get_gl_buffer(SrcSurface
));
3003 checkGLcall("glReadBuffer");
3005 xrel
= (float) (src_rect
->right
- src_rect
->left
) / (float) (dst_rect
.right
- dst_rect
.left
);
3006 yrel
= (float) (src_rect
->bottom
- src_rect
->top
) / (float) (dst_rect
.bottom
- dst_rect
.top
);
3008 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
3010 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
3012 if(Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
) {
3013 ERR("Texture filtering not supported in direct blit\n");
3016 else if ((Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
)
3017 && ((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
3019 ERR("Texture filtering not supported in direct blit\n");
3023 && !((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
3024 && !((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
3026 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
3028 glCopyTexSubImage2D(This
->texture_target
, This
->texture_level
,
3029 dst_rect
.left
/*xoffset */, dst_rect
.top
/* y offset */,
3030 src_rect
->left
, Src
->currentDesc
.Height
- src_rect
->bottom
,
3031 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
3033 UINT yoffset
= Src
->currentDesc
.Height
- src_rect
->top
+ dst_rect
.top
- 1;
3034 /* I have to process this row by row to swap the image,
3035 * otherwise it would be upside down, so stretching in y direction
3036 * doesn't cost extra time
3038 * However, stretching in x direction can be avoided if not necessary
3040 for(row
= dst_rect
.top
; row
< dst_rect
.bottom
; row
++) {
3041 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
3043 /* Well, that stuff works, but it's very slow.
3044 * find a better way instead
3048 for(col
= dst_rect
.left
; col
< dst_rect
.right
; col
++) {
3049 glCopyTexSubImage2D(This
->texture_target
, This
->texture_level
,
3050 dst_rect
.left
+ col
/* x offset */, row
/* y offset */,
3051 src_rect
->left
+ col
* xrel
, yoffset
- (int) (row
* yrel
), 1, 1);
3054 glCopyTexSubImage2D(This
->texture_target
, This
->texture_level
,
3055 dst_rect
.left
/* x offset */, row
/* y offset */,
3056 src_rect
->left
, yoffset
- (int) (row
* yrel
), dst_rect
.right
- dst_rect
.left
, 1);
3060 checkGLcall("glCopyTexSubImage2D");
3063 context_release(context
);
3065 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3066 * path is never entered
3068 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) This
, SFLAG_INTEXTURE
, TRUE
);
3071 /* Uses the hardware to stretch and flip the image */
3072 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl
*This
, IWineD3DSurface
*SrcSurface
,
3073 const RECT
*src_rect
, const RECT
*dst_rect_in
, WINED3DTEXTUREFILTERTYPE Filter
)
3075 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3076 GLuint src
, backup
= 0;
3077 IWineD3DSurfaceImpl
*Src
= (IWineD3DSurfaceImpl
*) SrcSurface
;
3078 IWineD3DSwapChainImpl
*src_swapchain
= NULL
;
3079 float left
, right
, top
, bottom
; /* Texture coordinates */
3080 UINT fbwidth
= Src
->currentDesc
.Width
;
3081 UINT fbheight
= Src
->currentDesc
.Height
;
3082 struct wined3d_context
*context
;
3083 GLenum drawBuffer
= GL_BACK
;
3084 GLenum texture_target
;
3085 BOOL noBackBufferBackup
;
3087 BOOL upsidedown
= FALSE
;
3088 RECT dst_rect
= *dst_rect_in
;
3090 TRACE("Using hwstretch blit\n");
3091 /* Activate the Proper context for reading from the source surface, set it up for blitting */
3092 context
= context_acquire(device
, SrcSurface
, CTXUSAGE_BLIT
);
3093 surface_internal_preload((IWineD3DSurface
*) This
, SRGB_RGB
);
3095 src_offscreen
= surface_is_offscreen(Src
);
3096 noBackBufferBackup
= src_offscreen
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
;
3097 if (!noBackBufferBackup
&& !Src
->texture_name
)
3099 /* Get it a description */
3100 surface_internal_preload(SrcSurface
, SRGB_RGB
);
3104 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
3105 * This way we don't have to wait for the 2nd readback to finish to leave this function.
3107 if (context
->aux_buffers
>= 2)
3109 /* Got more than one aux buffer? Use the 2nd aux buffer */
3110 drawBuffer
= GL_AUX1
;
3112 else if ((!src_offscreen
|| device
->offscreenBuffer
== GL_BACK
) && context
->aux_buffers
>= 1)
3114 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
3115 drawBuffer
= GL_AUX0
;
3118 if(noBackBufferBackup
) {
3119 glGenTextures(1, &backup
);
3120 checkGLcall("glGenTextures");
3121 glBindTexture(GL_TEXTURE_2D
, backup
);
3122 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3123 texture_target
= GL_TEXTURE_2D
;
3125 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
3126 * we are reading from the back buffer, the backup can be used as source texture
3128 texture_target
= Src
->texture_target
;
3129 glBindTexture(texture_target
, Src
->texture_name
);
3130 checkGLcall("glBindTexture(texture_target, Src->texture_name)");
3131 glEnable(texture_target
);
3132 checkGLcall("glEnable(texture_target)");
3134 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3135 Src
->Flags
&= ~SFLAG_INTEXTURE
;
3138 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3139 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3141 if(dst_rect
.top
> dst_rect
.bottom
) {
3142 UINT tmp
= dst_rect
.bottom
;
3143 dst_rect
.bottom
= dst_rect
.top
;
3150 TRACE("Reading from an offscreen target\n");
3151 upsidedown
= !upsidedown
;
3152 glReadBuffer(device
->offscreenBuffer
);
3156 glReadBuffer(surface_get_gl_buffer(SrcSurface
));
3159 /* TODO: Only back up the part that will be overwritten */
3160 glCopyTexSubImage2D(texture_target
, 0,
3161 0, 0 /* read offsets */,
3166 checkGLcall("glCopyTexSubImage2D");
3168 /* No issue with overriding these - the sampler is dirty due to blit usage */
3169 glTexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
,
3170 wined3d_gl_mag_filter(magLookup
, Filter
));
3171 checkGLcall("glTexParameteri");
3172 glTexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
,
3173 wined3d_gl_min_mip_filter(minMipLookup
, Filter
, WINED3DTEXF_NONE
));
3174 checkGLcall("glTexParameteri");
3176 IWineD3DSurface_GetContainer((IWineD3DSurface
*)SrcSurface
, &IID_IWineD3DSwapChain
, (void **)&src_swapchain
);
3177 if (src_swapchain
) IWineD3DSwapChain_Release((IWineD3DSwapChain
*)src_swapchain
);
3178 if (!src_swapchain
|| (IWineD3DSurface
*) Src
== src_swapchain
->backBuffer
[0]) {
3179 src
= backup
? backup
: Src
->texture_name
;
3181 glReadBuffer(GL_FRONT
);
3182 checkGLcall("glReadBuffer(GL_FRONT)");
3184 glGenTextures(1, &src
);
3185 checkGLcall("glGenTextures(1, &src)");
3186 glBindTexture(GL_TEXTURE_2D
, src
);
3187 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3189 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3190 * out for power of 2 sizes
3192 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, Src
->pow2Width
, Src
->pow2Height
, 0,
3193 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
3194 checkGLcall("glTexImage2D");
3195 glCopyTexSubImage2D(GL_TEXTURE_2D
, 0,
3196 0, 0 /* read offsets */,
3201 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3202 checkGLcall("glTexParameteri");
3203 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3204 checkGLcall("glTexParameteri");
3206 glReadBuffer(GL_BACK
);
3207 checkGLcall("glReadBuffer(GL_BACK)");
3209 if(texture_target
!= GL_TEXTURE_2D
) {
3210 glDisable(texture_target
);
3211 glEnable(GL_TEXTURE_2D
);
3212 texture_target
= GL_TEXTURE_2D
;
3215 checkGLcall("glEnd and previous");
3217 left
= src_rect
->left
;
3218 right
= src_rect
->right
;
3221 top
= Src
->currentDesc
.Height
- src_rect
->top
;
3222 bottom
= Src
->currentDesc
.Height
- src_rect
->bottom
;
3224 top
= Src
->currentDesc
.Height
- src_rect
->bottom
;
3225 bottom
= Src
->currentDesc
.Height
- src_rect
->top
;
3228 if(Src
->Flags
& SFLAG_NORMCOORD
) {
3229 left
/= Src
->pow2Width
;
3230 right
/= Src
->pow2Width
;
3231 top
/= Src
->pow2Height
;
3232 bottom
/= Src
->pow2Height
;
3235 /* draw the source texture stretched and upside down. The correct surface is bound already */
3236 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
3237 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
3239 context_set_draw_buffer(context
, drawBuffer
);
3240 glReadBuffer(drawBuffer
);
3244 glTexCoord2f(left
, bottom
);
3245 glVertex2i(0, fbheight
);
3248 glTexCoord2f(left
, top
);
3249 glVertex2i(0, fbheight
- dst_rect
.bottom
- dst_rect
.top
);
3252 glTexCoord2f(right
, top
);
3253 glVertex2i(dst_rect
.right
- dst_rect
.left
, fbheight
- dst_rect
.bottom
- dst_rect
.top
);
3256 glTexCoord2f(right
, bottom
);
3257 glVertex2i(dst_rect
.right
- dst_rect
.left
, fbheight
);
3259 checkGLcall("glEnd and previous");
3261 if (texture_target
!= This
->texture_target
)
3263 glDisable(texture_target
);
3264 glEnable(This
->texture_target
);
3265 texture_target
= This
->texture_target
;
3268 /* Now read the stretched and upside down image into the destination texture */
3269 glBindTexture(texture_target
, This
->texture_name
);
3270 checkGLcall("glBindTexture");
3271 glCopyTexSubImage2D(texture_target
,
3273 dst_rect
.left
, dst_rect
.top
, /* xoffset, yoffset */
3274 0, 0, /* We blitted the image to the origin */
3275 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
3276 checkGLcall("glCopyTexSubImage2D");
3278 if(drawBuffer
== GL_BACK
) {
3279 /* Write the back buffer backup back */
3281 if(texture_target
!= GL_TEXTURE_2D
) {
3282 glDisable(texture_target
);
3283 glEnable(GL_TEXTURE_2D
);
3284 texture_target
= GL_TEXTURE_2D
;
3286 glBindTexture(GL_TEXTURE_2D
, backup
);
3287 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3289 if (texture_target
!= Src
->texture_target
)
3291 glDisable(texture_target
);
3292 glEnable(Src
->texture_target
);
3293 texture_target
= Src
->texture_target
;
3295 glBindTexture(Src
->texture_target
, Src
->texture_name
);
3296 checkGLcall("glBindTexture(Src->texture_target, Src->texture_name)");
3301 glTexCoord2f(0.0f
, (float)fbheight
/ (float)Src
->pow2Height
);
3305 glTexCoord2f(0.0f
, 0.0f
);
3306 glVertex2i(0, fbheight
);
3309 glTexCoord2f((float)fbwidth
/ (float)Src
->pow2Width
, 0.0f
);
3310 glVertex2i(fbwidth
, Src
->currentDesc
.Height
);
3313 glTexCoord2f((float) fbwidth
/ (float) Src
->pow2Width
, (float) fbheight
/ (float) Src
->pow2Height
);
3314 glVertex2i(fbwidth
, 0);
3317 glDisable(texture_target
);
3318 checkGLcall("glDisable(texture_target)");
3321 if (src
!= Src
->texture_name
&& src
!= backup
)
3323 glDeleteTextures(1, &src
);
3324 checkGLcall("glDeleteTextures(1, &src)");
3327 glDeleteTextures(1, &backup
);
3328 checkGLcall("glDeleteTextures(1, &backup)");
3333 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
3335 context_release(context
);
3337 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3338 * path is never entered
3340 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) This
, SFLAG_INTEXTURE
, TRUE
);
3343 /* Until the blit_shader is ready, define some prototypes here. */
3344 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
3345 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
3346 const struct wined3d_format_desc
*src_format_desc
,
3347 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
3348 const struct wined3d_format_desc
*dst_format_desc
);
3350 /* Not called from the VTable */
3351 static HRESULT
IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl
*This
, const RECT
*DestRect
,
3352 IWineD3DSurface
*SrcSurface
, const RECT
*SrcRect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
,
3353 WINED3DTEXTUREFILTERTYPE Filter
)
3355 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3356 IWineD3DSwapChainImpl
*srcSwapchain
= NULL
, *dstSwapchain
= NULL
;
3357 IWineD3DSurfaceImpl
*Src
= (IWineD3DSurfaceImpl
*) SrcSurface
;
3358 RECT dst_rect
, src_rect
;
3360 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This
, DestRect
, SrcSurface
, SrcRect
, Flags
, DDBltFx
);
3362 /* Get the swapchain. One of the surfaces has to be a primary surface */
3363 if(This
->resource
.pool
== WINED3DPOOL_SYSTEMMEM
) {
3364 WARN("Destination is in sysmem, rejecting gl blt\n");
3365 return WINED3DERR_INVALIDCALL
;
3367 IWineD3DSurface_GetContainer( (IWineD3DSurface
*) This
, &IID_IWineD3DSwapChain
, (void **)&dstSwapchain
);
3368 if(dstSwapchain
) IWineD3DSwapChain_Release((IWineD3DSwapChain
*) dstSwapchain
);
3370 if(Src
->resource
.pool
== WINED3DPOOL_SYSTEMMEM
) {
3371 WARN("Src is in sysmem, rejecting gl blt\n");
3372 return WINED3DERR_INVALIDCALL
;
3374 IWineD3DSurface_GetContainer( (IWineD3DSurface
*) Src
, &IID_IWineD3DSwapChain
, (void **)&srcSwapchain
);
3375 if(srcSwapchain
) IWineD3DSwapChain_Release((IWineD3DSwapChain
*) srcSwapchain
);
3378 /* Early sort out of cases where no render target is used */
3379 if (!dstSwapchain
&& !srcSwapchain
3380 && SrcSurface
!= device
->render_targets
[0]
3381 && This
!= (IWineD3DSurfaceImpl
*)device
->render_targets
[0])
3383 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src
, This
);
3384 return WINED3DERR_INVALIDCALL
;
3387 /* No destination color keying supported */
3388 if(Flags
& (WINEDDBLT_KEYDEST
| WINEDDBLT_KEYDESTOVERRIDE
)) {
3389 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3390 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3391 return WINED3DERR_INVALIDCALL
;
3394 surface_get_rect(This
, DestRect
, &dst_rect
);
3395 if(Src
) surface_get_rect(Src
, SrcRect
, &src_rect
);
3397 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3398 if(dstSwapchain
&& dstSwapchain
== srcSwapchain
&& dstSwapchain
->backBuffer
&&
3399 ((IWineD3DSurface
*) This
== dstSwapchain
->frontBuffer
) && SrcSurface
== dstSwapchain
->backBuffer
[0]) {
3400 /* Half-life does a Blt from the back buffer to the front buffer,
3401 * Full surface size, no flags... Use present instead
3403 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3406 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3409 TRACE("Looking if a Present can be done...\n");
3410 /* Source Rectangle must be full surface */
3411 if(src_rect
.left
!= 0 || src_rect
.top
!= 0 ||
3412 src_rect
.right
!= Src
->currentDesc
.Width
|| src_rect
.bottom
!= Src
->currentDesc
.Height
) {
3413 TRACE("No, Source rectangle doesn't match\n");
3417 /* No stretching may occur */
3418 if(src_rect
.right
!= dst_rect
.right
- dst_rect
.left
||
3419 src_rect
.bottom
!= dst_rect
.bottom
- dst_rect
.top
) {
3420 TRACE("No, stretching is done\n");
3424 /* Destination must be full surface or match the clipping rectangle */
3425 if(This
->clipper
&& ((IWineD3DClipperImpl
*) This
->clipper
)->hWnd
)
3429 GetClientRect(((IWineD3DClipperImpl
*) This
->clipper
)->hWnd
, &cliprect
);
3430 pos
[0].x
= dst_rect
.left
;
3431 pos
[0].y
= dst_rect
.top
;
3432 pos
[1].x
= dst_rect
.right
;
3433 pos
[1].y
= dst_rect
.bottom
;
3434 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl
*) This
->clipper
)->hWnd
,
3437 if(pos
[0].x
!= cliprect
.left
|| pos
[0].y
!= cliprect
.top
||
3438 pos
[1].x
!= cliprect
.right
|| pos
[1].y
!= cliprect
.bottom
)
3440 TRACE("No, dest rectangle doesn't match(clipper)\n");
3441 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect
));
3442 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect
));
3448 if(dst_rect
.left
!= 0 || dst_rect
.top
!= 0 ||
3449 dst_rect
.right
!= This
->currentDesc
.Width
|| dst_rect
.bottom
!= This
->currentDesc
.Height
) {
3450 TRACE("No, dest rectangle doesn't match(surface size)\n");
3457 /* These flags are unimportant for the flag check, remove them */
3458 if((Flags
& ~(WINEDDBLT_DONOTWAIT
| WINEDDBLT_WAIT
)) == 0) {
3459 WINED3DSWAPEFFECT orig_swap
= dstSwapchain
->presentParms
.SwapEffect
;
3461 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3462 * take very long, while a flip is fast.
3463 * This applies to Half-Life, which does such Blts every time it finished
3464 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3465 * menu. This is also used by all apps when they do windowed rendering
3467 * The problem is that flipping is not really the same as copying. After a
3468 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3469 * untouched. Therefore it's necessary to override the swap effect
3470 * and to set it back after the flip.
3472 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3476 dstSwapchain
->presentParms
.SwapEffect
= WINED3DSWAPEFFECT_COPY
;
3477 dstSwapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_IMMEDIATE
;
3479 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3480 IWineD3DSwapChain_Present((IWineD3DSwapChain
*)dstSwapchain
,
3481 NULL
, NULL
, dstSwapchain
->win_handle
, NULL
, 0);
3483 dstSwapchain
->presentParms
.SwapEffect
= orig_swap
;
3490 TRACE("Unsupported blit between buffers on the same swapchain\n");
3491 return WINED3DERR_INVALIDCALL
;
3492 } else if(dstSwapchain
&& dstSwapchain
== srcSwapchain
) {
3493 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3494 return WINED3DERR_INVALIDCALL
;
3495 } else if(dstSwapchain
&& srcSwapchain
) {
3496 FIXME("Implement hardware blit between two different swapchains\n");
3497 return WINED3DERR_INVALIDCALL
;
3499 else if (dstSwapchain
)
3501 /* Handled with regular texture -> swapchain blit. */
3502 if (SrcSurface
== device
->render_targets
[0])
3503 TRACE("Blit from active render target to a swapchain\n");
3505 else if (srcSwapchain
&& This
== (IWineD3DSurfaceImpl
*)device
->render_targets
[0])
3507 FIXME("Implement blit from a swapchain to the active render target\n");
3508 return WINED3DERR_INVALIDCALL
;
3511 if ((srcSwapchain
|| SrcSurface
== device
->render_targets
[0]) && !dstSwapchain
)
3513 /* Blit from render target to texture */
3516 /* P8 read back is not implemented */
3517 if (Src
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
||
3518 This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
)
3520 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3521 return WINED3DERR_INVALIDCALL
;
3524 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3525 TRACE("Color keying not supported by frame buffer to texture blit\n");
3526 return WINED3DERR_INVALIDCALL
;
3527 /* Destination color key is checked above */
3530 if(dst_rect
.right
- dst_rect
.left
!= src_rect
.right
- src_rect
.left
) {
3536 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3537 * flip the image nor scale it.
3539 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3540 * -> If the app wants a image width an unscaled width, copy it line per line
3541 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3542 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3543 * back buffer. This is slower than reading line per line, thus not used for flipping
3544 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3547 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3548 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3551 if (fbo_blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3552 &src_rect
, Src
->resource
.usage
, Src
->resource
.pool
, Src
->resource
.format_desc
,
3553 &dst_rect
, This
->resource
.usage
, This
->resource
.pool
, This
->resource
.format_desc
))
3555 stretch_rect_fbo((IWineD3DDevice
*)device
, SrcSurface
, &src_rect
,
3556 (IWineD3DSurface
*)This
, &dst_rect
, Filter
);
3557 } else if((!stretchx
) || dst_rect
.right
- dst_rect
.left
> Src
->currentDesc
.Width
||
3558 dst_rect
.bottom
- dst_rect
.top
> Src
->currentDesc
.Height
) {
3559 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3560 fb_copy_to_texture_direct(This
, SrcSurface
, &src_rect
, &dst_rect
, Filter
);
3562 TRACE("Using hardware stretching to flip / stretch the texture\n");
3563 fb_copy_to_texture_hwstretch(This
, SrcSurface
, &src_rect
, &dst_rect
, Filter
);
3566 if(!(This
->Flags
& SFLAG_DONOTFREE
)) {
3567 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
3568 This
->resource
.allocatedMemory
= NULL
;
3569 This
->resource
.heapMemory
= NULL
;
3571 This
->Flags
&= ~SFLAG_INSYSMEM
;
3576 /* Blit from offscreen surface to render target */
3577 DWORD oldCKeyFlags
= Src
->CKeyFlags
;
3578 WINEDDCOLORKEY oldBltCKey
= Src
->SrcBltCKey
;
3579 struct wined3d_context
*context
;
3581 TRACE("Blt from surface %p to rendertarget %p\n", Src
, This
);
3583 if (!(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
))
3584 && fbo_blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3585 &src_rect
, Src
->resource
.usage
, Src
->resource
.pool
, Src
->resource
.format_desc
,
3586 &dst_rect
, This
->resource
.usage
, This
->resource
.pool
, This
->resource
.format_desc
))
3588 TRACE("Using stretch_rect_fbo\n");
3589 /* The source is always a texture, but never the currently active render target, and the texture
3590 * contents are never upside down. */
3591 stretch_rect_fbo((IWineD3DDevice
*)device
, SrcSurface
, &src_rect
,
3592 (IWineD3DSurface
*)This
, &dst_rect
, Filter
);
3596 if (!(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
))
3597 && arbfp_blit
.blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3598 &src_rect
, Src
->resource
.usage
, Src
->resource
.pool
, Src
->resource
.format_desc
,
3599 &dst_rect
, This
->resource
.usage
, This
->resource
.pool
, This
->resource
.format_desc
))
3601 return arbfp_blit_surface(device
, Src
, &src_rect
, This
, &dst_rect
, BLIT_OP_BLIT
, Filter
);
3604 /* Color keying: Check if we have to do a color keyed blt,
3605 * and if not check if a color key is activated.
3607 * Just modify the color keying parameters in the surface and restore them afterwards
3608 * The surface keeps track of the color key last used to load the opengl surface.
3609 * PreLoad will catch the change to the flags and color key and reload if necessary.
3611 if(Flags
& WINEDDBLT_KEYSRC
) {
3612 /* Use color key from surface */
3613 } else if(Flags
& WINEDDBLT_KEYSRCOVERRIDE
) {
3614 /* Use color key from DDBltFx */
3615 Src
->CKeyFlags
|= WINEDDSD_CKSRCBLT
;
3616 Src
->SrcBltCKey
= DDBltFx
->ddckSrcColorkey
;
3618 /* Do not use color key */
3619 Src
->CKeyFlags
&= ~WINEDDSD_CKSRCBLT
;
3622 /* Now load the surface */
3623 surface_internal_preload((IWineD3DSurface
*) Src
, SRGB_RGB
);
3625 /* Activate the destination context, set it up for blitting */
3626 context
= context_acquire(device
, (IWineD3DSurface
*)This
, CTXUSAGE_BLIT
);
3628 /* The coordinates of the ddraw front buffer are always fullscreen ('screen coordinates',
3629 * while OpenGL coordinates are window relative.
3630 * Also beware of the origin difference(top left vs bottom left).
3631 * Also beware that the front buffer's surface size is screen width x screen height,
3632 * whereas the real gl drawable size is the size of the window.
3634 if (dstSwapchain
&& (IWineD3DSurface
*)This
== dstSwapchain
->frontBuffer
) {
3636 POINT offset
= {0,0};
3638 ClientToScreen(context
->win_handle
, &offset
);
3639 GetClientRect(context
->win_handle
, &windowsize
);
3640 h
= windowsize
.bottom
- windowsize
.top
;
3641 dst_rect
.left
-= offset
.x
; dst_rect
.right
-=offset
.x
;
3642 dst_rect
.top
-= offset
.y
; dst_rect
.bottom
-=offset
.y
;
3643 dst_rect
.top
+= This
->currentDesc
.Height
- h
; dst_rect
.bottom
+= This
->currentDesc
.Height
- h
;
3646 if (!device
->blitter
->blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3647 &src_rect
, Src
->resource
.usage
, Src
->resource
.pool
, Src
->resource
.format_desc
,
3648 &dst_rect
, This
->resource
.usage
, This
->resource
.pool
, This
->resource
.format_desc
))
3650 FIXME("Unsupported blit operation falling back to software\n");
3651 return WINED3DERR_INVALIDCALL
;
3654 device
->blitter
->set_shader((IWineD3DDevice
*)device
, Src
);
3658 /* This is for color keying */
3659 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3660 glEnable(GL_ALPHA_TEST
);
3661 checkGLcall("glEnable(GL_ALPHA_TEST)");
3663 /* When the primary render target uses P8, the alpha component contains the palette index.
3664 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3665 * should be masked away have alpha set to 0. */
3666 if (primary_render_target_is_p8(device
))
3667 glAlphaFunc(GL_NOTEQUAL
, (float)Src
->SrcBltCKey
.dwColorSpaceLowValue
/ 256.0f
);
3669 glAlphaFunc(GL_NOTEQUAL
, 0.0f
);
3670 checkGLcall("glAlphaFunc");
3672 glDisable(GL_ALPHA_TEST
);
3673 checkGLcall("glDisable(GL_ALPHA_TEST)");
3676 /* Draw a textured quad
3678 draw_textured_quad(Src
, &src_rect
, &dst_rect
, Filter
);
3680 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3681 glDisable(GL_ALPHA_TEST
);
3682 checkGLcall("glDisable(GL_ALPHA_TEST)");
3685 /* Restore the color key parameters */
3686 Src
->CKeyFlags
= oldCKeyFlags
;
3687 Src
->SrcBltCKey
= oldBltCKey
;
3691 /* Leave the opengl state valid for blitting */
3692 device
->blitter
->unset_shader((IWineD3DDevice
*)device
);
3694 if (wined3d_settings
.strict_draw_ordering
|| (dstSwapchain
3695 && ((IWineD3DSurface
*)This
== dstSwapchain
->frontBuffer
3696 || dstSwapchain
->num_contexts
> 1)))
3697 wglFlush(); /* Flush to ensure ordering across contexts. */
3699 context_release(context
);
3701 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3702 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3705 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) This
, SFLAG_INDRAWABLE
, TRUE
);
3709 /* Source-Less Blit to render target */
3710 if (Flags
& WINEDDBLT_COLORFILL
) {
3713 TRACE("Colorfill\n");
3715 /* The color as given in the Blt function is in the format of the frame-buffer...
3716 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3718 if (!surface_convert_color_to_argb(This
, DDBltFx
->u5
.dwFillColor
, &color
))
3720 /* The color conversion function already prints an error, so need to do it here */
3721 return WINED3DERR_INVALIDCALL
;
3724 if (ffp_blit
.blit_supported(&device
->adapter
->gl_info
, BLIT_OP_COLOR_FILL
,
3726 &dst_rect
, This
->resource
.usage
, This
->resource
.pool
, This
->resource
.format_desc
))
3728 return ffp_blit
.color_fill(device
, This
, &dst_rect
, color
);
3730 else if (cpu_blit
.blit_supported(&device
->adapter
->gl_info
, BLIT_OP_COLOR_FILL
,
3732 &dst_rect
, This
->resource
.usage
, This
->resource
.pool
, This
->resource
.format_desc
))
3734 return cpu_blit
.color_fill(device
, This
, &dst_rect
, color
);
3736 return WINED3DERR_INVALIDCALL
;
3740 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3741 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3742 return WINED3DERR_INVALIDCALL
;
3745 static HRESULT
IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl
*This
, const RECT
*DestRect
,
3746 IWineD3DSurface
*SrcSurface
, const RECT
*SrcRect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
)
3748 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3751 if (Flags
& WINEDDBLT_DEPTHFILL
) {
3752 switch(This
->resource
.format_desc
->format
)
3754 case WINED3DFMT_D16_UNORM
:
3755 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x0000ffff;
3757 case WINED3DFMT_S1_UINT_D15_UNORM
:
3758 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x0000fffe;
3760 case WINED3DFMT_D24_UNORM_S8_UINT
:
3761 case WINED3DFMT_X8D24_UNORM
:
3762 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x00ffffff;
3764 case WINED3DFMT_D32_UNORM
:
3765 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0xffffffff;
3769 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This
->resource
.format_desc
->format
));
3772 return IWineD3DDevice_Clear((IWineD3DDevice
*)device
, DestRect
? 1 : 0, (const WINED3DRECT
*)DestRect
,
3773 WINED3DCLEAR_ZBUFFER
, 0x00000000, depth
, 0x00000000);
3776 FIXME("(%p): Unsupp depthstencil blit\n", This
);
3777 return WINED3DERR_INVALIDCALL
;
3780 static HRESULT WINAPI
IWineD3DSurfaceImpl_Blt(IWineD3DSurface
*iface
, const RECT
*DestRect
, IWineD3DSurface
*SrcSurface
,
3781 const RECT
*SrcRect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
, WINED3DTEXTUREFILTERTYPE Filter
) {
3782 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
3783 IWineD3DSurfaceImpl
*Src
= (IWineD3DSurfaceImpl
*) SrcSurface
;
3784 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3786 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This
, DestRect
, SrcSurface
, SrcRect
, Flags
, DDBltFx
);
3787 TRACE("(%p): Usage is %s\n", This
, debug_d3dusage(This
->resource
.usage
));
3789 if ( (This
->Flags
& SFLAG_LOCKED
) || ((Src
!= NULL
) && (Src
->Flags
& SFLAG_LOCKED
)))
3791 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3792 return WINEDDERR_SURFACEBUSY
;
3795 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3796 * except depth blits, which seem to work
3798 if (iface
== device
->stencilBufferTarget
|| (SrcSurface
&& SrcSurface
== device
->stencilBufferTarget
))
3800 if (device
->inScene
&& !(Flags
& WINEDDBLT_DEPTHFILL
))
3802 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3803 return WINED3DERR_INVALIDCALL
;
3804 } else if(IWineD3DSurfaceImpl_BltZ(This
, DestRect
, SrcSurface
, SrcRect
, Flags
, DDBltFx
) == WINED3D_OK
) {
3805 TRACE("Z Blit override handled the blit\n");
3810 /* Special cases for RenderTargets */
3811 if( (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) ||
3812 ( Src
&& (Src
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) )) {
3813 if(IWineD3DSurfaceImpl_BltOverride(This
, DestRect
, SrcSurface
, SrcRect
, Flags
, DDBltFx
, Filter
) == WINED3D_OK
) return WINED3D_OK
;
3816 /* For the rest call the X11 surface implementation.
3817 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3818 * other Blts are rather rare
3820 return IWineD3DBaseSurfaceImpl_Blt(iface
, DestRect
, SrcSurface
, SrcRect
, Flags
, DDBltFx
, Filter
);
3823 static HRESULT WINAPI
IWineD3DSurfaceImpl_BltFast(IWineD3DSurface
*iface
, DWORD dstx
, DWORD dsty
,
3824 IWineD3DSurface
*Source
, const RECT
*rsrc
, DWORD trans
)
3826 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3827 IWineD3DSurfaceImpl
*srcImpl
= (IWineD3DSurfaceImpl
*) Source
;
3828 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3830 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface
, dstx
, dsty
, Source
, rsrc
, trans
);
3832 if ( (This
->Flags
& SFLAG_LOCKED
) || (srcImpl
->Flags
& SFLAG_LOCKED
))
3834 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3835 return WINEDDERR_SURFACEBUSY
;
3838 if (device
->inScene
&& (iface
== device
->stencilBufferTarget
3839 || (Source
== device
->stencilBufferTarget
)))
3841 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3842 return WINED3DERR_INVALIDCALL
;
3845 /* Special cases for RenderTargets */
3846 if( (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) ||
3847 (srcImpl
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) ) {
3849 RECT SrcRect
, DstRect
;
3852 surface_get_rect(srcImpl
, rsrc
, &SrcRect
);
3854 DstRect
.left
= dstx
;
3856 DstRect
.right
= dstx
+ SrcRect
.right
- SrcRect
.left
;
3857 DstRect
.bottom
= dsty
+ SrcRect
.bottom
- SrcRect
.top
;
3859 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3860 if(trans
& WINEDDBLTFAST_SRCCOLORKEY
)
3861 Flags
|= WINEDDBLT_KEYSRC
;
3862 if(trans
& WINEDDBLTFAST_DESTCOLORKEY
)
3863 Flags
|= WINEDDBLT_KEYDEST
;
3864 if(trans
& WINEDDBLTFAST_WAIT
)
3865 Flags
|= WINEDDBLT_WAIT
;
3866 if(trans
& WINEDDBLTFAST_DONOTWAIT
)
3867 Flags
|= WINEDDBLT_DONOTWAIT
;
3869 if(IWineD3DSurfaceImpl_BltOverride(This
, &DstRect
, Source
, &SrcRect
, Flags
, NULL
, WINED3DTEXF_POINT
) == WINED3D_OK
) return WINED3D_OK
;
3873 return IWineD3DBaseSurfaceImpl_BltFast(iface
, dstx
, dsty
, Source
, rsrc
, trans
);
3876 static HRESULT WINAPI
IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface
*iface
)
3878 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3880 IWineD3DPaletteImpl
*pal
= This
->palette
;
3882 TRACE("(%p)\n", This
);
3884 if (!pal
) return WINED3D_OK
;
3886 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
3887 || This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT_A8_UNORM
)
3889 if(This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3891 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3892 IWineD3DSurface_LoadLocation(iface
, SFLAG_INTEXTURE
, NULL
);
3894 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3895 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INDRAWABLE
, FALSE
);
3897 if(!(This
->Flags
& SFLAG_INSYSMEM
)) {
3898 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3899 IWineD3DSurface_LoadLocation(iface
, SFLAG_INSYSMEM
, NULL
);
3901 TRACE("Dirtifying surface\n");
3902 IWineD3DSurface_ModifyLocation(iface
, SFLAG_INSYSMEM
, TRUE
);
3906 if(This
->Flags
& SFLAG_DIBSECTION
) {
3907 TRACE("(%p): Updating the hdc's palette\n", This
);
3908 for (n
=0; n
<256; n
++) {
3909 col
[n
].rgbRed
= pal
->palents
[n
].peRed
;
3910 col
[n
].rgbGreen
= pal
->palents
[n
].peGreen
;
3911 col
[n
].rgbBlue
= pal
->palents
[n
].peBlue
;
3912 col
[n
].rgbReserved
= 0;
3914 SetDIBColorTable(This
->hDC
, 0, 256, col
);
3917 /* Propagate the changes to the drawable when we have a palette. */
3918 if(This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3919 IWineD3DSurface_LoadLocation(iface
, SFLAG_INDRAWABLE
, NULL
);
3924 static HRESULT WINAPI
IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface
*iface
) {
3925 /** Check against the maximum texture sizes supported by the video card **/
3926 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3927 const struct wined3d_gl_info
*gl_info
= &This
->resource
.device
->adapter
->gl_info
;
3928 unsigned int pow2Width
, pow2Height
;
3930 This
->texture_name
= 0;
3931 This
->texture_target
= GL_TEXTURE_2D
;
3933 /* Non-power2 support */
3934 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
] || gl_info
->supported
[WINE_NORMALIZED_TEXRECT
])
3936 pow2Width
= This
->currentDesc
.Width
;
3937 pow2Height
= This
->currentDesc
.Height
;
3941 /* Find the nearest pow2 match */
3942 pow2Width
= pow2Height
= 1;
3943 while (pow2Width
< This
->currentDesc
.Width
) pow2Width
<<= 1;
3944 while (pow2Height
< This
->currentDesc
.Height
) pow2Height
<<= 1;
3946 This
->pow2Width
= pow2Width
;
3947 This
->pow2Height
= pow2Height
;
3949 if (pow2Width
> This
->currentDesc
.Width
|| pow2Height
> This
->currentDesc
.Height
) {
3950 /** TODO: add support for non power two compressed textures **/
3951 if (This
->resource
.format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
3953 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3954 This
, This
->currentDesc
.Width
, This
->currentDesc
.Height
);
3955 return WINED3DERR_NOTAVAILABLE
;
3959 if(pow2Width
!= This
->currentDesc
.Width
||
3960 pow2Height
!= This
->currentDesc
.Height
) {
3961 This
->Flags
|= SFLAG_NONPOW2
;
3964 TRACE("%p\n", This
);
3965 if ((This
->pow2Width
> gl_info
->limits
.texture_size
|| This
->pow2Height
> gl_info
->limits
.texture_size
)
3966 && !(This
->resource
.usage
& (WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
)))
3968 /* one of three options
3969 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)
3970 2: Set the texture to the maximum size (bad idea)
3971 3: WARN and return WINED3DERR_NOTAVAILABLE;
3972 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.
3974 if(This
->resource
.pool
== WINED3DPOOL_DEFAULT
|| This
->resource
.pool
== WINED3DPOOL_MANAGED
)
3976 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This
);
3977 return WINED3DERR_NOTAVAILABLE
;
3980 /* We should never use this surface in combination with OpenGL! */
3981 TRACE("(%p) Creating an oversized surface: %ux%u\n", This
, This
->pow2Width
, This
->pow2Height
);
3985 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3986 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3987 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3989 if (This
->Flags
& SFLAG_NONPOW2
&& gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
3990 && !(This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
3991 && gl_info
->supported
[EXT_PALETTED_TEXTURE
]
3992 && wined3d_settings
.rendertargetlock_mode
== RTL_READTEX
))
3994 This
->texture_target
= GL_TEXTURE_RECTANGLE_ARB
;
3995 This
->pow2Width
= This
->currentDesc
.Width
;
3996 This
->pow2Height
= This
->currentDesc
.Height
;
3997 This
->Flags
&= ~(SFLAG_NONPOW2
| SFLAG_NORMCOORD
);
4001 if(This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) {
4002 switch(wined3d_settings
.offscreen_rendering_mode
) {
4003 case ORM_FBO
: This
->get_drawable_size
= get_drawable_size_fbo
; break;
4004 case ORM_BACKBUFFER
: This
->get_drawable_size
= get_drawable_size_backbuffer
; break;
4008 This
->Flags
|= SFLAG_INSYSMEM
;
4013 /* GL locking is done by the caller */
4014 static void surface_depth_blt(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
4015 GLuint texture
, GLsizei w
, GLsizei h
, GLenum target
)
4017 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4018 struct blt_info info
;
4019 GLint old_binding
= 0;
4021 glPushAttrib(GL_ENABLE_BIT
| GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
| GL_VIEWPORT_BIT
);
4023 glDisable(GL_CULL_FACE
);
4024 glDisable(GL_BLEND
);
4025 glDisable(GL_ALPHA_TEST
);
4026 glDisable(GL_SCISSOR_TEST
);
4027 glDisable(GL_STENCIL_TEST
);
4028 glEnable(GL_DEPTH_TEST
);
4029 glDepthFunc(GL_ALWAYS
);
4030 glDepthMask(GL_TRUE
);
4031 glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
4032 glViewport(0, 0, w
, h
);
4034 surface_get_blt_info(target
, NULL
, w
, h
, &info
);
4035 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
4036 glGetIntegerv(info
.binding
, &old_binding
);
4037 glBindTexture(info
.bind_target
, texture
);
4039 device
->shader_backend
->shader_select_depth_blt((IWineD3DDevice
*)device
, info
.tex_type
);
4041 glBegin(GL_TRIANGLE_STRIP
);
4042 glTexCoord3fv(info
.coords
[0]);
4043 glVertex2f(-1.0f
, -1.0f
);
4044 glTexCoord3fv(info
.coords
[1]);
4045 glVertex2f(1.0f
, -1.0f
);
4046 glTexCoord3fv(info
.coords
[2]);
4047 glVertex2f(-1.0f
, 1.0f
);
4048 glTexCoord3fv(info
.coords
[3]);
4049 glVertex2f(1.0f
, 1.0f
);
4052 glBindTexture(info
.bind_target
, old_binding
);
4056 device
->shader_backend
->shader_deselect_depth_blt((IWineD3DDevice
*)device
);
4059 void surface_modify_ds_location(IWineD3DSurfaceImpl
*surface
, DWORD location
)
4061 TRACE("surface %p, new location %#x.\n", surface
, location
);
4063 if (location
& ~SFLAG_DS_LOCATIONS
)
4064 FIXME("Invalid location (%#x) specified.\n", location
);
4066 surface
->Flags
&= ~SFLAG_DS_LOCATIONS
;
4067 surface
->Flags
|= location
;
4070 /* Context activation is done by the caller. */
4071 void surface_load_ds_location(IWineD3DSurfaceImpl
*surface
, struct wined3d_context
*context
, DWORD location
)
4073 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
4074 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4076 TRACE("surface %p, new location %#x.\n", surface
, location
);
4078 /* TODO: Make this work for modes other than FBO */
4079 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return;
4081 if (surface
->Flags
& location
)
4083 TRACE("Location (%#x) is already up to date.\n", location
);
4087 if (surface
->current_renderbuffer
)
4089 FIXME("Not supported with fixed up depth stencil.\n");
4093 if (location
== SFLAG_DS_OFFSCREEN
)
4095 if (surface
->Flags
& SFLAG_DS_ONSCREEN
)
4097 GLint old_binding
= 0;
4100 TRACE("Copying onscreen depth buffer to depth texture.\n");
4104 if (!device
->depth_blt_texture
) {
4105 glGenTextures(1, &device
->depth_blt_texture
);
4108 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4109 * directly on the FBO texture. That's because we need to flip. */
4110 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4111 if (surface
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
4113 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
4114 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
4116 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
4117 bind_target
= GL_TEXTURE_2D
;
4119 glBindTexture(bind_target
, device
->depth_blt_texture
);
4120 glCopyTexImage2D(bind_target
, surface
->texture_level
, surface
->resource
.format_desc
->glInternal
,
4121 0, 0, surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, 0);
4122 glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4123 glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4124 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
4125 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
4126 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
4127 glTexParameteri(bind_target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
4128 glBindTexture(bind_target
, old_binding
);
4130 /* Setup the destination */
4131 if (!device
->depth_blt_rb
) {
4132 gl_info
->fbo_ops
.glGenRenderbuffers(1, &device
->depth_blt_rb
);
4133 checkGLcall("glGenRenderbuffersEXT");
4135 if (device
->depth_blt_rb_w
!= surface
->currentDesc
.Width
4136 || device
->depth_blt_rb_h
!= surface
->currentDesc
.Height
)
4138 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, device
->depth_blt_rb
);
4139 checkGLcall("glBindRenderbufferEXT");
4140 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, GL_RGBA8
,
4141 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
);
4142 checkGLcall("glRenderbufferStorageEXT");
4143 device
->depth_blt_rb_w
= surface
->currentDesc
.Width
;
4144 device
->depth_blt_rb_h
= surface
->currentDesc
.Height
;
4147 context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->dst_fbo
);
4148 gl_info
->fbo_ops
.glFramebufferRenderbuffer(GL_FRAMEBUFFER
,
4149 GL_COLOR_ATTACHMENT0
, GL_RENDERBUFFER
, device
->depth_blt_rb
);
4150 checkGLcall("glFramebufferRenderbufferEXT");
4151 context_attach_depth_stencil_fbo(context
, GL_FRAMEBUFFER
, surface
, FALSE
);
4153 /* Do the actual blit */
4154 surface_depth_blt(surface
, gl_info
, device
->depth_blt_texture
,
4155 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, bind_target
);
4156 checkGLcall("depth_blt");
4158 if (context
->current_fbo
) context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->current_fbo
->id
);
4159 else context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4163 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
4167 FIXME("No up to date depth stencil location\n");
4170 else if (location
== SFLAG_DS_ONSCREEN
)
4172 if (surface
->Flags
& SFLAG_DS_OFFSCREEN
)
4174 TRACE("Copying depth texture to onscreen depth buffer.\n");
4178 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4179 surface_depth_blt(surface
, gl_info
, surface
->texture_name
,
4180 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, surface
->texture_target
);
4181 checkGLcall("depth_blt");
4183 if (context
->current_fbo
) context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->current_fbo
->id
);
4187 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
4191 FIXME("No up to date depth stencil location\n");
4196 ERR("Invalid location (%#x) specified.\n", location
);
4199 surface
->Flags
|= location
;
4202 static void WINAPI
IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface
*iface
, DWORD flag
, BOOL persistent
) {
4203 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4204 IWineD3DBaseTexture
*texture
;
4205 IWineD3DSurfaceImpl
*overlay
;
4207 TRACE("(%p)->(%s, %s)\n", iface
, debug_surflocation(flag
),
4208 persistent
? "TRUE" : "FALSE");
4210 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4212 if (surface_is_offscreen(This
))
4214 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4215 if (flag
& (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
)) flag
|= (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
);
4219 TRACE("Surface %p is an onscreen surface\n", iface
);
4224 if(((This
->Flags
& SFLAG_INTEXTURE
) && !(flag
& SFLAG_INTEXTURE
)) ||
4225 ((This
->Flags
& SFLAG_INSRGBTEX
) && !(flag
& SFLAG_INSRGBTEX
))) {
4226 if (IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&texture
) == WINED3D_OK
) {
4227 TRACE("Passing to container\n");
4228 IWineD3DBaseTexture_SetDirty(texture
, TRUE
);
4229 IWineD3DBaseTexture_Release(texture
);
4232 This
->Flags
&= ~SFLAG_LOCATIONS
;
4233 This
->Flags
|= flag
;
4235 /* Redraw emulated overlays, if any */
4236 if(flag
& SFLAG_INDRAWABLE
&& !list_empty(&This
->overlays
)) {
4237 LIST_FOR_EACH_ENTRY(overlay
, &This
->overlays
, IWineD3DSurfaceImpl
, overlay_entry
) {
4238 IWineD3DSurface_DrawOverlay((IWineD3DSurface
*) overlay
);
4242 if((This
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
)) && (flag
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
))) {
4243 if (IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&texture
) == WINED3D_OK
) {
4244 TRACE("Passing to container\n");
4245 IWineD3DBaseTexture_SetDirty(texture
, TRUE
);
4246 IWineD3DBaseTexture_Release(texture
);
4249 This
->Flags
&= ~flag
;
4252 if(!(This
->Flags
& SFLAG_LOCATIONS
)) {
4253 ERR("%p: Surface does not have any up to date location\n", This
);
4257 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl
*This
, const RECT
*rect_in
)
4259 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4260 IWineD3DSwapChainImpl
*swapchain
;
4261 struct wined3d_context
*context
;
4262 RECT src_rect
, dst_rect
;
4264 surface_get_rect(This
, rect_in
, &src_rect
);
4266 context
= context_acquire(device
, (IWineD3DSurface
*)This
, CTXUSAGE_BLIT
);
4267 if (context
->render_offscreen
)
4269 dst_rect
.left
= src_rect
.left
;
4270 dst_rect
.right
= src_rect
.right
;
4271 dst_rect
.top
= src_rect
.bottom
;
4272 dst_rect
.bottom
= src_rect
.top
;
4276 dst_rect
= src_rect
;
4279 device
->blitter
->set_shader((IWineD3DDevice
*) device
, This
);
4282 draw_textured_quad(This
, &src_rect
, &dst_rect
, WINED3DTEXF_POINT
);
4285 device
->blitter
->set_shader((IWineD3DDevice
*) device
, This
);
4287 swapchain
= (This
->Flags
& SFLAG_SWAPCHAIN
) ? (IWineD3DSwapChainImpl
*)This
->container
: NULL
;
4288 if (wined3d_settings
.strict_draw_ordering
|| (swapchain
4289 && ((IWineD3DSurface
*)This
== swapchain
->frontBuffer
4290 || swapchain
->num_contexts
> 1)))
4291 wglFlush(); /* Flush to ensure ordering across contexts. */
4293 context_release(context
);
4296 /*****************************************************************************
4297 * IWineD3DSurface::LoadLocation
4299 * Copies the current surface data from wherever it is to the requested
4300 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4301 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4302 * multiple locations, the gl texture is preferred over the drawable, which is
4303 * preferred over system memory. The PBO counts as system memory. If rect is
4304 * not NULL, only the specified rectangle is copied (only supported for
4305 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4306 * location is marked up to date after the copy.
4309 * flag: Surface location flag to be updated
4310 * rect: rectangle to be copied
4313 * WINED3D_OK on success
4314 * WINED3DERR_DEVICELOST on an internal error
4316 *****************************************************************************/
4317 static HRESULT WINAPI
IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface
*iface
, DWORD flag
, const RECT
*rect
) {
4318 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4319 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4320 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4321 struct wined3d_format_desc desc
;
4322 CONVERT_TYPES convert
;
4323 int width
, pitch
, outpitch
;
4325 BOOL drawable_read_ok
= TRUE
;
4326 BOOL in_fbo
= FALSE
;
4328 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4330 if (surface_is_offscreen(This
))
4332 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4333 * Prefer SFLAG_INTEXTURE. */
4334 if (flag
== SFLAG_INDRAWABLE
) flag
= SFLAG_INTEXTURE
;
4335 drawable_read_ok
= FALSE
;
4340 TRACE("Surface %p is an onscreen surface\n", iface
);
4344 TRACE("(%p)->(%s, %p)\n", iface
, debug_surflocation(flag
), rect
);
4346 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
4349 if(This
->Flags
& flag
) {
4350 TRACE("Location already up to date\n");
4354 if(!(This
->Flags
& SFLAG_LOCATIONS
)) {
4355 ERR("%p: Surface does not have any up to date location\n", This
);
4356 This
->Flags
|= SFLAG_LOST
;
4357 return WINED3DERR_DEVICELOST
;
4360 if(flag
== SFLAG_INSYSMEM
) {
4361 surface_prepare_system_memory(This
);
4363 /* Download the surface to system memory */
4364 if (This
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
))
4366 struct wined3d_context
*context
= NULL
;
4368 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
4370 surface_bind_and_dirtify(This
, !(This
->Flags
& SFLAG_INTEXTURE
));
4371 surface_download_data(This
, gl_info
);
4373 if (context
) context_release(context
);
4377 /* Note: It might be faster to download into a texture first. */
4378 read_from_framebuffer(This
, rect
,
4379 This
->resource
.allocatedMemory
,
4380 IWineD3DSurface_GetPitch(iface
));
4382 } else if(flag
== SFLAG_INDRAWABLE
) {
4383 if(This
->Flags
& SFLAG_INTEXTURE
) {
4384 surface_blt_to_drawable(This
, rect
);
4387 if((This
->Flags
& SFLAG_LOCATIONS
) == SFLAG_INSRGBTEX
) {
4388 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4389 * values, otherwise we get incorrect values in the target. For now go the slow way
4390 * via a system memory copy
4392 IWineD3DSurfaceImpl_LoadLocation(iface
, SFLAG_INSYSMEM
, rect
);
4395 d3dfmt_get_conv(This
, FALSE
/* We need color keying */, FALSE
/* We won't use textures */, &desc
, &convert
);
4397 /* The width is in 'length' not in bytes */
4398 width
= This
->currentDesc
.Width
;
4399 pitch
= IWineD3DSurface_GetPitch(iface
);
4401 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4402 * but it isn't set (yet) in all cases it is getting called. */
4403 if ((convert
!= NO_CONVERSION
) && (This
->Flags
& SFLAG_PBO
))
4405 struct wined3d_context
*context
= NULL
;
4407 TRACE("Removing the pbo attached to surface %p\n", This
);
4409 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
4410 surface_remove_pbo(This
, gl_info
);
4411 if (context
) context_release(context
);
4414 if((convert
!= NO_CONVERSION
) && This
->resource
.allocatedMemory
) {
4415 int height
= This
->currentDesc
.Height
;
4416 byte_count
= desc
.conv_byte_count
;
4418 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4419 outpitch
= width
* byte_count
;
4420 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4422 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4424 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4425 return WINED3DERR_OUTOFVIDEOMEMORY
;
4427 d3dfmt_convert_surface(This
->resource
.allocatedMemory
, mem
, pitch
, width
, height
, outpitch
, convert
, This
);
4429 This
->Flags
|= SFLAG_CONVERTED
;
4431 This
->Flags
&= ~SFLAG_CONVERTED
;
4432 mem
= This
->resource
.allocatedMemory
;
4433 byte_count
= desc
.byte_count
;
4436 flush_to_framebuffer_drawpixels(This
, desc
.glFormat
, desc
.glType
, byte_count
, mem
);
4438 /* Don't delete PBO memory */
4439 if((mem
!= This
->resource
.allocatedMemory
) && !(This
->Flags
& SFLAG_PBO
))
4440 HeapFree(GetProcessHeap(), 0, mem
);
4442 } else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */ {
4443 if (drawable_read_ok
&& (This
->Flags
& SFLAG_INDRAWABLE
)) {
4444 read_from_framebuffer_texture(This
, flag
== SFLAG_INSRGBTEX
);
4448 /* Upload from system memory */
4449 BOOL srgb
= flag
== SFLAG_INSRGBTEX
;
4450 struct wined3d_context
*context
= NULL
;
4452 d3dfmt_get_conv(This
, TRUE
/* We need color keying */, TRUE
/* We will use textures */,
4456 if((This
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSYSMEM
)) == SFLAG_INTEXTURE
) {
4457 /* Performance warning ... */
4458 FIXME("%p: Downloading rgb texture to reload it as srgb\n", This
);
4459 IWineD3DSurfaceImpl_LoadLocation(iface
, SFLAG_INSYSMEM
, rect
);
4462 if((This
->Flags
& (SFLAG_INSRGBTEX
| SFLAG_INSYSMEM
)) == SFLAG_INSRGBTEX
) {
4463 /* Performance warning ... */
4464 FIXME("%p: Downloading srgb texture to reload it as rgb\n", This
);
4465 IWineD3DSurfaceImpl_LoadLocation(iface
, SFLAG_INSYSMEM
, rect
);
4468 if(!(This
->Flags
& SFLAG_INSYSMEM
)) {
4469 /* Should not happen */
4470 ERR("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set\n");
4471 /* Lets hope we get it from somewhere... */
4472 IWineD3DSurfaceImpl_LoadLocation(iface
, SFLAG_INSYSMEM
, rect
);
4475 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
4477 surface_prepare_texture(This
, gl_info
, srgb
);
4478 surface_bind_and_dirtify(This
, srgb
);
4480 if(This
->CKeyFlags
& WINEDDSD_CKSRCBLT
) {
4481 This
->Flags
|= SFLAG_GLCKEY
;
4482 This
->glCKey
= This
->SrcBltCKey
;
4484 else This
->Flags
&= ~SFLAG_GLCKEY
;
4486 /* The width is in 'length' not in bytes */
4487 width
= This
->currentDesc
.Width
;
4488 pitch
= IWineD3DSurface_GetPitch(iface
);
4490 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4491 * but it isn't set (yet) in all cases it is getting called. */
4492 if((convert
!= NO_CONVERSION
) && (This
->Flags
& SFLAG_PBO
)) {
4493 TRACE("Removing the pbo attached to surface %p\n", This
);
4494 surface_remove_pbo(This
, gl_info
);
4498 /* This code is entered for texture formats which need a fixup. */
4499 int height
= This
->currentDesc
.Height
;
4501 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4502 outpitch
= width
* desc
.conv_byte_count
;
4503 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4505 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4507 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4508 if (context
) context_release(context
);
4509 return WINED3DERR_OUTOFVIDEOMEMORY
;
4511 desc
.convert(This
->resource
.allocatedMemory
, mem
, pitch
, width
, height
);
4512 } else if((convert
!= NO_CONVERSION
) && This
->resource
.allocatedMemory
) {
4513 /* This code is only entered for color keying fixups */
4514 int height
= This
->currentDesc
.Height
;
4516 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4517 outpitch
= width
* desc
.conv_byte_count
;
4518 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4520 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4522 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4523 if (context
) context_release(context
);
4524 return WINED3DERR_OUTOFVIDEOMEMORY
;
4526 d3dfmt_convert_surface(This
->resource
.allocatedMemory
, mem
, pitch
, width
, height
, outpitch
, convert
, This
);
4528 mem
= This
->resource
.allocatedMemory
;
4531 /* Make sure the correct pitch is used */
4533 glPixelStorei(GL_UNPACK_ROW_LENGTH
, width
);
4536 if (mem
|| (This
->Flags
& SFLAG_PBO
))
4537 surface_upload_data(This
, gl_info
, &desc
, srgb
, mem
);
4539 /* Restore the default pitch */
4541 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
4544 if (context
) context_release(context
);
4546 /* Don't delete PBO memory */
4547 if((mem
!= This
->resource
.allocatedMemory
) && !(This
->Flags
& SFLAG_PBO
))
4548 HeapFree(GetProcessHeap(), 0, mem
);
4553 This
->Flags
|= flag
;
4556 if (in_fbo
&& (This
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
))) {
4557 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4558 This
->Flags
|= (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
);
4564 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface
*iface
, IWineD3DBase
*container
)
4566 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4567 IWineD3DSwapChain
*swapchain
= NULL
;
4569 /* Update the drawable size method */
4571 IWineD3DBase_QueryInterface(container
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
4574 This
->get_drawable_size
= get_drawable_size_swapchain
;
4575 IWineD3DSwapChain_Release(swapchain
);
4576 } else if(This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) {
4577 switch(wined3d_settings
.offscreen_rendering_mode
) {
4578 case ORM_FBO
: This
->get_drawable_size
= get_drawable_size_fbo
; break;
4579 case ORM_BACKBUFFER
: This
->get_drawable_size
= get_drawable_size_backbuffer
; break;
4583 return IWineD3DBaseSurfaceImpl_SetContainer(iface
, container
);
4586 static WINED3DSURFTYPE WINAPI
IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface
*iface
) {
4587 return SURFACE_OPENGL
;
4590 static HRESULT WINAPI
IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface
*iface
) {
4591 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4594 /* If there's no destination surface there is nothing to do */
4595 if(!This
->overlay_dest
) return WINED3D_OK
;
4597 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4598 * update the overlay. Prevent an endless recursion
4600 if(This
->overlay_dest
->Flags
& SFLAG_INOVERLAYDRAW
) {
4603 This
->overlay_dest
->Flags
|= SFLAG_INOVERLAYDRAW
;
4604 hr
= IWineD3DSurfaceImpl_Blt((IWineD3DSurface
*) This
->overlay_dest
, &This
->overlay_destrect
,
4605 iface
, &This
->overlay_srcrect
, WINEDDBLT_WAIT
,
4606 NULL
, WINED3DTEXF_LINEAR
);
4607 This
->overlay_dest
->Flags
&= ~SFLAG_INOVERLAYDRAW
;
4612 BOOL
surface_is_offscreen(IWineD3DSurfaceImpl
*surface
)
4614 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)surface
->container
;
4616 /* Not on a swapchain - must be offscreen */
4617 if (!(surface
->Flags
& SFLAG_SWAPCHAIN
)) return TRUE
;
4619 /* The front buffer is always onscreen */
4620 if (surface
== (IWineD3DSurfaceImpl
*)swapchain
->frontBuffer
) return FALSE
;
4622 /* If the swapchain is rendered to an FBO, the backbuffer is
4623 * offscreen, otherwise onscreen */
4624 return swapchain
->render_to_fbo
;
4627 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl
=
4630 IWineD3DBaseSurfaceImpl_QueryInterface
,
4631 IWineD3DBaseSurfaceImpl_AddRef
,
4632 IWineD3DSurfaceImpl_Release
,
4633 /* IWineD3DResource */
4634 IWineD3DBaseSurfaceImpl_GetParent
,
4635 IWineD3DBaseSurfaceImpl_SetPrivateData
,
4636 IWineD3DBaseSurfaceImpl_GetPrivateData
,
4637 IWineD3DBaseSurfaceImpl_FreePrivateData
,
4638 IWineD3DBaseSurfaceImpl_SetPriority
,
4639 IWineD3DBaseSurfaceImpl_GetPriority
,
4640 IWineD3DSurfaceImpl_PreLoad
,
4641 IWineD3DSurfaceImpl_UnLoad
,
4642 IWineD3DBaseSurfaceImpl_GetType
,
4643 /* IWineD3DSurface */
4644 IWineD3DBaseSurfaceImpl_GetContainer
,
4645 IWineD3DBaseSurfaceImpl_GetDesc
,
4646 IWineD3DSurfaceImpl_LockRect
,
4647 IWineD3DSurfaceImpl_UnlockRect
,
4648 IWineD3DSurfaceImpl_GetDC
,
4649 IWineD3DSurfaceImpl_ReleaseDC
,
4650 IWineD3DSurfaceImpl_Flip
,
4651 IWineD3DSurfaceImpl_Blt
,
4652 IWineD3DBaseSurfaceImpl_GetBltStatus
,
4653 IWineD3DBaseSurfaceImpl_GetFlipStatus
,
4654 IWineD3DBaseSurfaceImpl_IsLost
,
4655 IWineD3DBaseSurfaceImpl_Restore
,
4656 IWineD3DSurfaceImpl_BltFast
,
4657 IWineD3DBaseSurfaceImpl_GetPalette
,
4658 IWineD3DBaseSurfaceImpl_SetPalette
,
4659 IWineD3DSurfaceImpl_RealizePalette
,
4660 IWineD3DBaseSurfaceImpl_SetColorKey
,
4661 IWineD3DBaseSurfaceImpl_GetPitch
,
4662 IWineD3DSurfaceImpl_SetMem
,
4663 IWineD3DBaseSurfaceImpl_SetOverlayPosition
,
4664 IWineD3DBaseSurfaceImpl_GetOverlayPosition
,
4665 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder
,
4666 IWineD3DBaseSurfaceImpl_UpdateOverlay
,
4667 IWineD3DBaseSurfaceImpl_SetClipper
,
4668 IWineD3DBaseSurfaceImpl_GetClipper
,
4670 IWineD3DSurfaceImpl_LoadTexture
,
4671 IWineD3DSurfaceImpl_BindTexture
,
4672 IWineD3DSurfaceImpl_SaveSnapshot
,
4673 IWineD3DSurfaceImpl_SetContainer
,
4674 IWineD3DBaseSurfaceImpl_GetData
,
4675 IWineD3DSurfaceImpl_SetFormat
,
4676 IWineD3DSurfaceImpl_PrivateSetup
,
4677 IWineD3DSurfaceImpl_ModifyLocation
,
4678 IWineD3DSurfaceImpl_LoadLocation
,
4679 IWineD3DSurfaceImpl_GetImplType
,
4680 IWineD3DSurfaceImpl_DrawOverlay
4683 static HRESULT
ffp_blit_alloc(IWineD3DDevice
*iface
) { return WINED3D_OK
; }
4684 /* Context activation is done by the caller. */
4685 static void ffp_blit_free(IWineD3DDevice
*iface
) { }
4687 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4688 /* Context activation is done by the caller. */
4689 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl
*surface
, const struct wined3d_gl_info
*gl_info
)
4692 BOOL colorkey_active
= (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
) ? TRUE
: FALSE
;
4694 d3dfmt_p8_init_palette(surface
, table
, colorkey_active
);
4696 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4698 GL_EXTCALL(glColorTableEXT(surface
->texture_target
, GL_RGBA
, 256, GL_RGBA
, GL_UNSIGNED_BYTE
, table
));
4702 /* Context activation is done by the caller. */
4703 static HRESULT
ffp_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
4705 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
4706 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4707 enum complex_fixup fixup
= get_complex_fixup(surface
->resource
.format_desc
->color_fixup
);
4709 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4710 * else the surface is converted in software at upload time in LoadLocation.
4712 if(fixup
== COMPLEX_FIXUP_P8
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
])
4713 ffp_blit_p8_upload_palette(surface
, gl_info
);
4716 glEnable(surface
->texture_target
);
4717 checkGLcall("glEnable(surface->texture_target)");
4722 /* Context activation is done by the caller. */
4723 static void ffp_blit_unset(IWineD3DDevice
*iface
)
4725 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
4726 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4729 glDisable(GL_TEXTURE_2D
);
4730 checkGLcall("glDisable(GL_TEXTURE_2D)");
4731 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
4733 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
4734 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4736 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
4738 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
4739 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4744 static BOOL
ffp_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4745 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4746 const struct wined3d_format_desc
*src_format_desc
,
4747 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4748 const struct wined3d_format_desc
*dst_format_desc
)
4750 enum complex_fixup src_fixup
;
4752 if (blit_op
== BLIT_OP_COLOR_FILL
)
4754 if (!(dst_usage
& WINED3DUSAGE_RENDERTARGET
))
4756 TRACE("Color fill not supported\n");
4763 src_fixup
= get_complex_fixup(src_format_desc
->color_fixup
);
4764 if (TRACE_ON(d3d_surface
) && TRACE_ON(d3d
))
4766 TRACE("Checking support for fixup:\n");
4767 dump_color_fixup_desc(src_format_desc
->color_fixup
);
4770 if (blit_op
!= BLIT_OP_BLIT
)
4772 TRACE("Unsupported blit_op=%d\n", blit_op
);
4776 if (!is_identity_fixup(dst_format_desc
->color_fixup
))
4778 TRACE("Destination fixups are not supported\n");
4782 if (src_fixup
== COMPLEX_FIXUP_P8
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
])
4784 TRACE("P8 fixup supported\n");
4788 /* We only support identity conversions. */
4789 if (is_identity_fixup(src_format_desc
->color_fixup
))
4795 TRACE("[FAILED]\n");
4799 static HRESULT
ffp_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
, const RECT
*dst_rect
, DWORD fill_color
)
4801 return IWineD3DDeviceImpl_ClearSurface(device
, dst_surface
, 1 /* Number of rectangles */,
4802 (const WINED3DRECT
*)dst_rect
, WINED3DCLEAR_TARGET
, fill_color
, 0.0f
/* Z */, 0 /* Stencil */);
4805 const struct blit_shader ffp_blit
= {
4814 static HRESULT
cpu_blit_alloc(IWineD3DDevice
*iface
)
4819 /* Context activation is done by the caller. */
4820 static void cpu_blit_free(IWineD3DDevice
*iface
)
4824 /* Context activation is done by the caller. */
4825 static HRESULT
cpu_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
4830 /* Context activation is done by the caller. */
4831 static void cpu_blit_unset(IWineD3DDevice
*iface
)
4835 static BOOL
cpu_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4836 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4837 const struct wined3d_format_desc
*src_format_desc
,
4838 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4839 const struct wined3d_format_desc
*dst_format_desc
)
4841 if (blit_op
== BLIT_OP_COLOR_FILL
)
4849 static HRESULT
cpu_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
, const RECT
*dst_rect
, DWORD fill_color
)
4852 memset(&BltFx
, 0, sizeof(BltFx
));
4853 BltFx
.dwSize
= sizeof(BltFx
);
4854 BltFx
.u5
.dwFillColor
= color_convert_argb_to_fmt(fill_color
, dst_surface
->resource
.format_desc
->format
);
4855 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface
*)dst_surface
, dst_rect
, NULL
, NULL
, WINEDDBLT_COLORFILL
, &BltFx
, WINED3DTEXF_POINT
);
4858 const struct blit_shader cpu_blit
= {
4867 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4868 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4869 const struct wined3d_format_desc
*src_format_desc
,
4870 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4871 const struct wined3d_format_desc
*dst_format_desc
)
4873 if ((wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) || !gl_info
->fbo_ops
.glBlitFramebuffer
)
4876 /* We only support blitting. Things like color keying / color fill should
4877 * be handled by other blitters.
4879 if (blit_op
!= BLIT_OP_BLIT
)
4882 /* Source and/or destination need to be on the GL side */
4883 if (src_pool
== WINED3DPOOL_SYSTEMMEM
|| dst_pool
== WINED3DPOOL_SYSTEMMEM
)
4886 if(!((src_format_desc
->Flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE
) || (src_usage
& WINED3DUSAGE_RENDERTARGET
))
4887 && ((dst_format_desc
->Flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE
) || (dst_usage
& WINED3DUSAGE_RENDERTARGET
)))
4890 if (!is_identity_fixup(src_format_desc
->color_fixup
) ||
4891 !is_identity_fixup(dst_format_desc
->color_fixup
))
4894 if (!(src_format_desc
->format
== dst_format_desc
->format
4895 || (is_identity_fixup(src_format_desc
->color_fixup
)
4896 && is_identity_fixup(dst_format_desc
->color_fixup
))))