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 static void surface_cleanup(IWineD3DSurfaceImpl
*This
)
39 TRACE("(%p) : Cleaning up.\n", This
);
41 if (This
->texture_name
|| (This
->Flags
& SFLAG_PBO
) || !list_empty(&This
->renderbuffers
))
43 const struct wined3d_gl_info
*gl_info
;
44 renderbuffer_entry_t
*entry
, *entry2
;
45 struct wined3d_context
*context
;
47 context
= context_acquire(This
->resource
.device
, NULL
);
48 gl_info
= context
->gl_info
;
52 if (This
->texture_name
)
54 TRACE("Deleting texture %u.\n", This
->texture_name
);
55 glDeleteTextures(1, &This
->texture_name
);
58 if (This
->Flags
& SFLAG_PBO
)
60 TRACE("Deleting PBO %u.\n", This
->pbo
);
61 GL_EXTCALL(glDeleteBuffersARB(1, &This
->pbo
));
64 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &This
->renderbuffers
, renderbuffer_entry_t
, entry
)
66 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
67 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
68 HeapFree(GetProcessHeap(), 0, entry
);
73 context_release(context
);
76 if (This
->Flags
& SFLAG_DIBSECTION
)
79 SelectObject(This
->hDC
, This
->dib
.holdbitmap
);
81 /* Release the DIB section. */
82 DeleteObject(This
->dib
.DIBsection
);
83 This
->dib
.bitmap_data
= NULL
;
84 This
->resource
.allocatedMemory
= NULL
;
87 if (This
->Flags
& SFLAG_USERPTR
) IWineD3DSurface_SetMem((IWineD3DSurface
*)This
, NULL
);
88 if (This
->overlay_dest
) list_remove(&This
->overlay_entry
);
90 HeapFree(GetProcessHeap(), 0, This
->palette9
);
92 resource_cleanup((IWineD3DResource
*)This
);
95 void surface_set_container(IWineD3DSurfaceImpl
*surface
, enum wined3d_container_type type
, IWineD3DBase
*container
)
97 TRACE("surface %p, container %p.\n", surface
, container
);
99 if (!container
&& type
!= WINED3D_CONTAINER_NONE
)
100 ERR("Setting NULL container of type %#x.\n", type
);
102 if (type
== WINED3D_CONTAINER_SWAPCHAIN
)
104 surface
->get_drawable_size
= get_drawable_size_swapchain
;
108 switch (wined3d_settings
.offscreen_rendering_mode
)
111 surface
->get_drawable_size
= get_drawable_size_fbo
;
115 surface
->get_drawable_size
= get_drawable_size_backbuffer
;
119 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
124 surface
->container
.type
= type
;
125 surface
->container
.u
.base
= container
;
132 enum tex_types tex_type
;
133 GLfloat coords
[4][3];
144 static inline void cube_coords_float(const RECT
*r
, UINT w
, UINT h
, struct float_rect
*f
)
146 f
->l
= ((r
->left
* 2.0f
) / w
) - 1.0f
;
147 f
->t
= ((r
->top
* 2.0f
) / h
) - 1.0f
;
148 f
->r
= ((r
->right
* 2.0f
) / w
) - 1.0f
;
149 f
->b
= ((r
->bottom
* 2.0f
) / h
) - 1.0f
;
152 static void surface_get_blt_info(GLenum target
, const RECT
*rect_in
, GLsizei w
, GLsizei h
, struct blt_info
*info
)
154 GLfloat (*coords
)[3] = info
->coords
;
171 FIXME("Unsupported texture target %#x\n", target
);
172 /* Fall back to GL_TEXTURE_2D */
174 info
->binding
= GL_TEXTURE_BINDING_2D
;
175 info
->bind_target
= GL_TEXTURE_2D
;
176 info
->tex_type
= tex_2d
;
177 coords
[0][0] = (float)rect
.left
/ w
;
178 coords
[0][1] = (float)rect
.top
/ h
;
181 coords
[1][0] = (float)rect
.right
/ w
;
182 coords
[1][1] = (float)rect
.top
/ h
;
185 coords
[2][0] = (float)rect
.left
/ w
;
186 coords
[2][1] = (float)rect
.bottom
/ h
;
189 coords
[3][0] = (float)rect
.right
/ w
;
190 coords
[3][1] = (float)rect
.bottom
/ h
;
194 case GL_TEXTURE_RECTANGLE_ARB
:
195 info
->binding
= GL_TEXTURE_BINDING_RECTANGLE_ARB
;
196 info
->bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
197 info
->tex_type
= tex_rect
;
198 coords
[0][0] = rect
.left
; coords
[0][1] = rect
.top
; coords
[0][2] = 0.0f
;
199 coords
[1][0] = rect
.right
; coords
[1][1] = rect
.top
; coords
[1][2] = 0.0f
;
200 coords
[2][0] = rect
.left
; coords
[2][1] = rect
.bottom
; coords
[2][2] = 0.0f
;
201 coords
[3][0] = rect
.right
; coords
[3][1] = rect
.bottom
; coords
[3][2] = 0.0f
;
204 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
205 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
206 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
207 info
->tex_type
= tex_cube
;
208 cube_coords_float(&rect
, w
, h
, &f
);
210 coords
[0][0] = 1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = -f
.l
;
211 coords
[1][0] = 1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = -f
.r
;
212 coords
[2][0] = 1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = -f
.l
;
213 coords
[3][0] = 1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = -f
.r
;
216 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
217 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
218 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
219 info
->tex_type
= tex_cube
;
220 cube_coords_float(&rect
, w
, h
, &f
);
222 coords
[0][0] = -1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = f
.l
;
223 coords
[1][0] = -1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = f
.r
;
224 coords
[2][0] = -1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = f
.l
;
225 coords
[3][0] = -1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = f
.r
;
228 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
229 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
230 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
231 info
->tex_type
= tex_cube
;
232 cube_coords_float(&rect
, w
, h
, &f
);
234 coords
[0][0] = f
.l
; coords
[0][1] = 1.0f
; coords
[0][2] = f
.t
;
235 coords
[1][0] = f
.r
; coords
[1][1] = 1.0f
; coords
[1][2] = f
.t
;
236 coords
[2][0] = f
.l
; coords
[2][1] = 1.0f
; coords
[2][2] = f
.b
;
237 coords
[3][0] = f
.r
; coords
[3][1] = 1.0f
; coords
[3][2] = f
.b
;
240 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
241 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
242 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
243 info
->tex_type
= tex_cube
;
244 cube_coords_float(&rect
, w
, h
, &f
);
246 coords
[0][0] = f
.l
; coords
[0][1] = -1.0f
; coords
[0][2] = -f
.t
;
247 coords
[1][0] = f
.r
; coords
[1][1] = -1.0f
; coords
[1][2] = -f
.t
;
248 coords
[2][0] = f
.l
; coords
[2][1] = -1.0f
; coords
[2][2] = -f
.b
;
249 coords
[3][0] = f
.r
; coords
[3][1] = -1.0f
; coords
[3][2] = -f
.b
;
252 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
253 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
254 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
255 info
->tex_type
= tex_cube
;
256 cube_coords_float(&rect
, w
, h
, &f
);
258 coords
[0][0] = f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = 1.0f
;
259 coords
[1][0] = f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = 1.0f
;
260 coords
[2][0] = f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = 1.0f
;
261 coords
[3][0] = f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = 1.0f
;
264 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
265 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
266 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
267 info
->tex_type
= tex_cube
;
268 cube_coords_float(&rect
, w
, h
, &f
);
270 coords
[0][0] = -f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = -1.0f
;
271 coords
[1][0] = -f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = -1.0f
;
272 coords
[2][0] = -f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = -1.0f
;
273 coords
[3][0] = -f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = -1.0f
;
278 static inline void surface_get_rect(IWineD3DSurfaceImpl
*This
, const RECT
*rect_in
, RECT
*rect_out
)
281 *rect_out
= *rect_in
;
286 rect_out
->right
= This
->currentDesc
.Width
;
287 rect_out
->bottom
= This
->currentDesc
.Height
;
291 /* GL locking and context activation is done by the caller */
292 void draw_textured_quad(IWineD3DSurfaceImpl
*src_surface
, const RECT
*src_rect
, const RECT
*dst_rect
, WINED3DTEXTUREFILTERTYPE Filter
)
294 struct blt_info info
;
296 surface_get_blt_info(src_surface
->texture_target
, src_rect
, src_surface
->pow2Width
, src_surface
->pow2Height
, &info
);
298 glEnable(info
.bind_target
);
299 checkGLcall("glEnable(bind_target)");
301 /* Bind the texture */
302 glBindTexture(info
.bind_target
, src_surface
->texture_name
);
303 checkGLcall("glBindTexture");
305 /* Filtering for StretchRect */
306 glTexParameteri(info
.bind_target
, GL_TEXTURE_MAG_FILTER
,
307 wined3d_gl_mag_filter(magLookup
, Filter
));
308 checkGLcall("glTexParameteri");
309 glTexParameteri(info
.bind_target
, GL_TEXTURE_MIN_FILTER
,
310 wined3d_gl_min_mip_filter(minMipLookup
, Filter
, WINED3DTEXF_NONE
));
311 checkGLcall("glTexParameteri");
312 glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
313 glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
314 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
315 checkGLcall("glTexEnvi");
318 glBegin(GL_TRIANGLE_STRIP
);
319 glTexCoord3fv(info
.coords
[0]);
320 glVertex2i(dst_rect
->left
, dst_rect
->top
);
322 glTexCoord3fv(info
.coords
[1]);
323 glVertex2i(dst_rect
->right
, dst_rect
->top
);
325 glTexCoord3fv(info
.coords
[2]);
326 glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
328 glTexCoord3fv(info
.coords
[3]);
329 glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
332 /* Unbind the texture */
333 glBindTexture(info
.bind_target
, 0);
334 checkGLcall("glBindTexture(info->bind_target, 0)");
336 /* We changed the filtering settings on the texture. Inform the
337 * container about this to get the filters reset properly next draw. */
338 if (src_surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
340 IWineD3DBaseTextureImpl
*texture
= src_surface
->container
.u
.texture
;
341 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
342 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
343 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
347 HRESULT
surface_init(IWineD3DSurfaceImpl
*surface
, WINED3DSURFTYPE surface_type
, UINT alignment
,
348 UINT width
, UINT height
, UINT level
, BOOL lockable
, BOOL discard
, WINED3DMULTISAMPLE_TYPE multisample_type
,
349 UINT multisample_quality
, IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
,
350 WINED3DPOOL pool
, IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
352 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
353 const struct wined3d_format_desc
*format_desc
= getFormatDescEntry(format_id
, gl_info
);
354 void (*cleanup
)(IWineD3DSurfaceImpl
*This
);
355 unsigned int resource_size
;
358 if (multisample_quality
> 0)
360 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality
);
361 multisample_quality
= 0;
364 /* FIXME: Check that the format is supported by the device. */
366 resource_size
= wined3d_format_calculate_size(format_desc
, alignment
, width
, height
);
368 /* Look at the implementation and set the correct Vtable. */
369 switch (surface_type
)
372 surface
->lpVtbl
= &IWineD3DSurface_Vtbl
;
373 cleanup
= surface_cleanup
;
377 surface
->lpVtbl
= &IWineGDISurface_Vtbl
;
378 cleanup
= surface_gdi_cleanup
;
382 ERR("Requested unknown surface implementation %#x.\n", surface_type
);
383 return WINED3DERR_INVALIDCALL
;
386 hr
= resource_init((IWineD3DResource
*)surface
, WINED3DRTYPE_SURFACE
,
387 device
, resource_size
, usage
, format_desc
, pool
, parent
, parent_ops
);
390 WARN("Failed to initialize resource, returning %#x.\n", hr
);
394 /* "Standalone" surface. */
395 surface_set_container(surface
, WINED3D_CONTAINER_NONE
, NULL
);
397 surface
->currentDesc
.Width
= width
;
398 surface
->currentDesc
.Height
= height
;
399 surface
->currentDesc
.MultiSampleType
= multisample_type
;
400 surface
->currentDesc
.MultiSampleQuality
= multisample_quality
;
401 surface
->texture_level
= level
;
402 list_init(&surface
->overlays
);
405 surface
->Flags
= SFLAG_NORMCOORD
; /* Default to normalized coords. */
406 if (discard
) surface
->Flags
|= SFLAG_DISCARD
;
407 if (lockable
|| format_id
== WINED3DFMT_D16_LOCKABLE
) surface
->Flags
|= SFLAG_LOCKABLE
;
409 /* Quick lockable sanity check.
410 * TODO: remove this after surfaces, usage and lockability have been debugged properly
411 * this function is too deep to need to care about things like this.
412 * Levels need to be checked too, since they all affect what can be done. */
415 case WINED3DPOOL_SCRATCH
:
418 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
419 "which are mutually exclusive, setting lockable to TRUE.\n");
424 case WINED3DPOOL_SYSTEMMEM
:
426 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
429 case WINED3DPOOL_MANAGED
:
430 if (usage
& WINED3DUSAGE_DYNAMIC
)
431 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
434 case WINED3DPOOL_DEFAULT
:
435 if (lockable
&& !(usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
)))
436 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
440 FIXME("Unknown pool %#x.\n", pool
);
444 if (usage
& WINED3DUSAGE_RENDERTARGET
&& pool
!= WINED3DPOOL_DEFAULT
)
446 FIXME("Trying to create a render target that isn't in the default pool.\n");
449 /* Mark the texture as dirty so that it gets loaded first time around. */
450 surface_add_dirty_rect(surface
, NULL
);
451 list_init(&surface
->renderbuffers
);
453 TRACE("surface %p, memory %p, size %u\n", surface
, surface
->resource
.allocatedMemory
, surface
->resource
.size
);
455 /* Call the private setup routine */
456 hr
= IWineD3DSurface_PrivateSetup((IWineD3DSurface
*)surface
);
459 ERR("Private setup failed, returning %#x\n", hr
);
467 static void surface_force_reload(IWineD3DSurfaceImpl
*surface
)
469 surface
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
472 void surface_set_texture_name(IWineD3DSurfaceImpl
*surface
, GLuint new_name
, BOOL srgb
)
477 TRACE("surface %p, new_name %u, srgb %#x.\n", surface
, new_name
, srgb
);
481 name
= &surface
->texture_name_srgb
;
482 flag
= SFLAG_INSRGBTEX
;
486 name
= &surface
->texture_name
;
487 flag
= SFLAG_INTEXTURE
;
490 if (!*name
&& new_name
)
492 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
493 * surface has no texture name yet. See if we can get rid of this. */
494 if (surface
->Flags
& flag
)
495 ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag
));
496 surface_modify_location(surface
, flag
, FALSE
);
500 surface_force_reload(surface
);
503 void surface_set_texture_target(IWineD3DSurfaceImpl
*surface
, GLenum target
)
505 TRACE("surface %p, target %#x.\n", surface
, target
);
507 if (surface
->texture_target
!= target
)
509 if (target
== GL_TEXTURE_RECTANGLE_ARB
)
511 surface
->Flags
&= ~SFLAG_NORMCOORD
;
513 else if (surface
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
515 surface
->Flags
|= SFLAG_NORMCOORD
;
518 surface
->texture_target
= target
;
519 surface_force_reload(surface
);
522 /* Context activation is done by the caller. */
523 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl
*This
, BOOL srgb
) {
524 DWORD active_sampler
;
526 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
527 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
528 * gl states. The current texture unit should always be a valid one.
530 * To be more specific, this is tricky because we can implicitly be called
531 * from sampler() in state.c. This means we can't touch anything other than
532 * whatever happens to be the currently active texture, or we would risk
533 * marking already applied sampler states dirty again.
535 * TODO: Track the current active texture per GL context instead of using glGet
537 GLint active_texture
;
539 glGetIntegerv(GL_ACTIVE_TEXTURE
, &active_texture
);
541 active_sampler
= This
->resource
.device
->rev_tex_unit_map
[active_texture
- GL_TEXTURE0_ARB
];
543 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
545 IWineD3DDeviceImpl_MarkStateDirty(This
->resource
.device
, STATE_SAMPLER(active_sampler
));
547 IWineD3DSurface_BindTexture((IWineD3DSurface
*)This
, srgb
);
550 /* This function checks if the primary render target uses the 8bit paletted format. */
551 static BOOL
primary_render_target_is_p8(IWineD3DDeviceImpl
*device
)
553 if (device
->render_targets
&& device
->render_targets
[0])
555 IWineD3DSurfaceImpl
*render_target
= device
->render_targets
[0];
556 if ((render_target
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
557 && (render_target
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
))
563 /* This call just downloads data, the caller is responsible for binding the
564 * correct texture. */
565 /* Context activation is done by the caller. */
566 static void surface_download_data(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
)
568 const struct wined3d_format_desc
*format_desc
= This
->resource
.format_desc
;
570 /* Only support read back of converted P8 surfaces */
571 if (This
->Flags
& SFLAG_CONVERTED
&& format_desc
->id
!= WINED3DFMT_P8_UINT
)
573 FIXME("Readback conversion not supported for format %s.\n", debug_d3dformat(format_desc
->id
));
579 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
581 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
582 This
, This
->texture_level
, format_desc
->glFormat
, format_desc
->glType
,
583 This
->resource
.allocatedMemory
);
585 if (This
->Flags
& SFLAG_PBO
)
587 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
588 checkGLcall("glBindBufferARB");
589 GL_EXTCALL(glGetCompressedTexImageARB(This
->texture_target
, This
->texture_level
, NULL
));
590 checkGLcall("glGetCompressedTexImageARB");
591 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
592 checkGLcall("glBindBufferARB");
596 GL_EXTCALL(glGetCompressedTexImageARB(This
->texture_target
,
597 This
->texture_level
, This
->resource
.allocatedMemory
));
598 checkGLcall("glGetCompressedTexImageARB");
604 GLenum format
= format_desc
->glFormat
;
605 GLenum type
= format_desc
->glType
;
609 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
610 if (format_desc
->id
== WINED3DFMT_P8_UINT
&& primary_render_target_is_p8(This
->resource
.device
))
613 type
= GL_UNSIGNED_BYTE
;
616 if (This
->Flags
& SFLAG_NONPOW2
) {
617 unsigned char alignment
= This
->resource
.device
->surface_alignment
;
618 src_pitch
= format_desc
->byte_count
* This
->pow2Width
;
619 dst_pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*) This
);
620 src_pitch
= (src_pitch
+ alignment
- 1) & ~(alignment
- 1);
621 mem
= HeapAlloc(GetProcessHeap(), 0, src_pitch
* This
->pow2Height
);
623 mem
= This
->resource
.allocatedMemory
;
626 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
627 This
, This
->texture_level
, format
, type
, mem
);
629 if(This
->Flags
& SFLAG_PBO
) {
630 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
631 checkGLcall("glBindBufferARB");
633 glGetTexImage(This
->texture_target
, This
->texture_level
, format
, type
, NULL
);
634 checkGLcall("glGetTexImage");
636 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
637 checkGLcall("glBindBufferARB");
639 glGetTexImage(This
->texture_target
, This
->texture_level
, format
, type
, mem
);
640 checkGLcall("glGetTexImage");
644 if (This
->Flags
& SFLAG_NONPOW2
) {
645 const BYTE
*src_data
;
649 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
650 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
651 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
653 * We're doing this...
655 * instead of boxing the texture :
656 * |<-texture width ->| -->pow2width| /\
657 * |111111111111111111| | |
658 * |222 Texture 222222| boxed empty | texture height
659 * |3333 Data 33333333| | |
660 * |444444444444444444| | \/
661 * ----------------------------------- |
662 * | boxed empty | boxed empty | pow2height
664 * -----------------------------------
667 * we're repacking the data to the expected texture width
669 * |<-texture width ->| -->pow2width| /\
670 * |111111111111111111222222222222222| |
671 * |222333333333333333333444444444444| texture height
675 * | empty | pow2height
677 * -----------------------------------
681 * |<-texture width ->| /\
682 * |111111111111111111|
683 * |222222222222222222|texture height
684 * |333333333333333333|
685 * |444444444444444444| \/
686 * --------------------
688 * this also means that any references to allocatedMemory should work with the data as if were a
689 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
691 * internally the texture is still stored in a boxed format so any references to textureName will
692 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
694 * Performance should not be an issue, because applications normally do not lock the surfaces when
695 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
696 * and doesn't have to be re-read.
699 dst_data
= This
->resource
.allocatedMemory
;
700 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This
, src_pitch
, dst_pitch
);
701 for (y
= 1 ; y
< This
->currentDesc
.Height
; y
++) {
702 /* skip the first row */
703 src_data
+= src_pitch
;
704 dst_data
+= dst_pitch
;
705 memcpy(dst_data
, src_data
, dst_pitch
);
708 HeapFree(GetProcessHeap(), 0, mem
);
712 /* Surface has now been downloaded */
713 This
->Flags
|= SFLAG_INSYSMEM
;
716 /* This call just uploads data, the caller is responsible for binding the
717 * correct texture. */
718 /* Context activation is done by the caller. */
719 static void surface_upload_data(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
720 const struct wined3d_format_desc
*format_desc
, BOOL srgb
, const GLvoid
*data
)
722 GLsizei width
= This
->currentDesc
.Width
;
723 GLsizei height
= This
->currentDesc
.Height
;
728 internal
= format_desc
->glGammaInternal
;
730 else if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
&& surface_is_offscreen(This
))
732 internal
= format_desc
->rtInternal
;
736 internal
= format_desc
->glInternal
;
739 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
740 This
, internal
, width
, height
, format_desc
->glFormat
, format_desc
->glType
, data
);
741 TRACE("target %#x, level %u, resource size %u.\n",
742 This
->texture_target
, This
->texture_level
, This
->resource
.size
);
744 if (format_desc
->heightscale
!= 1.0f
&& format_desc
->heightscale
!= 0.0f
) height
*= format_desc
->heightscale
;
748 if (This
->Flags
& SFLAG_PBO
)
750 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
751 checkGLcall("glBindBufferARB");
753 TRACE("(%p) pbo: %#x, data: %p.\n", This
, This
->pbo
, data
);
757 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
759 TRACE("Calling glCompressedTexSubImage2DARB.\n");
761 GL_EXTCALL(glCompressedTexSubImage2DARB(This
->texture_target
, This
->texture_level
,
762 0, 0, width
, height
, internal
, This
->resource
.size
, data
));
763 checkGLcall("glCompressedTexSubImage2DARB");
767 TRACE("Calling glTexSubImage2D.\n");
769 glTexSubImage2D(This
->texture_target
, This
->texture_level
,
770 0, 0, width
, height
, format_desc
->glFormat
, format_desc
->glType
, data
);
771 checkGLcall("glTexSubImage2D");
774 if (This
->Flags
& SFLAG_PBO
)
776 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
777 checkGLcall("glBindBufferARB");
782 if (gl_info
->quirks
& WINED3D_QUIRK_FBO_TEX_UPDATE
)
784 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
787 for (i
= 0; i
< device
->numContexts
; ++i
)
789 context_surface_update(device
->contexts
[i
], This
);
794 /* This call just allocates the texture, the caller is responsible for binding
795 * the correct texture. */
796 /* Context activation is done by the caller. */
797 static void surface_allocate_surface(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
798 const struct wined3d_format_desc
*format_desc
, BOOL srgb
)
800 BOOL enable_client_storage
= FALSE
;
801 GLsizei width
= This
->pow2Width
;
802 GLsizei height
= This
->pow2Height
;
803 const BYTE
*mem
= NULL
;
808 internal
= format_desc
->glGammaInternal
;
810 else if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
&& surface_is_offscreen(This
))
812 internal
= format_desc
->rtInternal
;
816 internal
= format_desc
->glInternal
;
819 if (format_desc
->heightscale
!= 1.0f
&& format_desc
->heightscale
!= 0.0f
) height
*= format_desc
->heightscale
;
821 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",
822 This
, This
->texture_target
, This
->texture_level
, debug_d3dformat(format_desc
->id
),
823 internal
, width
, height
, format_desc
->glFormat
, format_desc
->glType
);
827 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
829 if(This
->Flags
& (SFLAG_NONPOW2
| SFLAG_DIBSECTION
| SFLAG_CONVERTED
) || This
->resource
.allocatedMemory
== NULL
) {
830 /* In some cases we want to disable client storage.
831 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
832 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
833 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
834 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
836 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
837 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
838 This
->Flags
&= ~SFLAG_CLIENT
;
839 enable_client_storage
= TRUE
;
841 This
->Flags
|= SFLAG_CLIENT
;
843 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
844 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
846 mem
= (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
850 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
&& mem
)
852 GL_EXTCALL(glCompressedTexImage2DARB(This
->texture_target
, This
->texture_level
,
853 internal
, width
, height
, 0, This
->resource
.size
, mem
));
854 checkGLcall("glCompressedTexImage2DARB");
858 glTexImage2D(This
->texture_target
, This
->texture_level
,
859 internal
, width
, height
, 0, format_desc
->glFormat
, format_desc
->glType
, mem
);
860 checkGLcall("glTexImage2D");
863 if(enable_client_storage
) {
864 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
865 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
870 /* In D3D the depth stencil dimensions have to be greater than or equal to the
871 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
872 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
873 /* GL locking is done by the caller */
874 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl
*surface
, unsigned int width
, unsigned int height
)
876 const struct wined3d_gl_info
*gl_info
= &surface
->resource
.device
->adapter
->gl_info
;
877 renderbuffer_entry_t
*entry
;
878 GLuint renderbuffer
= 0;
879 unsigned int src_width
, src_height
;
881 src_width
= surface
->pow2Width
;
882 src_height
= surface
->pow2Height
;
884 /* A depth stencil smaller than the render target is not valid */
885 if (width
> src_width
|| height
> src_height
) return;
887 /* Remove any renderbuffer set if the sizes match */
888 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
]
889 || (width
== src_width
&& height
== src_height
))
891 surface
->current_renderbuffer
= NULL
;
895 /* Look if we've already got a renderbuffer of the correct dimensions */
896 LIST_FOR_EACH_ENTRY(entry
, &surface
->renderbuffers
, renderbuffer_entry_t
, entry
)
898 if (entry
->width
== width
&& entry
->height
== height
)
900 renderbuffer
= entry
->id
;
901 surface
->current_renderbuffer
= entry
;
908 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
909 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
910 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
,
911 surface
->resource
.format_desc
->glInternal
, width
, height
);
913 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t
));
914 entry
->width
= width
;
915 entry
->height
= height
;
916 entry
->id
= renderbuffer
;
917 list_add_head(&surface
->renderbuffers
, &entry
->entry
);
919 surface
->current_renderbuffer
= entry
;
922 checkGLcall("set_compatible_renderbuffer");
925 GLenum
surface_get_gl_buffer(IWineD3DSurfaceImpl
*surface
)
927 IWineD3DSwapChainImpl
*swapchain
= surface
->container
.u
.swapchain
;
929 TRACE("surface %p.\n", surface
);
931 if (surface
->container
.type
!= WINED3D_CONTAINER_SWAPCHAIN
)
933 ERR("Surface %p is not on a swapchain.\n", surface
);
937 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0] == surface
)
939 if (swapchain
->render_to_fbo
)
941 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
942 return GL_COLOR_ATTACHMENT0
;
944 TRACE("Returning GL_BACK\n");
947 else if (surface
== swapchain
->front_buffer
)
949 TRACE("Returning GL_FRONT\n");
953 FIXME("Higher back buffer, returning GL_BACK\n");
957 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
958 void surface_add_dirty_rect(IWineD3DSurfaceImpl
*surface
, const RECT
*dirty_rect
)
960 TRACE("surface %p, dirty_rect %s.\n", surface
, wine_dbgstr_rect(dirty_rect
));
962 if (!(surface
->Flags
& SFLAG_INSYSMEM
) && (surface
->Flags
& SFLAG_INTEXTURE
))
963 /* No partial locking for textures yet. */
964 surface_load_location(surface
, SFLAG_INSYSMEM
, NULL
);
966 surface_modify_location(surface
, SFLAG_INSYSMEM
, TRUE
);
969 surface
->dirtyRect
.left
= min(surface
->dirtyRect
.left
, dirty_rect
->left
);
970 surface
->dirtyRect
.top
= min(surface
->dirtyRect
.top
, dirty_rect
->top
);
971 surface
->dirtyRect
.right
= max(surface
->dirtyRect
.right
, dirty_rect
->right
);
972 surface
->dirtyRect
.bottom
= max(surface
->dirtyRect
.bottom
, dirty_rect
->bottom
);
976 surface
->dirtyRect
.left
= 0;
977 surface
->dirtyRect
.top
= 0;
978 surface
->dirtyRect
.right
= surface
->currentDesc
.Width
;
979 surface
->dirtyRect
.bottom
= surface
->currentDesc
.Height
;
982 /* if the container is a basetexture then mark it dirty. */
983 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
985 TRACE("Passing to container.\n");
986 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture
*)surface
->container
.u
.texture
, TRUE
);
990 static BOOL
surface_convert_color_to_float(IWineD3DSurfaceImpl
*surface
, DWORD color
, WINED3DCOLORVALUE
*float_color
)
992 const struct wined3d_format_desc
*format
= surface
->resource
.format_desc
;
993 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
997 case WINED3DFMT_P8_UINT
:
998 if (surface
->palette
)
1000 float_color
->r
= surface
->palette
->palents
[color
].peRed
/ 255.0f
;
1001 float_color
->g
= surface
->palette
->palents
[color
].peGreen
/ 255.0f
;
1002 float_color
->b
= surface
->palette
->palents
[color
].peBlue
/ 255.0f
;
1006 float_color
->r
= 0.0f
;
1007 float_color
->g
= 0.0f
;
1008 float_color
->b
= 0.0f
;
1010 float_color
->a
= primary_render_target_is_p8(device
) ? color
/ 255.0f
: 1.0f
;
1013 case WINED3DFMT_B5G6R5_UNORM
:
1014 float_color
->r
= ((color
>> 11) & 0x1f) / 31.0f
;
1015 float_color
->g
= ((color
>> 5) & 0x3f) / 63.0f
;
1016 float_color
->b
= (color
& 0x1f) / 31.0f
;
1017 float_color
->a
= 1.0f
;
1020 case WINED3DFMT_B8G8R8_UNORM
:
1021 case WINED3DFMT_B8G8R8X8_UNORM
:
1022 float_color
->r
= D3DCOLOR_R(color
);
1023 float_color
->g
= D3DCOLOR_G(color
);
1024 float_color
->b
= D3DCOLOR_B(color
);
1025 float_color
->a
= 1.0f
;
1028 case WINED3DFMT_B8G8R8A8_UNORM
:
1029 float_color
->r
= D3DCOLOR_R(color
);
1030 float_color
->g
= D3DCOLOR_G(color
);
1031 float_color
->b
= D3DCOLOR_B(color
);
1032 float_color
->a
= D3DCOLOR_A(color
);
1036 ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format
->id
));
1043 static ULONG WINAPI
IWineD3DSurfaceImpl_Release(IWineD3DSurface
*iface
)
1045 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1046 ULONG ref
= InterlockedDecrement(&This
->resource
.ref
);
1047 TRACE("(%p) : Releasing from %d\n", This
, ref
+ 1);
1051 surface_cleanup(This
);
1052 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
1054 TRACE("(%p) Released.\n", This
);
1055 HeapFree(GetProcessHeap(), 0, This
);
1061 /* ****************************************************
1062 IWineD3DSurface IWineD3DResource parts follow
1063 **************************************************** */
1065 void surface_internal_preload(IWineD3DSurfaceImpl
*surface
, enum WINED3DSRGB srgb
)
1067 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
1069 TRACE("iface %p, srgb %#x.\n", surface
, srgb
);
1071 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
1073 IWineD3DBaseTextureImpl
*texture
= surface
->container
.u
.texture
;
1075 TRACE("Passing to container.\n");
1076 texture
->baseTexture
.internal_preload((IWineD3DBaseTexture
*)texture
, srgb
);
1080 struct wined3d_context
*context
= NULL
;
1082 TRACE("(%p) : About to load surface\n", surface
);
1084 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
1086 if (surface
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
1087 || surface
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT_A8_UNORM
)
1089 if (palette9_changed(surface
))
1091 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1092 /* TODO: This is not necessarily needed with hw palettized texture support */
1093 surface_load_location(surface
, SFLAG_INSYSMEM
, NULL
);
1094 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1095 surface_modify_location(surface
, SFLAG_INTEXTURE
, FALSE
);
1099 IWineD3DSurface_LoadTexture((IWineD3DSurface
*)surface
, srgb
== SRGB_SRGB
? TRUE
: FALSE
);
1101 if (surface
->resource
.pool
== WINED3DPOOL_DEFAULT
)
1103 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1107 glPrioritizeTextures(1, &surface
->texture_name
, &tmp
);
1111 if (context
) context_release(context
);
1115 static void WINAPI
IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface
*iface
)
1117 surface_internal_preload((IWineD3DSurfaceImpl
*)iface
, SRGB_ANY
);
1120 /* Context activation is done by the caller. */
1121 static void surface_remove_pbo(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
)
1123 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap() ,0 , This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1124 This
->resource
.allocatedMemory
=
1125 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1128 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1129 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1130 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0, This
->resource
.size
, This
->resource
.allocatedMemory
));
1131 checkGLcall("glGetBufferSubDataARB");
1132 GL_EXTCALL(glDeleteBuffersARB(1, &This
->pbo
));
1133 checkGLcall("glDeleteBuffersARB");
1137 This
->Flags
&= ~SFLAG_PBO
;
1140 BOOL
surface_init_sysmem(IWineD3DSurfaceImpl
*surface
)
1142 if (!surface
->resource
.allocatedMemory
)
1144 surface
->resource
.heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1145 surface
->resource
.size
+ RESOURCE_ALIGNMENT
);
1146 if (!surface
->resource
.heapMemory
)
1148 ERR("Out of memory\n");
1151 surface
->resource
.allocatedMemory
=
1152 (BYTE
*)(((ULONG_PTR
)surface
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1156 memset(surface
->resource
.allocatedMemory
, 0, surface
->resource
.size
);
1159 surface_modify_location(surface
, SFLAG_INSYSMEM
, TRUE
);
1164 static void WINAPI
IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface
*iface
)
1166 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
1167 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1168 const struct wined3d_gl_info
*gl_info
;
1169 renderbuffer_entry_t
*entry
, *entry2
;
1170 struct wined3d_context
*context
;
1172 TRACE("(%p)\n", iface
);
1174 if(This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
1175 /* Default pool resources are supposed to be destroyed before Reset is called.
1176 * Implicit resources stay however. So this means we have an implicit render target
1177 * or depth stencil. The content may be destroyed, but we still have to tear down
1178 * opengl resources, so we cannot leave early.
1180 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1181 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1182 * or the depth stencil into an FBO the texture or render buffer will be removed
1183 * and all flags get lost
1185 surface_init_sysmem(This
);
1189 /* Load the surface into system memory */
1190 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
1191 surface_modify_location(This
, SFLAG_INDRAWABLE
, FALSE
);
1193 surface_modify_location(This
, SFLAG_INTEXTURE
, FALSE
);
1194 surface_modify_location(This
, SFLAG_INSRGBTEX
, FALSE
);
1195 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
1197 context
= context_acquire(device
, NULL
);
1198 gl_info
= context
->gl_info
;
1200 /* Destroy PBOs, but load them into real sysmem before */
1201 if (This
->Flags
& SFLAG_PBO
)
1202 surface_remove_pbo(This
, gl_info
);
1204 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1205 * all application-created targets the application has to release the surface
1206 * before calling _Reset
1208 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &This
->renderbuffers
, renderbuffer_entry_t
, entry
) {
1210 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1212 list_remove(&entry
->entry
);
1213 HeapFree(GetProcessHeap(), 0, entry
);
1215 list_init(&This
->renderbuffers
);
1216 This
->current_renderbuffer
= NULL
;
1218 /* If we're in a texture, the texture name belongs to the texture.
1219 * Otherwise, destroy it. */
1220 if (This
->container
.type
!= WINED3D_CONTAINER_TEXTURE
)
1223 glDeleteTextures(1, &This
->texture_name
);
1224 This
->texture_name
= 0;
1225 glDeleteTextures(1, &This
->texture_name_srgb
);
1226 This
->texture_name_srgb
= 0;
1230 context_release(context
);
1232 resource_unload((IWineD3DResourceImpl
*)This
);
1235 /* ******************************************************
1236 IWineD3DSurface IWineD3DSurface parts follow
1237 ****************************************************** */
1239 /* Read the framebuffer back into the surface */
1240 static void read_from_framebuffer(IWineD3DSurfaceImpl
*This
, const RECT
*rect
, void *dest
, UINT pitch
)
1242 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1243 const struct wined3d_gl_info
*gl_info
;
1244 struct wined3d_context
*context
;
1248 BYTE
*row
, *top
, *bottom
;
1252 BOOL srcIsUpsideDown
;
1257 if(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
) {
1258 static BOOL warned
= FALSE
;
1260 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1266 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
1267 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
1268 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
1269 * context->last_was_blit set on the unlock.
1271 context
= context_acquire(device
, This
);
1272 context_apply_blit_state(context
, device
);
1273 gl_info
= context
->gl_info
;
1277 /* Select the correct read buffer, and give some debug output.
1278 * There is no need to keep track of the current read buffer or reset it, every part of the code
1279 * that reads sets the read buffer as desired.
1281 if (surface_is_offscreen(This
))
1283 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1284 * Read from the back buffer
1286 TRACE("Locking offscreen render target\n");
1287 glReadBuffer(device
->offscreenBuffer
);
1288 srcIsUpsideDown
= TRUE
;
1292 /* Onscreen surfaces are always part of a swapchain */
1293 GLenum buffer
= surface_get_gl_buffer(This
);
1294 TRACE("Locking %#x buffer\n", buffer
);
1295 glReadBuffer(buffer
);
1296 checkGLcall("glReadBuffer");
1297 srcIsUpsideDown
= FALSE
;
1300 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1302 local_rect
.left
= 0;
1304 local_rect
.right
= This
->currentDesc
.Width
;
1305 local_rect
.bottom
= This
->currentDesc
.Height
;
1309 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1311 switch (This
->resource
.format_desc
->id
)
1313 case WINED3DFMT_P8_UINT
:
1315 if (primary_render_target_is_p8(device
))
1317 /* In case of P8 render targets the index is stored in the alpha component */
1319 type
= GL_UNSIGNED_BYTE
;
1321 bpp
= This
->resource
.format_desc
->byte_count
;
1323 /* GL can't return palettized data, so read ARGB pixels into a
1324 * separate block of memory and convert them into palettized format
1325 * in software. Slow, but if the app means to use palettized render
1326 * targets and locks it...
1328 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1329 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1330 * for the color channels when palettizing the colors.
1333 type
= GL_UNSIGNED_BYTE
;
1335 mem
= HeapAlloc(GetProcessHeap(), 0, This
->resource
.size
* 3);
1337 ERR("Out of memory\n");
1341 bpp
= This
->resource
.format_desc
->byte_count
* 3;
1348 fmt
= This
->resource
.format_desc
->glFormat
;
1349 type
= This
->resource
.format_desc
->glType
;
1350 bpp
= This
->resource
.format_desc
->byte_count
;
1353 if(This
->Flags
& SFLAG_PBO
) {
1354 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
1355 checkGLcall("glBindBufferARB");
1357 ERR("mem not null for pbo -- unexpected\n");
1362 /* Save old pixel store pack state */
1363 glGetIntegerv(GL_PACK_ROW_LENGTH
, &rowLen
);
1364 checkGLcall("glGetIntegerv");
1365 glGetIntegerv(GL_PACK_SKIP_PIXELS
, &skipPix
);
1366 checkGLcall("glGetIntegerv");
1367 glGetIntegerv(GL_PACK_SKIP_ROWS
, &skipRow
);
1368 checkGLcall("glGetIntegerv");
1370 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1371 glPixelStorei(GL_PACK_ROW_LENGTH
, This
->currentDesc
.Width
);
1372 checkGLcall("glPixelStorei");
1373 glPixelStorei(GL_PACK_SKIP_PIXELS
, local_rect
.left
);
1374 checkGLcall("glPixelStorei");
1375 glPixelStorei(GL_PACK_SKIP_ROWS
, local_rect
.top
);
1376 checkGLcall("glPixelStorei");
1378 glReadPixels(local_rect
.left
, (!srcIsUpsideDown
) ? (This
->currentDesc
.Height
- local_rect
.bottom
) : local_rect
.top
,
1379 local_rect
.right
- local_rect
.left
,
1380 local_rect
.bottom
- local_rect
.top
,
1382 checkGLcall("glReadPixels");
1384 /* Reset previous pixel store pack state */
1385 glPixelStorei(GL_PACK_ROW_LENGTH
, rowLen
);
1386 checkGLcall("glPixelStorei");
1387 glPixelStorei(GL_PACK_SKIP_PIXELS
, skipPix
);
1388 checkGLcall("glPixelStorei");
1389 glPixelStorei(GL_PACK_SKIP_ROWS
, skipRow
);
1390 checkGLcall("glPixelStorei");
1392 if(This
->Flags
& SFLAG_PBO
) {
1393 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
1394 checkGLcall("glBindBufferARB");
1396 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1397 * to get a pointer to it and perform the flipping in software. This is a lot
1398 * faster than calling glReadPixels for each line. In case we want more speed
1399 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1400 if(!srcIsUpsideDown
) {
1401 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1402 checkGLcall("glBindBufferARB");
1404 mem
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, GL_READ_WRITE_ARB
));
1405 checkGLcall("glMapBufferARB");
1409 /* TODO: Merge this with the palettization loop below for P8 targets */
1410 if(!srcIsUpsideDown
) {
1412 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1413 Flip the lines in software */
1414 len
= (local_rect
.right
- local_rect
.left
) * bpp
;
1415 off
= local_rect
.left
* bpp
;
1417 row
= HeapAlloc(GetProcessHeap(), 0, len
);
1419 ERR("Out of memory\n");
1420 if (This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
) HeapFree(GetProcessHeap(), 0, mem
);
1425 top
= mem
+ pitch
* local_rect
.top
;
1426 bottom
= mem
+ pitch
* (local_rect
.bottom
- 1);
1427 for(i
= 0; i
< (local_rect
.bottom
- local_rect
.top
) / 2; i
++) {
1428 memcpy(row
, top
+ off
, len
);
1429 memcpy(top
+ off
, bottom
+ off
, len
);
1430 memcpy(bottom
+ off
, row
, len
);
1434 HeapFree(GetProcessHeap(), 0, row
);
1436 /* Unmap the temp PBO buffer */
1437 if(This
->Flags
& SFLAG_PBO
) {
1438 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
1439 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1444 context_release(context
);
1446 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1447 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1448 * the same color but we have no choice.
1449 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1451 if (This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
&& !primary_render_target_is_p8(device
))
1453 const PALETTEENTRY
*pal
= NULL
;
1454 DWORD width
= pitch
/ 3;
1458 pal
= This
->palette
->palents
;
1460 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1461 HeapFree(GetProcessHeap(), 0, mem
);
1465 for(y
= local_rect
.top
; y
< local_rect
.bottom
; y
++) {
1466 for(x
= local_rect
.left
; x
< local_rect
.right
; x
++) {
1467 /* start lines pixels */
1468 const BYTE
*blue
= mem
+ y
* pitch
+ x
* (sizeof(BYTE
) * 3);
1469 const BYTE
*green
= blue
+ 1;
1470 const BYTE
*red
= green
+ 1;
1472 for(c
= 0; c
< 256; c
++) {
1473 if(*red
== pal
[c
].peRed
&&
1474 *green
== pal
[c
].peGreen
&&
1475 *blue
== pal
[c
].peBlue
)
1477 *((BYTE
*) dest
+ y
* width
+ x
) = c
;
1483 HeapFree(GetProcessHeap(), 0, mem
);
1487 /* Read the framebuffer contents into a texture */
1488 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl
*This
, BOOL srgb
)
1490 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1491 const struct wined3d_gl_info
*gl_info
;
1492 struct wined3d_context
*context
;
1494 if (!surface_is_offscreen(This
))
1496 /* We would need to flip onscreen surfaces, but there's no efficient
1497 * way to do that here. It makes more sense for the caller to
1498 * explicitly go through sysmem. */
1499 ERR("Not supported for onscreen targets.\n");
1503 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1504 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1505 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1507 context
= context_acquire(device
, This
);
1508 gl_info
= context
->gl_info
;
1510 surface_prepare_texture(This
, gl_info
, srgb
);
1511 surface_bind_and_dirtify(This
, srgb
);
1513 TRACE("Reading back offscreen render target %p.\n", This
);
1517 glReadBuffer(device
->offscreenBuffer
);
1518 checkGLcall("glReadBuffer");
1520 glCopyTexSubImage2D(This
->texture_target
, This
->texture_level
,
1521 0, 0, 0, 0, This
->currentDesc
.Width
, This
->currentDesc
.Height
);
1522 checkGLcall("glCopyTexSubImage2D");
1526 context_release(context
);
1529 /* Context activation is done by the caller. */
1530 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl
*surface
,
1531 const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
1533 DWORD alloc_flag
= srgb
? SFLAG_SRGBALLOCATED
: SFLAG_ALLOCATED
;
1534 CONVERT_TYPES convert
;
1535 struct wined3d_format_desc desc
;
1537 if (surface
->Flags
& alloc_flag
) return;
1539 d3dfmt_get_conv(surface
, TRUE
, TRUE
, &desc
, &convert
);
1540 if(convert
!= NO_CONVERSION
|| desc
.convert
) surface
->Flags
|= SFLAG_CONVERTED
;
1541 else surface
->Flags
&= ~SFLAG_CONVERTED
;
1543 surface_bind_and_dirtify(surface
, srgb
);
1544 surface_allocate_surface(surface
, gl_info
, &desc
, srgb
);
1545 surface
->Flags
|= alloc_flag
;
1548 /* Context activation is done by the caller. */
1549 void surface_prepare_texture(IWineD3DSurfaceImpl
*surface
, const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
1551 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
1553 IWineD3DBaseTextureImpl
*texture
= surface
->container
.u
.texture
;
1554 UINT sub_count
= texture
->baseTexture
.level_count
* texture
->baseTexture
.layer_count
;
1557 TRACE("surface %p is a subresource of texture %p.\n", surface
, texture
);
1559 for (i
= 0; i
< sub_count
; ++i
)
1561 IWineD3DSurfaceImpl
*s
= (IWineD3DSurfaceImpl
*)texture
->baseTexture
.sub_resources
[i
];
1562 surface_prepare_texture_internal(s
, gl_info
, srgb
);
1568 surface_prepare_texture_internal(surface
, gl_info
, srgb
);
1571 static void surface_prepare_system_memory(IWineD3DSurfaceImpl
*This
)
1573 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1574 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1576 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1577 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1580 if(!(This
->Flags
& SFLAG_DYNLOCK
)) {
1582 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1583 if(This
->lockCount
> MAXLOCKCOUNT
) {
1584 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1585 This
->Flags
|= SFLAG_DYNLOCK
;
1589 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1590 * Also don't create a PBO for systemmem surfaces.
1592 if (gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
] && (This
->Flags
& SFLAG_DYNLOCK
)
1593 && !(This
->Flags
& (SFLAG_PBO
| SFLAG_CONVERTED
| SFLAG_NONPOW2
))
1594 && (This
->resource
.pool
!= WINED3DPOOL_SYSTEMMEM
))
1597 struct wined3d_context
*context
;
1599 context
= context_acquire(device
, NULL
);
1602 GL_EXTCALL(glGenBuffersARB(1, &This
->pbo
));
1603 error
= glGetError();
1604 if(This
->pbo
== 0 || error
!= GL_NO_ERROR
) {
1605 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error
), error
);
1608 TRACE("Attaching pbo=%#x to (%p)\n", This
->pbo
, This
);
1610 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1611 checkGLcall("glBindBufferARB");
1613 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->resource
.size
+ 4, This
->resource
.allocatedMemory
, GL_STREAM_DRAW_ARB
));
1614 checkGLcall("glBufferDataARB");
1616 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1617 checkGLcall("glBindBufferARB");
1619 /* We don't need the system memory anymore and we can't even use it for PBOs */
1620 if(!(This
->Flags
& SFLAG_CLIENT
)) {
1621 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
1622 This
->resource
.heapMemory
= NULL
;
1624 This
->resource
.allocatedMemory
= NULL
;
1625 This
->Flags
|= SFLAG_PBO
;
1627 context_release(context
);
1629 else if (!(This
->resource
.allocatedMemory
|| This
->Flags
& SFLAG_PBO
))
1631 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1634 if(!This
->resource
.heapMemory
) {
1635 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap() ,0 , This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1637 This
->resource
.allocatedMemory
=
1638 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1639 if(This
->Flags
& SFLAG_INSYSMEM
) {
1640 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1645 static HRESULT WINAPI
IWineD3DSurfaceImpl_LockRect(IWineD3DSurface
*iface
, WINED3DLOCKED_RECT
* pLockedRect
, CONST RECT
* pRect
, DWORD Flags
) {
1646 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1647 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1648 const RECT
*pass_rect
= pRect
;
1650 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1651 iface
, pLockedRect
, wine_dbgstr_rect(pRect
), Flags
);
1653 /* This is also done in the base class, but we have to verify this before loading any data from
1654 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1655 * may interfere, and all other bad things may happen
1657 if (This
->Flags
& SFLAG_LOCKED
) {
1658 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1659 return WINED3DERR_INVALIDCALL
;
1661 This
->Flags
|= SFLAG_LOCKED
;
1663 if (!(This
->Flags
& SFLAG_LOCKABLE
))
1665 TRACE("Warning: trying to lock unlockable surf@%p\n", This
);
1668 if (Flags
& WINED3DLOCK_DISCARD
) {
1669 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1670 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1671 surface_prepare_system_memory(This
); /* Makes sure memory is allocated */
1672 This
->Flags
|= SFLAG_INSYSMEM
;
1676 if (This
->Flags
& SFLAG_INSYSMEM
) {
1677 TRACE("Local copy is up to date, not downloading data\n");
1678 surface_prepare_system_memory(This
); /* Makes sure memory is allocated */
1682 /* surface_load_location() does not check if the rectangle specifies
1683 * the full surface. Most callers don't need that, so do it here. */
1684 if (pRect
&& pRect
->top
== 0 && pRect
->left
== 0
1685 && pRect
->right
== This
->currentDesc
.Width
1686 && pRect
->bottom
== This
->currentDesc
.Height
)
1691 if (!(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
1692 && ((This
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
) || This
== device
->render_targets
[0])))
1694 surface_load_location(This
, SFLAG_INSYSMEM
, pass_rect
);
1698 if (This
->Flags
& SFLAG_PBO
)
1700 const struct wined3d_gl_info
*gl_info
;
1701 struct wined3d_context
*context
;
1703 context
= context_acquire(device
, NULL
);
1704 gl_info
= context
->gl_info
;
1707 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1708 checkGLcall("glBindBufferARB");
1710 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1711 if(This
->resource
.allocatedMemory
) {
1712 ERR("The surface already has PBO memory allocated!\n");
1715 This
->resource
.allocatedMemory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, GL_READ_WRITE_ARB
));
1716 checkGLcall("glMapBufferARB");
1718 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1719 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1720 checkGLcall("glBindBufferARB");
1723 context_release(context
);
1726 if (Flags
& (WINED3DLOCK_NO_DIRTY_UPDATE
| WINED3DLOCK_READONLY
)) {
1731 surface_add_dirty_rect(This
, pRect
);
1733 if (This
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
1735 TRACE("Making container dirty.\n");
1736 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture
*)This
->container
.u
.texture
, TRUE
);
1740 TRACE("Surface is standalone, no need to dirty the container\n");
1744 return IWineD3DBaseSurfaceImpl_LockRect(iface
, pLockedRect
, pRect
, Flags
);
1747 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl
*This
, GLenum fmt
, GLenum type
, UINT bpp
, const BYTE
*mem
) {
1749 GLint prev_rasterpos
[4];
1750 GLint skipBytes
= 0;
1751 UINT pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*) This
); /* target is argb, 4 byte */
1752 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1753 const struct wined3d_gl_info
*gl_info
;
1754 struct wined3d_context
*context
;
1756 /* Activate the correct context for the render target */
1757 context
= context_acquire(device
, This
);
1758 context_apply_blit_state(context
, device
);
1759 gl_info
= context
->gl_info
;
1763 if (!surface_is_offscreen(This
))
1765 GLenum buffer
= surface_get_gl_buffer(This
);
1766 TRACE("Unlocking %#x buffer.\n", buffer
);
1767 context_set_draw_buffer(context
, buffer
);
1771 /* Primary offscreen render target */
1772 TRACE("Offscreen render target.\n");
1773 context_set_draw_buffer(context
, device
->offscreenBuffer
);
1776 glGetIntegerv(GL_PACK_SWAP_BYTES
, &prev_store
);
1777 checkGLcall("glGetIntegerv");
1778 glGetIntegerv(GL_CURRENT_RASTER_POSITION
, &prev_rasterpos
[0]);
1779 checkGLcall("glGetIntegerv");
1780 glPixelZoom(1.0f
, -1.0f
);
1781 checkGLcall("glPixelZoom");
1783 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1784 glGetIntegerv(GL_UNPACK_ROW_LENGTH
, &skipBytes
);
1785 glPixelStorei(GL_UNPACK_ROW_LENGTH
, This
->currentDesc
.Width
);
1787 glRasterPos3i(This
->lockedRect
.left
, This
->lockedRect
.top
, 1);
1788 checkGLcall("glRasterPos3i");
1790 /* Some drivers(radeon dri, others?) don't like exceptions during
1791 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1792 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1793 * catch to put the dib section in InSync mode, which leads to a crash
1794 * and a blocked x server on my radeon card.
1796 * The following lines read the dib section so it is put in InSync mode
1797 * before glDrawPixels is called and the crash is prevented. There won't
1798 * be any interfering gdi accesses, because UnlockRect is called from
1799 * ReleaseDC, and the app won't use the dc any more afterwards.
1801 if((This
->Flags
& SFLAG_DIBSECTION
) && !(This
->Flags
& SFLAG_PBO
)) {
1803 read
= This
->resource
.allocatedMemory
[0];
1806 if(This
->Flags
& SFLAG_PBO
) {
1807 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1808 checkGLcall("glBindBufferARB");
1811 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1812 if(This
->Flags
& SFLAG_LOCKED
) {
1813 glDrawPixels(This
->lockedRect
.right
- This
->lockedRect
.left
,
1814 (This
->lockedRect
.bottom
- This
->lockedRect
.top
)-1,
1816 mem
+ bpp
* This
->lockedRect
.left
+ pitch
* This
->lockedRect
.top
);
1817 checkGLcall("glDrawPixels");
1819 glDrawPixels(This
->currentDesc
.Width
,
1820 This
->currentDesc
.Height
,
1822 checkGLcall("glDrawPixels");
1825 if(This
->Flags
& SFLAG_PBO
) {
1826 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1827 checkGLcall("glBindBufferARB");
1830 glPixelZoom(1.0f
, 1.0f
);
1831 checkGLcall("glPixelZoom");
1833 glRasterPos3iv(&prev_rasterpos
[0]);
1834 checkGLcall("glRasterPos3iv");
1836 /* Reset to previous pack row length */
1837 glPixelStorei(GL_UNPACK_ROW_LENGTH
, skipBytes
);
1838 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH)");
1841 context_release(context
);
1844 static HRESULT WINAPI
IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface
*iface
) {
1845 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1846 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1849 if (!(This
->Flags
& SFLAG_LOCKED
)) {
1850 WARN("trying to Unlock an unlocked surf@%p\n", This
);
1851 return WINEDDERR_NOTLOCKED
;
1854 if (This
->Flags
& SFLAG_PBO
)
1856 const struct wined3d_gl_info
*gl_info
;
1857 struct wined3d_context
*context
;
1859 TRACE("Freeing PBO memory\n");
1861 context
= context_acquire(device
, NULL
);
1862 gl_info
= context
->gl_info
;
1865 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1866 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
1867 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1868 checkGLcall("glUnmapBufferARB");
1870 context_release(context
);
1872 This
->resource
.allocatedMemory
= NULL
;
1875 TRACE("(%p) : dirtyfied(%d)\n", This
, This
->Flags
& (SFLAG_INDRAWABLE
| SFLAG_INTEXTURE
) ? 0 : 1);
1877 if (This
->Flags
& (SFLAG_INDRAWABLE
| SFLAG_INTEXTURE
)) {
1878 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This
);
1882 if (This
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
1883 || (device
->render_targets
&& This
== device
->render_targets
[0]))
1885 if(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
) {
1886 static BOOL warned
= FALSE
;
1888 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1894 if(This
->dirtyRect
.left
== 0 &&
1895 This
->dirtyRect
.top
== 0 &&
1896 This
->dirtyRect
.right
== This
->currentDesc
.Width
&&
1897 This
->dirtyRect
.bottom
== This
->currentDesc
.Height
) {
1900 /* TODO: Proper partial rectangle tracking */
1901 fullsurface
= FALSE
;
1902 This
->Flags
|= SFLAG_INSYSMEM
;
1905 switch(wined3d_settings
.rendertargetlock_mode
) {
1907 surface_load_location(This
, SFLAG_INTEXTURE
, NULL
/* partial texture loading not supported yet */);
1911 surface_load_location(This
, SFLAG_INDRAWABLE
, fullsurface
? NULL
: &This
->dirtyRect
);
1916 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1917 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1918 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1919 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1920 * not fully up to date because only a subrectangle was read in LockRect.
1922 This
->Flags
&= ~SFLAG_INSYSMEM
;
1923 This
->Flags
|= SFLAG_INDRAWABLE
;
1926 This
->dirtyRect
.left
= This
->currentDesc
.Width
;
1927 This
->dirtyRect
.top
= This
->currentDesc
.Height
;
1928 This
->dirtyRect
.right
= 0;
1929 This
->dirtyRect
.bottom
= 0;
1931 else if (This
== device
->depth_stencil
)
1933 FIXME("Depth Stencil buffer locking is not implemented\n");
1935 /* The rest should be a normal texture */
1936 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1937 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1938 * states need resetting
1940 if (This
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
1942 IWineD3DBaseTextureImpl
*texture
= This
->container
.u
.texture
;
1943 if (texture
->baseTexture
.bindCount
)
1944 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(texture
->baseTexture
.sampler
));
1949 This
->Flags
&= ~SFLAG_LOCKED
;
1950 memset(&This
->lockedRect
, 0, sizeof(RECT
));
1952 /* Overlays have to be redrawn manually after changes with the GL implementation */
1953 if(This
->overlay_dest
) {
1954 IWineD3DSurface_DrawOverlay(iface
);
1959 static void surface_release_client_storage(IWineD3DSurfaceImpl
*surface
)
1961 struct wined3d_context
*context
;
1963 context
= context_acquire(surface
->resource
.device
, NULL
);
1966 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
1967 if (surface
->texture_name
)
1969 surface_bind_and_dirtify(surface
, FALSE
);
1970 glTexImage2D(surface
->texture_target
, surface
->texture_level
,
1971 GL_RGB
, 1, 1, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
1973 if (surface
->texture_name_srgb
)
1975 surface_bind_and_dirtify(surface
, TRUE
);
1976 glTexImage2D(surface
->texture_target
, surface
->texture_level
,
1977 GL_RGB
, 1, 1, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
1979 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1982 context_release(context
);
1984 surface_modify_location(surface
, SFLAG_INSRGBTEX
, FALSE
);
1985 surface_modify_location(surface
, SFLAG_INTEXTURE
, FALSE
);
1986 surface_force_reload(surface
);
1989 static HRESULT WINAPI
IWineD3DSurfaceImpl_GetDC(IWineD3DSurface
*iface
, HDC
*pHDC
)
1991 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1992 WINED3DLOCKED_RECT lock
;
1996 TRACE("(%p)->(%p)\n",This
,pHDC
);
1998 if(This
->Flags
& SFLAG_USERPTR
) {
1999 ERR("Not supported on surfaces with an application-provided surfaces\n");
2000 return WINEDDERR_NODC
;
2003 /* Give more detailed info for ddraw */
2004 if (This
->Flags
& SFLAG_DCINUSE
)
2005 return WINEDDERR_DCALREADYCREATED
;
2007 /* Can't GetDC if the surface is locked */
2008 if (This
->Flags
& SFLAG_LOCKED
)
2009 return WINED3DERR_INVALIDCALL
;
2011 memset(&lock
, 0, sizeof(lock
)); /* To be sure */
2013 /* Create a DIB section if there isn't a hdc yet */
2016 if (This
->Flags
& SFLAG_CLIENT
)
2018 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
2019 surface_release_client_storage(This
);
2021 hr
= IWineD3DBaseSurfaceImpl_CreateDIBSection(iface
);
2022 if(FAILED(hr
)) return WINED3DERR_INVALIDCALL
;
2024 /* Use the dib section from now on if we are not using a PBO */
2025 if(!(This
->Flags
& SFLAG_PBO
))
2026 This
->resource
.allocatedMemory
= This
->dib
.bitmap_data
;
2029 /* Lock the surface */
2030 hr
= IWineD3DSurface_LockRect(iface
,
2035 if(This
->Flags
& SFLAG_PBO
) {
2036 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
2037 memcpy(This
->dib
.bitmap_data
, This
->resource
.allocatedMemory
, This
->dib
.bitmap_size
);
2041 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr
);
2042 /* keep the dib section */
2046 if (This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
2047 || This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT_A8_UNORM
)
2049 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2050 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2052 const PALETTEENTRY
*pal
= NULL
;
2055 pal
= This
->palette
->palents
;
2057 IWineD3DSurfaceImpl
*dds_primary
;
2058 IWineD3DSwapChainImpl
*swapchain
;
2059 swapchain
= (IWineD3DSwapChainImpl
*)This
->resource
.device
->swapchains
[0];
2060 dds_primary
= swapchain
->front_buffer
;
2061 if (dds_primary
&& dds_primary
->palette
)
2062 pal
= dds_primary
->palette
->palents
;
2066 for (n
=0; n
<256; n
++) {
2067 col
[n
].rgbRed
= pal
[n
].peRed
;
2068 col
[n
].rgbGreen
= pal
[n
].peGreen
;
2069 col
[n
].rgbBlue
= pal
[n
].peBlue
;
2070 col
[n
].rgbReserved
= 0;
2072 SetDIBColorTable(This
->hDC
, 0, 256, col
);
2077 TRACE("returning %p\n",*pHDC
);
2078 This
->Flags
|= SFLAG_DCINUSE
;
2083 static HRESULT WINAPI
IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface
*iface
, HDC hDC
)
2085 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2087 TRACE("(%p)->(%p)\n",This
,hDC
);
2089 if (!(This
->Flags
& SFLAG_DCINUSE
))
2090 return WINEDDERR_NODC
;
2092 if (This
->hDC
!=hDC
) {
2093 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC
, This
->hDC
);
2094 return WINEDDERR_NODC
;
2097 if((This
->Flags
& SFLAG_PBO
) && This
->resource
.allocatedMemory
) {
2098 /* Copy the contents of the DIB over to the PBO */
2099 memcpy(This
->resource
.allocatedMemory
, This
->dib
.bitmap_data
, This
->dib
.bitmap_size
);
2102 /* we locked first, so unlock now */
2103 IWineD3DSurface_UnlockRect(iface
);
2105 This
->Flags
&= ~SFLAG_DCINUSE
;
2110 /* ******************************************************
2111 IWineD3DSurface Internal (No mapping to directx api) parts follow
2112 ****************************************************** */
2114 HRESULT
d3dfmt_get_conv(IWineD3DSurfaceImpl
*This
, BOOL need_alpha_ck
, BOOL use_texturing
, struct wined3d_format_desc
*desc
, CONVERT_TYPES
*convert
)
2116 BOOL colorkey_active
= need_alpha_ck
&& (This
->CKeyFlags
& WINEDDSD_CKSRCBLT
);
2117 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2118 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2119 BOOL blit_supported
= FALSE
;
2121 /* Copy the default values from the surface. Below we might perform fixups */
2122 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2123 *desc
= *This
->resource
.format_desc
;
2124 *convert
= NO_CONVERSION
;
2126 /* Ok, now look if we have to do any conversion */
2127 switch (This
->resource
.format_desc
->id
)
2129 case WINED3DFMT_P8_UINT
:
2134 /* Below the call to blit_supported is disabled for Wine 1.2 because the function isn't operating correctly yet.
2135 * At the moment 8-bit blits are handled in software and if certain GL extensions are around, surface conversion
2136 * is performed at upload time. The blit_supported call recognizes it as a destination fixup. This type of upload 'fixup'
2137 * and 8-bit to 8-bit blits need to be handled by the blit_shader.
2138 * TODO: get rid of this #if 0
2141 blit_supported
= device
->blitter
->blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
2142 &rect
, This
->resource
.usage
, This
->resource
.pool
,
2143 This
->resource
.format_desc
, &rect
, This
->resource
.usage
,
2144 This
->resource
.pool
, This
->resource
.format_desc
);
2146 blit_supported
= gl_info
->supported
[EXT_PALETTED_TEXTURE
] || gl_info
->supported
[ARB_FRAGMENT_PROGRAM
];
2148 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2149 * texturing. Further also use conversion in case of color keying.
2150 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2151 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2152 * conflicts with this.
2154 if (!((blit_supported
&& device
->render_targets
&& This
== device
->render_targets
[0]))
2155 || colorkey_active
|| !use_texturing
)
2157 desc
->glFormat
= GL_RGBA
;
2158 desc
->glInternal
= GL_RGBA
;
2159 desc
->glType
= GL_UNSIGNED_BYTE
;
2160 desc
->conv_byte_count
= 4;
2161 if(colorkey_active
) {
2162 *convert
= CONVERT_PALETTED_CK
;
2164 *convert
= CONVERT_PALETTED
;
2169 case WINED3DFMT_B2G3R3_UNORM
:
2170 /* **********************
2171 GL_UNSIGNED_BYTE_3_3_2
2172 ********************** */
2173 if (colorkey_active
) {
2174 /* This texture format will never be used.. So do not care about color keying
2175 up until the point in time it will be needed :-) */
2176 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2180 case WINED3DFMT_B5G6R5_UNORM
:
2181 if (colorkey_active
) {
2182 *convert
= CONVERT_CK_565
;
2183 desc
->glFormat
= GL_RGBA
;
2184 desc
->glInternal
= GL_RGB5_A1
;
2185 desc
->glType
= GL_UNSIGNED_SHORT_5_5_5_1
;
2186 desc
->conv_byte_count
= 2;
2190 case WINED3DFMT_B5G5R5X1_UNORM
:
2191 if (colorkey_active
) {
2192 *convert
= CONVERT_CK_5551
;
2193 desc
->glFormat
= GL_BGRA
;
2194 desc
->glInternal
= GL_RGB5_A1
;
2195 desc
->glType
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
2196 desc
->conv_byte_count
= 2;
2200 case WINED3DFMT_B8G8R8_UNORM
:
2201 if (colorkey_active
) {
2202 *convert
= CONVERT_CK_RGB24
;
2203 desc
->glFormat
= GL_RGBA
;
2204 desc
->glInternal
= GL_RGBA8
;
2205 desc
->glType
= GL_UNSIGNED_INT_8_8_8_8
;
2206 desc
->conv_byte_count
= 4;
2210 case WINED3DFMT_B8G8R8X8_UNORM
:
2211 if (colorkey_active
) {
2212 *convert
= CONVERT_RGB32_888
;
2213 desc
->glFormat
= GL_RGBA
;
2214 desc
->glInternal
= GL_RGBA8
;
2215 desc
->glType
= GL_UNSIGNED_INT_8_8_8_8
;
2216 desc
->conv_byte_count
= 4;
2227 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl
*This
, BYTE table
[256][4], BOOL colorkey
)
2229 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2230 IWineD3DPaletteImpl
*pal
= This
->palette
;
2231 BOOL index_in_alpha
= FALSE
;
2234 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2235 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2236 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2237 * duplicate entries. Store the color key in the unused alpha component to speed the
2238 * download up and to make conversion unneeded. */
2239 index_in_alpha
= primary_render_target_is_p8(device
);
2243 UINT dxVersion
= ((IWineD3DImpl
*)device
->wined3d
)->dxVersion
;
2245 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2248 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2251 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2252 * there's no palette at this time. */
2253 for (i
= 0; i
< 256; i
++) table
[i
][3] = i
;
2258 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2259 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2260 * capability flag is present (wine does advertise this capability) */
2261 for (i
= 0; i
< 256; ++i
)
2263 table
[i
][0] = device
->palettes
[device
->currentPalette
][i
].peRed
;
2264 table
[i
][1] = device
->palettes
[device
->currentPalette
][i
].peGreen
;
2265 table
[i
][2] = device
->palettes
[device
->currentPalette
][i
].peBlue
;
2266 table
[i
][3] = device
->palettes
[device
->currentPalette
][i
].peFlags
;
2272 TRACE("Using surface palette %p\n", pal
);
2273 /* Get the surface's palette */
2274 for (i
= 0; i
< 256; ++i
)
2276 table
[i
][0] = pal
->palents
[i
].peRed
;
2277 table
[i
][1] = pal
->palents
[i
].peGreen
;
2278 table
[i
][2] = pal
->palents
[i
].peBlue
;
2280 /* When index_in_alpha is set the palette index is stored in the
2281 * alpha component. In case of a readback we can then read
2282 * GL_ALPHA. Color keying is handled in BltOverride using a
2283 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2284 * color key itself is passed to glAlphaFunc in other cases the
2285 * alpha component of pixels that should be masked away is set to 0. */
2290 else if (colorkey
&& (i
>= This
->SrcBltCKey
.dwColorSpaceLowValue
)
2291 && (i
<= This
->SrcBltCKey
.dwColorSpaceHighValue
))
2295 else if(pal
->Flags
& WINEDDPCAPS_ALPHA
)
2297 table
[i
][3] = pal
->palents
[i
].peFlags
;
2307 static HRESULT
d3dfmt_convert_surface(const BYTE
*src
, BYTE
*dst
, UINT pitch
, UINT width
,
2308 UINT height
, UINT outpitch
, CONVERT_TYPES convert
, IWineD3DSurfaceImpl
*This
)
2312 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src
, dst
, pitch
, height
, outpitch
, convert
,This
);
2317 memcpy(dst
, src
, pitch
* height
);
2320 case CONVERT_PALETTED
:
2321 case CONVERT_PALETTED_CK
:
2323 IWineD3DPaletteImpl
* pal
= This
->palette
;
2328 /* TODO: If we are a sublevel, try to get the palette from level 0 */
2331 d3dfmt_p8_init_palette(This
, table
, (convert
== CONVERT_PALETTED_CK
));
2333 for (y
= 0; y
< height
; y
++)
2335 source
= src
+ pitch
* y
;
2336 dest
= dst
+ outpitch
* y
;
2337 /* This is an 1 bpp format, using the width here is fine */
2338 for (x
= 0; x
< width
; x
++) {
2339 BYTE color
= *source
++;
2340 *dest
++ = table
[color
][0];
2341 *dest
++ = table
[color
][1];
2342 *dest
++ = table
[color
][2];
2343 *dest
++ = table
[color
][3];
2349 case CONVERT_CK_565
:
2351 /* Converting the 565 format in 5551 packed to emulate color-keying.
2353 Note : in all these conversion, it would be best to average the averaging
2354 pixels to get the color of the pixel that will be color-keyed to
2355 prevent 'color bleeding'. This will be done later on if ever it is
2358 Note2: Nvidia documents say that their driver does not support alpha + color keying
2359 on the same surface and disables color keying in such a case
2365 TRACE("Color keyed 565\n");
2367 for (y
= 0; y
< height
; y
++) {
2368 Source
= (const WORD
*)(src
+ y
* pitch
);
2369 Dest
= (WORD
*) (dst
+ y
* outpitch
);
2370 for (x
= 0; x
< width
; x
++ ) {
2371 WORD color
= *Source
++;
2372 *Dest
= ((color
& 0xFFC0) | ((color
& 0x1F) << 1));
2373 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2374 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2383 case CONVERT_CK_5551
:
2385 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2389 TRACE("Color keyed 5551\n");
2390 for (y
= 0; y
< height
; y
++) {
2391 Source
= (const WORD
*)(src
+ y
* pitch
);
2392 Dest
= (WORD
*) (dst
+ y
* outpitch
);
2393 for (x
= 0; x
< width
; x
++ ) {
2394 WORD color
= *Source
++;
2396 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2397 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2401 *Dest
&= ~(1 << 15);
2409 case CONVERT_CK_RGB24
:
2411 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2413 for (y
= 0; y
< height
; y
++)
2415 source
= src
+ pitch
* y
;
2416 dest
= dst
+ outpitch
* y
;
2417 for (x
= 0; x
< width
; x
++) {
2418 DWORD color
= ((DWORD
)source
[0] << 16) + ((DWORD
)source
[1] << 8) + (DWORD
)source
[2] ;
2419 DWORD dstcolor
= color
<< 8;
2420 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2421 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2424 *(DWORD
*)dest
= dstcolor
;
2432 case CONVERT_RGB32_888
:
2434 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2436 for (y
= 0; y
< height
; y
++)
2438 source
= src
+ pitch
* y
;
2439 dest
= dst
+ outpitch
* y
;
2440 for (x
= 0; x
< width
; x
++) {
2441 DWORD color
= 0xffffff & *(const DWORD
*)source
;
2442 DWORD dstcolor
= color
<< 8;
2443 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2444 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2447 *(DWORD
*)dest
= dstcolor
;
2456 ERR("Unsupported conversion type %#x.\n", convert
);
2461 BOOL
palette9_changed(IWineD3DSurfaceImpl
*This
)
2463 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2465 if (This
->palette
|| (This
->resource
.format_desc
->id
!= WINED3DFMT_P8_UINT
2466 && This
->resource
.format_desc
->id
!= WINED3DFMT_P8_UINT_A8_UNORM
))
2468 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2469 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2476 if (!memcmp(This
->palette9
, device
->palettes
[device
->currentPalette
], sizeof(PALETTEENTRY
) * 256))
2481 This
->palette9
= HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
) * 256);
2483 memcpy(This
->palette9
, device
->palettes
[device
->currentPalette
], sizeof(PALETTEENTRY
) * 256);
2487 static HRESULT WINAPI
IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface
*iface
, BOOL srgb_mode
) {
2488 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2489 DWORD flag
= srgb_mode
? SFLAG_INSRGBTEX
: SFLAG_INTEXTURE
;
2491 TRACE("iface %p, srgb %#x.\n", iface
, srgb_mode
);
2493 if (!(This
->Flags
& flag
)) {
2494 TRACE("Reloading because surface is dirty\n");
2495 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2496 ((This
->Flags
& SFLAG_GLCKEY
) && (!(This
->CKeyFlags
& WINEDDSD_CKSRCBLT
))) ||
2497 /* Reload: vice versa OR */
2498 ((!(This
->Flags
& SFLAG_GLCKEY
)) && (This
->CKeyFlags
& WINEDDSD_CKSRCBLT
)) ||
2499 /* Also reload: Color key is active AND the color key has changed */
2500 ((This
->CKeyFlags
& WINEDDSD_CKSRCBLT
) && (
2501 (This
->glCKey
.dwColorSpaceLowValue
!= This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2502 (This
->glCKey
.dwColorSpaceHighValue
!= This
->SrcBltCKey
.dwColorSpaceHighValue
)))) {
2503 TRACE("Reloading because of color keying\n");
2504 /* To perform the color key conversion we need a sysmem copy of
2505 * the surface. Make sure we have it
2508 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
2509 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2510 /* TODO: This is not necessarily needed with hw palettized texture support */
2511 surface_modify_location(This
, SFLAG_INSYSMEM
, TRUE
);
2513 TRACE("surface is already in texture\n");
2517 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2518 * These resources are not bound by device size or format restrictions. Because of this,
2519 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2520 * However, these resources can always be created, locked, and copied.
2522 if (This
->resource
.pool
== WINED3DPOOL_SCRATCH
)
2524 FIXME("(%p) Operation not supported for scratch textures\n",This
);
2525 return WINED3DERR_INVALIDCALL
;
2528 surface_load_location(This
, flag
, NULL
/* no partial locking for textures yet */);
2530 if (!(This
->Flags
& SFLAG_DONOTFREE
)) {
2531 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
2532 This
->resource
.allocatedMemory
= NULL
;
2533 This
->resource
.heapMemory
= NULL
;
2534 surface_modify_location(This
, SFLAG_INSYSMEM
, FALSE
);
2540 /* Context activation is done by the caller. */
2541 static void WINAPI
IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface
*iface
, BOOL srgb
)
2543 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2545 TRACE("iface %p, srgb %#x.\n", iface
, srgb
);
2547 if (This
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
2549 TRACE("Passing to container.\n");
2550 IWineD3DBaseTexture_BindTexture((IWineD3DBaseTexture
*)This
->container
.u
.texture
, srgb
);
2556 TRACE("(%p) : Binding surface\n", This
);
2558 name
= srgb
? &This
->texture_name_srgb
: &This
->texture_name
;
2562 if (!This
->texture_level
)
2565 glGenTextures(1, name
);
2566 checkGLcall("glGenTextures");
2567 TRACE("Surface %p given name %d\n", This
, *name
);
2569 glBindTexture(This
->texture_target
, *name
);
2570 checkGLcall("glBindTexture");
2571 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2572 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2573 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2574 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2575 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2576 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2577 glTexParameteri(This
->texture_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2578 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2579 glTexParameteri(This
->texture_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2580 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2582 /* This is where we should be reducing the amount of GLMemoryUsed */
2584 /* Mipmap surfaces should have a base texture container */
2585 ERR("Mipmap surface has a glTexture bound to it!\n");
2588 glBindTexture(This
->texture_target
, *name
);
2589 checkGLcall("glBindTexture");
2595 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface
*iface
, enum wined3d_format_id format
)
2597 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2600 TRACE("(%p) : Calling base function first\n", This
);
2601 hr
= IWineD3DBaseSurfaceImpl_SetFormat(iface
, format
);
2603 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
2604 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This
, This
->resource
.format_desc
->glFormat
,
2605 This
->resource
.format_desc
->glInternal
, This
->resource
.format_desc
->glType
);
2610 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetMem(IWineD3DSurface
*iface
, void *Mem
) {
2611 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
2613 if(This
->Flags
& (SFLAG_LOCKED
| SFLAG_DCINUSE
)) {
2614 WARN("Surface is locked or the HDC is in use\n");
2615 return WINED3DERR_INVALIDCALL
;
2618 if(Mem
&& Mem
!= This
->resource
.allocatedMemory
) {
2619 void *release
= NULL
;
2621 /* Do I have to copy the old surface content? */
2622 if(This
->Flags
& SFLAG_DIBSECTION
) {
2623 /* Release the DC. No need to hold the critical section for the update
2624 * Thread because this thread runs only on front buffers, but this method
2625 * fails for render targets in the check above.
2627 SelectObject(This
->hDC
, This
->dib
.holdbitmap
);
2628 DeleteDC(This
->hDC
);
2629 /* Release the DIB section */
2630 DeleteObject(This
->dib
.DIBsection
);
2631 This
->dib
.bitmap_data
= NULL
;
2632 This
->resource
.allocatedMemory
= NULL
;
2634 This
->Flags
&= ~SFLAG_DIBSECTION
;
2635 } else if(!(This
->Flags
& SFLAG_USERPTR
)) {
2636 release
= This
->resource
.heapMemory
;
2637 This
->resource
.heapMemory
= NULL
;
2639 This
->resource
.allocatedMemory
= Mem
;
2640 This
->Flags
|= SFLAG_USERPTR
| SFLAG_INSYSMEM
;
2642 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2643 surface_modify_location(This
, SFLAG_INSYSMEM
, TRUE
);
2645 /* For client textures opengl has to be notified */
2646 if (This
->Flags
& SFLAG_CLIENT
)
2647 surface_release_client_storage(This
);
2649 /* Now free the old memory if any */
2650 HeapFree(GetProcessHeap(), 0, release
);
2651 } else if(This
->Flags
& SFLAG_USERPTR
) {
2652 /* LockRect and GetDC will re-create the dib section and allocated memory */
2653 This
->resource
.allocatedMemory
= NULL
;
2654 /* HeapMemory should be NULL already */
2655 if(This
->resource
.heapMemory
!= NULL
) ERR("User pointer surface has heap memory allocated\n");
2656 This
->Flags
&= ~SFLAG_USERPTR
;
2658 if (This
->Flags
& SFLAG_CLIENT
)
2659 surface_release_client_storage(This
);
2664 void flip_surface(IWineD3DSurfaceImpl
*front
, IWineD3DSurfaceImpl
*back
) {
2666 /* Flip the surface contents */
2671 front
->hDC
= back
->hDC
;
2675 /* Flip the DIBsection */
2678 BOOL hasDib
= front
->Flags
& SFLAG_DIBSECTION
;
2679 tmp
= front
->dib
.DIBsection
;
2680 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
2681 back
->dib
.DIBsection
= tmp
;
2683 if(back
->Flags
& SFLAG_DIBSECTION
) front
->Flags
|= SFLAG_DIBSECTION
;
2684 else front
->Flags
&= ~SFLAG_DIBSECTION
;
2685 if(hasDib
) back
->Flags
|= SFLAG_DIBSECTION
;
2686 else back
->Flags
&= ~SFLAG_DIBSECTION
;
2689 /* Flip the surface data */
2693 tmp
= front
->dib
.bitmap_data
;
2694 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
2695 back
->dib
.bitmap_data
= tmp
;
2697 tmp
= front
->resource
.allocatedMemory
;
2698 front
->resource
.allocatedMemory
= back
->resource
.allocatedMemory
;
2699 back
->resource
.allocatedMemory
= tmp
;
2701 tmp
= front
->resource
.heapMemory
;
2702 front
->resource
.heapMemory
= back
->resource
.heapMemory
;
2703 back
->resource
.heapMemory
= tmp
;
2708 GLuint tmp_pbo
= front
->pbo
;
2709 front
->pbo
= back
->pbo
;
2710 back
->pbo
= tmp_pbo
;
2713 /* client_memory should not be different, but just in case */
2716 tmp
= front
->dib
.client_memory
;
2717 front
->dib
.client_memory
= back
->dib
.client_memory
;
2718 back
->dib
.client_memory
= tmp
;
2721 /* Flip the opengl texture */
2725 tmp
= back
->texture_name
;
2726 back
->texture_name
= front
->texture_name
;
2727 front
->texture_name
= tmp
;
2729 tmp
= back
->texture_name_srgb
;
2730 back
->texture_name_srgb
= front
->texture_name_srgb
;
2731 front
->texture_name_srgb
= tmp
;
2735 DWORD tmp_flags
= back
->Flags
;
2736 back
->Flags
= front
->Flags
;
2737 front
->Flags
= tmp_flags
;
2741 static HRESULT WINAPI
IWineD3DSurfaceImpl_Flip(IWineD3DSurface
*iface
, IWineD3DSurface
*override
, DWORD Flags
) {
2742 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2743 IWineD3DSwapChainImpl
*swapchain
= NULL
;
2745 TRACE("(%p)->(%p,%x)\n", This
, override
, Flags
);
2747 /* Flipping is only supported on RenderTargets and overlays*/
2748 if( !(This
->resource
.usage
& (WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_OVERLAY
)) ) {
2749 WARN("Tried to flip a non-render target, non-overlay surface\n");
2750 return WINEDDERR_NOTFLIPPABLE
;
2753 if(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
) {
2754 flip_surface(This
, (IWineD3DSurfaceImpl
*) override
);
2756 /* Update the overlay if it is visible */
2757 if(This
->overlay_dest
) {
2758 return IWineD3DSurface_DrawOverlay((IWineD3DSurface
*) This
);
2765 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2766 * FIXME("(%p) Target override is not supported by now\n", This);
2767 * Additionally, it isn't really possible to support triple-buffering
2768 * properly on opengl at all
2772 if (This
->container
.type
!= WINED3D_CONTAINER_SWAPCHAIN
)
2774 ERR("Flipped surface is not on a swapchain\n");
2775 return WINEDDERR_NOTFLIPPABLE
;
2777 swapchain
= This
->container
.u
.swapchain
;
2779 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2780 * and only d3d8 and d3d9 apps specify the presentation interval
2782 if((Flags
& (WINEDDFLIP_NOVSYNC
| WINEDDFLIP_INTERVAL2
| WINEDDFLIP_INTERVAL3
| WINEDDFLIP_INTERVAL4
)) == 0) {
2783 /* Most common case first to avoid wasting time on all the other cases */
2784 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_ONE
;
2785 } else if(Flags
& WINEDDFLIP_NOVSYNC
) {
2786 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_IMMEDIATE
;
2787 } else if(Flags
& WINEDDFLIP_INTERVAL2
) {
2788 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_TWO
;
2789 } else if(Flags
& WINEDDFLIP_INTERVAL3
) {
2790 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_THREE
;
2792 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_FOUR
;
2795 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2796 return IWineD3DSwapChain_Present((IWineD3DSwapChain
*)swapchain
,
2797 NULL
, NULL
, swapchain
->win_handle
, NULL
, 0);
2800 /* Does a direct frame buffer -> texture copy. Stretching is done
2801 * with single pixel copy calls
2803 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl
*dst_surface
, IWineD3DSurfaceImpl
*src_surface
,
2804 const RECT
*src_rect
, const RECT
*dst_rect_in
, WINED3DTEXTUREFILTERTYPE Filter
)
2806 IWineD3DDeviceImpl
*device
= dst_surface
->resource
.device
;
2809 struct wined3d_context
*context
;
2810 BOOL upsidedown
= FALSE
;
2811 RECT dst_rect
= *dst_rect_in
;
2813 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2814 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2816 if(dst_rect
.top
> dst_rect
.bottom
) {
2817 UINT tmp
= dst_rect
.bottom
;
2818 dst_rect
.bottom
= dst_rect
.top
;
2823 context
= context_acquire(device
, src_surface
);
2824 context_apply_blit_state(context
, device
);
2825 surface_internal_preload(dst_surface
, SRGB_RGB
);
2828 /* Bind the target texture */
2829 glBindTexture(dst_surface
->texture_target
, dst_surface
->texture_name
);
2830 checkGLcall("glBindTexture");
2831 if (surface_is_offscreen(src_surface
))
2833 TRACE("Reading from an offscreen target\n");
2834 upsidedown
= !upsidedown
;
2835 glReadBuffer(device
->offscreenBuffer
);
2839 glReadBuffer(surface_get_gl_buffer(src_surface
));
2841 checkGLcall("glReadBuffer");
2843 xrel
= (float) (src_rect
->right
- src_rect
->left
) / (float) (dst_rect
.right
- dst_rect
.left
);
2844 yrel
= (float) (src_rect
->bottom
- src_rect
->top
) / (float) (dst_rect
.bottom
- dst_rect
.top
);
2846 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
2848 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2850 if(Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
) {
2851 ERR("Texture filtering not supported in direct blit\n");
2854 else if ((Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
)
2855 && ((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
2857 ERR("Texture filtering not supported in direct blit\n");
2861 && !((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
2862 && !((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
2864 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2866 glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
2867 dst_rect
.left
/*xoffset */, dst_rect
.top
/* y offset */,
2868 src_rect
->left
, src_surface
->currentDesc
.Height
- src_rect
->bottom
,
2869 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
2871 UINT yoffset
= src_surface
->currentDesc
.Height
- src_rect
->top
+ dst_rect
.top
- 1;
2872 /* I have to process this row by row to swap the image,
2873 * otherwise it would be upside down, so stretching in y direction
2874 * doesn't cost extra time
2876 * However, stretching in x direction can be avoided if not necessary
2878 for(row
= dst_rect
.top
; row
< dst_rect
.bottom
; row
++) {
2879 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
2881 /* Well, that stuff works, but it's very slow.
2882 * find a better way instead
2886 for (col
= dst_rect
.left
; col
< dst_rect
.right
; ++col
)
2888 glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
2889 dst_rect
.left
+ col
/* x offset */, row
/* y offset */,
2890 src_rect
->left
+ col
* xrel
, yoffset
- (int) (row
* yrel
), 1, 1);
2895 glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
2896 dst_rect
.left
/* x offset */, row
/* y offset */,
2897 src_rect
->left
, yoffset
- (int) (row
* yrel
), dst_rect
.right
- dst_rect
.left
, 1);
2901 checkGLcall("glCopyTexSubImage2D");
2904 context_release(context
);
2906 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2907 * path is never entered
2909 surface_modify_location(dst_surface
, SFLAG_INTEXTURE
, TRUE
);
2912 /* Uses the hardware to stretch and flip the image */
2913 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl
*dst_surface
, IWineD3DSurfaceImpl
*src_surface
,
2914 const RECT
*src_rect
, const RECT
*dst_rect_in
, WINED3DTEXTUREFILTERTYPE Filter
)
2916 IWineD3DDeviceImpl
*device
= dst_surface
->resource
.device
;
2917 GLuint src
, backup
= 0;
2918 IWineD3DSwapChainImpl
*src_swapchain
= NULL
;
2919 float left
, right
, top
, bottom
; /* Texture coordinates */
2920 UINT fbwidth
= src_surface
->currentDesc
.Width
;
2921 UINT fbheight
= src_surface
->currentDesc
.Height
;
2922 struct wined3d_context
*context
;
2923 GLenum drawBuffer
= GL_BACK
;
2924 GLenum texture_target
;
2925 BOOL noBackBufferBackup
;
2927 BOOL upsidedown
= FALSE
;
2928 RECT dst_rect
= *dst_rect_in
;
2930 TRACE("Using hwstretch blit\n");
2931 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2932 context
= context_acquire(device
, src_surface
);
2933 context_apply_blit_state(context
, device
);
2934 surface_internal_preload(dst_surface
, SRGB_RGB
);
2936 src_offscreen
= surface_is_offscreen(src_surface
);
2937 noBackBufferBackup
= src_offscreen
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
;
2938 if (!noBackBufferBackup
&& !src_surface
->texture_name
)
2940 /* Get it a description */
2941 surface_internal_preload(src_surface
, SRGB_RGB
);
2945 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2946 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2948 if (context
->aux_buffers
>= 2)
2950 /* Got more than one aux buffer? Use the 2nd aux buffer */
2951 drawBuffer
= GL_AUX1
;
2953 else if ((!src_offscreen
|| device
->offscreenBuffer
== GL_BACK
) && context
->aux_buffers
>= 1)
2955 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2956 drawBuffer
= GL_AUX0
;
2959 if(noBackBufferBackup
) {
2960 glGenTextures(1, &backup
);
2961 checkGLcall("glGenTextures");
2962 glBindTexture(GL_TEXTURE_2D
, backup
);
2963 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2964 texture_target
= GL_TEXTURE_2D
;
2966 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2967 * we are reading from the back buffer, the backup can be used as source texture
2969 texture_target
= src_surface
->texture_target
;
2970 glBindTexture(texture_target
, src_surface
->texture_name
);
2971 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
2972 glEnable(texture_target
);
2973 checkGLcall("glEnable(texture_target)");
2975 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2976 src_surface
->Flags
&= ~SFLAG_INTEXTURE
;
2979 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2980 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2982 if(dst_rect
.top
> dst_rect
.bottom
) {
2983 UINT tmp
= dst_rect
.bottom
;
2984 dst_rect
.bottom
= dst_rect
.top
;
2991 TRACE("Reading from an offscreen target\n");
2992 upsidedown
= !upsidedown
;
2993 glReadBuffer(device
->offscreenBuffer
);
2997 glReadBuffer(surface_get_gl_buffer(src_surface
));
3000 /* TODO: Only back up the part that will be overwritten */
3001 glCopyTexSubImage2D(texture_target
, 0,
3002 0, 0 /* read offsets */,
3007 checkGLcall("glCopyTexSubImage2D");
3009 /* No issue with overriding these - the sampler is dirty due to blit usage */
3010 glTexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
,
3011 wined3d_gl_mag_filter(magLookup
, Filter
));
3012 checkGLcall("glTexParameteri");
3013 glTexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
,
3014 wined3d_gl_min_mip_filter(minMipLookup
, Filter
, WINED3DTEXF_NONE
));
3015 checkGLcall("glTexParameteri");
3017 if (src_surface
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
3018 src_swapchain
= src_surface
->container
.u
.swapchain
;
3019 if (!src_swapchain
|| src_surface
== src_swapchain
->back_buffers
[0])
3021 src
= backup
? backup
: src_surface
->texture_name
;
3025 glReadBuffer(GL_FRONT
);
3026 checkGLcall("glReadBuffer(GL_FRONT)");
3028 glGenTextures(1, &src
);
3029 checkGLcall("glGenTextures(1, &src)");
3030 glBindTexture(GL_TEXTURE_2D
, src
);
3031 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3033 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3034 * out for power of 2 sizes
3036 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, src_surface
->pow2Width
,
3037 src_surface
->pow2Height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
3038 checkGLcall("glTexImage2D");
3039 glCopyTexSubImage2D(GL_TEXTURE_2D
, 0,
3040 0, 0 /* read offsets */,
3045 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3046 checkGLcall("glTexParameteri");
3047 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3048 checkGLcall("glTexParameteri");
3050 glReadBuffer(GL_BACK
);
3051 checkGLcall("glReadBuffer(GL_BACK)");
3053 if(texture_target
!= GL_TEXTURE_2D
) {
3054 glDisable(texture_target
);
3055 glEnable(GL_TEXTURE_2D
);
3056 texture_target
= GL_TEXTURE_2D
;
3059 checkGLcall("glEnd and previous");
3061 left
= src_rect
->left
;
3062 right
= src_rect
->right
;
3066 top
= src_surface
->currentDesc
.Height
- src_rect
->top
;
3067 bottom
= src_surface
->currentDesc
.Height
- src_rect
->bottom
;
3071 top
= src_surface
->currentDesc
.Height
- src_rect
->bottom
;
3072 bottom
= src_surface
->currentDesc
.Height
- src_rect
->top
;
3075 if (src_surface
->Flags
& SFLAG_NORMCOORD
)
3077 left
/= src_surface
->pow2Width
;
3078 right
/= src_surface
->pow2Width
;
3079 top
/= src_surface
->pow2Height
;
3080 bottom
/= src_surface
->pow2Height
;
3083 /* draw the source texture stretched and upside down. The correct surface is bound already */
3084 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
3085 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
3087 context_set_draw_buffer(context
, drawBuffer
);
3088 glReadBuffer(drawBuffer
);
3092 glTexCoord2f(left
, bottom
);
3093 glVertex2i(0, fbheight
);
3096 glTexCoord2f(left
, top
);
3097 glVertex2i(0, fbheight
- dst_rect
.bottom
- dst_rect
.top
);
3100 glTexCoord2f(right
, top
);
3101 glVertex2i(dst_rect
.right
- dst_rect
.left
, fbheight
- dst_rect
.bottom
- dst_rect
.top
);
3104 glTexCoord2f(right
, bottom
);
3105 glVertex2i(dst_rect
.right
- dst_rect
.left
, fbheight
);
3107 checkGLcall("glEnd and previous");
3109 if (texture_target
!= dst_surface
->texture_target
)
3111 glDisable(texture_target
);
3112 glEnable(dst_surface
->texture_target
);
3113 texture_target
= dst_surface
->texture_target
;
3116 /* Now read the stretched and upside down image into the destination texture */
3117 glBindTexture(texture_target
, dst_surface
->texture_name
);
3118 checkGLcall("glBindTexture");
3119 glCopyTexSubImage2D(texture_target
,
3121 dst_rect
.left
, dst_rect
.top
, /* xoffset, yoffset */
3122 0, 0, /* We blitted the image to the origin */
3123 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
3124 checkGLcall("glCopyTexSubImage2D");
3126 if(drawBuffer
== GL_BACK
) {
3127 /* Write the back buffer backup back */
3129 if(texture_target
!= GL_TEXTURE_2D
) {
3130 glDisable(texture_target
);
3131 glEnable(GL_TEXTURE_2D
);
3132 texture_target
= GL_TEXTURE_2D
;
3134 glBindTexture(GL_TEXTURE_2D
, backup
);
3135 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3139 if (texture_target
!= src_surface
->texture_target
)
3141 glDisable(texture_target
);
3142 glEnable(src_surface
->texture_target
);
3143 texture_target
= src_surface
->texture_target
;
3145 glBindTexture(src_surface
->texture_target
, src_surface
->texture_name
);
3146 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3151 glTexCoord2f(0.0f
, (float)fbheight
/ (float)src_surface
->pow2Height
);
3155 glTexCoord2f(0.0f
, 0.0f
);
3156 glVertex2i(0, fbheight
);
3159 glTexCoord2f((float)fbwidth
/ (float)src_surface
->pow2Width
, 0.0f
);
3160 glVertex2i(fbwidth
, src_surface
->currentDesc
.Height
);
3163 glTexCoord2f((float)fbwidth
/ (float)src_surface
->pow2Width
,
3164 (float)fbheight
/ (float)src_surface
->pow2Height
);
3165 glVertex2i(fbwidth
, 0);
3168 glDisable(texture_target
);
3169 checkGLcall("glDisable(texture_target)");
3172 if (src
!= src_surface
->texture_name
&& src
!= backup
)
3174 glDeleteTextures(1, &src
);
3175 checkGLcall("glDeleteTextures(1, &src)");
3178 glDeleteTextures(1, &backup
);
3179 checkGLcall("glDeleteTextures(1, &backup)");
3184 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
3186 context_release(context
);
3188 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3189 * path is never entered
3191 surface_modify_location(dst_surface
, SFLAG_INTEXTURE
, TRUE
);
3194 /* Until the blit_shader is ready, define some prototypes here. */
3195 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
3196 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
3197 const struct wined3d_format_desc
*src_format_desc
,
3198 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
3199 const struct wined3d_format_desc
*dst_format_desc
);
3201 /* Front buffer coordinates are always full screen coordinates, but our GL
3202 * drawable is limited to the window's client area. The sysmem and texture
3203 * copies do have the full screen size. Note that GL has a bottom-left
3204 * origin, while D3D has a top-left origin. */
3205 void surface_translate_frontbuffer_coords(IWineD3DSurfaceImpl
*surface
, HWND window
, RECT
*rect
)
3207 POINT offset
= {0, surface
->currentDesc
.Height
};
3210 GetClientRect(window
, &windowsize
);
3211 offset
.y
-= windowsize
.bottom
- windowsize
.top
;
3212 ScreenToClient(window
, &offset
);
3213 OffsetRect(rect
, offset
.x
, offset
.y
);
3216 static BOOL
surface_is_full_rect(IWineD3DSurfaceImpl
*surface
, const RECT
*r
)
3218 if ((r
->left
&& r
->right
) || abs(r
->right
- r
->left
) != surface
->currentDesc
.Width
)
3220 if ((r
->top
&& r
->bottom
) || abs(r
->bottom
- r
->top
) != surface
->currentDesc
.Height
)
3225 /* blit between surface locations. onscreen on different swapchains is not supported.
3226 * depth / stencil is not supported. */
3227 static void surface_blt_fbo(IWineD3DDeviceImpl
*device
, const WINED3DTEXTUREFILTERTYPE filter
,
3228 IWineD3DSurfaceImpl
*src_surface
, DWORD src_location
, const RECT
*src_rect_in
,
3229 IWineD3DSurfaceImpl
*dst_surface
, DWORD dst_location
, const RECT
*dst_rect_in
)
3231 const struct wined3d_gl_info
*gl_info
;
3232 struct wined3d_context
*context
;
3233 RECT src_rect
, dst_rect
;
3236 TRACE("device %p, filter %s,\n", device
, debug_d3dtexturefiltertype(filter
));
3237 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3238 src_surface
, debug_surflocation(src_location
), wine_dbgstr_rect(src_rect_in
));
3239 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3240 dst_surface
, debug_surflocation(dst_location
), wine_dbgstr_rect(dst_rect_in
));
3242 src_rect
= *src_rect_in
;
3243 dst_rect
= *dst_rect_in
;
3247 case WINED3DTEXF_LINEAR
:
3248 gl_filter
= GL_LINEAR
;
3252 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter
), filter
);
3253 case WINED3DTEXF_NONE
:
3254 case WINED3DTEXF_POINT
:
3255 gl_filter
= GL_NEAREST
;
3259 if (src_location
== SFLAG_INDRAWABLE
&& surface_is_offscreen(src_surface
))
3260 src_location
= SFLAG_INTEXTURE
;
3261 if (dst_location
== SFLAG_INDRAWABLE
&& surface_is_offscreen(dst_surface
))
3262 dst_location
= SFLAG_INTEXTURE
;
3264 /* Make sure the locations are up-to-date. Loading the destination
3265 * surface isn't required if the entire surface is overwritten. (And is
3266 * in fact harmful if we're being called by surface_load_location() with
3267 * the purpose of loading the destination surface.) */
3268 surface_load_location(src_surface
, src_location
, NULL
);
3269 if (!surface_is_full_rect(dst_surface
, &dst_rect
))
3270 surface_load_location(dst_surface
, dst_location
, NULL
);
3272 if (src_location
== SFLAG_INDRAWABLE
) context
= context_acquire(device
, src_surface
);
3273 else if (dst_location
== SFLAG_INDRAWABLE
) context
= context_acquire(device
, dst_surface
);
3274 else context
= context_acquire(device
, NULL
);
3276 if (!context
->valid
)
3278 context_release(context
);
3279 WARN("Invalid context, skipping blit.\n");
3283 gl_info
= context
->gl_info
;
3285 if (src_location
== SFLAG_INDRAWABLE
)
3287 GLenum buffer
= surface_get_gl_buffer(src_surface
);
3289 TRACE("Source surface %p is onscreen.\n", src_surface
);
3291 if (buffer
== GL_FRONT
)
3292 surface_translate_frontbuffer_coords(src_surface
, context
->win_handle
, &src_rect
);
3294 src_rect
.top
= src_surface
->currentDesc
.Height
- src_rect
.top
;
3295 src_rect
.bottom
= src_surface
->currentDesc
.Height
- src_rect
.bottom
;
3298 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, NULL
);
3299 glReadBuffer(buffer
);
3300 checkGLcall("glReadBuffer()");
3304 TRACE("Source surface %p is offscreen.\n", src_surface
);
3306 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, src_surface
, NULL
, src_location
);
3307 glReadBuffer(GL_COLOR_ATTACHMENT0
);
3308 checkGLcall("glReadBuffer()");
3312 if (dst_location
== SFLAG_INDRAWABLE
)
3314 GLenum buffer
= surface_get_gl_buffer(dst_surface
);
3316 TRACE("Destination surface %p is onscreen.\n", dst_surface
);
3318 if (buffer
== GL_FRONT
)
3319 surface_translate_frontbuffer_coords(dst_surface
, context
->win_handle
, &dst_rect
);
3321 dst_rect
.top
= dst_surface
->currentDesc
.Height
- dst_rect
.top
;
3322 dst_rect
.bottom
= dst_surface
->currentDesc
.Height
- dst_rect
.bottom
;
3325 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, NULL
);
3326 context_set_draw_buffer(context
, buffer
);
3330 TRACE("Destination surface %p is offscreen.\n", dst_surface
);
3333 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, dst_surface
, NULL
, dst_location
);
3334 context_set_draw_buffer(context
, GL_COLOR_ATTACHMENT0
);
3337 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3338 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
));
3339 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
));
3340 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
));
3341 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
));
3343 glDisable(GL_SCISSOR_TEST
);
3344 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
3346 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
.left
, src_rect
.top
, src_rect
.right
, src_rect
.bottom
,
3347 dst_rect
.left
, dst_rect
.top
, dst_rect
.right
, dst_rect
.bottom
, GL_COLOR_BUFFER_BIT
, gl_filter
);
3348 checkGLcall("glBlitFramebuffer()");
3352 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
3354 context_release(context
);
3357 /* Not called from the VTable */
3358 static HRESULT
IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl
*dst_surface
, const RECT
*DestRect
,
3359 IWineD3DSurfaceImpl
*src_surface
, const RECT
*SrcRect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
,
3360 WINED3DTEXTUREFILTERTYPE Filter
)
3362 IWineD3DDeviceImpl
*device
= dst_surface
->resource
.device
;
3363 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
3364 IWineD3DSwapChainImpl
*srcSwapchain
= NULL
, *dstSwapchain
= NULL
;
3365 RECT dst_rect
, src_rect
;
3367 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3368 dst_surface
, wine_dbgstr_rect(DestRect
), src_surface
, wine_dbgstr_rect(SrcRect
),
3369 Flags
, DDBltFx
, debug_d3dtexturefiltertype(Filter
));
3371 /* Get the swapchain. One of the surfaces has to be a primary surface */
3372 if (dst_surface
->resource
.pool
== WINED3DPOOL_SYSTEMMEM
)
3374 WARN("Destination is in sysmem, rejecting gl blt\n");
3375 return WINED3DERR_INVALIDCALL
;
3378 if (dst_surface
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
3379 dstSwapchain
= dst_surface
->container
.u
.swapchain
;
3383 if (src_surface
->resource
.pool
== WINED3DPOOL_SYSTEMMEM
)
3385 WARN("Src is in sysmem, rejecting gl blt\n");
3386 return WINED3DERR_INVALIDCALL
;
3389 if (src_surface
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
3390 srcSwapchain
= src_surface
->container
.u
.swapchain
;
3393 /* Early sort out of cases where no render target is used */
3394 if (!dstSwapchain
&& !srcSwapchain
3395 && src_surface
!= device
->render_targets
[0]
3396 && dst_surface
!= device
->render_targets
[0])
3398 TRACE("No surface is render target, not using hardware blit.\n");
3399 return WINED3DERR_INVALIDCALL
;
3402 /* No destination color keying supported */
3403 if(Flags
& (WINEDDBLT_KEYDEST
| WINEDDBLT_KEYDESTOVERRIDE
)) {
3404 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3405 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3406 return WINED3DERR_INVALIDCALL
;
3409 surface_get_rect(dst_surface
, DestRect
, &dst_rect
);
3410 if (src_surface
) surface_get_rect(src_surface
, SrcRect
, &src_rect
);
3412 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3413 if (dstSwapchain
&& dstSwapchain
== srcSwapchain
&& dstSwapchain
->back_buffers
3414 && dst_surface
== dstSwapchain
->front_buffer
3415 && src_surface
== dstSwapchain
->back_buffers
[0])
3417 /* Half-Life does a Blt from the back buffer to the front buffer,
3418 * Full surface size, no flags... Use present instead
3420 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3423 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3426 TRACE("Looking if a Present can be done...\n");
3427 /* Source Rectangle must be full surface */
3428 if (src_rect
.left
|| src_rect
.top
3429 || src_rect
.right
!= src_surface
->currentDesc
.Width
3430 || src_rect
.bottom
!= src_surface
->currentDesc
.Height
)
3432 TRACE("No, Source rectangle doesn't match\n");
3436 /* No stretching may occur */
3437 if(src_rect
.right
!= dst_rect
.right
- dst_rect
.left
||
3438 src_rect
.bottom
!= dst_rect
.bottom
- dst_rect
.top
) {
3439 TRACE("No, stretching is done\n");
3443 /* Destination must be full surface or match the clipping rectangle */
3444 if (dst_surface
->clipper
&& ((IWineD3DClipperImpl
*)dst_surface
->clipper
)->hWnd
)
3448 GetClientRect(((IWineD3DClipperImpl
*)dst_surface
->clipper
)->hWnd
, &cliprect
);
3449 pos
[0].x
= dst_rect
.left
;
3450 pos
[0].y
= dst_rect
.top
;
3451 pos
[1].x
= dst_rect
.right
;
3452 pos
[1].y
= dst_rect
.bottom
;
3453 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl
*)dst_surface
->clipper
)->hWnd
, pos
, 2);
3455 if(pos
[0].x
!= cliprect
.left
|| pos
[0].y
!= cliprect
.top
||
3456 pos
[1].x
!= cliprect
.right
|| pos
[1].y
!= cliprect
.bottom
)
3458 TRACE("No, dest rectangle doesn't match(clipper)\n");
3459 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect
));
3460 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect
));
3464 else if (dst_rect
.left
|| dst_rect
.top
3465 || dst_rect
.right
!= dst_surface
->currentDesc
.Width
3466 || dst_rect
.bottom
!= dst_surface
->currentDesc
.Height
)
3468 TRACE("No, dest rectangle doesn't match(surface size)\n");
3474 /* These flags are unimportant for the flag check, remove them */
3475 if((Flags
& ~(WINEDDBLT_DONOTWAIT
| WINEDDBLT_WAIT
)) == 0) {
3476 WINED3DSWAPEFFECT orig_swap
= dstSwapchain
->presentParms
.SwapEffect
;
3478 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3479 * take very long, while a flip is fast.
3480 * This applies to Half-Life, which does such Blts every time it finished
3481 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3482 * menu. This is also used by all apps when they do windowed rendering
3484 * The problem is that flipping is not really the same as copying. After a
3485 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3486 * untouched. Therefore it's necessary to override the swap effect
3487 * and to set it back after the flip.
3489 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3493 dstSwapchain
->presentParms
.SwapEffect
= WINED3DSWAPEFFECT_COPY
;
3494 dstSwapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_IMMEDIATE
;
3496 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3497 IWineD3DSwapChain_Present((IWineD3DSwapChain
*)dstSwapchain
,
3498 NULL
, NULL
, dstSwapchain
->win_handle
, NULL
, 0);
3500 dstSwapchain
->presentParms
.SwapEffect
= orig_swap
;
3507 TRACE("Unsupported blit between buffers on the same swapchain\n");
3508 return WINED3DERR_INVALIDCALL
;
3509 } else if(dstSwapchain
&& dstSwapchain
== srcSwapchain
) {
3510 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3511 return WINED3DERR_INVALIDCALL
;
3512 } else if(dstSwapchain
&& srcSwapchain
) {
3513 FIXME("Implement hardware blit between two different swapchains\n");
3514 return WINED3DERR_INVALIDCALL
;
3516 else if (dstSwapchain
)
3518 /* Handled with regular texture -> swapchain blit */
3519 if (src_surface
== device
->render_targets
[0])
3520 TRACE("Blit from active render target to a swapchain\n");
3522 else if (srcSwapchain
&& dst_surface
== device
->render_targets
[0])
3524 FIXME("Implement blit from a swapchain to the active render target\n");
3525 return WINED3DERR_INVALIDCALL
;
3528 if ((srcSwapchain
|| src_surface
== device
->render_targets
[0]) && !dstSwapchain
)
3530 /* Blit from render target to texture */
3533 /* P8 read back is not implemented */
3534 if (src_surface
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
3535 || dst_surface
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
)
3537 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3538 return WINED3DERR_INVALIDCALL
;
3541 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3542 TRACE("Color keying not supported by frame buffer to texture blit\n");
3543 return WINED3DERR_INVALIDCALL
;
3544 /* Destination color key is checked above */
3547 if(dst_rect
.right
- dst_rect
.left
!= src_rect
.right
- src_rect
.left
) {
3553 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3554 * flip the image nor scale it.
3556 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3557 * -> If the app wants a image width an unscaled width, copy it line per line
3558 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3559 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3560 * back buffer. This is slower than reading line per line, thus not used for flipping
3561 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3564 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3565 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3568 if (fbo_blit_supported(gl_info
, BLIT_OP_BLIT
,
3569 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
, src_surface
->resource
.format_desc
,
3570 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
, dst_surface
->resource
.format_desc
))
3572 surface_blt_fbo(device
, Filter
,
3573 src_surface
, SFLAG_INDRAWABLE
, &src_rect
,
3574 dst_surface
, SFLAG_INDRAWABLE
, &dst_rect
);
3575 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
3577 else if (!stretchx
|| dst_rect
.right
- dst_rect
.left
> src_surface
->currentDesc
.Width
3578 || dst_rect
.bottom
- dst_rect
.top
> src_surface
->currentDesc
.Height
)
3580 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3581 fb_copy_to_texture_direct(dst_surface
, src_surface
, &src_rect
, &dst_rect
, Filter
);
3583 TRACE("Using hardware stretching to flip / stretch the texture\n");
3584 fb_copy_to_texture_hwstretch(dst_surface
, src_surface
, &src_rect
, &dst_rect
, Filter
);
3587 if (!(dst_surface
->Flags
& SFLAG_DONOTFREE
))
3589 HeapFree(GetProcessHeap(), 0, dst_surface
->resource
.heapMemory
);
3590 dst_surface
->resource
.allocatedMemory
= NULL
;
3591 dst_surface
->resource
.heapMemory
= NULL
;
3595 dst_surface
->Flags
&= ~SFLAG_INSYSMEM
;
3600 else if (src_surface
)
3602 /* Blit from offscreen surface to render target */
3603 DWORD oldCKeyFlags
= src_surface
->CKeyFlags
;
3604 WINEDDCOLORKEY oldBltCKey
= src_surface
->SrcBltCKey
;
3605 struct wined3d_context
*context
;
3607 TRACE("Blt from surface %p to rendertarget %p\n", src_surface
, dst_surface
);
3609 if (!(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
))
3610 && fbo_blit_supported(gl_info
, BLIT_OP_BLIT
,
3611 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
,
3612 src_surface
->resource
.format_desc
,
3613 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3614 dst_surface
->resource
.format_desc
))
3616 TRACE("Using surface_blt_fbo.\n");
3617 /* The source is always a texture, but never the currently active render target, and the texture
3618 * contents are never upside down. */
3619 surface_blt_fbo(device
, Filter
,
3620 src_surface
, SFLAG_INDRAWABLE
, &src_rect
,
3621 dst_surface
, SFLAG_INDRAWABLE
, &dst_rect
);
3622 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
3626 if (!(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
))
3627 && arbfp_blit
.blit_supported(gl_info
, BLIT_OP_BLIT
,
3628 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
,
3629 src_surface
->resource
.format_desc
,
3630 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3631 dst_surface
->resource
.format_desc
))
3633 return arbfp_blit_surface(device
, src_surface
, &src_rect
, dst_surface
, &dst_rect
, BLIT_OP_BLIT
, Filter
);
3636 /* Color keying: Check if we have to do a color keyed blt,
3637 * and if not check if a color key is activated.
3639 * Just modify the color keying parameters in the surface and restore them afterwards
3640 * The surface keeps track of the color key last used to load the opengl surface.
3641 * PreLoad will catch the change to the flags and color key and reload if necessary.
3643 if(Flags
& WINEDDBLT_KEYSRC
) {
3644 /* Use color key from surface */
3645 } else if(Flags
& WINEDDBLT_KEYSRCOVERRIDE
) {
3646 /* Use color key from DDBltFx */
3647 src_surface
->CKeyFlags
|= WINEDDSD_CKSRCBLT
;
3648 src_surface
->SrcBltCKey
= DDBltFx
->ddckSrcColorkey
;
3650 /* Do not use color key */
3651 src_surface
->CKeyFlags
&= ~WINEDDSD_CKSRCBLT
;
3654 /* Now load the surface */
3655 surface_internal_preload(src_surface
, SRGB_RGB
);
3657 /* Activate the destination context, set it up for blitting */
3658 context
= context_acquire(device
, dst_surface
);
3659 context_apply_blit_state(context
, device
);
3661 if (dstSwapchain
&& dst_surface
== dstSwapchain
->front_buffer
)
3662 surface_translate_frontbuffer_coords(dst_surface
, context
->win_handle
, &dst_rect
);
3664 if (!device
->blitter
->blit_supported(gl_info
, BLIT_OP_BLIT
,
3665 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
, src_surface
->resource
.format_desc
,
3666 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
, dst_surface
->resource
.format_desc
))
3668 FIXME("Unsupported blit operation falling back to software\n");
3669 return WINED3DERR_INVALIDCALL
;
3672 device
->blitter
->set_shader((IWineD3DDevice
*)device
, src_surface
);
3676 /* This is for color keying */
3677 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3678 glEnable(GL_ALPHA_TEST
);
3679 checkGLcall("glEnable(GL_ALPHA_TEST)");
3681 /* When the primary render target uses P8, the alpha component contains the palette index.
3682 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3683 * should be masked away have alpha set to 0. */
3684 if (primary_render_target_is_p8(device
))
3685 glAlphaFunc(GL_NOTEQUAL
, (float)src_surface
->SrcBltCKey
.dwColorSpaceLowValue
/ 256.0f
);
3687 glAlphaFunc(GL_NOTEQUAL
, 0.0f
);
3688 checkGLcall("glAlphaFunc");
3690 glDisable(GL_ALPHA_TEST
);
3691 checkGLcall("glDisable(GL_ALPHA_TEST)");
3694 /* Draw a textured quad
3696 draw_textured_quad(src_surface
, &src_rect
, &dst_rect
, Filter
);
3698 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3699 glDisable(GL_ALPHA_TEST
);
3700 checkGLcall("glDisable(GL_ALPHA_TEST)");
3703 /* Restore the color key parameters */
3704 src_surface
->CKeyFlags
= oldCKeyFlags
;
3705 src_surface
->SrcBltCKey
= oldBltCKey
;
3709 /* Leave the opengl state valid for blitting */
3710 device
->blitter
->unset_shader((IWineD3DDevice
*)device
);
3712 if (wined3d_settings
.strict_draw_ordering
|| (dstSwapchain
3713 && (dst_surface
== dstSwapchain
->front_buffer
3714 || dstSwapchain
->num_contexts
> 1)))
3715 wglFlush(); /* Flush to ensure ordering across contexts. */
3717 context_release(context
);
3719 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3720 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3723 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
3729 /* Source-Less Blit to render target */
3730 if (Flags
& WINEDDBLT_COLORFILL
)
3732 WINED3DCOLORVALUE color
;
3734 TRACE("Colorfill\n");
3736 /* The color as given in the Blt function is in the surface format. */
3737 if (!surface_convert_color_to_float(dst_surface
, DDBltFx
->u5
.dwFillColor
, &color
))
3738 return WINED3DERR_INVALIDCALL
;
3740 if (ffp_blit
.blit_supported(gl_info
, BLIT_OP_COLOR_FILL
,
3742 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3743 dst_surface
->resource
.format_desc
))
3745 return ffp_blit
.color_fill(device
, dst_surface
, &dst_rect
, &color
);
3747 else if (cpu_blit
.blit_supported(gl_info
, BLIT_OP_COLOR_FILL
,
3749 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3750 dst_surface
->resource
.format_desc
))
3752 return cpu_blit
.color_fill(device
, dst_surface
, &dst_rect
, &color
);
3754 return WINED3DERR_INVALIDCALL
;
3758 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3759 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3760 return WINED3DERR_INVALIDCALL
;
3763 static HRESULT
IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl
*This
, const RECT
*DestRect
,
3764 IWineD3DSurface
*src_surface
, const RECT
*src_rect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
)
3766 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3769 if (Flags
& WINEDDBLT_DEPTHFILL
)
3771 switch (This
->resource
.format_desc
->id
)
3773 case WINED3DFMT_D16_UNORM
:
3774 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x0000ffff;
3776 case WINED3DFMT_S1_UINT_D15_UNORM
:
3777 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x00007fff;
3779 case WINED3DFMT_D24_UNORM_S8_UINT
:
3780 case WINED3DFMT_X8D24_UNORM
:
3781 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x00ffffff;
3783 case WINED3DFMT_D32_UNORM
:
3784 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0xffffffff;
3788 ERR("Unexpected format for depth fill: %s.\n", debug_d3dformat(This
->resource
.format_desc
->id
));
3791 return IWineD3DDevice_Clear((IWineD3DDevice
*)device
, DestRect
? 1 : 0, (const WINED3DRECT
*)DestRect
,
3792 WINED3DCLEAR_ZBUFFER
, 0x00000000, depth
, 0x00000000);
3795 FIXME("(%p): Unsupp depthstencil blit\n", This
);
3796 return WINED3DERR_INVALIDCALL
;
3799 static HRESULT WINAPI
IWineD3DSurfaceImpl_Blt(IWineD3DSurface
*iface
, const RECT
*DestRect
,
3800 IWineD3DSurface
*src_surface
, const RECT
*SrcRect
, DWORD Flags
,
3801 const WINEDDBLTFX
*DDBltFx
, WINED3DTEXTUREFILTERTYPE Filter
)
3803 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
3804 IWineD3DSurfaceImpl
*src
= (IWineD3DSurfaceImpl
*)src_surface
;
3805 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3807 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3808 iface
, wine_dbgstr_rect(DestRect
), src_surface
, wine_dbgstr_rect(SrcRect
),
3809 Flags
, DDBltFx
, debug_d3dtexturefiltertype(Filter
));
3810 TRACE("Usage is %s.\n", debug_d3dusage(This
->resource
.usage
));
3812 if ((This
->Flags
& SFLAG_LOCKED
) || (src
&& (src
->Flags
& SFLAG_LOCKED
)))
3814 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3815 return WINEDDERR_SURFACEBUSY
;
3818 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3819 * except depth blits, which seem to work
3821 if (This
== device
->depth_stencil
|| (src
&& src
== device
->depth_stencil
))
3823 if (device
->inScene
&& !(Flags
& WINEDDBLT_DEPTHFILL
))
3825 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3826 return WINED3DERR_INVALIDCALL
;
3828 else if (SUCCEEDED(IWineD3DSurfaceImpl_BltZ(This
, DestRect
, src_surface
, SrcRect
, Flags
, DDBltFx
)))
3830 TRACE("Z Blit override handled the blit\n");
3835 /* Special cases for RenderTargets */
3836 if ((This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3837 || (src
&& (src
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)))
3839 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This
, DestRect
, src
, SrcRect
, Flags
, DDBltFx
, Filter
)))
3843 /* For the rest call the X11 surface implementation.
3844 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3845 * other Blts are rather rare. */
3846 return IWineD3DBaseSurfaceImpl_Blt(iface
, DestRect
, src_surface
, SrcRect
, Flags
, DDBltFx
, Filter
);
3849 static HRESULT WINAPI
IWineD3DSurfaceImpl_BltFast(IWineD3DSurface
*iface
, DWORD dstx
, DWORD dsty
,
3850 IWineD3DSurface
*src_surface
, const RECT
*rsrc
, DWORD trans
)
3852 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
3853 IWineD3DSurfaceImpl
*src
= (IWineD3DSurfaceImpl
*)src_surface
;
3854 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3856 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3857 iface
, dstx
, dsty
, src_surface
, wine_dbgstr_rect(rsrc
), trans
);
3859 if ((This
->Flags
& SFLAG_LOCKED
) || (src
->Flags
& SFLAG_LOCKED
))
3861 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3862 return WINEDDERR_SURFACEBUSY
;
3865 if (device
->inScene
&& (This
== device
->depth_stencil
|| src
== device
->depth_stencil
))
3867 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3868 return WINED3DERR_INVALIDCALL
;
3871 /* Special cases for RenderTargets */
3872 if ((This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3873 || (src
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
))
3876 RECT SrcRect
, DstRect
;
3879 surface_get_rect(src
, rsrc
, &SrcRect
);
3881 DstRect
.left
= dstx
;
3883 DstRect
.right
= dstx
+ SrcRect
.right
- SrcRect
.left
;
3884 DstRect
.bottom
= dsty
+ SrcRect
.bottom
- SrcRect
.top
;
3886 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3887 if(trans
& WINEDDBLTFAST_SRCCOLORKEY
)
3888 Flags
|= WINEDDBLT_KEYSRC
;
3889 if(trans
& WINEDDBLTFAST_DESTCOLORKEY
)
3890 Flags
|= WINEDDBLT_KEYDEST
;
3891 if(trans
& WINEDDBLTFAST_WAIT
)
3892 Flags
|= WINEDDBLT_WAIT
;
3893 if(trans
& WINEDDBLTFAST_DONOTWAIT
)
3894 Flags
|= WINEDDBLT_DONOTWAIT
;
3896 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This
,
3897 &DstRect
, src
, &SrcRect
, Flags
, NULL
, WINED3DTEXF_POINT
)))
3901 return IWineD3DBaseSurfaceImpl_BltFast(iface
, dstx
, dsty
, src_surface
, rsrc
, trans
);
3904 static HRESULT WINAPI
IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface
*iface
)
3906 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3908 IWineD3DPaletteImpl
*pal
= This
->palette
;
3910 TRACE("(%p)\n", This
);
3912 if (!pal
) return WINED3D_OK
;
3914 if (This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
3915 || This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT_A8_UNORM
)
3917 if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3919 /* Make sure the texture is up to date. This call doesn't do
3920 * anything if the texture is already up to date. */
3921 surface_load_location(This
, SFLAG_INTEXTURE
, NULL
);
3923 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3924 surface_modify_location(This
, SFLAG_INDRAWABLE
, FALSE
);
3928 if (!(This
->Flags
& SFLAG_INSYSMEM
))
3930 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
3931 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
3933 TRACE("Dirtifying surface\n");
3934 surface_modify_location(This
, SFLAG_INSYSMEM
, TRUE
);
3938 if(This
->Flags
& SFLAG_DIBSECTION
) {
3939 TRACE("(%p): Updating the hdc's palette\n", This
);
3940 for (n
=0; n
<256; n
++) {
3941 col
[n
].rgbRed
= pal
->palents
[n
].peRed
;
3942 col
[n
].rgbGreen
= pal
->palents
[n
].peGreen
;
3943 col
[n
].rgbBlue
= pal
->palents
[n
].peBlue
;
3944 col
[n
].rgbReserved
= 0;
3946 SetDIBColorTable(This
->hDC
, 0, 256, col
);
3949 /* Propagate the changes to the drawable when we have a palette. */
3950 if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3951 surface_load_location(This
, SFLAG_INDRAWABLE
, NULL
);
3956 static HRESULT WINAPI
IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface
*iface
) {
3957 /** Check against the maximum texture sizes supported by the video card **/
3958 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3959 const struct wined3d_gl_info
*gl_info
= &This
->resource
.device
->adapter
->gl_info
;
3960 unsigned int pow2Width
, pow2Height
;
3962 This
->texture_name
= 0;
3963 This
->texture_target
= GL_TEXTURE_2D
;
3965 /* Non-power2 support */
3966 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
] || gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
3968 pow2Width
= This
->currentDesc
.Width
;
3969 pow2Height
= This
->currentDesc
.Height
;
3973 /* Find the nearest pow2 match */
3974 pow2Width
= pow2Height
= 1;
3975 while (pow2Width
< This
->currentDesc
.Width
) pow2Width
<<= 1;
3976 while (pow2Height
< This
->currentDesc
.Height
) pow2Height
<<= 1;
3978 This
->pow2Width
= pow2Width
;
3979 This
->pow2Height
= pow2Height
;
3981 if (pow2Width
> This
->currentDesc
.Width
|| pow2Height
> This
->currentDesc
.Height
) {
3982 /** TODO: add support for non power two compressed textures **/
3983 if (This
->resource
.format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
3985 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3986 This
, This
->currentDesc
.Width
, This
->currentDesc
.Height
);
3987 return WINED3DERR_NOTAVAILABLE
;
3991 if(pow2Width
!= This
->currentDesc
.Width
||
3992 pow2Height
!= This
->currentDesc
.Height
) {
3993 This
->Flags
|= SFLAG_NONPOW2
;
3996 TRACE("%p\n", This
);
3997 if ((This
->pow2Width
> gl_info
->limits
.texture_size
|| This
->pow2Height
> gl_info
->limits
.texture_size
)
3998 && !(This
->resource
.usage
& (WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
)))
4000 /* one of three options
4001 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)
4002 2: Set the texture to the maximum size (bad idea)
4003 3: WARN and return WINED3DERR_NOTAVAILABLE;
4004 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.
4006 if(This
->resource
.pool
== WINED3DPOOL_DEFAULT
|| This
->resource
.pool
== WINED3DPOOL_MANAGED
)
4008 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This
);
4009 return WINED3DERR_NOTAVAILABLE
;
4012 /* We should never use this surface in combination with OpenGL! */
4013 TRACE("(%p) Creating an oversized surface: %ux%u\n", This
, This
->pow2Width
, This
->pow2Height
);
4017 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4018 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4019 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4021 if (This
->Flags
& SFLAG_NONPOW2
&& gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
4022 && !(This
->resource
.format_desc
->id
== WINED3DFMT_P8_UINT
4023 && gl_info
->supported
[EXT_PALETTED_TEXTURE
]
4024 && wined3d_settings
.rendertargetlock_mode
== RTL_READTEX
))
4026 This
->texture_target
= GL_TEXTURE_RECTANGLE_ARB
;
4027 This
->pow2Width
= This
->currentDesc
.Width
;
4028 This
->pow2Height
= This
->currentDesc
.Height
;
4029 This
->Flags
&= ~(SFLAG_NONPOW2
| SFLAG_NORMCOORD
);
4033 switch (wined3d_settings
.offscreen_rendering_mode
)
4036 This
->get_drawable_size
= get_drawable_size_fbo
;
4039 case ORM_BACKBUFFER
:
4040 This
->get_drawable_size
= get_drawable_size_backbuffer
;
4044 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
4045 return WINED3DERR_INVALIDCALL
;
4048 This
->Flags
|= SFLAG_INSYSMEM
;
4053 /* GL locking is done by the caller */
4054 static void surface_depth_blt(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
4055 GLuint texture
, GLsizei w
, GLsizei h
, GLenum target
)
4057 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4058 GLint compare_mode
= GL_NONE
;
4059 struct blt_info info
;
4060 GLint old_binding
= 0;
4062 glPushAttrib(GL_ENABLE_BIT
| GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
| GL_VIEWPORT_BIT
);
4064 glDisable(GL_CULL_FACE
);
4065 glDisable(GL_BLEND
);
4066 glDisable(GL_ALPHA_TEST
);
4067 glDisable(GL_SCISSOR_TEST
);
4068 glDisable(GL_STENCIL_TEST
);
4069 glEnable(GL_DEPTH_TEST
);
4070 glDepthFunc(GL_ALWAYS
);
4071 glDepthMask(GL_TRUE
);
4072 glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
4073 glViewport(0, 0, w
, h
);
4075 surface_get_blt_info(target
, NULL
, w
, h
, &info
);
4076 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
4077 glGetIntegerv(info
.binding
, &old_binding
);
4078 glBindTexture(info
.bind_target
, texture
);
4079 if (gl_info
->supported
[ARB_SHADOW
])
4081 glGetTexParameteriv(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, &compare_mode
);
4082 if (compare_mode
!= GL_NONE
) glTexParameteri(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
4085 device
->shader_backend
->shader_select_depth_blt((IWineD3DDevice
*)device
,
4086 info
.tex_type
, &This
->ds_current_size
);
4088 glBegin(GL_TRIANGLE_STRIP
);
4089 glTexCoord3fv(info
.coords
[0]);
4090 glVertex2f(-1.0f
, -1.0f
);
4091 glTexCoord3fv(info
.coords
[1]);
4092 glVertex2f(1.0f
, -1.0f
);
4093 glTexCoord3fv(info
.coords
[2]);
4094 glVertex2f(-1.0f
, 1.0f
);
4095 glTexCoord3fv(info
.coords
[3]);
4096 glVertex2f(1.0f
, 1.0f
);
4099 if (compare_mode
!= GL_NONE
) glTexParameteri(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, compare_mode
);
4100 glBindTexture(info
.bind_target
, old_binding
);
4104 device
->shader_backend
->shader_deselect_depth_blt((IWineD3DDevice
*)device
);
4107 void surface_modify_ds_location(IWineD3DSurfaceImpl
*surface
,
4108 DWORD location
, UINT w
, UINT h
)
4110 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface
, location
, w
, h
);
4112 if (location
& ~SFLAG_DS_LOCATIONS
)
4113 FIXME("Invalid location (%#x) specified.\n", location
);
4115 surface
->ds_current_size
.cx
= w
;
4116 surface
->ds_current_size
.cy
= h
;
4117 surface
->Flags
&= ~SFLAG_DS_LOCATIONS
;
4118 surface
->Flags
|= location
;
4121 /* Context activation is done by the caller. */
4122 void surface_load_ds_location(IWineD3DSurfaceImpl
*surface
, struct wined3d_context
*context
, DWORD location
)
4124 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
4125 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4127 TRACE("surface %p, new location %#x.\n", surface
, location
);
4129 /* TODO: Make this work for modes other than FBO */
4130 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return;
4132 if (!(surface
->Flags
& location
))
4134 surface
->ds_current_size
.cx
= 0;
4135 surface
->ds_current_size
.cy
= 0;
4138 if (surface
->ds_current_size
.cx
== surface
->currentDesc
.Width
4139 && surface
->ds_current_size
.cy
== surface
->currentDesc
.Height
)
4141 TRACE("Location (%#x) is already up to date.\n", location
);
4145 if (surface
->current_renderbuffer
)
4147 FIXME("Not supported with fixed up depth stencil.\n");
4151 if (!(surface
->Flags
& SFLAG_LOCATIONS
))
4153 FIXME("No up to date depth stencil location.\n");
4154 surface
->Flags
|= location
;
4158 if (location
== SFLAG_DS_OFFSCREEN
)
4160 GLint old_binding
= 0;
4163 TRACE("Copying onscreen depth buffer to depth texture.\n");
4167 if (!device
->depth_blt_texture
)
4169 glGenTextures(1, &device
->depth_blt_texture
);
4172 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4173 * directly on the FBO texture. That's because we need to flip. */
4174 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4175 if (surface
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
4177 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
4178 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
4182 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
4183 bind_target
= GL_TEXTURE_2D
;
4185 glBindTexture(bind_target
, device
->depth_blt_texture
);
4186 glCopyTexImage2D(bind_target
, surface
->texture_level
, surface
->resource
.format_desc
->glInternal
,
4187 0, 0, surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, 0);
4188 glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4189 glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4190 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
4191 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
4192 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
4193 glTexParameteri(bind_target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
4194 glBindTexture(bind_target
, old_binding
);
4196 /* Setup the destination */
4197 if (!device
->depth_blt_rb
)
4199 gl_info
->fbo_ops
.glGenRenderbuffers(1, &device
->depth_blt_rb
);
4200 checkGLcall("glGenRenderbuffersEXT");
4202 if (device
->depth_blt_rb_w
!= surface
->currentDesc
.Width
4203 || device
->depth_blt_rb_h
!= surface
->currentDesc
.Height
)
4205 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, device
->depth_blt_rb
);
4206 checkGLcall("glBindRenderbufferEXT");
4207 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, GL_RGBA8
,
4208 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
);
4209 checkGLcall("glRenderbufferStorageEXT");
4210 device
->depth_blt_rb_w
= surface
->currentDesc
.Width
;
4211 device
->depth_blt_rb_h
= surface
->currentDesc
.Height
;
4214 context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->dst_fbo
);
4215 gl_info
->fbo_ops
.glFramebufferRenderbuffer(GL_FRAMEBUFFER
,
4216 GL_COLOR_ATTACHMENT0
, GL_RENDERBUFFER
, device
->depth_blt_rb
);
4217 checkGLcall("glFramebufferRenderbufferEXT");
4218 context_attach_depth_stencil_fbo(context
, GL_FRAMEBUFFER
, surface
, FALSE
);
4220 /* Do the actual blit */
4221 surface_depth_blt(surface
, gl_info
, device
->depth_blt_texture
,
4222 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, bind_target
);
4223 checkGLcall("depth_blt");
4225 if (context
->current_fbo
) context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->current_fbo
->id
);
4226 else context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4230 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
4232 else if (location
== SFLAG_DS_ONSCREEN
)
4234 TRACE("Copying depth texture to onscreen depth buffer.\n");
4238 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4239 surface_depth_blt(surface
, gl_info
, surface
->texture_name
,
4240 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, surface
->texture_target
);
4241 checkGLcall("depth_blt");
4243 if (context
->current_fbo
) context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->current_fbo
->id
);
4247 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
4251 ERR("Invalid location (%#x) specified.\n", location
);
4254 surface
->Flags
|= location
;
4255 surface
->ds_current_size
.cx
= surface
->currentDesc
.Width
;
4256 surface
->ds_current_size
.cy
= surface
->currentDesc
.Height
;
4259 void surface_modify_location(IWineD3DSurfaceImpl
*surface
, DWORD flag
, BOOL persistent
)
4261 IWineD3DSurfaceImpl
*overlay
;
4263 TRACE("surface %p, location %s, persistent %#x.\n",
4264 surface
, debug_surflocation(flag
), persistent
);
4266 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4268 if (surface_is_offscreen(surface
))
4270 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4271 if (flag
& (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
)) flag
|= (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
);
4275 TRACE("Surface %p is an onscreen surface.\n", surface
);
4281 if (((surface
->Flags
& SFLAG_INTEXTURE
) && !(flag
& SFLAG_INTEXTURE
))
4282 || ((surface
->Flags
& SFLAG_INSRGBTEX
) && !(flag
& SFLAG_INSRGBTEX
)))
4284 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
4286 TRACE("Passing to container.\n");
4287 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture
*)surface
->container
.u
.texture
, TRUE
);
4290 surface
->Flags
&= ~SFLAG_LOCATIONS
;
4291 surface
->Flags
|= flag
;
4293 /* Redraw emulated overlays, if any */
4294 if (flag
& SFLAG_INDRAWABLE
&& !list_empty(&surface
->overlays
))
4296 LIST_FOR_EACH_ENTRY(overlay
, &surface
->overlays
, IWineD3DSurfaceImpl
, overlay_entry
)
4298 IWineD3DSurface_DrawOverlay((IWineD3DSurface
*)overlay
);
4304 if ((surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
)) && (flag
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
)))
4306 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
4308 TRACE("Passing to container\n");
4309 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture
*)surface
->container
.u
.texture
, TRUE
);
4312 surface
->Flags
&= ~flag
;
4315 if (!(surface
->Flags
& SFLAG_LOCATIONS
))
4317 ERR("Surface %p does not have any up to date location.\n", surface
);
4321 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl
*This
, const RECT
*rect_in
)
4323 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4324 IWineD3DSwapChainImpl
*swapchain
;
4325 struct wined3d_context
*context
;
4326 RECT src_rect
, dst_rect
;
4328 surface_get_rect(This
, rect_in
, &src_rect
);
4330 context
= context_acquire(device
, This
);
4331 context_apply_blit_state(context
, device
);
4332 if (context
->render_offscreen
)
4334 dst_rect
.left
= src_rect
.left
;
4335 dst_rect
.right
= src_rect
.right
;
4336 dst_rect
.top
= src_rect
.bottom
;
4337 dst_rect
.bottom
= src_rect
.top
;
4341 dst_rect
= src_rect
;
4344 swapchain
= This
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
? This
->container
.u
.swapchain
: NULL
;
4345 if (swapchain
&& This
== swapchain
->front_buffer
)
4346 surface_translate_frontbuffer_coords(This
, context
->win_handle
, &dst_rect
);
4348 device
->blitter
->set_shader((IWineD3DDevice
*) device
, This
);
4351 draw_textured_quad(This
, &src_rect
, &dst_rect
, WINED3DTEXF_POINT
);
4354 device
->blitter
->unset_shader((IWineD3DDevice
*) device
);
4356 if (wined3d_settings
.strict_draw_ordering
|| (swapchain
4357 && (This
== swapchain
->front_buffer
|| swapchain
->num_contexts
> 1)))
4358 wglFlush(); /* Flush to ensure ordering across contexts. */
4360 context_release(context
);
4363 HRESULT
surface_load_location(IWineD3DSurfaceImpl
*surface
, DWORD flag
, const RECT
*rect
)
4365 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
4366 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4367 BOOL drawable_read_ok
= surface_is_offscreen(surface
);
4368 struct wined3d_format_desc desc
;
4369 CONVERT_TYPES convert
;
4370 int width
, pitch
, outpitch
;
4372 BOOL in_fbo
= FALSE
;
4374 TRACE("surface %p, location %s, rect %s.\n", surface
, debug_surflocation(flag
), wine_dbgstr_rect(rect
));
4376 if (surface
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
)
4378 if (flag
== SFLAG_INTEXTURE
)
4380 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4381 surface_load_ds_location(surface
, context
, SFLAG_DS_OFFSCREEN
);
4382 context_release(context
);
4387 FIXME("Unimplemented location %#x for depth/stencil buffers.\n", flag
);
4388 return WINED3DERR_INVALIDCALL
;
4392 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4394 if (surface_is_offscreen(surface
))
4396 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4397 * Prefer SFLAG_INTEXTURE. */
4398 if (flag
== SFLAG_INDRAWABLE
) flag
= SFLAG_INTEXTURE
;
4399 drawable_read_ok
= FALSE
;
4404 TRACE("Surface %p is an onscreen surface.\n", surface
);
4408 if (surface
->Flags
& flag
)
4410 TRACE("Location already up to date\n");
4414 if (!(surface
->Flags
& SFLAG_LOCATIONS
))
4416 ERR("Surface %p does not have any up to date location.\n", surface
);
4417 surface
->Flags
|= SFLAG_LOST
;
4418 return WINED3DERR_DEVICELOST
;
4421 if (flag
== SFLAG_INSYSMEM
)
4423 surface_prepare_system_memory(surface
);
4425 /* Download the surface to system memory */
4426 if (surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
))
4428 struct wined3d_context
*context
= NULL
;
4430 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
4432 surface_bind_and_dirtify(surface
, !(surface
->Flags
& SFLAG_INTEXTURE
));
4433 surface_download_data(surface
, gl_info
);
4435 if (context
) context_release(context
);
4439 /* Note: It might be faster to download into a texture first. */
4440 read_from_framebuffer(surface
, rect
, surface
->resource
.allocatedMemory
,
4441 IWineD3DSurface_GetPitch((IWineD3DSurface
*)surface
));
4444 else if (flag
== SFLAG_INDRAWABLE
)
4446 if (surface
->Flags
& SFLAG_INTEXTURE
)
4448 surface_blt_to_drawable(surface
, rect
);
4453 if ((surface
->Flags
& SFLAG_LOCATIONS
) == SFLAG_INSRGBTEX
)
4455 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4456 * values, otherwise we get incorrect values in the target. For now go the slow way
4457 * via a system memory copy
4459 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4462 d3dfmt_get_conv(surface
, FALSE
/* We need color keying */,
4463 FALSE
/* We won't use textures */, &desc
, &convert
);
4465 /* The width is in 'length' not in bytes */
4466 width
= surface
->currentDesc
.Width
;
4467 pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*)surface
);
4469 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4470 * but it isn't set (yet) in all cases it is getting called. */
4471 if ((convert
!= NO_CONVERSION
) && (surface
->Flags
& SFLAG_PBO
))
4473 struct wined3d_context
*context
= NULL
;
4475 TRACE("Removing the pbo attached to surface %p.\n", surface
);
4477 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
4478 surface_remove_pbo(surface
, gl_info
);
4479 if (context
) context_release(context
);
4482 if ((convert
!= NO_CONVERSION
) && surface
->resource
.allocatedMemory
)
4484 int height
= surface
->currentDesc
.Height
;
4485 byte_count
= desc
.conv_byte_count
;
4487 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4488 outpitch
= width
* byte_count
;
4489 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4491 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4493 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4494 return WINED3DERR_OUTOFVIDEOMEMORY
;
4496 d3dfmt_convert_surface(surface
->resource
.allocatedMemory
, mem
, pitch
,
4497 width
, height
, outpitch
, convert
, surface
);
4499 surface
->Flags
|= SFLAG_CONVERTED
;
4503 surface
->Flags
&= ~SFLAG_CONVERTED
;
4504 mem
= surface
->resource
.allocatedMemory
;
4505 byte_count
= desc
.byte_count
;
4508 flush_to_framebuffer_drawpixels(surface
, desc
.glFormat
, desc
.glType
, byte_count
, mem
);
4510 /* Don't delete PBO memory */
4511 if ((mem
!= surface
->resource
.allocatedMemory
) && !(surface
->Flags
& SFLAG_PBO
))
4512 HeapFree(GetProcessHeap(), 0, mem
);
4515 else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4517 const DWORD attach_flags
= WINED3DFMT_FLAG_FBO_ATTACHABLE
| WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB
;
4519 if (drawable_read_ok
&& (surface
->Flags
& SFLAG_INDRAWABLE
))
4521 read_from_framebuffer_texture(surface
, flag
== SFLAG_INSRGBTEX
);
4523 else if (surface
->Flags
& (SFLAG_INSRGBTEX
| SFLAG_INTEXTURE
)
4524 && (surface
->resource
.format_desc
->Flags
& attach_flags
) == attach_flags
4525 && fbo_blit_supported(gl_info
, BLIT_OP_BLIT
,
4526 NULL
, surface
->resource
.usage
, surface
->resource
.pool
, surface
->resource
.format_desc
,
4527 NULL
, surface
->resource
.usage
, surface
->resource
.pool
, surface
->resource
.format_desc
))
4529 DWORD src_location
= flag
== SFLAG_INSRGBTEX
? SFLAG_INTEXTURE
: SFLAG_INSRGBTEX
;
4530 RECT rect
= {0, 0, surface
->currentDesc
.Width
, surface
->currentDesc
.Height
};
4532 surface_blt_fbo(surface
->resource
.device
, WINED3DTEXF_POINT
,
4533 surface
, src_location
, &rect
, surface
, flag
, &rect
);
4537 /* Upload from system memory */
4538 BOOL srgb
= flag
== SFLAG_INSRGBTEX
;
4539 struct wined3d_context
*context
= NULL
;
4541 d3dfmt_get_conv(surface
, TRUE
/* We need color keying */,
4542 TRUE
/* We will use textures */, &desc
, &convert
);
4546 if ((surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSYSMEM
)) == SFLAG_INTEXTURE
)
4548 /* Performance warning... */
4549 FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface
);
4550 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4555 if ((surface
->Flags
& (SFLAG_INSRGBTEX
| SFLAG_INSYSMEM
)) == SFLAG_INSRGBTEX
)
4557 /* Performance warning... */
4558 FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface
);
4559 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4562 if (!(surface
->Flags
& SFLAG_INSYSMEM
))
4564 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4565 /* Lets hope we get it from somewhere... */
4566 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4569 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
4571 surface_prepare_texture(surface
, gl_info
, srgb
);
4572 surface_bind_and_dirtify(surface
, srgb
);
4574 if (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
)
4576 surface
->Flags
|= SFLAG_GLCKEY
;
4577 surface
->glCKey
= surface
->SrcBltCKey
;
4579 else surface
->Flags
&= ~SFLAG_GLCKEY
;
4581 /* The width is in 'length' not in bytes */
4582 width
= surface
->currentDesc
.Width
;
4583 pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*)surface
);
4585 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4586 * but it isn't set (yet) in all cases it is getting called. */
4587 if ((convert
!= NO_CONVERSION
|| desc
.convert
) && (surface
->Flags
& SFLAG_PBO
))
4589 TRACE("Removing the pbo attached to surface %p.\n", surface
);
4590 surface_remove_pbo(surface
, gl_info
);
4595 /* This code is entered for texture formats which need a fixup. */
4596 int height
= surface
->currentDesc
.Height
;
4598 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4599 outpitch
= width
* desc
.conv_byte_count
;
4600 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4602 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4604 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4605 if (context
) context_release(context
);
4606 return WINED3DERR_OUTOFVIDEOMEMORY
;
4608 desc
.convert(surface
->resource
.allocatedMemory
, mem
, pitch
, width
, height
);
4610 else if (convert
!= NO_CONVERSION
&& surface
->resource
.allocatedMemory
)
4612 /* This code is only entered for color keying fixups */
4613 int height
= surface
->currentDesc
.Height
;
4615 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4616 outpitch
= width
* desc
.conv_byte_count
;
4617 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4619 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4621 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4622 if (context
) context_release(context
);
4623 return WINED3DERR_OUTOFVIDEOMEMORY
;
4625 d3dfmt_convert_surface(surface
->resource
.allocatedMemory
, mem
, pitch
,
4626 width
, height
, outpitch
, convert
, surface
);
4630 mem
= surface
->resource
.allocatedMemory
;
4633 /* Make sure the correct pitch is used */
4635 glPixelStorei(GL_UNPACK_ROW_LENGTH
, width
);
4638 if (mem
|| (surface
->Flags
& SFLAG_PBO
))
4639 surface_upload_data(surface
, gl_info
, &desc
, srgb
, mem
);
4641 /* Restore the default pitch */
4643 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
4646 if (context
) context_release(context
);
4648 /* Don't delete PBO memory */
4649 if ((mem
!= surface
->resource
.allocatedMemory
) && !(surface
->Flags
& SFLAG_PBO
))
4650 HeapFree(GetProcessHeap(), 0, mem
);
4654 if (!rect
) surface
->Flags
|= flag
;
4656 if (in_fbo
&& (surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
)))
4658 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4659 surface
->Flags
|= (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
);
4665 static WINED3DSURFTYPE WINAPI
IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface
*iface
) {
4666 return SURFACE_OPENGL
;
4669 static HRESULT WINAPI
IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface
*iface
) {
4670 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4673 /* If there's no destination surface there is nothing to do */
4674 if(!This
->overlay_dest
) return WINED3D_OK
;
4676 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4677 * update the overlay. Prevent an endless recursion
4679 if(This
->overlay_dest
->Flags
& SFLAG_INOVERLAYDRAW
) {
4682 This
->overlay_dest
->Flags
|= SFLAG_INOVERLAYDRAW
;
4683 hr
= IWineD3DSurfaceImpl_Blt((IWineD3DSurface
*) This
->overlay_dest
, &This
->overlay_destrect
,
4684 iface
, &This
->overlay_srcrect
, WINEDDBLT_WAIT
,
4685 NULL
, WINED3DTEXF_LINEAR
);
4686 This
->overlay_dest
->Flags
&= ~SFLAG_INOVERLAYDRAW
;
4691 BOOL
surface_is_offscreen(IWineD3DSurfaceImpl
*surface
)
4693 IWineD3DSwapChainImpl
*swapchain
= surface
->container
.u
.swapchain
;
4695 /* Not on a swapchain - must be offscreen */
4696 if (surface
->container
.type
!= WINED3D_CONTAINER_SWAPCHAIN
) return TRUE
;
4698 /* The front buffer is always onscreen */
4699 if (surface
== swapchain
->front_buffer
) return FALSE
;
4701 /* If the swapchain is rendered to an FBO, the backbuffer is
4702 * offscreen, otherwise onscreen */
4703 return swapchain
->render_to_fbo
;
4706 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl
=
4709 IWineD3DBaseSurfaceImpl_QueryInterface
,
4710 IWineD3DBaseSurfaceImpl_AddRef
,
4711 IWineD3DSurfaceImpl_Release
,
4712 /* IWineD3DResource */
4713 IWineD3DBaseSurfaceImpl_GetParent
,
4714 IWineD3DBaseSurfaceImpl_SetPrivateData
,
4715 IWineD3DBaseSurfaceImpl_GetPrivateData
,
4716 IWineD3DBaseSurfaceImpl_FreePrivateData
,
4717 IWineD3DBaseSurfaceImpl_SetPriority
,
4718 IWineD3DBaseSurfaceImpl_GetPriority
,
4719 IWineD3DSurfaceImpl_PreLoad
,
4720 IWineD3DSurfaceImpl_UnLoad
,
4721 IWineD3DBaseSurfaceImpl_GetType
,
4722 /* IWineD3DSurface */
4723 IWineD3DBaseSurfaceImpl_GetDesc
,
4724 IWineD3DSurfaceImpl_LockRect
,
4725 IWineD3DSurfaceImpl_UnlockRect
,
4726 IWineD3DSurfaceImpl_GetDC
,
4727 IWineD3DSurfaceImpl_ReleaseDC
,
4728 IWineD3DSurfaceImpl_Flip
,
4729 IWineD3DSurfaceImpl_Blt
,
4730 IWineD3DBaseSurfaceImpl_GetBltStatus
,
4731 IWineD3DBaseSurfaceImpl_GetFlipStatus
,
4732 IWineD3DBaseSurfaceImpl_IsLost
,
4733 IWineD3DBaseSurfaceImpl_Restore
,
4734 IWineD3DSurfaceImpl_BltFast
,
4735 IWineD3DBaseSurfaceImpl_GetPalette
,
4736 IWineD3DBaseSurfaceImpl_SetPalette
,
4737 IWineD3DSurfaceImpl_RealizePalette
,
4738 IWineD3DBaseSurfaceImpl_SetColorKey
,
4739 IWineD3DBaseSurfaceImpl_GetPitch
,
4740 IWineD3DSurfaceImpl_SetMem
,
4741 IWineD3DBaseSurfaceImpl_SetOverlayPosition
,
4742 IWineD3DBaseSurfaceImpl_GetOverlayPosition
,
4743 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder
,
4744 IWineD3DBaseSurfaceImpl_UpdateOverlay
,
4745 IWineD3DBaseSurfaceImpl_SetClipper
,
4746 IWineD3DBaseSurfaceImpl_GetClipper
,
4748 IWineD3DSurfaceImpl_LoadTexture
,
4749 IWineD3DSurfaceImpl_BindTexture
,
4750 IWineD3DBaseSurfaceImpl_GetData
,
4751 IWineD3DSurfaceImpl_SetFormat
,
4752 IWineD3DSurfaceImpl_PrivateSetup
,
4753 IWineD3DSurfaceImpl_GetImplType
,
4754 IWineD3DSurfaceImpl_DrawOverlay
4757 static HRESULT
ffp_blit_alloc(IWineD3DDevice
*iface
) { return WINED3D_OK
; }
4758 /* Context activation is done by the caller. */
4759 static void ffp_blit_free(IWineD3DDevice
*iface
) { }
4761 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4762 /* Context activation is done by the caller. */
4763 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl
*surface
, const struct wined3d_gl_info
*gl_info
)
4766 BOOL colorkey_active
= (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
) ? TRUE
: FALSE
;
4768 d3dfmt_p8_init_palette(surface
, table
, colorkey_active
);
4770 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4772 GL_EXTCALL(glColorTableEXT(surface
->texture_target
, GL_RGBA
, 256, GL_RGBA
, GL_UNSIGNED_BYTE
, table
));
4776 /* Context activation is done by the caller. */
4777 static HRESULT
ffp_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
4779 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
4780 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4781 enum complex_fixup fixup
= get_complex_fixup(surface
->resource
.format_desc
->color_fixup
);
4783 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4784 * else the surface is converted in software at upload time in LoadLocation.
4786 if(fixup
== COMPLEX_FIXUP_P8
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
])
4787 ffp_blit_p8_upload_palette(surface
, gl_info
);
4790 glEnable(surface
->texture_target
);
4791 checkGLcall("glEnable(surface->texture_target)");
4796 /* Context activation is done by the caller. */
4797 static void ffp_blit_unset(IWineD3DDevice
*iface
)
4799 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
4800 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4803 glDisable(GL_TEXTURE_2D
);
4804 checkGLcall("glDisable(GL_TEXTURE_2D)");
4805 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
4807 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
4808 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4810 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
4812 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
4813 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4818 static BOOL
ffp_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4819 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4820 const struct wined3d_format_desc
*src_format_desc
,
4821 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4822 const struct wined3d_format_desc
*dst_format_desc
)
4824 enum complex_fixup src_fixup
;
4826 if (blit_op
== BLIT_OP_COLOR_FILL
)
4828 if (!(dst_usage
& WINED3DUSAGE_RENDERTARGET
))
4830 TRACE("Color fill not supported\n");
4837 src_fixup
= get_complex_fixup(src_format_desc
->color_fixup
);
4838 if (TRACE_ON(d3d_surface
) && TRACE_ON(d3d
))
4840 TRACE("Checking support for fixup:\n");
4841 dump_color_fixup_desc(src_format_desc
->color_fixup
);
4844 if (blit_op
!= BLIT_OP_BLIT
)
4846 TRACE("Unsupported blit_op=%d\n", blit_op
);
4850 if (!is_identity_fixup(dst_format_desc
->color_fixup
))
4852 TRACE("Destination fixups are not supported\n");
4856 if (src_fixup
== COMPLEX_FIXUP_P8
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
])
4858 TRACE("P8 fixup supported\n");
4862 /* We only support identity conversions. */
4863 if (is_identity_fixup(src_format_desc
->color_fixup
))
4869 TRACE("[FAILED]\n");
4873 static HRESULT
ffp_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
,
4874 const RECT
*dst_rect
, const WINED3DCOLORVALUE
*color
)
4876 const RECT draw_rect
= {0, 0, dst_surface
->currentDesc
.Width
, dst_surface
->currentDesc
.Height
};
4878 return device_clear_render_targets(device
, 1 /* rt_count */, &dst_surface
, 1 /* rect_count */,
4879 dst_rect
, &draw_rect
, WINED3DCLEAR_TARGET
, color
, 0.0f
/* depth */, 0 /* stencil */);
4882 const struct blit_shader ffp_blit
= {
4891 static HRESULT
cpu_blit_alloc(IWineD3DDevice
*iface
)
4896 /* Context activation is done by the caller. */
4897 static void cpu_blit_free(IWineD3DDevice
*iface
)
4901 /* Context activation is done by the caller. */
4902 static HRESULT
cpu_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
4907 /* Context activation is done by the caller. */
4908 static void cpu_blit_unset(IWineD3DDevice
*iface
)
4912 static BOOL
cpu_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4913 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4914 const struct wined3d_format_desc
*src_format_desc
,
4915 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4916 const struct wined3d_format_desc
*dst_format_desc
)
4918 if (blit_op
== BLIT_OP_COLOR_FILL
)
4926 static HRESULT
cpu_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
,
4927 const RECT
*dst_rect
, const WINED3DCOLORVALUE
*color
)
4931 memset(&BltFx
, 0, sizeof(BltFx
));
4932 BltFx
.dwSize
= sizeof(BltFx
);
4933 BltFx
.u5
.dwFillColor
= wined3d_format_convert_from_float(dst_surface
->resource
.format_desc
, color
);
4934 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface
*)dst_surface
, dst_rect
,
4935 NULL
, NULL
, WINEDDBLT_COLORFILL
, &BltFx
, WINED3DTEXF_POINT
);
4938 const struct blit_shader cpu_blit
= {
4947 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4948 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4949 const struct wined3d_format_desc
*src_format_desc
,
4950 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4951 const struct wined3d_format_desc
*dst_format_desc
)
4953 if ((wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) || !gl_info
->fbo_ops
.glBlitFramebuffer
)
4956 /* We only support blitting. Things like color keying / color fill should
4957 * be handled by other blitters.
4959 if (blit_op
!= BLIT_OP_BLIT
)
4962 /* Source and/or destination need to be on the GL side */
4963 if (src_pool
== WINED3DPOOL_SYSTEMMEM
|| dst_pool
== WINED3DPOOL_SYSTEMMEM
)
4966 if(!((src_format_desc
->Flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE
) || (src_usage
& WINED3DUSAGE_RENDERTARGET
))
4967 && ((dst_format_desc
->Flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE
) || (dst_usage
& WINED3DUSAGE_RENDERTARGET
)))
4970 if (!is_identity_fixup(src_format_desc
->color_fixup
) ||
4971 !is_identity_fixup(dst_format_desc
->color_fixup
))
4974 if (!(src_format_desc
->id
== dst_format_desc
->id
4975 || (is_identity_fixup(src_format_desc
->color_fixup
)
4976 && is_identity_fixup(dst_format_desc
->color_fixup
))))