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
);
99 enum tex_types tex_type
;
100 GLfloat coords
[4][3];
111 static inline void cube_coords_float(const RECT
*r
, UINT w
, UINT h
, struct float_rect
*f
)
113 f
->l
= ((r
->left
* 2.0f
) / w
) - 1.0f
;
114 f
->t
= ((r
->top
* 2.0f
) / h
) - 1.0f
;
115 f
->r
= ((r
->right
* 2.0f
) / w
) - 1.0f
;
116 f
->b
= ((r
->bottom
* 2.0f
) / h
) - 1.0f
;
119 static void surface_get_blt_info(GLenum target
, const RECT
*rect_in
, GLsizei w
, GLsizei h
, struct blt_info
*info
)
121 GLfloat (*coords
)[3] = info
->coords
;
138 FIXME("Unsupported texture target %#x\n", target
);
139 /* Fall back to GL_TEXTURE_2D */
141 info
->binding
= GL_TEXTURE_BINDING_2D
;
142 info
->bind_target
= GL_TEXTURE_2D
;
143 info
->tex_type
= tex_2d
;
144 coords
[0][0] = (float)rect
.left
/ w
;
145 coords
[0][1] = (float)rect
.top
/ h
;
148 coords
[1][0] = (float)rect
.right
/ w
;
149 coords
[1][1] = (float)rect
.top
/ h
;
152 coords
[2][0] = (float)rect
.left
/ w
;
153 coords
[2][1] = (float)rect
.bottom
/ h
;
156 coords
[3][0] = (float)rect
.right
/ w
;
157 coords
[3][1] = (float)rect
.bottom
/ h
;
161 case GL_TEXTURE_RECTANGLE_ARB
:
162 info
->binding
= GL_TEXTURE_BINDING_RECTANGLE_ARB
;
163 info
->bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
164 info
->tex_type
= tex_rect
;
165 coords
[0][0] = rect
.left
; coords
[0][1] = rect
.top
; coords
[0][2] = 0.0f
;
166 coords
[1][0] = rect
.right
; coords
[1][1] = rect
.top
; coords
[1][2] = 0.0f
;
167 coords
[2][0] = rect
.left
; coords
[2][1] = rect
.bottom
; coords
[2][2] = 0.0f
;
168 coords
[3][0] = rect
.right
; coords
[3][1] = rect
.bottom
; coords
[3][2] = 0.0f
;
171 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
172 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
173 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
174 info
->tex_type
= tex_cube
;
175 cube_coords_float(&rect
, w
, h
, &f
);
177 coords
[0][0] = 1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = -f
.l
;
178 coords
[1][0] = 1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = -f
.r
;
179 coords
[2][0] = 1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = -f
.l
;
180 coords
[3][0] = 1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = -f
.r
;
183 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
184 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
185 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
186 info
->tex_type
= tex_cube
;
187 cube_coords_float(&rect
, w
, h
, &f
);
189 coords
[0][0] = -1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = f
.l
;
190 coords
[1][0] = -1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = f
.r
;
191 coords
[2][0] = -1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = f
.l
;
192 coords
[3][0] = -1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = f
.r
;
195 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
196 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
197 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
198 info
->tex_type
= tex_cube
;
199 cube_coords_float(&rect
, w
, h
, &f
);
201 coords
[0][0] = f
.l
; coords
[0][1] = 1.0f
; coords
[0][2] = f
.t
;
202 coords
[1][0] = f
.r
; coords
[1][1] = 1.0f
; coords
[1][2] = f
.t
;
203 coords
[2][0] = f
.l
; coords
[2][1] = 1.0f
; coords
[2][2] = f
.b
;
204 coords
[3][0] = f
.r
; coords
[3][1] = 1.0f
; coords
[3][2] = f
.b
;
207 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
208 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
209 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
210 info
->tex_type
= tex_cube
;
211 cube_coords_float(&rect
, w
, h
, &f
);
213 coords
[0][0] = f
.l
; coords
[0][1] = -1.0f
; coords
[0][2] = -f
.t
;
214 coords
[1][0] = f
.r
; coords
[1][1] = -1.0f
; coords
[1][2] = -f
.t
;
215 coords
[2][0] = f
.l
; coords
[2][1] = -1.0f
; coords
[2][2] = -f
.b
;
216 coords
[3][0] = f
.r
; coords
[3][1] = -1.0f
; coords
[3][2] = -f
.b
;
219 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
220 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
221 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
222 info
->tex_type
= tex_cube
;
223 cube_coords_float(&rect
, w
, h
, &f
);
225 coords
[0][0] = f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = 1.0f
;
226 coords
[1][0] = f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = 1.0f
;
227 coords
[2][0] = f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = 1.0f
;
228 coords
[3][0] = f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = 1.0f
;
231 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
232 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
233 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
234 info
->tex_type
= tex_cube
;
235 cube_coords_float(&rect
, w
, h
, &f
);
237 coords
[0][0] = -f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = -1.0f
;
238 coords
[1][0] = -f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = -1.0f
;
239 coords
[2][0] = -f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = -1.0f
;
240 coords
[3][0] = -f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = -1.0f
;
245 static inline void surface_get_rect(IWineD3DSurfaceImpl
*This
, const RECT
*rect_in
, RECT
*rect_out
)
248 *rect_out
= *rect_in
;
253 rect_out
->right
= This
->currentDesc
.Width
;
254 rect_out
->bottom
= This
->currentDesc
.Height
;
258 /* GL locking and context activation is done by the caller */
259 void draw_textured_quad(IWineD3DSurfaceImpl
*src_surface
, const RECT
*src_rect
, const RECT
*dst_rect
, WINED3DTEXTUREFILTERTYPE Filter
)
261 IWineD3DBaseTextureImpl
*texture
;
262 struct blt_info info
;
264 surface_get_blt_info(src_surface
->texture_target
, src_rect
, src_surface
->pow2Width
, src_surface
->pow2Height
, &info
);
266 glEnable(info
.bind_target
);
267 checkGLcall("glEnable(bind_target)");
269 /* Bind the texture */
270 glBindTexture(info
.bind_target
, src_surface
->texture_name
);
271 checkGLcall("glBindTexture");
273 /* Filtering for StretchRect */
274 glTexParameteri(info
.bind_target
, GL_TEXTURE_MAG_FILTER
,
275 wined3d_gl_mag_filter(magLookup
, Filter
));
276 checkGLcall("glTexParameteri");
277 glTexParameteri(info
.bind_target
, GL_TEXTURE_MIN_FILTER
,
278 wined3d_gl_min_mip_filter(minMipLookup
, Filter
, WINED3DTEXF_NONE
));
279 checkGLcall("glTexParameteri");
280 glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
281 glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
282 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
283 checkGLcall("glTexEnvi");
286 glBegin(GL_TRIANGLE_STRIP
);
287 glTexCoord3fv(info
.coords
[0]);
288 glVertex2i(dst_rect
->left
, dst_rect
->top
);
290 glTexCoord3fv(info
.coords
[1]);
291 glVertex2i(dst_rect
->right
, dst_rect
->top
);
293 glTexCoord3fv(info
.coords
[2]);
294 glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
296 glTexCoord3fv(info
.coords
[3]);
297 glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
300 /* Unbind the texture */
301 glBindTexture(info
.bind_target
, 0);
302 checkGLcall("glBindTexture(info->bind_target, 0)");
304 /* We changed the filtering settings on the texture. Inform the
305 * container about this to get the filters reset properly next draw. */
306 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)src_surface
, &IID_IWineD3DBaseTexture
, (void **)&texture
)))
308 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
309 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
310 texture
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
311 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture
);
315 HRESULT
surface_init(IWineD3DSurfaceImpl
*surface
, WINED3DSURFTYPE surface_type
, UINT alignment
,
316 UINT width
, UINT height
, UINT level
, BOOL lockable
, BOOL discard
, WINED3DMULTISAMPLE_TYPE multisample_type
,
317 UINT multisample_quality
, IWineD3DDeviceImpl
*device
, DWORD usage
, WINED3DFORMAT format
,
318 WINED3DPOOL pool
, IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
320 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
321 const struct wined3d_format_desc
*format_desc
= getFormatDescEntry(format
, gl_info
);
322 void (*cleanup
)(IWineD3DSurfaceImpl
*This
);
323 unsigned int resource_size
;
326 if (multisample_quality
> 0)
328 FIXME("multisample_quality set to %u, substituting 0\n", multisample_quality
);
329 multisample_quality
= 0;
332 /* FIXME: Check that the format is supported by the device. */
334 resource_size
= wined3d_format_calculate_size(format_desc
, alignment
, width
, height
);
336 /* Look at the implementation and set the correct Vtable. */
337 switch (surface_type
)
340 surface
->lpVtbl
= &IWineD3DSurface_Vtbl
;
341 cleanup
= surface_cleanup
;
345 surface
->lpVtbl
= &IWineGDISurface_Vtbl
;
346 cleanup
= surface_gdi_cleanup
;
350 ERR("Requested unknown surface implementation %#x.\n", surface_type
);
351 return WINED3DERR_INVALIDCALL
;
354 hr
= resource_init((IWineD3DResource
*)surface
, WINED3DRTYPE_SURFACE
,
355 device
, resource_size
, usage
, format_desc
, pool
, parent
, parent_ops
);
358 WARN("Failed to initialize resource, returning %#x.\n", hr
);
362 /* "Standalone" surface. */
363 IWineD3DSurface_SetContainer((IWineD3DSurface
*)surface
, NULL
);
365 surface
->currentDesc
.Width
= width
;
366 surface
->currentDesc
.Height
= height
;
367 surface
->currentDesc
.MultiSampleType
= multisample_type
;
368 surface
->currentDesc
.MultiSampleQuality
= multisample_quality
;
369 surface
->texture_level
= level
;
370 list_init(&surface
->overlays
);
373 surface
->Flags
= SFLAG_NORMCOORD
; /* Default to normalized coords. */
374 if (discard
) surface
->Flags
|= SFLAG_DISCARD
;
375 if (lockable
|| format
== WINED3DFMT_D16_LOCKABLE
) surface
->Flags
|= SFLAG_LOCKABLE
;
377 /* Quick lockable sanity check.
378 * TODO: remove this after surfaces, usage and lockability have been debugged properly
379 * this function is too deep to need to care about things like this.
380 * Levels need to be checked too, since they all affect what can be done. */
383 case WINED3DPOOL_SCRATCH
:
386 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
387 "which are mutually exclusive, setting lockable to TRUE.\n");
392 case WINED3DPOOL_SYSTEMMEM
:
394 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
397 case WINED3DPOOL_MANAGED
:
398 if (usage
& WINED3DUSAGE_DYNAMIC
)
399 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
402 case WINED3DPOOL_DEFAULT
:
403 if (lockable
&& !(usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
)))
404 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
408 FIXME("Unknown pool %#x.\n", pool
);
412 if (usage
& WINED3DUSAGE_RENDERTARGET
&& pool
!= WINED3DPOOL_DEFAULT
)
414 FIXME("Trying to create a render target that isn't in the default pool.\n");
417 /* Mark the texture as dirty so that it gets loaded first time around. */
418 surface_add_dirty_rect(surface
, NULL
);
419 list_init(&surface
->renderbuffers
);
421 TRACE("surface %p, memory %p, size %u\n", surface
, surface
->resource
.allocatedMemory
, surface
->resource
.size
);
423 /* Call the private setup routine */
424 hr
= IWineD3DSurface_PrivateSetup((IWineD3DSurface
*)surface
);
427 ERR("Private setup failed, returning %#x\n", hr
);
435 static void surface_force_reload(IWineD3DSurfaceImpl
*surface
)
437 surface
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
440 void surface_set_texture_name(IWineD3DSurfaceImpl
*surface
, GLuint new_name
, BOOL srgb
)
445 TRACE("surface %p, new_name %u, srgb %#x.\n", surface
, new_name
, srgb
);
449 name
= &surface
->texture_name_srgb
;
450 flag
= SFLAG_INSRGBTEX
;
454 name
= &surface
->texture_name
;
455 flag
= SFLAG_INTEXTURE
;
458 if (!*name
&& new_name
)
460 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
461 * surface has no texture name yet. See if we can get rid of this. */
462 if (surface
->Flags
& flag
)
463 ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag
));
464 surface_modify_location(surface
, flag
, FALSE
);
468 surface_force_reload(surface
);
471 void surface_set_texture_target(IWineD3DSurfaceImpl
*surface
, GLenum target
)
473 TRACE("surface %p, target %#x.\n", surface
, target
);
475 if (surface
->texture_target
!= target
)
477 if (target
== GL_TEXTURE_RECTANGLE_ARB
)
479 surface
->Flags
&= ~SFLAG_NORMCOORD
;
481 else if (surface
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
483 surface
->Flags
|= SFLAG_NORMCOORD
;
486 surface
->texture_target
= target
;
487 surface_force_reload(surface
);
490 /* Context activation is done by the caller. */
491 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl
*This
, BOOL srgb
) {
492 DWORD active_sampler
;
494 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
495 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
496 * gl states. The current texture unit should always be a valid one.
498 * To be more specific, this is tricky because we can implicitly be called
499 * from sampler() in state.c. This means we can't touch anything other than
500 * whatever happens to be the currently active texture, or we would risk
501 * marking already applied sampler states dirty again.
503 * TODO: Track the current active texture per GL context instead of using glGet
505 GLint active_texture
;
507 glGetIntegerv(GL_ACTIVE_TEXTURE
, &active_texture
);
509 active_sampler
= This
->resource
.device
->rev_tex_unit_map
[active_texture
- GL_TEXTURE0_ARB
];
511 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
513 IWineD3DDeviceImpl_MarkStateDirty(This
->resource
.device
, STATE_SAMPLER(active_sampler
));
515 IWineD3DSurface_BindTexture((IWineD3DSurface
*)This
, srgb
);
518 /* This function checks if the primary render target uses the 8bit paletted format. */
519 static BOOL
primary_render_target_is_p8(IWineD3DDeviceImpl
*device
)
521 if (device
->render_targets
&& device
->render_targets
[0])
523 IWineD3DSurfaceImpl
*render_target
= device
->render_targets
[0];
524 if ((render_target
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
525 && (render_target
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
))
531 /* This call just downloads data, the caller is responsible for binding the
532 * correct texture. */
533 /* Context activation is done by the caller. */
534 static void surface_download_data(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
)
536 const struct wined3d_format_desc
*format_desc
= This
->resource
.format_desc
;
538 /* Only support read back of converted P8 surfaces */
539 if (This
->Flags
& SFLAG_CONVERTED
&& format_desc
->format
!= WINED3DFMT_P8_UINT
)
541 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(format_desc
->format
));
547 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
549 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
550 This
, This
->texture_level
, format_desc
->glFormat
, format_desc
->glType
,
551 This
->resource
.allocatedMemory
);
553 if (This
->Flags
& SFLAG_PBO
)
555 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
556 checkGLcall("glBindBufferARB");
557 GL_EXTCALL(glGetCompressedTexImageARB(This
->texture_target
, This
->texture_level
, NULL
));
558 checkGLcall("glGetCompressedTexImageARB");
559 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
560 checkGLcall("glBindBufferARB");
564 GL_EXTCALL(glGetCompressedTexImageARB(This
->texture_target
,
565 This
->texture_level
, This
->resource
.allocatedMemory
));
566 checkGLcall("glGetCompressedTexImageARB");
572 GLenum format
= format_desc
->glFormat
;
573 GLenum type
= format_desc
->glType
;
577 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
578 if (format_desc
->format
== WINED3DFMT_P8_UINT
&& primary_render_target_is_p8(This
->resource
.device
))
581 type
= GL_UNSIGNED_BYTE
;
584 if (This
->Flags
& SFLAG_NONPOW2
) {
585 unsigned char alignment
= This
->resource
.device
->surface_alignment
;
586 src_pitch
= format_desc
->byte_count
* This
->pow2Width
;
587 dst_pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*) This
);
588 src_pitch
= (src_pitch
+ alignment
- 1) & ~(alignment
- 1);
589 mem
= HeapAlloc(GetProcessHeap(), 0, src_pitch
* This
->pow2Height
);
591 mem
= This
->resource
.allocatedMemory
;
594 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
595 This
, This
->texture_level
, format
, type
, mem
);
597 if(This
->Flags
& SFLAG_PBO
) {
598 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
599 checkGLcall("glBindBufferARB");
601 glGetTexImage(This
->texture_target
, This
->texture_level
, format
, type
, NULL
);
602 checkGLcall("glGetTexImage");
604 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
605 checkGLcall("glBindBufferARB");
607 glGetTexImage(This
->texture_target
, This
->texture_level
, format
, type
, mem
);
608 checkGLcall("glGetTexImage");
612 if (This
->Flags
& SFLAG_NONPOW2
) {
613 const BYTE
*src_data
;
617 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
618 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
619 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
621 * We're doing this...
623 * instead of boxing the texture :
624 * |<-texture width ->| -->pow2width| /\
625 * |111111111111111111| | |
626 * |222 Texture 222222| boxed empty | texture height
627 * |3333 Data 33333333| | |
628 * |444444444444444444| | \/
629 * ----------------------------------- |
630 * | boxed empty | boxed empty | pow2height
632 * -----------------------------------
635 * we're repacking the data to the expected texture width
637 * |<-texture width ->| -->pow2width| /\
638 * |111111111111111111222222222222222| |
639 * |222333333333333333333444444444444| texture height
643 * | empty | pow2height
645 * -----------------------------------
649 * |<-texture width ->| /\
650 * |111111111111111111|
651 * |222222222222222222|texture height
652 * |333333333333333333|
653 * |444444444444444444| \/
654 * --------------------
656 * this also means that any references to allocatedMemory should work with the data as if were a
657 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
659 * internally the texture is still stored in a boxed format so any references to textureName will
660 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
662 * Performance should not be an issue, because applications normally do not lock the surfaces when
663 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
664 * and doesn't have to be re-read.
667 dst_data
= This
->resource
.allocatedMemory
;
668 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This
, src_pitch
, dst_pitch
);
669 for (y
= 1 ; y
< This
->currentDesc
.Height
; y
++) {
670 /* skip the first row */
671 src_data
+= src_pitch
;
672 dst_data
+= dst_pitch
;
673 memcpy(dst_data
, src_data
, dst_pitch
);
676 HeapFree(GetProcessHeap(), 0, mem
);
680 /* Surface has now been downloaded */
681 This
->Flags
|= SFLAG_INSYSMEM
;
684 /* This call just uploads data, the caller is responsible for binding the
685 * correct texture. */
686 /* Context activation is done by the caller. */
687 static void surface_upload_data(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
688 const struct wined3d_format_desc
*format_desc
, BOOL srgb
, const GLvoid
*data
)
690 GLsizei width
= This
->currentDesc
.Width
;
691 GLsizei height
= This
->currentDesc
.Height
;
696 internal
= format_desc
->glGammaInternal
;
698 else if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
&& surface_is_offscreen(This
))
700 internal
= format_desc
->rtInternal
;
704 internal
= format_desc
->glInternal
;
707 TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
708 This
, internal
, width
, height
, format_desc
->glFormat
, format_desc
->glType
, data
);
709 TRACE("target %#x, level %u, resource size %u.\n",
710 This
->texture_target
, This
->texture_level
, This
->resource
.size
);
712 if (format_desc
->heightscale
!= 1.0f
&& format_desc
->heightscale
!= 0.0f
) height
*= format_desc
->heightscale
;
716 if (This
->Flags
& SFLAG_PBO
)
718 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
719 checkGLcall("glBindBufferARB");
721 TRACE("(%p) pbo: %#x, data: %p.\n", This
, This
->pbo
, data
);
725 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
727 TRACE("Calling glCompressedTexSubImage2DARB.\n");
729 GL_EXTCALL(glCompressedTexSubImage2DARB(This
->texture_target
, This
->texture_level
,
730 0, 0, width
, height
, internal
, This
->resource
.size
, data
));
731 checkGLcall("glCompressedTexSubImage2DARB");
735 TRACE("Calling glTexSubImage2D.\n");
737 glTexSubImage2D(This
->texture_target
, This
->texture_level
,
738 0, 0, width
, height
, format_desc
->glFormat
, format_desc
->glType
, data
);
739 checkGLcall("glTexSubImage2D");
742 if (This
->Flags
& SFLAG_PBO
)
744 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
745 checkGLcall("glBindBufferARB");
750 if (gl_info
->quirks
& WINED3D_QUIRK_FBO_TEX_UPDATE
)
752 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
755 for (i
= 0; i
< device
->numContexts
; ++i
)
757 context_surface_update(device
->contexts
[i
], This
);
762 /* This call just allocates the texture, the caller is responsible for binding
763 * the correct texture. */
764 /* Context activation is done by the caller. */
765 static void surface_allocate_surface(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
766 const struct wined3d_format_desc
*format_desc
, BOOL srgb
)
768 BOOL enable_client_storage
= FALSE
;
769 GLsizei width
= This
->pow2Width
;
770 GLsizei height
= This
->pow2Height
;
771 const BYTE
*mem
= NULL
;
776 internal
= format_desc
->glGammaInternal
;
778 else if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
&& surface_is_offscreen(This
))
780 internal
= format_desc
->rtInternal
;
784 internal
= format_desc
->glInternal
;
787 if (format_desc
->heightscale
!= 1.0f
&& format_desc
->heightscale
!= 0.0f
) height
*= format_desc
->heightscale
;
789 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",
790 This
, This
->texture_target
, This
->texture_level
, debug_d3dformat(format_desc
->format
),
791 internal
, width
, height
, format_desc
->glFormat
, format_desc
->glType
);
795 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
797 if(This
->Flags
& (SFLAG_NONPOW2
| SFLAG_DIBSECTION
| SFLAG_CONVERTED
) || This
->resource
.allocatedMemory
== NULL
) {
798 /* In some cases we want to disable client storage.
799 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
800 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
801 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
802 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
804 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
805 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
806 This
->Flags
&= ~SFLAG_CLIENT
;
807 enable_client_storage
= TRUE
;
809 This
->Flags
|= SFLAG_CLIENT
;
811 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
812 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
814 mem
= (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
818 if (format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
&& mem
)
820 GL_EXTCALL(glCompressedTexImage2DARB(This
->texture_target
, This
->texture_level
,
821 internal
, width
, height
, 0, This
->resource
.size
, mem
));
822 checkGLcall("glCompressedTexImage2DARB");
826 glTexImage2D(This
->texture_target
, This
->texture_level
,
827 internal
, width
, height
, 0, format_desc
->glFormat
, format_desc
->glType
, mem
);
828 checkGLcall("glTexImage2D");
831 if(enable_client_storage
) {
832 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
833 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
838 /* In D3D the depth stencil dimensions have to be greater than or equal to the
839 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
840 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
841 /* GL locking is done by the caller */
842 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl
*surface
, unsigned int width
, unsigned int height
)
844 const struct wined3d_gl_info
*gl_info
= &surface
->resource
.device
->adapter
->gl_info
;
845 renderbuffer_entry_t
*entry
;
846 GLuint renderbuffer
= 0;
847 unsigned int src_width
, src_height
;
849 src_width
= surface
->pow2Width
;
850 src_height
= surface
->pow2Height
;
852 /* A depth stencil smaller than the render target is not valid */
853 if (width
> src_width
|| height
> src_height
) return;
855 /* Remove any renderbuffer set if the sizes match */
856 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
]
857 || (width
== src_width
&& height
== src_height
))
859 surface
->current_renderbuffer
= NULL
;
863 /* Look if we've already got a renderbuffer of the correct dimensions */
864 LIST_FOR_EACH_ENTRY(entry
, &surface
->renderbuffers
, renderbuffer_entry_t
, entry
)
866 if (entry
->width
== width
&& entry
->height
== height
)
868 renderbuffer
= entry
->id
;
869 surface
->current_renderbuffer
= entry
;
876 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
877 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
878 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
,
879 surface
->resource
.format_desc
->glInternal
, width
, height
);
881 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t
));
882 entry
->width
= width
;
883 entry
->height
= height
;
884 entry
->id
= renderbuffer
;
885 list_add_head(&surface
->renderbuffers
, &entry
->entry
);
887 surface
->current_renderbuffer
= entry
;
890 checkGLcall("set_compatible_renderbuffer");
893 GLenum
surface_get_gl_buffer(IWineD3DSurfaceImpl
*surface
)
895 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)surface
->container
;
897 TRACE("surface %p.\n", surface
);
899 if (!(surface
->Flags
& SFLAG_SWAPCHAIN
))
901 ERR("Surface %p is not on a swapchain.\n", surface
);
905 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0] == surface
)
907 if (swapchain
->render_to_fbo
)
909 TRACE("Returning GL_COLOR_ATTACHMENT0\n");
910 return GL_COLOR_ATTACHMENT0
;
912 TRACE("Returning GL_BACK\n");
915 else if (surface
== swapchain
->front_buffer
)
917 TRACE("Returning GL_FRONT\n");
921 FIXME("Higher back buffer, returning GL_BACK\n");
925 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
926 void surface_add_dirty_rect(IWineD3DSurfaceImpl
*surface
, const RECT
*dirty_rect
)
928 IWineD3DBaseTexture
*baseTexture
= NULL
;
930 TRACE("surface %p, dirty_rect %s.\n", surface
, wine_dbgstr_rect(dirty_rect
));
932 if (!(surface
->Flags
& SFLAG_INSYSMEM
) && (surface
->Flags
& SFLAG_INTEXTURE
))
933 /* No partial locking for textures yet. */
934 surface_load_location(surface
, SFLAG_INSYSMEM
, NULL
);
936 surface_modify_location(surface
, SFLAG_INSYSMEM
, TRUE
);
939 surface
->dirtyRect
.left
= min(surface
->dirtyRect
.left
, dirty_rect
->left
);
940 surface
->dirtyRect
.top
= min(surface
->dirtyRect
.top
, dirty_rect
->top
);
941 surface
->dirtyRect
.right
= max(surface
->dirtyRect
.right
, dirty_rect
->right
);
942 surface
->dirtyRect
.bottom
= max(surface
->dirtyRect
.bottom
, dirty_rect
->bottom
);
946 surface
->dirtyRect
.left
= 0;
947 surface
->dirtyRect
.top
= 0;
948 surface
->dirtyRect
.right
= surface
->currentDesc
.Width
;
949 surface
->dirtyRect
.bottom
= surface
->currentDesc
.Height
;
952 /* if the container is a basetexture then mark it dirty. */
953 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
954 &IID_IWineD3DBaseTexture
, (void **)&baseTexture
)))
956 TRACE("Passing to container\n");
957 IWineD3DBaseTexture_SetDirty(baseTexture
, TRUE
);
958 IWineD3DBaseTexture_Release(baseTexture
);
962 static BOOL
surface_convert_color_to_argb(IWineD3DSurfaceImpl
*This
, DWORD color
, DWORD
*argb_color
)
964 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
966 switch(This
->resource
.format_desc
->format
)
968 case WINED3DFMT_P8_UINT
:
972 if (primary_render_target_is_p8(device
))
978 *argb_color
= (alpha
|
979 (This
->palette
->palents
[color
].peRed
<< 16) |
980 (This
->palette
->palents
[color
].peGreen
<< 8) |
981 (This
->palette
->palents
[color
].peBlue
));
988 case WINED3DFMT_B5G6R5_UNORM
:
990 if (color
== 0xFFFF) {
991 *argb_color
= 0xFFFFFFFF;
993 *argb_color
= ((0xFF000000) |
994 ((color
& 0xF800) << 8) |
995 ((color
& 0x07E0) << 5) |
996 ((color
& 0x001F) << 3));
1001 case WINED3DFMT_B8G8R8_UNORM
:
1002 case WINED3DFMT_B8G8R8X8_UNORM
:
1003 *argb_color
= 0xFF000000 | color
;
1006 case WINED3DFMT_B8G8R8A8_UNORM
:
1007 *argb_color
= color
;
1011 ERR("Unhandled conversion from %s to ARGB!\n", debug_d3dformat(This
->resource
.format_desc
->format
));
1017 static ULONG WINAPI
IWineD3DSurfaceImpl_Release(IWineD3DSurface
*iface
)
1019 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1020 ULONG ref
= InterlockedDecrement(&This
->resource
.ref
);
1021 TRACE("(%p) : Releasing from %d\n", This
, ref
+ 1);
1025 surface_cleanup(This
);
1026 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
1028 TRACE("(%p) Released.\n", This
);
1029 HeapFree(GetProcessHeap(), 0, This
);
1035 /* ****************************************************
1036 IWineD3DSurface IWineD3DResource parts follow
1037 **************************************************** */
1039 void surface_internal_preload(IWineD3DSurfaceImpl
*surface
, enum WINED3DSRGB srgb
)
1041 /* TODO: check for locks */
1042 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
1043 IWineD3DBaseTexture
*baseTexture
= NULL
;
1045 TRACE("(%p)Checking to see if the container is a base texture\n", surface
);
1046 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
1047 &IID_IWineD3DBaseTexture
, (void **)&baseTexture
)))
1049 IWineD3DBaseTextureImpl
*tex_impl
= (IWineD3DBaseTextureImpl
*)baseTexture
;
1050 TRACE("Passing to container\n");
1051 tex_impl
->baseTexture
.internal_preload(baseTexture
, srgb
);
1052 IWineD3DBaseTexture_Release(baseTexture
);
1054 struct wined3d_context
*context
= NULL
;
1056 TRACE("(%p) : About to load surface\n", surface
);
1058 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
1060 if (surface
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
1061 || surface
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT_A8_UNORM
)
1063 if (palette9_changed(surface
))
1065 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1066 /* TODO: This is not necessarily needed with hw palettized texture support */
1067 surface_load_location(surface
, SFLAG_INSYSMEM
, NULL
);
1068 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1069 surface_modify_location(surface
, SFLAG_INTEXTURE
, FALSE
);
1073 IWineD3DSurface_LoadTexture((IWineD3DSurface
*)surface
, srgb
== SRGB_SRGB
? TRUE
: FALSE
);
1075 if (surface
->resource
.pool
== WINED3DPOOL_DEFAULT
)
1077 /* Tell opengl to try and keep this texture in video ram (well mostly) */
1081 glPrioritizeTextures(1, &surface
->texture_name
, &tmp
);
1085 if (context
) context_release(context
);
1089 static void WINAPI
IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface
*iface
)
1091 surface_internal_preload((IWineD3DSurfaceImpl
*)iface
, SRGB_ANY
);
1094 /* Context activation is done by the caller. */
1095 static void surface_remove_pbo(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
)
1097 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap() ,0 , This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1098 This
->resource
.allocatedMemory
=
1099 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1102 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1103 checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
1104 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0, This
->resource
.size
, This
->resource
.allocatedMemory
));
1105 checkGLcall("glGetBufferSubDataARB");
1106 GL_EXTCALL(glDeleteBuffersARB(1, &This
->pbo
));
1107 checkGLcall("glDeleteBuffersARB");
1111 This
->Flags
&= ~SFLAG_PBO
;
1114 BOOL
surface_init_sysmem(IWineD3DSurfaceImpl
*surface
)
1116 if (!surface
->resource
.allocatedMemory
)
1118 surface
->resource
.heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1119 surface
->resource
.size
+ RESOURCE_ALIGNMENT
);
1120 if (!surface
->resource
.heapMemory
)
1122 ERR("Out of memory\n");
1125 surface
->resource
.allocatedMemory
=
1126 (BYTE
*)(((ULONG_PTR
)surface
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1130 memset(surface
->resource
.allocatedMemory
, 0, surface
->resource
.size
);
1133 surface_modify_location(surface
, SFLAG_INSYSMEM
, TRUE
);
1138 static void WINAPI
IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface
*iface
) {
1139 IWineD3DBaseTexture
*texture
= NULL
;
1140 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
1141 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1142 const struct wined3d_gl_info
*gl_info
;
1143 renderbuffer_entry_t
*entry
, *entry2
;
1144 struct wined3d_context
*context
;
1146 TRACE("(%p)\n", iface
);
1148 if(This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
1149 /* Default pool resources are supposed to be destroyed before Reset is called.
1150 * Implicit resources stay however. So this means we have an implicit render target
1151 * or depth stencil. The content may be destroyed, but we still have to tear down
1152 * opengl resources, so we cannot leave early.
1154 * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
1155 * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
1156 * or the depth stencil into an FBO the texture or render buffer will be removed
1157 * and all flags get lost
1159 surface_init_sysmem(This
);
1163 /* Load the surface into system memory */
1164 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
1165 surface_modify_location(This
, SFLAG_INDRAWABLE
, FALSE
);
1167 surface_modify_location(This
, SFLAG_INTEXTURE
, FALSE
);
1168 surface_modify_location(This
, SFLAG_INSRGBTEX
, FALSE
);
1169 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
1171 context
= context_acquire(device
, NULL
);
1172 gl_info
= context
->gl_info
;
1174 /* Destroy PBOs, but load them into real sysmem before */
1175 if (This
->Flags
& SFLAG_PBO
)
1176 surface_remove_pbo(This
, gl_info
);
1178 /* Destroy fbo render buffers. This is needed for implicit render targets, for
1179 * all application-created targets the application has to release the surface
1180 * before calling _Reset
1182 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &This
->renderbuffers
, renderbuffer_entry_t
, entry
) {
1184 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1186 list_remove(&entry
->entry
);
1187 HeapFree(GetProcessHeap(), 0, entry
);
1189 list_init(&This
->renderbuffers
);
1190 This
->current_renderbuffer
= NULL
;
1192 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
1195 IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **) &texture
);
1198 glDeleteTextures(1, &This
->texture_name
);
1199 This
->texture_name
= 0;
1200 glDeleteTextures(1, &This
->texture_name_srgb
);
1201 This
->texture_name_srgb
= 0;
1204 IWineD3DBaseTexture_Release(texture
);
1207 context_release(context
);
1209 resource_unload((IWineD3DResourceImpl
*)This
);
1212 /* ******************************************************
1213 IWineD3DSurface IWineD3DSurface parts follow
1214 ****************************************************** */
1216 /* Read the framebuffer back into the surface */
1217 static void read_from_framebuffer(IWineD3DSurfaceImpl
*This
, const RECT
*rect
, void *dest
, UINT pitch
)
1219 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1220 const struct wined3d_gl_info
*gl_info
;
1221 struct wined3d_context
*context
;
1225 BYTE
*row
, *top
, *bottom
;
1229 BOOL srcIsUpsideDown
;
1234 if(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
) {
1235 static BOOL warned
= FALSE
;
1237 ERR("The application tries to lock the render target, but render target locking is disabled\n");
1243 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
1244 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
1245 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
1246 * context->last_was_blit set on the unlock.
1248 context
= context_acquire(device
, This
);
1249 context_apply_blit_state(context
, device
);
1250 gl_info
= context
->gl_info
;
1254 /* Select the correct read buffer, and give some debug output.
1255 * There is no need to keep track of the current read buffer or reset it, every part of the code
1256 * that reads sets the read buffer as desired.
1258 if (surface_is_offscreen(This
))
1260 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1261 * Read from the back buffer
1263 TRACE("Locking offscreen render target\n");
1264 glReadBuffer(device
->offscreenBuffer
);
1265 srcIsUpsideDown
= TRUE
;
1269 /* Onscreen surfaces are always part of a swapchain */
1270 GLenum buffer
= surface_get_gl_buffer(This
);
1271 TRACE("Locking %#x buffer\n", buffer
);
1272 glReadBuffer(buffer
);
1273 checkGLcall("glReadBuffer");
1274 srcIsUpsideDown
= FALSE
;
1277 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1279 local_rect
.left
= 0;
1281 local_rect
.right
= This
->currentDesc
.Width
;
1282 local_rect
.bottom
= This
->currentDesc
.Height
;
1286 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1288 switch(This
->resource
.format_desc
->format
)
1290 case WINED3DFMT_P8_UINT
:
1292 if (primary_render_target_is_p8(device
))
1294 /* In case of P8 render targets the index is stored in the alpha component */
1296 type
= GL_UNSIGNED_BYTE
;
1298 bpp
= This
->resource
.format_desc
->byte_count
;
1300 /* GL can't return palettized data, so read ARGB pixels into a
1301 * separate block of memory and convert them into palettized format
1302 * in software. Slow, but if the app means to use palettized render
1303 * targets and locks it...
1305 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1306 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1307 * for the color channels when palettizing the colors.
1310 type
= GL_UNSIGNED_BYTE
;
1312 mem
= HeapAlloc(GetProcessHeap(), 0, This
->resource
.size
* 3);
1314 ERR("Out of memory\n");
1318 bpp
= This
->resource
.format_desc
->byte_count
* 3;
1325 fmt
= This
->resource
.format_desc
->glFormat
;
1326 type
= This
->resource
.format_desc
->glType
;
1327 bpp
= This
->resource
.format_desc
->byte_count
;
1330 if(This
->Flags
& SFLAG_PBO
) {
1331 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, This
->pbo
));
1332 checkGLcall("glBindBufferARB");
1334 ERR("mem not null for pbo -- unexpected\n");
1339 /* Save old pixel store pack state */
1340 glGetIntegerv(GL_PACK_ROW_LENGTH
, &rowLen
);
1341 checkGLcall("glGetIntegerv");
1342 glGetIntegerv(GL_PACK_SKIP_PIXELS
, &skipPix
);
1343 checkGLcall("glGetIntegerv");
1344 glGetIntegerv(GL_PACK_SKIP_ROWS
, &skipRow
);
1345 checkGLcall("glGetIntegerv");
1347 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1348 glPixelStorei(GL_PACK_ROW_LENGTH
, This
->currentDesc
.Width
);
1349 checkGLcall("glPixelStorei");
1350 glPixelStorei(GL_PACK_SKIP_PIXELS
, local_rect
.left
);
1351 checkGLcall("glPixelStorei");
1352 glPixelStorei(GL_PACK_SKIP_ROWS
, local_rect
.top
);
1353 checkGLcall("glPixelStorei");
1355 glReadPixels(local_rect
.left
, (!srcIsUpsideDown
) ? (This
->currentDesc
.Height
- local_rect
.bottom
) : local_rect
.top
,
1356 local_rect
.right
- local_rect
.left
,
1357 local_rect
.bottom
- local_rect
.top
,
1359 checkGLcall("glReadPixels");
1361 /* Reset previous pixel store pack state */
1362 glPixelStorei(GL_PACK_ROW_LENGTH
, rowLen
);
1363 checkGLcall("glPixelStorei");
1364 glPixelStorei(GL_PACK_SKIP_PIXELS
, skipPix
);
1365 checkGLcall("glPixelStorei");
1366 glPixelStorei(GL_PACK_SKIP_ROWS
, skipRow
);
1367 checkGLcall("glPixelStorei");
1369 if(This
->Flags
& SFLAG_PBO
) {
1370 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
1371 checkGLcall("glBindBufferARB");
1373 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1374 * to get a pointer to it and perform the flipping in software. This is a lot
1375 * faster than calling glReadPixels for each line. In case we want more speed
1376 * we should rerender it flipped in a FBO and read the data back from the FBO. */
1377 if(!srcIsUpsideDown
) {
1378 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1379 checkGLcall("glBindBufferARB");
1381 mem
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, GL_READ_WRITE_ARB
));
1382 checkGLcall("glMapBufferARB");
1386 /* TODO: Merge this with the palettization loop below for P8 targets */
1387 if(!srcIsUpsideDown
) {
1389 /* glReadPixels returns the image upside down, and there is no way to prevent this.
1390 Flip the lines in software */
1391 len
= (local_rect
.right
- local_rect
.left
) * bpp
;
1392 off
= local_rect
.left
* bpp
;
1394 row
= HeapAlloc(GetProcessHeap(), 0, len
);
1396 ERR("Out of memory\n");
1397 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
) HeapFree(GetProcessHeap(), 0, mem
);
1402 top
= mem
+ pitch
* local_rect
.top
;
1403 bottom
= mem
+ pitch
* (local_rect
.bottom
- 1);
1404 for(i
= 0; i
< (local_rect
.bottom
- local_rect
.top
) / 2; i
++) {
1405 memcpy(row
, top
+ off
, len
);
1406 memcpy(top
+ off
, bottom
+ off
, len
);
1407 memcpy(bottom
+ off
, row
, len
);
1411 HeapFree(GetProcessHeap(), 0, row
);
1413 /* Unmap the temp PBO buffer */
1414 if(This
->Flags
& SFLAG_PBO
) {
1415 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
1416 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1421 context_release(context
);
1423 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1424 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1425 * the same color but we have no choice.
1426 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1428 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
&& !primary_render_target_is_p8(device
))
1430 const PALETTEENTRY
*pal
= NULL
;
1431 DWORD width
= pitch
/ 3;
1435 pal
= This
->palette
->palents
;
1437 ERR("Palette is missing, cannot perform inverse palette lookup\n");
1438 HeapFree(GetProcessHeap(), 0, mem
);
1442 for(y
= local_rect
.top
; y
< local_rect
.bottom
; y
++) {
1443 for(x
= local_rect
.left
; x
< local_rect
.right
; x
++) {
1444 /* start lines pixels */
1445 const BYTE
*blue
= mem
+ y
* pitch
+ x
* (sizeof(BYTE
) * 3);
1446 const BYTE
*green
= blue
+ 1;
1447 const BYTE
*red
= green
+ 1;
1449 for(c
= 0; c
< 256; c
++) {
1450 if(*red
== pal
[c
].peRed
&&
1451 *green
== pal
[c
].peGreen
&&
1452 *blue
== pal
[c
].peBlue
)
1454 *((BYTE
*) dest
+ y
* width
+ x
) = c
;
1460 HeapFree(GetProcessHeap(), 0, mem
);
1464 /* Read the framebuffer contents into a texture */
1465 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl
*This
, BOOL srgb
)
1467 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1468 const struct wined3d_gl_info
*gl_info
;
1469 struct wined3d_context
*context
;
1471 if (!surface_is_offscreen(This
))
1473 /* We would need to flip onscreen surfaces, but there's no efficient
1474 * way to do that here. It makes more sense for the caller to
1475 * explicitly go through sysmem. */
1476 ERR("Not supported for onscreen targets.\n");
1480 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1481 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1482 * states in the stateblock, and no driver was found yet that had bugs in that regard.
1484 context
= context_acquire(device
, This
);
1485 gl_info
= context
->gl_info
;
1487 surface_prepare_texture(This
, gl_info
, srgb
);
1488 surface_bind_and_dirtify(This
, srgb
);
1490 TRACE("Reading back offscreen render target %p.\n", This
);
1494 glReadBuffer(device
->offscreenBuffer
);
1495 checkGLcall("glReadBuffer");
1497 glCopyTexSubImage2D(This
->texture_target
, This
->texture_level
,
1498 0, 0, 0, 0, This
->currentDesc
.Width
, This
->currentDesc
.Height
);
1499 checkGLcall("glCopyTexSubImage2D");
1503 context_release(context
);
1506 /* Context activation is done by the caller. */
1507 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl
*surface
,
1508 const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
1510 DWORD alloc_flag
= srgb
? SFLAG_SRGBALLOCATED
: SFLAG_ALLOCATED
;
1511 CONVERT_TYPES convert
;
1512 struct wined3d_format_desc desc
;
1514 if (surface
->Flags
& alloc_flag
) return;
1516 d3dfmt_get_conv(surface
, TRUE
, TRUE
, &desc
, &convert
);
1517 if(convert
!= NO_CONVERSION
|| desc
.convert
) surface
->Flags
|= SFLAG_CONVERTED
;
1518 else surface
->Flags
&= ~SFLAG_CONVERTED
;
1520 surface_bind_and_dirtify(surface
, srgb
);
1521 surface_allocate_surface(surface
, gl_info
, &desc
, srgb
);
1522 surface
->Flags
|= alloc_flag
;
1525 /* Context activation is done by the caller. */
1526 void surface_prepare_texture(IWineD3DSurfaceImpl
*surface
, const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
1528 IWineD3DBaseTextureImpl
*texture
;
1530 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
1531 &IID_IWineD3DBaseTexture
, (void **)&texture
)))
1533 UINT sub_count
= texture
->baseTexture
.level_count
* texture
->baseTexture
.layer_count
;
1536 TRACE("surface %p is a subresource of texture %p.\n", surface
, texture
);
1538 for (i
= 0; i
< sub_count
; ++i
)
1540 IWineD3DSurfaceImpl
*s
= (IWineD3DSurfaceImpl
*)texture
->baseTexture
.sub_resources
[i
];
1541 surface_prepare_texture_internal(s
, gl_info
, srgb
);
1544 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture
);
1549 surface_prepare_texture_internal(surface
, gl_info
, srgb
);
1552 static void surface_prepare_system_memory(IWineD3DSurfaceImpl
*This
)
1554 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1555 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1557 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1558 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1561 if(!(This
->Flags
& SFLAG_DYNLOCK
)) {
1563 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1564 if(This
->lockCount
> MAXLOCKCOUNT
) {
1565 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1566 This
->Flags
|= SFLAG_DYNLOCK
;
1570 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1571 * Also don't create a PBO for systemmem surfaces.
1573 if (gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
] && (This
->Flags
& SFLAG_DYNLOCK
)
1574 && !(This
->Flags
& (SFLAG_PBO
| SFLAG_CONVERTED
| SFLAG_NONPOW2
))
1575 && (This
->resource
.pool
!= WINED3DPOOL_SYSTEMMEM
))
1578 struct wined3d_context
*context
;
1580 context
= context_acquire(device
, NULL
);
1583 GL_EXTCALL(glGenBuffersARB(1, &This
->pbo
));
1584 error
= glGetError();
1585 if(This
->pbo
== 0 || error
!= GL_NO_ERROR
) {
1586 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error
), error
);
1589 TRACE("Attaching pbo=%#x to (%p)\n", This
->pbo
, This
);
1591 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1592 checkGLcall("glBindBufferARB");
1594 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->resource
.size
+ 4, This
->resource
.allocatedMemory
, GL_STREAM_DRAW_ARB
));
1595 checkGLcall("glBufferDataARB");
1597 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1598 checkGLcall("glBindBufferARB");
1600 /* We don't need the system memory anymore and we can't even use it for PBOs */
1601 if(!(This
->Flags
& SFLAG_CLIENT
)) {
1602 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
1603 This
->resource
.heapMemory
= NULL
;
1605 This
->resource
.allocatedMemory
= NULL
;
1606 This
->Flags
|= SFLAG_PBO
;
1608 context_release(context
);
1610 else if (!(This
->resource
.allocatedMemory
|| This
->Flags
& SFLAG_PBO
))
1612 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1615 if(!This
->resource
.heapMemory
) {
1616 This
->resource
.heapMemory
= HeapAlloc(GetProcessHeap() ,0 , This
->resource
.size
+ RESOURCE_ALIGNMENT
);
1618 This
->resource
.allocatedMemory
=
1619 (BYTE
*)(((ULONG_PTR
) This
->resource
.heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
1620 if(This
->Flags
& SFLAG_INSYSMEM
) {
1621 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1626 static HRESULT WINAPI
IWineD3DSurfaceImpl_LockRect(IWineD3DSurface
*iface
, WINED3DLOCKED_RECT
* pLockedRect
, CONST RECT
* pRect
, DWORD Flags
) {
1627 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1628 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1629 const RECT
*pass_rect
= pRect
;
1631 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1632 iface
, pLockedRect
, wine_dbgstr_rect(pRect
), Flags
);
1634 /* This is also done in the base class, but we have to verify this before loading any data from
1635 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1636 * may interfere, and all other bad things may happen
1638 if (This
->Flags
& SFLAG_LOCKED
) {
1639 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1640 return WINED3DERR_INVALIDCALL
;
1642 This
->Flags
|= SFLAG_LOCKED
;
1644 if (!(This
->Flags
& SFLAG_LOCKABLE
))
1646 TRACE("Warning: trying to lock unlockable surf@%p\n", This
);
1649 if (Flags
& WINED3DLOCK_DISCARD
) {
1650 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1651 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1652 surface_prepare_system_memory(This
); /* Makes sure memory is allocated */
1653 This
->Flags
|= SFLAG_INSYSMEM
;
1657 if (This
->Flags
& SFLAG_INSYSMEM
) {
1658 TRACE("Local copy is up to date, not downloading data\n");
1659 surface_prepare_system_memory(This
); /* Makes sure memory is allocated */
1663 /* surface_load_location() does not check if the rectangle specifies
1664 * the full surface. Most callers don't need that, so do it here. */
1665 if (pRect
&& pRect
->top
== 0 && pRect
->left
== 0
1666 && pRect
->right
== This
->currentDesc
.Width
1667 && pRect
->bottom
== This
->currentDesc
.Height
)
1672 if (!(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
1673 && ((This
->Flags
& SFLAG_SWAPCHAIN
) || This
== device
->render_targets
[0])))
1675 surface_load_location(This
, SFLAG_INSYSMEM
, pass_rect
);
1679 if (This
->Flags
& SFLAG_PBO
)
1681 const struct wined3d_gl_info
*gl_info
;
1682 struct wined3d_context
*context
;
1684 context
= context_acquire(device
, NULL
);
1685 gl_info
= context
->gl_info
;
1688 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1689 checkGLcall("glBindBufferARB");
1691 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1692 if(This
->resource
.allocatedMemory
) {
1693 ERR("The surface already has PBO memory allocated!\n");
1696 This
->resource
.allocatedMemory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, GL_READ_WRITE_ARB
));
1697 checkGLcall("glMapBufferARB");
1699 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1700 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1701 checkGLcall("glBindBufferARB");
1704 context_release(context
);
1707 if (Flags
& (WINED3DLOCK_NO_DIRTY_UPDATE
| WINED3DLOCK_READONLY
)) {
1710 IWineD3DBaseTexture
*pBaseTexture
;
1713 * as seen in msdn docs
1715 surface_add_dirty_rect(This
, pRect
);
1717 /** Dirtify Container if needed */
1718 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&pBaseTexture
))) {
1719 TRACE("Making container dirty\n");
1720 IWineD3DBaseTexture_SetDirty(pBaseTexture
, TRUE
);
1721 IWineD3DBaseTexture_Release(pBaseTexture
);
1723 TRACE("Surface is standalone, no need to dirty the container\n");
1727 return IWineD3DBaseSurfaceImpl_LockRect(iface
, pLockedRect
, pRect
, Flags
);
1730 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl
*This
, GLenum fmt
, GLenum type
, UINT bpp
, const BYTE
*mem
) {
1732 GLint prev_rasterpos
[4];
1733 GLint skipBytes
= 0;
1734 UINT pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*) This
); /* target is argb, 4 byte */
1735 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1736 const struct wined3d_gl_info
*gl_info
;
1737 struct wined3d_context
*context
;
1739 /* Activate the correct context for the render target */
1740 context
= context_acquire(device
, This
);
1741 context_apply_blit_state(context
, device
);
1742 gl_info
= context
->gl_info
;
1746 if (!surface_is_offscreen(This
))
1748 GLenum buffer
= surface_get_gl_buffer(This
);
1749 TRACE("Unlocking %#x buffer.\n", buffer
);
1750 context_set_draw_buffer(context
, buffer
);
1754 /* Primary offscreen render target */
1755 TRACE("Offscreen render target.\n");
1756 context_set_draw_buffer(context
, device
->offscreenBuffer
);
1759 glGetIntegerv(GL_PACK_SWAP_BYTES
, &prev_store
);
1760 checkGLcall("glGetIntegerv");
1761 glGetIntegerv(GL_CURRENT_RASTER_POSITION
, &prev_rasterpos
[0]);
1762 checkGLcall("glGetIntegerv");
1763 glPixelZoom(1.0f
, -1.0f
);
1764 checkGLcall("glPixelZoom");
1766 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1767 glGetIntegerv(GL_UNPACK_ROW_LENGTH
, &skipBytes
);
1768 glPixelStorei(GL_UNPACK_ROW_LENGTH
, This
->currentDesc
.Width
);
1770 glRasterPos3i(This
->lockedRect
.left
, This
->lockedRect
.top
, 1);
1771 checkGLcall("glRasterPos3i");
1773 /* Some drivers(radeon dri, others?) don't like exceptions during
1774 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1775 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1776 * catch to put the dib section in InSync mode, which leads to a crash
1777 * and a blocked x server on my radeon card.
1779 * The following lines read the dib section so it is put in InSync mode
1780 * before glDrawPixels is called and the crash is prevented. There won't
1781 * be any interfering gdi accesses, because UnlockRect is called from
1782 * ReleaseDC, and the app won't use the dc any more afterwards.
1784 if((This
->Flags
& SFLAG_DIBSECTION
) && !(This
->Flags
& SFLAG_PBO
)) {
1786 read
= This
->resource
.allocatedMemory
[0];
1789 if(This
->Flags
& SFLAG_PBO
) {
1790 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1791 checkGLcall("glBindBufferARB");
1794 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1795 if(This
->Flags
& SFLAG_LOCKED
) {
1796 glDrawPixels(This
->lockedRect
.right
- This
->lockedRect
.left
,
1797 (This
->lockedRect
.bottom
- This
->lockedRect
.top
)-1,
1799 mem
+ bpp
* This
->lockedRect
.left
+ pitch
* This
->lockedRect
.top
);
1800 checkGLcall("glDrawPixels");
1802 glDrawPixels(This
->currentDesc
.Width
,
1803 This
->currentDesc
.Height
,
1805 checkGLcall("glDrawPixels");
1808 if(This
->Flags
& SFLAG_PBO
) {
1809 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1810 checkGLcall("glBindBufferARB");
1813 glPixelZoom(1.0f
, 1.0f
);
1814 checkGLcall("glPixelZoom");
1816 glRasterPos3iv(&prev_rasterpos
[0]);
1817 checkGLcall("glRasterPos3iv");
1819 /* Reset to previous pack row length */
1820 glPixelStorei(GL_UNPACK_ROW_LENGTH
, skipBytes
);
1821 checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH)");
1824 context_release(context
);
1827 static HRESULT WINAPI
IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface
*iface
) {
1828 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1829 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
1832 if (!(This
->Flags
& SFLAG_LOCKED
)) {
1833 WARN("trying to Unlock an unlocked surf@%p\n", This
);
1834 return WINEDDERR_NOTLOCKED
;
1837 if (This
->Flags
& SFLAG_PBO
)
1839 const struct wined3d_gl_info
*gl_info
;
1840 struct wined3d_context
*context
;
1842 TRACE("Freeing PBO memory\n");
1844 context
= context_acquire(device
, NULL
);
1845 gl_info
= context
->gl_info
;
1848 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, This
->pbo
));
1849 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
1850 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
1851 checkGLcall("glUnmapBufferARB");
1853 context_release(context
);
1855 This
->resource
.allocatedMemory
= NULL
;
1858 TRACE("(%p) : dirtyfied(%d)\n", This
, This
->Flags
& (SFLAG_INDRAWABLE
| SFLAG_INTEXTURE
) ? 0 : 1);
1860 if (This
->Flags
& (SFLAG_INDRAWABLE
| SFLAG_INTEXTURE
)) {
1861 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This
);
1865 if ((This
->Flags
& SFLAG_SWAPCHAIN
) || (device
->render_targets
&& This
== device
->render_targets
[0]))
1867 if(wined3d_settings
.rendertargetlock_mode
== RTL_DISABLE
) {
1868 static BOOL warned
= FALSE
;
1870 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1876 if(This
->dirtyRect
.left
== 0 &&
1877 This
->dirtyRect
.top
== 0 &&
1878 This
->dirtyRect
.right
== This
->currentDesc
.Width
&&
1879 This
->dirtyRect
.bottom
== This
->currentDesc
.Height
) {
1882 /* TODO: Proper partial rectangle tracking */
1883 fullsurface
= FALSE
;
1884 This
->Flags
|= SFLAG_INSYSMEM
;
1887 switch(wined3d_settings
.rendertargetlock_mode
) {
1889 surface_load_location(This
, SFLAG_INTEXTURE
, NULL
/* partial texture loading not supported yet */);
1893 surface_load_location(This
, SFLAG_INDRAWABLE
, fullsurface
? NULL
: &This
->dirtyRect
);
1898 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1899 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1900 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1901 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1902 * not fully up to date because only a subrectangle was read in LockRect.
1904 This
->Flags
&= ~SFLAG_INSYSMEM
;
1905 This
->Flags
|= SFLAG_INDRAWABLE
;
1908 This
->dirtyRect
.left
= This
->currentDesc
.Width
;
1909 This
->dirtyRect
.top
= This
->currentDesc
.Height
;
1910 This
->dirtyRect
.right
= 0;
1911 This
->dirtyRect
.bottom
= 0;
1913 else if (This
== device
->depth_stencil
)
1915 FIXME("Depth Stencil buffer locking is not implemented\n");
1917 /* The rest should be a normal texture */
1918 IWineD3DBaseTextureImpl
*impl
;
1919 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1920 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1921 * states need resetting
1923 if(IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&impl
) == WINED3D_OK
) {
1924 if (impl
->baseTexture
.bindCount
)
1925 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(impl
->baseTexture
.sampler
));
1926 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*) impl
);
1931 This
->Flags
&= ~SFLAG_LOCKED
;
1932 memset(&This
->lockedRect
, 0, sizeof(RECT
));
1934 /* Overlays have to be redrawn manually after changes with the GL implementation */
1935 if(This
->overlay_dest
) {
1936 IWineD3DSurface_DrawOverlay(iface
);
1941 static void surface_release_client_storage(IWineD3DSurfaceImpl
*surface
)
1943 struct wined3d_context
*context
;
1945 context
= context_acquire(surface
->resource
.device
, NULL
);
1948 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
1949 if (surface
->texture_name
)
1951 surface_bind_and_dirtify(surface
, FALSE
);
1952 glTexImage2D(surface
->texture_target
, surface
->texture_level
,
1953 GL_RGB
, 1, 1, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
1955 if (surface
->texture_name_srgb
)
1957 surface_bind_and_dirtify(surface
, TRUE
);
1958 glTexImage2D(surface
->texture_target
, surface
->texture_level
,
1959 GL_RGB
, 1, 1, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
1961 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1964 context_release(context
);
1966 surface_modify_location(surface
, SFLAG_INSRGBTEX
, FALSE
);
1967 surface_modify_location(surface
, SFLAG_INTEXTURE
, FALSE
);
1968 surface_force_reload(surface
);
1971 static HRESULT WINAPI
IWineD3DSurfaceImpl_GetDC(IWineD3DSurface
*iface
, HDC
*pHDC
)
1973 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1974 WINED3DLOCKED_RECT lock
;
1978 TRACE("(%p)->(%p)\n",This
,pHDC
);
1980 if(This
->Flags
& SFLAG_USERPTR
) {
1981 ERR("Not supported on surfaces with an application-provided surfaces\n");
1982 return WINEDDERR_NODC
;
1985 /* Give more detailed info for ddraw */
1986 if (This
->Flags
& SFLAG_DCINUSE
)
1987 return WINEDDERR_DCALREADYCREATED
;
1989 /* Can't GetDC if the surface is locked */
1990 if (This
->Flags
& SFLAG_LOCKED
)
1991 return WINED3DERR_INVALIDCALL
;
1993 memset(&lock
, 0, sizeof(lock
)); /* To be sure */
1995 /* Create a DIB section if there isn't a hdc yet */
1998 if (This
->Flags
& SFLAG_CLIENT
)
2000 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
2001 surface_release_client_storage(This
);
2003 hr
= IWineD3DBaseSurfaceImpl_CreateDIBSection(iface
);
2004 if(FAILED(hr
)) return WINED3DERR_INVALIDCALL
;
2006 /* Use the dib section from now on if we are not using a PBO */
2007 if(!(This
->Flags
& SFLAG_PBO
))
2008 This
->resource
.allocatedMemory
= This
->dib
.bitmap_data
;
2011 /* Lock the surface */
2012 hr
= IWineD3DSurface_LockRect(iface
,
2017 if(This
->Flags
& SFLAG_PBO
) {
2018 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
2019 memcpy(This
->dib
.bitmap_data
, This
->resource
.allocatedMemory
, This
->dib
.bitmap_size
);
2023 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr
);
2024 /* keep the dib section */
2028 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
2029 || This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT_A8_UNORM
)
2031 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2032 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2034 const PALETTEENTRY
*pal
= NULL
;
2037 pal
= This
->palette
->palents
;
2039 IWineD3DSurfaceImpl
*dds_primary
;
2040 IWineD3DSwapChainImpl
*swapchain
;
2041 swapchain
= (IWineD3DSwapChainImpl
*)This
->resource
.device
->swapchains
[0];
2042 dds_primary
= swapchain
->front_buffer
;
2043 if (dds_primary
&& dds_primary
->palette
)
2044 pal
= dds_primary
->palette
->palents
;
2048 for (n
=0; n
<256; n
++) {
2049 col
[n
].rgbRed
= pal
[n
].peRed
;
2050 col
[n
].rgbGreen
= pal
[n
].peGreen
;
2051 col
[n
].rgbBlue
= pal
[n
].peBlue
;
2052 col
[n
].rgbReserved
= 0;
2054 SetDIBColorTable(This
->hDC
, 0, 256, col
);
2059 TRACE("returning %p\n",*pHDC
);
2060 This
->Flags
|= SFLAG_DCINUSE
;
2065 static HRESULT WINAPI
IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface
*iface
, HDC hDC
)
2067 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2069 TRACE("(%p)->(%p)\n",This
,hDC
);
2071 if (!(This
->Flags
& SFLAG_DCINUSE
))
2072 return WINEDDERR_NODC
;
2074 if (This
->hDC
!=hDC
) {
2075 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC
, This
->hDC
);
2076 return WINEDDERR_NODC
;
2079 if((This
->Flags
& SFLAG_PBO
) && This
->resource
.allocatedMemory
) {
2080 /* Copy the contents of the DIB over to the PBO */
2081 memcpy(This
->resource
.allocatedMemory
, This
->dib
.bitmap_data
, This
->dib
.bitmap_size
);
2084 /* we locked first, so unlock now */
2085 IWineD3DSurface_UnlockRect(iface
);
2087 This
->Flags
&= ~SFLAG_DCINUSE
;
2092 /* ******************************************************
2093 IWineD3DSurface Internal (No mapping to directx api) parts follow
2094 ****************************************************** */
2096 HRESULT
d3dfmt_get_conv(IWineD3DSurfaceImpl
*This
, BOOL need_alpha_ck
, BOOL use_texturing
, struct wined3d_format_desc
*desc
, CONVERT_TYPES
*convert
)
2098 BOOL colorkey_active
= need_alpha_ck
&& (This
->CKeyFlags
& WINEDDSD_CKSRCBLT
);
2099 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2100 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2101 BOOL blit_supported
= FALSE
;
2103 /* Copy the default values from the surface. Below we might perform fixups */
2104 /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2105 *desc
= *This
->resource
.format_desc
;
2106 *convert
= NO_CONVERSION
;
2108 /* Ok, now look if we have to do any conversion */
2109 switch(This
->resource
.format_desc
->format
)
2111 case WINED3DFMT_P8_UINT
:
2116 /* Below the call to blit_supported is disabled for Wine 1.2 because the function isn't operating correctly yet.
2117 * At the moment 8-bit blits are handled in software and if certain GL extensions are around, surface conversion
2118 * is performed at upload time. The blit_supported call recognizes it as a destination fixup. This type of upload 'fixup'
2119 * and 8-bit to 8-bit blits need to be handled by the blit_shader.
2120 * TODO: get rid of this #if 0
2123 blit_supported
= device
->blitter
->blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
2124 &rect
, This
->resource
.usage
, This
->resource
.pool
,
2125 This
->resource
.format_desc
, &rect
, This
->resource
.usage
,
2126 This
->resource
.pool
, This
->resource
.format_desc
);
2128 blit_supported
= gl_info
->supported
[EXT_PALETTED_TEXTURE
] || gl_info
->supported
[ARB_FRAGMENT_PROGRAM
];
2130 /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2131 * texturing. Further also use conversion in case of color keying.
2132 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2133 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2134 * conflicts with this.
2136 if (!((blit_supported
&& device
->render_targets
&& This
== device
->render_targets
[0]))
2137 || colorkey_active
|| !use_texturing
)
2139 desc
->glFormat
= GL_RGBA
;
2140 desc
->glInternal
= GL_RGBA
;
2141 desc
->glType
= GL_UNSIGNED_BYTE
;
2142 desc
->conv_byte_count
= 4;
2143 if(colorkey_active
) {
2144 *convert
= CONVERT_PALETTED_CK
;
2146 *convert
= CONVERT_PALETTED
;
2151 case WINED3DFMT_B2G3R3_UNORM
:
2152 /* **********************
2153 GL_UNSIGNED_BYTE_3_3_2
2154 ********************** */
2155 if (colorkey_active
) {
2156 /* This texture format will never be used.. So do not care about color keying
2157 up until the point in time it will be needed :-) */
2158 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2162 case WINED3DFMT_B5G6R5_UNORM
:
2163 if (colorkey_active
) {
2164 *convert
= CONVERT_CK_565
;
2165 desc
->glFormat
= GL_RGBA
;
2166 desc
->glInternal
= GL_RGB5_A1
;
2167 desc
->glType
= GL_UNSIGNED_SHORT_5_5_5_1
;
2168 desc
->conv_byte_count
= 2;
2172 case WINED3DFMT_B5G5R5X1_UNORM
:
2173 if (colorkey_active
) {
2174 *convert
= CONVERT_CK_5551
;
2175 desc
->glFormat
= GL_BGRA
;
2176 desc
->glInternal
= GL_RGB5_A1
;
2177 desc
->glType
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
2178 desc
->conv_byte_count
= 2;
2182 case WINED3DFMT_B8G8R8_UNORM
:
2183 if (colorkey_active
) {
2184 *convert
= CONVERT_CK_RGB24
;
2185 desc
->glFormat
= GL_RGBA
;
2186 desc
->glInternal
= GL_RGBA8
;
2187 desc
->glType
= GL_UNSIGNED_INT_8_8_8_8
;
2188 desc
->conv_byte_count
= 4;
2192 case WINED3DFMT_B8G8R8X8_UNORM
:
2193 if (colorkey_active
) {
2194 *convert
= CONVERT_RGB32_888
;
2195 desc
->glFormat
= GL_RGBA
;
2196 desc
->glInternal
= GL_RGBA8
;
2197 desc
->glType
= GL_UNSIGNED_INT_8_8_8_8
;
2198 desc
->conv_byte_count
= 4;
2209 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl
*This
, BYTE table
[256][4], BOOL colorkey
)
2211 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2212 IWineD3DPaletteImpl
*pal
= This
->palette
;
2213 BOOL index_in_alpha
= FALSE
;
2216 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2217 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2218 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2219 * duplicate entries. Store the color key in the unused alpha component to speed the
2220 * download up and to make conversion unneeded. */
2221 index_in_alpha
= primary_render_target_is_p8(device
);
2225 UINT dxVersion
= ((IWineD3DImpl
*)device
->wined3d
)->dxVersion
;
2227 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2230 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2233 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2234 * there's no palette at this time. */
2235 for (i
= 0; i
< 256; i
++) table
[i
][3] = i
;
2240 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2241 * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2242 * capability flag is present (wine does advertise this capability) */
2243 for (i
= 0; i
< 256; ++i
)
2245 table
[i
][0] = device
->palettes
[device
->currentPalette
][i
].peRed
;
2246 table
[i
][1] = device
->palettes
[device
->currentPalette
][i
].peGreen
;
2247 table
[i
][2] = device
->palettes
[device
->currentPalette
][i
].peBlue
;
2248 table
[i
][3] = device
->palettes
[device
->currentPalette
][i
].peFlags
;
2254 TRACE("Using surface palette %p\n", pal
);
2255 /* Get the surface's palette */
2256 for (i
= 0; i
< 256; ++i
)
2258 table
[i
][0] = pal
->palents
[i
].peRed
;
2259 table
[i
][1] = pal
->palents
[i
].peGreen
;
2260 table
[i
][2] = pal
->palents
[i
].peBlue
;
2262 /* When index_in_alpha is set the palette index is stored in the
2263 * alpha component. In case of a readback we can then read
2264 * GL_ALPHA. Color keying is handled in BltOverride using a
2265 * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2266 * color key itself is passed to glAlphaFunc in other cases the
2267 * alpha component of pixels that should be masked away is set to 0. */
2272 else if (colorkey
&& (i
>= This
->SrcBltCKey
.dwColorSpaceLowValue
)
2273 && (i
<= This
->SrcBltCKey
.dwColorSpaceHighValue
))
2277 else if(pal
->Flags
& WINEDDPCAPS_ALPHA
)
2279 table
[i
][3] = pal
->palents
[i
].peFlags
;
2289 static HRESULT
d3dfmt_convert_surface(const BYTE
*src
, BYTE
*dst
, UINT pitch
, UINT width
,
2290 UINT height
, UINT outpitch
, CONVERT_TYPES convert
, IWineD3DSurfaceImpl
*This
)
2294 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src
, dst
, pitch
, height
, outpitch
, convert
,This
);
2299 memcpy(dst
, src
, pitch
* height
);
2302 case CONVERT_PALETTED
:
2303 case CONVERT_PALETTED_CK
:
2305 IWineD3DPaletteImpl
* pal
= This
->palette
;
2310 /* TODO: If we are a sublevel, try to get the palette from level 0 */
2313 d3dfmt_p8_init_palette(This
, table
, (convert
== CONVERT_PALETTED_CK
));
2315 for (y
= 0; y
< height
; y
++)
2317 source
= src
+ pitch
* y
;
2318 dest
= dst
+ outpitch
* y
;
2319 /* This is an 1 bpp format, using the width here is fine */
2320 for (x
= 0; x
< width
; x
++) {
2321 BYTE color
= *source
++;
2322 *dest
++ = table
[color
][0];
2323 *dest
++ = table
[color
][1];
2324 *dest
++ = table
[color
][2];
2325 *dest
++ = table
[color
][3];
2331 case CONVERT_CK_565
:
2333 /* Converting the 565 format in 5551 packed to emulate color-keying.
2335 Note : in all these conversion, it would be best to average the averaging
2336 pixels to get the color of the pixel that will be color-keyed to
2337 prevent 'color bleeding'. This will be done later on if ever it is
2340 Note2: Nvidia documents say that their driver does not support alpha + color keying
2341 on the same surface and disables color keying in such a case
2347 TRACE("Color keyed 565\n");
2349 for (y
= 0; y
< height
; y
++) {
2350 Source
= (const WORD
*)(src
+ y
* pitch
);
2351 Dest
= (WORD
*) (dst
+ y
* outpitch
);
2352 for (x
= 0; x
< width
; x
++ ) {
2353 WORD color
= *Source
++;
2354 *Dest
= ((color
& 0xFFC0) | ((color
& 0x1F) << 1));
2355 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2356 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2365 case CONVERT_CK_5551
:
2367 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2371 TRACE("Color keyed 5551\n");
2372 for (y
= 0; y
< height
; y
++) {
2373 Source
= (const WORD
*)(src
+ y
* pitch
);
2374 Dest
= (WORD
*) (dst
+ y
* outpitch
);
2375 for (x
= 0; x
< width
; x
++ ) {
2376 WORD color
= *Source
++;
2378 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2379 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2383 *Dest
&= ~(1 << 15);
2391 case CONVERT_CK_RGB24
:
2393 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2395 for (y
= 0; y
< height
; y
++)
2397 source
= src
+ pitch
* y
;
2398 dest
= dst
+ outpitch
* y
;
2399 for (x
= 0; x
< width
; x
++) {
2400 DWORD color
= ((DWORD
)source
[0] << 16) + ((DWORD
)source
[1] << 8) + (DWORD
)source
[2] ;
2401 DWORD dstcolor
= color
<< 8;
2402 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2403 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2406 *(DWORD
*)dest
= dstcolor
;
2414 case CONVERT_RGB32_888
:
2416 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2418 for (y
= 0; y
< height
; y
++)
2420 source
= src
+ pitch
* y
;
2421 dest
= dst
+ outpitch
* y
;
2422 for (x
= 0; x
< width
; x
++) {
2423 DWORD color
= 0xffffff & *(const DWORD
*)source
;
2424 DWORD dstcolor
= color
<< 8;
2425 if ((color
< This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2426 (color
> This
->SrcBltCKey
.dwColorSpaceHighValue
)) {
2429 *(DWORD
*)dest
= dstcolor
;
2438 ERR("Unsupported conversion type %#x.\n", convert
);
2443 BOOL
palette9_changed(IWineD3DSurfaceImpl
*This
)
2445 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
2447 if (This
->palette
|| (This
->resource
.format_desc
->format
!= WINED3DFMT_P8_UINT
2448 && This
->resource
.format_desc
->format
!= WINED3DFMT_P8_UINT_A8_UNORM
))
2450 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2451 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2458 if (!memcmp(This
->palette9
, device
->palettes
[device
->currentPalette
], sizeof(PALETTEENTRY
) * 256))
2463 This
->palette9
= HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY
) * 256);
2465 memcpy(This
->palette9
, device
->palettes
[device
->currentPalette
], sizeof(PALETTEENTRY
) * 256);
2469 static HRESULT WINAPI
IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface
*iface
, BOOL srgb_mode
) {
2470 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2471 DWORD flag
= srgb_mode
? SFLAG_INSRGBTEX
: SFLAG_INTEXTURE
;
2473 if (!(This
->Flags
& flag
)) {
2474 TRACE("Reloading because surface is dirty\n");
2475 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2476 ((This
->Flags
& SFLAG_GLCKEY
) && (!(This
->CKeyFlags
& WINEDDSD_CKSRCBLT
))) ||
2477 /* Reload: vice versa OR */
2478 ((!(This
->Flags
& SFLAG_GLCKEY
)) && (This
->CKeyFlags
& WINEDDSD_CKSRCBLT
)) ||
2479 /* Also reload: Color key is active AND the color key has changed */
2480 ((This
->CKeyFlags
& WINEDDSD_CKSRCBLT
) && (
2481 (This
->glCKey
.dwColorSpaceLowValue
!= This
->SrcBltCKey
.dwColorSpaceLowValue
) ||
2482 (This
->glCKey
.dwColorSpaceHighValue
!= This
->SrcBltCKey
.dwColorSpaceHighValue
)))) {
2483 TRACE("Reloading because of color keying\n");
2484 /* To perform the color key conversion we need a sysmem copy of
2485 * the surface. Make sure we have it
2488 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
2489 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2490 /* TODO: This is not necessarily needed with hw palettized texture support */
2491 surface_modify_location(This
, SFLAG_INSYSMEM
, TRUE
);
2493 TRACE("surface is already in texture\n");
2497 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2498 * These resources are not bound by device size or format restrictions. Because of this,
2499 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2500 * However, these resources can always be created, locked, and copied.
2502 if (This
->resource
.pool
== WINED3DPOOL_SCRATCH
)
2504 FIXME("(%p) Operation not supported for scratch textures\n",This
);
2505 return WINED3DERR_INVALIDCALL
;
2508 surface_load_location(This
, flag
, NULL
/* no partial locking for textures yet */);
2510 if (!(This
->Flags
& SFLAG_DONOTFREE
)) {
2511 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
2512 This
->resource
.allocatedMemory
= NULL
;
2513 This
->resource
.heapMemory
= NULL
;
2514 surface_modify_location(This
, SFLAG_INSYSMEM
, FALSE
);
2520 /* Context activation is done by the caller. */
2521 static void WINAPI
IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface
*iface
, BOOL srgb
) {
2522 /* TODO: check for locks */
2523 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2524 IWineD3DBaseTexture
*baseTexture
= NULL
;
2526 TRACE("(%p)Checking to see if the container is a base texture\n", This
);
2527 if (IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DBaseTexture
, (void **)&baseTexture
) == WINED3D_OK
) {
2528 TRACE("Passing to container\n");
2529 IWineD3DBaseTexture_BindTexture(baseTexture
, srgb
);
2530 IWineD3DBaseTexture_Release(baseTexture
);
2536 TRACE("(%p) : Binding surface\n", This
);
2538 name
= srgb
? &This
->texture_name_srgb
: &This
->texture_name
;
2542 if (!This
->texture_level
)
2545 glGenTextures(1, name
);
2546 checkGLcall("glGenTextures");
2547 TRACE("Surface %p given name %d\n", This
, *name
);
2549 glBindTexture(This
->texture_target
, *name
);
2550 checkGLcall("glBindTexture");
2551 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2552 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2553 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2554 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2555 glTexParameteri(This
->texture_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2556 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2557 glTexParameteri(This
->texture_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2558 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2559 glTexParameteri(This
->texture_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2560 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2562 /* This is where we should be reducing the amount of GLMemoryUsed */
2564 /* Mipmap surfaces should have a base texture container */
2565 ERR("Mipmap surface has a glTexture bound to it!\n");
2568 glBindTexture(This
->texture_target
, *name
);
2569 checkGLcall("glBindTexture");
2575 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface
*iface
, WINED3DFORMAT format
) {
2576 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2579 TRACE("(%p) : Calling base function first\n", This
);
2580 hr
= IWineD3DBaseSurfaceImpl_SetFormat(iface
, format
);
2582 This
->Flags
&= ~(SFLAG_ALLOCATED
| SFLAG_SRGBALLOCATED
);
2583 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This
, This
->resource
.format_desc
->glFormat
,
2584 This
->resource
.format_desc
->glInternal
, This
->resource
.format_desc
->glType
);
2589 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetMem(IWineD3DSurface
*iface
, void *Mem
) {
2590 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
2592 if(This
->Flags
& (SFLAG_LOCKED
| SFLAG_DCINUSE
)) {
2593 WARN("Surface is locked or the HDC is in use\n");
2594 return WINED3DERR_INVALIDCALL
;
2597 if(Mem
&& Mem
!= This
->resource
.allocatedMemory
) {
2598 void *release
= NULL
;
2600 /* Do I have to copy the old surface content? */
2601 if(This
->Flags
& SFLAG_DIBSECTION
) {
2602 /* Release the DC. No need to hold the critical section for the update
2603 * Thread because this thread runs only on front buffers, but this method
2604 * fails for render targets in the check above.
2606 SelectObject(This
->hDC
, This
->dib
.holdbitmap
);
2607 DeleteDC(This
->hDC
);
2608 /* Release the DIB section */
2609 DeleteObject(This
->dib
.DIBsection
);
2610 This
->dib
.bitmap_data
= NULL
;
2611 This
->resource
.allocatedMemory
= NULL
;
2613 This
->Flags
&= ~SFLAG_DIBSECTION
;
2614 } else if(!(This
->Flags
& SFLAG_USERPTR
)) {
2615 release
= This
->resource
.heapMemory
;
2616 This
->resource
.heapMemory
= NULL
;
2618 This
->resource
.allocatedMemory
= Mem
;
2619 This
->Flags
|= SFLAG_USERPTR
| SFLAG_INSYSMEM
;
2621 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2622 surface_modify_location(This
, SFLAG_INSYSMEM
, TRUE
);
2624 /* For client textures opengl has to be notified */
2625 if (This
->Flags
& SFLAG_CLIENT
)
2626 surface_release_client_storage(This
);
2628 /* Now free the old memory if any */
2629 HeapFree(GetProcessHeap(), 0, release
);
2630 } else if(This
->Flags
& SFLAG_USERPTR
) {
2631 /* LockRect and GetDC will re-create the dib section and allocated memory */
2632 This
->resource
.allocatedMemory
= NULL
;
2633 /* HeapMemory should be NULL already */
2634 if(This
->resource
.heapMemory
!= NULL
) ERR("User pointer surface has heap memory allocated\n");
2635 This
->Flags
&= ~SFLAG_USERPTR
;
2637 if (This
->Flags
& SFLAG_CLIENT
)
2638 surface_release_client_storage(This
);
2643 void flip_surface(IWineD3DSurfaceImpl
*front
, IWineD3DSurfaceImpl
*back
) {
2645 /* Flip the surface contents */
2650 front
->hDC
= back
->hDC
;
2654 /* Flip the DIBsection */
2657 BOOL hasDib
= front
->Flags
& SFLAG_DIBSECTION
;
2658 tmp
= front
->dib
.DIBsection
;
2659 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
2660 back
->dib
.DIBsection
= tmp
;
2662 if(back
->Flags
& SFLAG_DIBSECTION
) front
->Flags
|= SFLAG_DIBSECTION
;
2663 else front
->Flags
&= ~SFLAG_DIBSECTION
;
2664 if(hasDib
) back
->Flags
|= SFLAG_DIBSECTION
;
2665 else back
->Flags
&= ~SFLAG_DIBSECTION
;
2668 /* Flip the surface data */
2672 tmp
= front
->dib
.bitmap_data
;
2673 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
2674 back
->dib
.bitmap_data
= tmp
;
2676 tmp
= front
->resource
.allocatedMemory
;
2677 front
->resource
.allocatedMemory
= back
->resource
.allocatedMemory
;
2678 back
->resource
.allocatedMemory
= tmp
;
2680 tmp
= front
->resource
.heapMemory
;
2681 front
->resource
.heapMemory
= back
->resource
.heapMemory
;
2682 back
->resource
.heapMemory
= tmp
;
2687 GLuint tmp_pbo
= front
->pbo
;
2688 front
->pbo
= back
->pbo
;
2689 back
->pbo
= tmp_pbo
;
2692 /* client_memory should not be different, but just in case */
2695 tmp
= front
->dib
.client_memory
;
2696 front
->dib
.client_memory
= back
->dib
.client_memory
;
2697 back
->dib
.client_memory
= tmp
;
2700 /* Flip the opengl texture */
2704 tmp
= back
->texture_name
;
2705 back
->texture_name
= front
->texture_name
;
2706 front
->texture_name
= tmp
;
2708 tmp
= back
->texture_name_srgb
;
2709 back
->texture_name_srgb
= front
->texture_name_srgb
;
2710 front
->texture_name_srgb
= tmp
;
2714 DWORD tmp_flags
= back
->Flags
;
2715 back
->Flags
= front
->Flags
;
2716 front
->Flags
= tmp_flags
;
2720 static HRESULT WINAPI
IWineD3DSurfaceImpl_Flip(IWineD3DSurface
*iface
, IWineD3DSurface
*override
, DWORD Flags
) {
2721 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
2722 IWineD3DSwapChainImpl
*swapchain
= NULL
;
2724 TRACE("(%p)->(%p,%x)\n", This
, override
, Flags
);
2726 /* Flipping is only supported on RenderTargets and overlays*/
2727 if( !(This
->resource
.usage
& (WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_OVERLAY
)) ) {
2728 WARN("Tried to flip a non-render target, non-overlay surface\n");
2729 return WINEDDERR_NOTFLIPPABLE
;
2732 if(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
) {
2733 flip_surface(This
, (IWineD3DSurfaceImpl
*) override
);
2735 /* Update the overlay if it is visible */
2736 if(This
->overlay_dest
) {
2737 return IWineD3DSurface_DrawOverlay((IWineD3DSurface
*) This
);
2744 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2745 * FIXME("(%p) Target override is not supported by now\n", This);
2746 * Additionally, it isn't really possible to support triple-buffering
2747 * properly on opengl at all
2751 IWineD3DSurface_GetContainer(iface
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
2753 ERR("Flipped surface is not on a swapchain\n");
2754 return WINEDDERR_NOTFLIPPABLE
;
2757 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2758 * and only d3d8 and d3d9 apps specify the presentation interval
2760 if((Flags
& (WINEDDFLIP_NOVSYNC
| WINEDDFLIP_INTERVAL2
| WINEDDFLIP_INTERVAL3
| WINEDDFLIP_INTERVAL4
)) == 0) {
2761 /* Most common case first to avoid wasting time on all the other cases */
2762 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_ONE
;
2763 } else if(Flags
& WINEDDFLIP_NOVSYNC
) {
2764 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_IMMEDIATE
;
2765 } else if(Flags
& WINEDDFLIP_INTERVAL2
) {
2766 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_TWO
;
2767 } else if(Flags
& WINEDDFLIP_INTERVAL3
) {
2768 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_THREE
;
2770 swapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_FOUR
;
2773 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2774 hr
= IWineD3DSwapChain_Present((IWineD3DSwapChain
*)swapchain
,
2775 NULL
, NULL
, swapchain
->win_handle
, NULL
, 0);
2776 IWineD3DSwapChain_Release((IWineD3DSwapChain
*) swapchain
);
2780 /* Does a direct frame buffer -> texture copy. Stretching is done
2781 * with single pixel copy calls
2783 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl
*dst_surface
, IWineD3DSurfaceImpl
*src_surface
,
2784 const RECT
*src_rect
, const RECT
*dst_rect_in
, WINED3DTEXTUREFILTERTYPE Filter
)
2786 IWineD3DDeviceImpl
*device
= dst_surface
->resource
.device
;
2789 struct wined3d_context
*context
;
2790 BOOL upsidedown
= FALSE
;
2791 RECT dst_rect
= *dst_rect_in
;
2793 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2794 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2796 if(dst_rect
.top
> dst_rect
.bottom
) {
2797 UINT tmp
= dst_rect
.bottom
;
2798 dst_rect
.bottom
= dst_rect
.top
;
2803 context
= context_acquire(device
, src_surface
);
2804 context_apply_blit_state(context
, device
);
2805 surface_internal_preload(dst_surface
, SRGB_RGB
);
2808 /* Bind the target texture */
2809 glBindTexture(dst_surface
->texture_target
, dst_surface
->texture_name
);
2810 checkGLcall("glBindTexture");
2811 if (surface_is_offscreen(src_surface
))
2813 TRACE("Reading from an offscreen target\n");
2814 upsidedown
= !upsidedown
;
2815 glReadBuffer(device
->offscreenBuffer
);
2819 glReadBuffer(surface_get_gl_buffer(src_surface
));
2821 checkGLcall("glReadBuffer");
2823 xrel
= (float) (src_rect
->right
- src_rect
->left
) / (float) (dst_rect
.right
- dst_rect
.left
);
2824 yrel
= (float) (src_rect
->bottom
- src_rect
->top
) / (float) (dst_rect
.bottom
- dst_rect
.top
);
2826 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
2828 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2830 if(Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
) {
2831 ERR("Texture filtering not supported in direct blit\n");
2834 else if ((Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
)
2835 && ((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
2837 ERR("Texture filtering not supported in direct blit\n");
2841 && !((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
2842 && !((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
2844 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2846 glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
2847 dst_rect
.left
/*xoffset */, dst_rect
.top
/* y offset */,
2848 src_rect
->left
, src_surface
->currentDesc
.Height
- src_rect
->bottom
,
2849 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
2851 UINT yoffset
= src_surface
->currentDesc
.Height
- src_rect
->top
+ dst_rect
.top
- 1;
2852 /* I have to process this row by row to swap the image,
2853 * otherwise it would be upside down, so stretching in y direction
2854 * doesn't cost extra time
2856 * However, stretching in x direction can be avoided if not necessary
2858 for(row
= dst_rect
.top
; row
< dst_rect
.bottom
; row
++) {
2859 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
2861 /* Well, that stuff works, but it's very slow.
2862 * find a better way instead
2866 for (col
= dst_rect
.left
; col
< dst_rect
.right
; ++col
)
2868 glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
2869 dst_rect
.left
+ col
/* x offset */, row
/* y offset */,
2870 src_rect
->left
+ col
* xrel
, yoffset
- (int) (row
* yrel
), 1, 1);
2875 glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
2876 dst_rect
.left
/* x offset */, row
/* y offset */,
2877 src_rect
->left
, yoffset
- (int) (row
* yrel
), dst_rect
.right
- dst_rect
.left
, 1);
2881 checkGLcall("glCopyTexSubImage2D");
2884 context_release(context
);
2886 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2887 * path is never entered
2889 surface_modify_location(dst_surface
, SFLAG_INTEXTURE
, TRUE
);
2892 /* Uses the hardware to stretch and flip the image */
2893 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl
*dst_surface
, IWineD3DSurfaceImpl
*src_surface
,
2894 const RECT
*src_rect
, const RECT
*dst_rect_in
, WINED3DTEXTUREFILTERTYPE Filter
)
2896 IWineD3DDeviceImpl
*device
= dst_surface
->resource
.device
;
2897 GLuint src
, backup
= 0;
2898 IWineD3DSwapChainImpl
*src_swapchain
= NULL
;
2899 float left
, right
, top
, bottom
; /* Texture coordinates */
2900 UINT fbwidth
= src_surface
->currentDesc
.Width
;
2901 UINT fbheight
= src_surface
->currentDesc
.Height
;
2902 struct wined3d_context
*context
;
2903 GLenum drawBuffer
= GL_BACK
;
2904 GLenum texture_target
;
2905 BOOL noBackBufferBackup
;
2907 BOOL upsidedown
= FALSE
;
2908 RECT dst_rect
= *dst_rect_in
;
2910 TRACE("Using hwstretch blit\n");
2911 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2912 context
= context_acquire(device
, src_surface
);
2913 context_apply_blit_state(context
, device
);
2914 surface_internal_preload(dst_surface
, SRGB_RGB
);
2916 src_offscreen
= surface_is_offscreen(src_surface
);
2917 noBackBufferBackup
= src_offscreen
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
;
2918 if (!noBackBufferBackup
&& !src_surface
->texture_name
)
2920 /* Get it a description */
2921 surface_internal_preload(src_surface
, SRGB_RGB
);
2925 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2926 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2928 if (context
->aux_buffers
>= 2)
2930 /* Got more than one aux buffer? Use the 2nd aux buffer */
2931 drawBuffer
= GL_AUX1
;
2933 else if ((!src_offscreen
|| device
->offscreenBuffer
== GL_BACK
) && context
->aux_buffers
>= 1)
2935 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2936 drawBuffer
= GL_AUX0
;
2939 if(noBackBufferBackup
) {
2940 glGenTextures(1, &backup
);
2941 checkGLcall("glGenTextures");
2942 glBindTexture(GL_TEXTURE_2D
, backup
);
2943 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2944 texture_target
= GL_TEXTURE_2D
;
2946 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2947 * we are reading from the back buffer, the backup can be used as source texture
2949 texture_target
= src_surface
->texture_target
;
2950 glBindTexture(texture_target
, src_surface
->texture_name
);
2951 checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
2952 glEnable(texture_target
);
2953 checkGLcall("glEnable(texture_target)");
2955 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2956 src_surface
->Flags
&= ~SFLAG_INTEXTURE
;
2959 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2960 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2962 if(dst_rect
.top
> dst_rect
.bottom
) {
2963 UINT tmp
= dst_rect
.bottom
;
2964 dst_rect
.bottom
= dst_rect
.top
;
2971 TRACE("Reading from an offscreen target\n");
2972 upsidedown
= !upsidedown
;
2973 glReadBuffer(device
->offscreenBuffer
);
2977 glReadBuffer(surface_get_gl_buffer(src_surface
));
2980 /* TODO: Only back up the part that will be overwritten */
2981 glCopyTexSubImage2D(texture_target
, 0,
2982 0, 0 /* read offsets */,
2987 checkGLcall("glCopyTexSubImage2D");
2989 /* No issue with overriding these - the sampler is dirty due to blit usage */
2990 glTexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
,
2991 wined3d_gl_mag_filter(magLookup
, Filter
));
2992 checkGLcall("glTexParameteri");
2993 glTexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
,
2994 wined3d_gl_min_mip_filter(minMipLookup
, Filter
, WINED3DTEXF_NONE
));
2995 checkGLcall("glTexParameteri");
2997 IWineD3DSurface_GetContainer((IWineD3DSurface
*)src_surface
, &IID_IWineD3DSwapChain
, (void **)&src_swapchain
);
2998 if (src_swapchain
) IWineD3DSwapChain_Release((IWineD3DSwapChain
*)src_swapchain
);
2999 if (!src_swapchain
|| src_surface
== src_swapchain
->back_buffers
[0])
3001 src
= backup
? backup
: src_surface
->texture_name
;
3005 glReadBuffer(GL_FRONT
);
3006 checkGLcall("glReadBuffer(GL_FRONT)");
3008 glGenTextures(1, &src
);
3009 checkGLcall("glGenTextures(1, &src)");
3010 glBindTexture(GL_TEXTURE_2D
, src
);
3011 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3013 /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3014 * out for power of 2 sizes
3016 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, src_surface
->pow2Width
,
3017 src_surface
->pow2Height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
3018 checkGLcall("glTexImage2D");
3019 glCopyTexSubImage2D(GL_TEXTURE_2D
, 0,
3020 0, 0 /* read offsets */,
3025 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3026 checkGLcall("glTexParameteri");
3027 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3028 checkGLcall("glTexParameteri");
3030 glReadBuffer(GL_BACK
);
3031 checkGLcall("glReadBuffer(GL_BACK)");
3033 if(texture_target
!= GL_TEXTURE_2D
) {
3034 glDisable(texture_target
);
3035 glEnable(GL_TEXTURE_2D
);
3036 texture_target
= GL_TEXTURE_2D
;
3039 checkGLcall("glEnd and previous");
3041 left
= src_rect
->left
;
3042 right
= src_rect
->right
;
3046 top
= src_surface
->currentDesc
.Height
- src_rect
->top
;
3047 bottom
= src_surface
->currentDesc
.Height
- src_rect
->bottom
;
3051 top
= src_surface
->currentDesc
.Height
- src_rect
->bottom
;
3052 bottom
= src_surface
->currentDesc
.Height
- src_rect
->top
;
3055 if (src_surface
->Flags
& SFLAG_NORMCOORD
)
3057 left
/= src_surface
->pow2Width
;
3058 right
/= src_surface
->pow2Width
;
3059 top
/= src_surface
->pow2Height
;
3060 bottom
/= src_surface
->pow2Height
;
3063 /* draw the source texture stretched and upside down. The correct surface is bound already */
3064 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
3065 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
3067 context_set_draw_buffer(context
, drawBuffer
);
3068 glReadBuffer(drawBuffer
);
3072 glTexCoord2f(left
, bottom
);
3073 glVertex2i(0, fbheight
);
3076 glTexCoord2f(left
, top
);
3077 glVertex2i(0, fbheight
- dst_rect
.bottom
- dst_rect
.top
);
3080 glTexCoord2f(right
, top
);
3081 glVertex2i(dst_rect
.right
- dst_rect
.left
, fbheight
- dst_rect
.bottom
- dst_rect
.top
);
3084 glTexCoord2f(right
, bottom
);
3085 glVertex2i(dst_rect
.right
- dst_rect
.left
, fbheight
);
3087 checkGLcall("glEnd and previous");
3089 if (texture_target
!= dst_surface
->texture_target
)
3091 glDisable(texture_target
);
3092 glEnable(dst_surface
->texture_target
);
3093 texture_target
= dst_surface
->texture_target
;
3096 /* Now read the stretched and upside down image into the destination texture */
3097 glBindTexture(texture_target
, dst_surface
->texture_name
);
3098 checkGLcall("glBindTexture");
3099 glCopyTexSubImage2D(texture_target
,
3101 dst_rect
.left
, dst_rect
.top
, /* xoffset, yoffset */
3102 0, 0, /* We blitted the image to the origin */
3103 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
3104 checkGLcall("glCopyTexSubImage2D");
3106 if(drawBuffer
== GL_BACK
) {
3107 /* Write the back buffer backup back */
3109 if(texture_target
!= GL_TEXTURE_2D
) {
3110 glDisable(texture_target
);
3111 glEnable(GL_TEXTURE_2D
);
3112 texture_target
= GL_TEXTURE_2D
;
3114 glBindTexture(GL_TEXTURE_2D
, backup
);
3115 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3119 if (texture_target
!= src_surface
->texture_target
)
3121 glDisable(texture_target
);
3122 glEnable(src_surface
->texture_target
);
3123 texture_target
= src_surface
->texture_target
;
3125 glBindTexture(src_surface
->texture_target
, src_surface
->texture_name
);
3126 checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3131 glTexCoord2f(0.0f
, (float)fbheight
/ (float)src_surface
->pow2Height
);
3135 glTexCoord2f(0.0f
, 0.0f
);
3136 glVertex2i(0, fbheight
);
3139 glTexCoord2f((float)fbwidth
/ (float)src_surface
->pow2Width
, 0.0f
);
3140 glVertex2i(fbwidth
, src_surface
->currentDesc
.Height
);
3143 glTexCoord2f((float)fbwidth
/ (float)src_surface
->pow2Width
,
3144 (float)fbheight
/ (float)src_surface
->pow2Height
);
3145 glVertex2i(fbwidth
, 0);
3148 glDisable(texture_target
);
3149 checkGLcall("glDisable(texture_target)");
3152 if (src
!= src_surface
->texture_name
&& src
!= backup
)
3154 glDeleteTextures(1, &src
);
3155 checkGLcall("glDeleteTextures(1, &src)");
3158 glDeleteTextures(1, &backup
);
3159 checkGLcall("glDeleteTextures(1, &backup)");
3164 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
3166 context_release(context
);
3168 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3169 * path is never entered
3171 surface_modify_location(dst_surface
, SFLAG_INTEXTURE
, TRUE
);
3174 /* Until the blit_shader is ready, define some prototypes here. */
3175 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
3176 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
3177 const struct wined3d_format_desc
*src_format_desc
,
3178 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
3179 const struct wined3d_format_desc
*dst_format_desc
);
3181 /* Front buffer coordinates are always full screen coordinates, but our GL
3182 * drawable is limited to the window's client area. The sysmem and texture
3183 * copies do have the full screen size. Note that GL has a bottom-left
3184 * origin, while D3D has a top-left origin. */
3185 void surface_translate_frontbuffer_coords(IWineD3DSurfaceImpl
*surface
, HWND window
, RECT
*rect
)
3187 POINT offset
= {0, surface
->currentDesc
.Height
};
3190 GetClientRect(window
, &windowsize
);
3191 offset
.y
-= windowsize
.bottom
- windowsize
.top
;
3192 ScreenToClient(window
, &offset
);
3193 OffsetRect(rect
, offset
.x
, offset
.y
);
3196 static BOOL
surface_is_full_rect(IWineD3DSurfaceImpl
*surface
, const RECT
*r
)
3198 if ((r
->left
&& r
->right
) || abs(r
->right
- r
->left
) != surface
->currentDesc
.Width
)
3200 if ((r
->top
&& r
->bottom
) || abs(r
->bottom
- r
->top
) != surface
->currentDesc
.Height
)
3205 /* blit between surface locations. onscreen on different swapchains is not supported.
3206 * depth / stencil is not supported. */
3207 static void surface_blt_fbo(IWineD3DDeviceImpl
*device
, const WINED3DTEXTUREFILTERTYPE filter
,
3208 IWineD3DSurfaceImpl
*src_surface
, DWORD src_location
, const RECT
*src_rect_in
,
3209 IWineD3DSurfaceImpl
*dst_surface
, DWORD dst_location
, const RECT
*dst_rect_in
)
3211 const struct wined3d_gl_info
*gl_info
;
3212 struct wined3d_context
*context
;
3213 RECT src_rect
, dst_rect
;
3216 TRACE("device %p, filter %s,\n", device
, debug_d3dtexturefiltertype(filter
));
3217 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3218 src_surface
, debug_surflocation(src_location
), wine_dbgstr_rect(src_rect_in
));
3219 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3220 dst_surface
, debug_surflocation(dst_location
), wine_dbgstr_rect(dst_rect_in
));
3222 src_rect
= *src_rect_in
;
3223 dst_rect
= *dst_rect_in
;
3227 case WINED3DTEXF_LINEAR
:
3228 gl_filter
= GL_LINEAR
;
3232 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter
), filter
);
3233 case WINED3DTEXF_NONE
:
3234 case WINED3DTEXF_POINT
:
3235 gl_filter
= GL_NEAREST
;
3239 if (src_location
== SFLAG_INDRAWABLE
&& surface_is_offscreen(src_surface
))
3240 src_location
= SFLAG_INTEXTURE
;
3241 if (dst_location
== SFLAG_INDRAWABLE
&& surface_is_offscreen(dst_surface
))
3242 dst_location
= SFLAG_INTEXTURE
;
3244 /* Make sure the locations are up-to-date. Loading the destination
3245 * surface isn't required if the entire surface is overwritten. (And is
3246 * in fact harmful if we're being called by surface_load_location() with
3247 * the purpose of loading the destination surface.) */
3248 surface_load_location(src_surface
, src_location
, NULL
);
3249 if (!surface_is_full_rect(dst_surface
, &dst_rect
))
3250 surface_load_location(dst_surface
, dst_location
, NULL
);
3252 if (src_location
== SFLAG_INDRAWABLE
) context
= context_acquire(device
, src_surface
);
3253 else if (dst_location
== SFLAG_INDRAWABLE
) context
= context_acquire(device
, dst_surface
);
3254 else context
= context_acquire(device
, NULL
);
3256 if (!context
->valid
)
3258 context_release(context
);
3259 WARN("Invalid context, skipping blit.\n");
3263 gl_info
= context
->gl_info
;
3265 if (src_location
== SFLAG_INDRAWABLE
)
3267 GLenum buffer
= surface_get_gl_buffer(src_surface
);
3269 TRACE("Source surface %p is onscreen.\n", src_surface
);
3271 if (buffer
== GL_FRONT
)
3272 surface_translate_frontbuffer_coords(src_surface
, context
->win_handle
, &src_rect
);
3274 src_rect
.top
= src_surface
->currentDesc
.Height
- src_rect
.top
;
3275 src_rect
.bottom
= src_surface
->currentDesc
.Height
- src_rect
.bottom
;
3278 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, NULL
);
3279 glReadBuffer(buffer
);
3280 checkGLcall("glReadBuffer()");
3284 TRACE("Source surface %p is offscreen.\n", src_surface
);
3286 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, src_surface
, NULL
, src_location
);
3287 glReadBuffer(GL_COLOR_ATTACHMENT0
);
3288 checkGLcall("glReadBuffer()");
3292 if (dst_location
== SFLAG_INDRAWABLE
)
3294 GLenum buffer
= surface_get_gl_buffer(dst_surface
);
3296 TRACE("Destination surface %p is onscreen.\n", dst_surface
);
3298 if (buffer
== GL_FRONT
)
3299 surface_translate_frontbuffer_coords(dst_surface
, context
->win_handle
, &dst_rect
);
3301 dst_rect
.top
= dst_surface
->currentDesc
.Height
- dst_rect
.top
;
3302 dst_rect
.bottom
= dst_surface
->currentDesc
.Height
- dst_rect
.bottom
;
3305 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, NULL
);
3306 context_set_draw_buffer(context
, buffer
);
3310 TRACE("Destination surface %p is offscreen.\n", dst_surface
);
3313 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, dst_surface
, NULL
, dst_location
);
3314 context_set_draw_buffer(context
, GL_COLOR_ATTACHMENT0
);
3317 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3318 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
));
3319 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
));
3320 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
));
3321 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
));
3323 glDisable(GL_SCISSOR_TEST
);
3324 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
3326 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
.left
, src_rect
.top
, src_rect
.right
, src_rect
.bottom
,
3327 dst_rect
.left
, dst_rect
.top
, dst_rect
.right
, dst_rect
.bottom
, GL_COLOR_BUFFER_BIT
, gl_filter
);
3328 checkGLcall("glBlitFramebuffer()");
3332 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
3334 context_release(context
);
3337 /* Not called from the VTable */
3338 static HRESULT
IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl
*dst_surface
, const RECT
*DestRect
,
3339 IWineD3DSurfaceImpl
*src_surface
, const RECT
*SrcRect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
,
3340 WINED3DTEXTUREFILTERTYPE Filter
)
3342 IWineD3DDeviceImpl
*device
= dst_surface
->resource
.device
;
3343 IWineD3DSwapChainImpl
*srcSwapchain
= NULL
, *dstSwapchain
= NULL
;
3344 RECT dst_rect
, src_rect
;
3346 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3347 dst_surface
, wine_dbgstr_rect(DestRect
), src_surface
, wine_dbgstr_rect(SrcRect
),
3348 Flags
, DDBltFx
, debug_d3dtexturefiltertype(Filter
));
3350 /* Get the swapchain. One of the surfaces has to be a primary surface */
3351 if (dst_surface
->resource
.pool
== WINED3DPOOL_SYSTEMMEM
)
3353 WARN("Destination is in sysmem, rejecting gl blt\n");
3354 return WINED3DERR_INVALIDCALL
;
3356 IWineD3DSurface_GetContainer((IWineD3DSurface
*)dst_surface
, &IID_IWineD3DSwapChain
, (void **)&dstSwapchain
);
3357 if (dstSwapchain
) IWineD3DSwapChain_Release((IWineD3DSwapChain
*)dstSwapchain
);
3360 if (src_surface
->resource
.pool
== WINED3DPOOL_SYSTEMMEM
)
3362 WARN("Src is in sysmem, rejecting gl blt\n");
3363 return WINED3DERR_INVALIDCALL
;
3365 IWineD3DSurface_GetContainer((IWineD3DSurface
*)src_surface
, &IID_IWineD3DSwapChain
, (void **)&srcSwapchain
);
3366 if (srcSwapchain
) IWineD3DSwapChain_Release((IWineD3DSwapChain
*)srcSwapchain
);
3369 /* Early sort out of cases where no render target is used */
3370 if (!dstSwapchain
&& !srcSwapchain
3371 && src_surface
!= device
->render_targets
[0]
3372 && dst_surface
!= device
->render_targets
[0])
3374 TRACE("No surface is render target, not using hardware blit.\n");
3375 return WINED3DERR_INVALIDCALL
;
3378 /* No destination color keying supported */
3379 if(Flags
& (WINEDDBLT_KEYDEST
| WINEDDBLT_KEYDESTOVERRIDE
)) {
3380 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3381 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3382 return WINED3DERR_INVALIDCALL
;
3385 surface_get_rect(dst_surface
, DestRect
, &dst_rect
);
3386 if (src_surface
) surface_get_rect(src_surface
, SrcRect
, &src_rect
);
3388 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3389 if (dstSwapchain
&& dstSwapchain
== srcSwapchain
&& dstSwapchain
->back_buffers
3390 && dst_surface
== dstSwapchain
->front_buffer
3391 && src_surface
== dstSwapchain
->back_buffers
[0])
3393 /* Half-Life does a Blt from the back buffer to the front buffer,
3394 * Full surface size, no flags... Use present instead
3396 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3399 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3402 TRACE("Looking if a Present can be done...\n");
3403 /* Source Rectangle must be full surface */
3404 if (src_rect
.left
|| src_rect
.top
3405 || src_rect
.right
!= src_surface
->currentDesc
.Width
3406 || src_rect
.bottom
!= src_surface
->currentDesc
.Height
)
3408 TRACE("No, Source rectangle doesn't match\n");
3412 /* No stretching may occur */
3413 if(src_rect
.right
!= dst_rect
.right
- dst_rect
.left
||
3414 src_rect
.bottom
!= dst_rect
.bottom
- dst_rect
.top
) {
3415 TRACE("No, stretching is done\n");
3419 /* Destination must be full surface or match the clipping rectangle */
3420 if (dst_surface
->clipper
&& ((IWineD3DClipperImpl
*)dst_surface
->clipper
)->hWnd
)
3424 GetClientRect(((IWineD3DClipperImpl
*)dst_surface
->clipper
)->hWnd
, &cliprect
);
3425 pos
[0].x
= dst_rect
.left
;
3426 pos
[0].y
= dst_rect
.top
;
3427 pos
[1].x
= dst_rect
.right
;
3428 pos
[1].y
= dst_rect
.bottom
;
3429 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl
*)dst_surface
->clipper
)->hWnd
, pos
, 2);
3431 if(pos
[0].x
!= cliprect
.left
|| pos
[0].y
!= cliprect
.top
||
3432 pos
[1].x
!= cliprect
.right
|| pos
[1].y
!= cliprect
.bottom
)
3434 TRACE("No, dest rectangle doesn't match(clipper)\n");
3435 TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect
));
3436 TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect
));
3440 else if (dst_rect
.left
|| dst_rect
.top
3441 || dst_rect
.right
!= dst_surface
->currentDesc
.Width
3442 || dst_rect
.bottom
!= dst_surface
->currentDesc
.Height
)
3444 TRACE("No, dest rectangle doesn't match(surface size)\n");
3450 /* These flags are unimportant for the flag check, remove them */
3451 if((Flags
& ~(WINEDDBLT_DONOTWAIT
| WINEDDBLT_WAIT
)) == 0) {
3452 WINED3DSWAPEFFECT orig_swap
= dstSwapchain
->presentParms
.SwapEffect
;
3454 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3455 * take very long, while a flip is fast.
3456 * This applies to Half-Life, which does such Blts every time it finished
3457 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3458 * menu. This is also used by all apps when they do windowed rendering
3460 * The problem is that flipping is not really the same as copying. After a
3461 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3462 * untouched. Therefore it's necessary to override the swap effect
3463 * and to set it back after the flip.
3465 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3469 dstSwapchain
->presentParms
.SwapEffect
= WINED3DSWAPEFFECT_COPY
;
3470 dstSwapchain
->presentParms
.PresentationInterval
= WINED3DPRESENT_INTERVAL_IMMEDIATE
;
3472 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3473 IWineD3DSwapChain_Present((IWineD3DSwapChain
*)dstSwapchain
,
3474 NULL
, NULL
, dstSwapchain
->win_handle
, NULL
, 0);
3476 dstSwapchain
->presentParms
.SwapEffect
= orig_swap
;
3483 TRACE("Unsupported blit between buffers on the same swapchain\n");
3484 return WINED3DERR_INVALIDCALL
;
3485 } else if(dstSwapchain
&& dstSwapchain
== srcSwapchain
) {
3486 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3487 return WINED3DERR_INVALIDCALL
;
3488 } else if(dstSwapchain
&& srcSwapchain
) {
3489 FIXME("Implement hardware blit between two different swapchains\n");
3490 return WINED3DERR_INVALIDCALL
;
3492 else if (dstSwapchain
)
3494 /* Handled with regular texture -> swapchain blit */
3495 if (src_surface
== device
->render_targets
[0])
3496 TRACE("Blit from active render target to a swapchain\n");
3498 else if (srcSwapchain
&& dst_surface
== device
->render_targets
[0])
3500 FIXME("Implement blit from a swapchain to the active render target\n");
3501 return WINED3DERR_INVALIDCALL
;
3504 if ((srcSwapchain
|| src_surface
== device
->render_targets
[0]) && !dstSwapchain
)
3506 /* Blit from render target to texture */
3509 /* P8 read back is not implemented */
3510 if (src_surface
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
3511 || dst_surface
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
)
3513 TRACE("P8 read back not supported by frame buffer to texture blit\n");
3514 return WINED3DERR_INVALIDCALL
;
3517 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3518 TRACE("Color keying not supported by frame buffer to texture blit\n");
3519 return WINED3DERR_INVALIDCALL
;
3520 /* Destination color key is checked above */
3523 if(dst_rect
.right
- dst_rect
.left
!= src_rect
.right
- src_rect
.left
) {
3529 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3530 * flip the image nor scale it.
3532 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3533 * -> If the app wants a image width an unscaled width, copy it line per line
3534 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3535 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3536 * back buffer. This is slower than reading line per line, thus not used for flipping
3537 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3540 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3541 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3544 if (fbo_blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3545 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
, src_surface
->resource
.format_desc
,
3546 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
, dst_surface
->resource
.format_desc
))
3548 surface_blt_fbo(device
, Filter
,
3549 src_surface
, SFLAG_INDRAWABLE
, &src_rect
,
3550 dst_surface
, SFLAG_INDRAWABLE
, &dst_rect
);
3551 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
3553 else if (!stretchx
|| dst_rect
.right
- dst_rect
.left
> src_surface
->currentDesc
.Width
3554 || dst_rect
.bottom
- dst_rect
.top
> src_surface
->currentDesc
.Height
)
3556 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3557 fb_copy_to_texture_direct(dst_surface
, src_surface
, &src_rect
, &dst_rect
, Filter
);
3559 TRACE("Using hardware stretching to flip / stretch the texture\n");
3560 fb_copy_to_texture_hwstretch(dst_surface
, src_surface
, &src_rect
, &dst_rect
, Filter
);
3563 if (!(dst_surface
->Flags
& SFLAG_DONOTFREE
))
3565 HeapFree(GetProcessHeap(), 0, dst_surface
->resource
.heapMemory
);
3566 dst_surface
->resource
.allocatedMemory
= NULL
;
3567 dst_surface
->resource
.heapMemory
= NULL
;
3571 dst_surface
->Flags
&= ~SFLAG_INSYSMEM
;
3576 else if (src_surface
)
3578 /* Blit from offscreen surface to render target */
3579 DWORD oldCKeyFlags
= src_surface
->CKeyFlags
;
3580 WINEDDCOLORKEY oldBltCKey
= src_surface
->SrcBltCKey
;
3581 struct wined3d_context
*context
;
3583 TRACE("Blt from surface %p to rendertarget %p\n", src_surface
, dst_surface
);
3585 if (!(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
))
3586 && fbo_blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3587 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
,
3588 src_surface
->resource
.format_desc
,
3589 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3590 dst_surface
->resource
.format_desc
))
3592 TRACE("Using surface_blt_fbo.\n");
3593 /* The source is always a texture, but never the currently active render target, and the texture
3594 * contents are never upside down. */
3595 surface_blt_fbo(device
, Filter
,
3596 src_surface
, SFLAG_INDRAWABLE
, &src_rect
,
3597 dst_surface
, SFLAG_INDRAWABLE
, &dst_rect
);
3598 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
3602 if (!(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
))
3603 && arbfp_blit
.blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3604 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
,
3605 src_surface
->resource
.format_desc
,
3606 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3607 dst_surface
->resource
.format_desc
))
3609 return arbfp_blit_surface(device
, src_surface
, &src_rect
, dst_surface
, &dst_rect
, BLIT_OP_BLIT
, Filter
);
3612 /* Color keying: Check if we have to do a color keyed blt,
3613 * and if not check if a color key is activated.
3615 * Just modify the color keying parameters in the surface and restore them afterwards
3616 * The surface keeps track of the color key last used to load the opengl surface.
3617 * PreLoad will catch the change to the flags and color key and reload if necessary.
3619 if(Flags
& WINEDDBLT_KEYSRC
) {
3620 /* Use color key from surface */
3621 } else if(Flags
& WINEDDBLT_KEYSRCOVERRIDE
) {
3622 /* Use color key from DDBltFx */
3623 src_surface
->CKeyFlags
|= WINEDDSD_CKSRCBLT
;
3624 src_surface
->SrcBltCKey
= DDBltFx
->ddckSrcColorkey
;
3626 /* Do not use color key */
3627 src_surface
->CKeyFlags
&= ~WINEDDSD_CKSRCBLT
;
3630 /* Now load the surface */
3631 surface_internal_preload(src_surface
, SRGB_RGB
);
3633 /* Activate the destination context, set it up for blitting */
3634 context
= context_acquire(device
, dst_surface
);
3635 context_apply_blit_state(context
, device
);
3637 if (dstSwapchain
&& dst_surface
== dstSwapchain
->front_buffer
)
3638 surface_translate_frontbuffer_coords(dst_surface
, context
->win_handle
, &dst_rect
);
3640 if (!device
->blitter
->blit_supported(&device
->adapter
->gl_info
, BLIT_OP_BLIT
,
3641 &src_rect
, src_surface
->resource
.usage
, src_surface
->resource
.pool
, src_surface
->resource
.format_desc
,
3642 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
, dst_surface
->resource
.format_desc
))
3644 FIXME("Unsupported blit operation falling back to software\n");
3645 return WINED3DERR_INVALIDCALL
;
3648 device
->blitter
->set_shader((IWineD3DDevice
*)device
, src_surface
);
3652 /* This is for color keying */
3653 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3654 glEnable(GL_ALPHA_TEST
);
3655 checkGLcall("glEnable(GL_ALPHA_TEST)");
3657 /* When the primary render target uses P8, the alpha component contains the palette index.
3658 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3659 * should be masked away have alpha set to 0. */
3660 if (primary_render_target_is_p8(device
))
3661 glAlphaFunc(GL_NOTEQUAL
, (float)src_surface
->SrcBltCKey
.dwColorSpaceLowValue
/ 256.0f
);
3663 glAlphaFunc(GL_NOTEQUAL
, 0.0f
);
3664 checkGLcall("glAlphaFunc");
3666 glDisable(GL_ALPHA_TEST
);
3667 checkGLcall("glDisable(GL_ALPHA_TEST)");
3670 /* Draw a textured quad
3672 draw_textured_quad(src_surface
, &src_rect
, &dst_rect
, Filter
);
3674 if(Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYSRCOVERRIDE
)) {
3675 glDisable(GL_ALPHA_TEST
);
3676 checkGLcall("glDisable(GL_ALPHA_TEST)");
3679 /* Restore the color key parameters */
3680 src_surface
->CKeyFlags
= oldCKeyFlags
;
3681 src_surface
->SrcBltCKey
= oldBltCKey
;
3685 /* Leave the opengl state valid for blitting */
3686 device
->blitter
->unset_shader((IWineD3DDevice
*)device
);
3688 if (wined3d_settings
.strict_draw_ordering
|| (dstSwapchain
3689 && (dst_surface
== dstSwapchain
->front_buffer
3690 || dstSwapchain
->num_contexts
> 1)))
3691 wglFlush(); /* Flush to ensure ordering across contexts. */
3693 context_release(context
);
3695 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3696 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3699 surface_modify_location(dst_surface
, SFLAG_INDRAWABLE
, TRUE
);
3703 /* Source-Less Blit to render target */
3704 if (Flags
& WINEDDBLT_COLORFILL
) {
3707 TRACE("Colorfill\n");
3709 /* The color as given in the Blt function is in the format of the frame-buffer...
3710 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3712 if (!surface_convert_color_to_argb(dst_surface
, DDBltFx
->u5
.dwFillColor
, &color
))
3714 /* The color conversion function already prints an error, so need to do it here */
3715 return WINED3DERR_INVALIDCALL
;
3718 if (ffp_blit
.blit_supported(&device
->adapter
->gl_info
, BLIT_OP_COLOR_FILL
,
3720 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3721 dst_surface
->resource
.format_desc
))
3723 return ffp_blit
.color_fill(device
, dst_surface
, &dst_rect
, color
);
3725 else if (cpu_blit
.blit_supported(&device
->adapter
->gl_info
, BLIT_OP_COLOR_FILL
,
3727 &dst_rect
, dst_surface
->resource
.usage
, dst_surface
->resource
.pool
,
3728 dst_surface
->resource
.format_desc
))
3730 return cpu_blit
.color_fill(device
, dst_surface
, &dst_rect
, color
);
3732 return WINED3DERR_INVALIDCALL
;
3736 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3737 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3738 return WINED3DERR_INVALIDCALL
;
3741 static HRESULT
IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl
*This
, const RECT
*DestRect
,
3742 IWineD3DSurface
*src_surface
, const RECT
*src_rect
, DWORD Flags
, const WINEDDBLTFX
*DDBltFx
)
3744 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3747 if (Flags
& WINEDDBLT_DEPTHFILL
) {
3748 switch(This
->resource
.format_desc
->format
)
3750 case WINED3DFMT_D16_UNORM
:
3751 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x0000ffff;
3753 case WINED3DFMT_S1_UINT_D15_UNORM
:
3754 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x00007fff;
3756 case WINED3DFMT_D24_UNORM_S8_UINT
:
3757 case WINED3DFMT_X8D24_UNORM
:
3758 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0x00ffffff;
3760 case WINED3DFMT_D32_UNORM
:
3761 depth
= (float) DDBltFx
->u5
.dwFillDepth
/ (float) 0xffffffff;
3765 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This
->resource
.format_desc
->format
));
3768 return IWineD3DDevice_Clear((IWineD3DDevice
*)device
, DestRect
? 1 : 0, (const WINED3DRECT
*)DestRect
,
3769 WINED3DCLEAR_ZBUFFER
, 0x00000000, depth
, 0x00000000);
3772 FIXME("(%p): Unsupp depthstencil blit\n", This
);
3773 return WINED3DERR_INVALIDCALL
;
3776 static HRESULT WINAPI
IWineD3DSurfaceImpl_Blt(IWineD3DSurface
*iface
, const RECT
*DestRect
,
3777 IWineD3DSurface
*src_surface
, const RECT
*SrcRect
, DWORD Flags
,
3778 const WINEDDBLTFX
*DDBltFx
, WINED3DTEXTUREFILTERTYPE Filter
)
3780 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
3781 IWineD3DSurfaceImpl
*src
= (IWineD3DSurfaceImpl
*)src_surface
;
3782 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3784 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3785 iface
, wine_dbgstr_rect(DestRect
), src_surface
, wine_dbgstr_rect(SrcRect
),
3786 Flags
, DDBltFx
, debug_d3dtexturefiltertype(Filter
));
3787 TRACE("Usage is %s.\n", debug_d3dusage(This
->resource
.usage
));
3789 if ((This
->Flags
& SFLAG_LOCKED
) || (src
&& (src
->Flags
& SFLAG_LOCKED
)))
3791 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3792 return WINEDDERR_SURFACEBUSY
;
3795 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3796 * except depth blits, which seem to work
3798 if (This
== device
->depth_stencil
|| (src
&& src
== device
->depth_stencil
))
3800 if (device
->inScene
&& !(Flags
& WINEDDBLT_DEPTHFILL
))
3802 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3803 return WINED3DERR_INVALIDCALL
;
3805 else if (SUCCEEDED(IWineD3DSurfaceImpl_BltZ(This
, DestRect
, src_surface
, SrcRect
, Flags
, DDBltFx
)))
3807 TRACE("Z Blit override handled the blit\n");
3812 /* Special cases for RenderTargets */
3813 if ((This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3814 || (src
&& (src
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)))
3816 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This
, DestRect
, src
, SrcRect
, Flags
, DDBltFx
, Filter
)))
3820 /* For the rest call the X11 surface implementation.
3821 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3822 * other Blts are rather rare. */
3823 return IWineD3DBaseSurfaceImpl_Blt(iface
, DestRect
, src_surface
, SrcRect
, Flags
, DDBltFx
, Filter
);
3826 static HRESULT WINAPI
IWineD3DSurfaceImpl_BltFast(IWineD3DSurface
*iface
, DWORD dstx
, DWORD dsty
,
3827 IWineD3DSurface
*src_surface
, const RECT
*rsrc
, DWORD trans
)
3829 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
3830 IWineD3DSurfaceImpl
*src
= (IWineD3DSurfaceImpl
*)src_surface
;
3831 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
3833 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3834 iface
, dstx
, dsty
, src_surface
, wine_dbgstr_rect(rsrc
), trans
);
3836 if ((This
->Flags
& SFLAG_LOCKED
) || (src
->Flags
& SFLAG_LOCKED
))
3838 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3839 return WINEDDERR_SURFACEBUSY
;
3842 if (device
->inScene
&& (This
== device
->depth_stencil
|| src
== device
->depth_stencil
))
3844 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3845 return WINED3DERR_INVALIDCALL
;
3848 /* Special cases for RenderTargets */
3849 if ((This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3850 || (src
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
))
3853 RECT SrcRect
, DstRect
;
3856 surface_get_rect(src
, rsrc
, &SrcRect
);
3858 DstRect
.left
= dstx
;
3860 DstRect
.right
= dstx
+ SrcRect
.right
- SrcRect
.left
;
3861 DstRect
.bottom
= dsty
+ SrcRect
.bottom
- SrcRect
.top
;
3863 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3864 if(trans
& WINEDDBLTFAST_SRCCOLORKEY
)
3865 Flags
|= WINEDDBLT_KEYSRC
;
3866 if(trans
& WINEDDBLTFAST_DESTCOLORKEY
)
3867 Flags
|= WINEDDBLT_KEYDEST
;
3868 if(trans
& WINEDDBLTFAST_WAIT
)
3869 Flags
|= WINEDDBLT_WAIT
;
3870 if(trans
& WINEDDBLTFAST_DONOTWAIT
)
3871 Flags
|= WINEDDBLT_DONOTWAIT
;
3873 if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This
,
3874 &DstRect
, src
, &SrcRect
, Flags
, NULL
, WINED3DTEXF_POINT
)))
3878 return IWineD3DBaseSurfaceImpl_BltFast(iface
, dstx
, dsty
, src_surface
, rsrc
, trans
);
3881 static HRESULT WINAPI
IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface
*iface
)
3883 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3885 IWineD3DPaletteImpl
*pal
= This
->palette
;
3887 TRACE("(%p)\n", This
);
3889 if (!pal
) return WINED3D_OK
;
3891 if (This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
3892 || This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT_A8_UNORM
)
3894 if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3896 /* Make sure the texture is up to date. This call doesn't do
3897 * anything if the texture is already up to date. */
3898 surface_load_location(This
, SFLAG_INTEXTURE
, NULL
);
3900 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3901 surface_modify_location(This
, SFLAG_INDRAWABLE
, FALSE
);
3905 if (!(This
->Flags
& SFLAG_INSYSMEM
))
3907 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
3908 surface_load_location(This
, SFLAG_INSYSMEM
, NULL
);
3910 TRACE("Dirtifying surface\n");
3911 surface_modify_location(This
, SFLAG_INSYSMEM
, TRUE
);
3915 if(This
->Flags
& SFLAG_DIBSECTION
) {
3916 TRACE("(%p): Updating the hdc's palette\n", This
);
3917 for (n
=0; n
<256; n
++) {
3918 col
[n
].rgbRed
= pal
->palents
[n
].peRed
;
3919 col
[n
].rgbGreen
= pal
->palents
[n
].peGreen
;
3920 col
[n
].rgbBlue
= pal
->palents
[n
].peBlue
;
3921 col
[n
].rgbReserved
= 0;
3923 SetDIBColorTable(This
->hDC
, 0, 256, col
);
3926 /* Propagate the changes to the drawable when we have a palette. */
3927 if (This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
3928 surface_load_location(This
, SFLAG_INDRAWABLE
, NULL
);
3933 static HRESULT WINAPI
IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface
*iface
) {
3934 /** Check against the maximum texture sizes supported by the video card **/
3935 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
3936 const struct wined3d_gl_info
*gl_info
= &This
->resource
.device
->adapter
->gl_info
;
3937 unsigned int pow2Width
, pow2Height
;
3939 This
->texture_name
= 0;
3940 This
->texture_target
= GL_TEXTURE_2D
;
3942 /* Non-power2 support */
3943 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
] || gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
3945 pow2Width
= This
->currentDesc
.Width
;
3946 pow2Height
= This
->currentDesc
.Height
;
3950 /* Find the nearest pow2 match */
3951 pow2Width
= pow2Height
= 1;
3952 while (pow2Width
< This
->currentDesc
.Width
) pow2Width
<<= 1;
3953 while (pow2Height
< This
->currentDesc
.Height
) pow2Height
<<= 1;
3955 This
->pow2Width
= pow2Width
;
3956 This
->pow2Height
= pow2Height
;
3958 if (pow2Width
> This
->currentDesc
.Width
|| pow2Height
> This
->currentDesc
.Height
) {
3959 /** TODO: add support for non power two compressed textures **/
3960 if (This
->resource
.format_desc
->Flags
& WINED3DFMT_FLAG_COMPRESSED
)
3962 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3963 This
, This
->currentDesc
.Width
, This
->currentDesc
.Height
);
3964 return WINED3DERR_NOTAVAILABLE
;
3968 if(pow2Width
!= This
->currentDesc
.Width
||
3969 pow2Height
!= This
->currentDesc
.Height
) {
3970 This
->Flags
|= SFLAG_NONPOW2
;
3973 TRACE("%p\n", This
);
3974 if ((This
->pow2Width
> gl_info
->limits
.texture_size
|| This
->pow2Height
> gl_info
->limits
.texture_size
)
3975 && !(This
->resource
.usage
& (WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
)))
3977 /* one of three options
3978 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)
3979 2: Set the texture to the maximum size (bad idea)
3980 3: WARN and return WINED3DERR_NOTAVAILABLE;
3981 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.
3983 if(This
->resource
.pool
== WINED3DPOOL_DEFAULT
|| This
->resource
.pool
== WINED3DPOOL_MANAGED
)
3985 WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This
);
3986 return WINED3DERR_NOTAVAILABLE
;
3989 /* We should never use this surface in combination with OpenGL! */
3990 TRACE("(%p) Creating an oversized surface: %ux%u\n", This
, This
->pow2Width
, This
->pow2Height
);
3994 /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3995 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3996 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3998 if (This
->Flags
& SFLAG_NONPOW2
&& gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
3999 && !(This
->resource
.format_desc
->format
== WINED3DFMT_P8_UINT
4000 && gl_info
->supported
[EXT_PALETTED_TEXTURE
]
4001 && wined3d_settings
.rendertargetlock_mode
== RTL_READTEX
))
4003 This
->texture_target
= GL_TEXTURE_RECTANGLE_ARB
;
4004 This
->pow2Width
= This
->currentDesc
.Width
;
4005 This
->pow2Height
= This
->currentDesc
.Height
;
4006 This
->Flags
&= ~(SFLAG_NONPOW2
| SFLAG_NORMCOORD
);
4010 switch (wined3d_settings
.offscreen_rendering_mode
)
4013 This
->get_drawable_size
= get_drawable_size_fbo
;
4016 case ORM_BACKBUFFER
:
4017 This
->get_drawable_size
= get_drawable_size_backbuffer
;
4021 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
4022 return WINED3DERR_INVALIDCALL
;
4025 This
->Flags
|= SFLAG_INSYSMEM
;
4030 /* GL locking is done by the caller */
4031 static void surface_depth_blt(IWineD3DSurfaceImpl
*This
, const struct wined3d_gl_info
*gl_info
,
4032 GLuint texture
, GLsizei w
, GLsizei h
, GLenum target
)
4034 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4035 GLint compare_mode
= GL_NONE
;
4036 struct blt_info info
;
4037 GLint old_binding
= 0;
4039 glPushAttrib(GL_ENABLE_BIT
| GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
| GL_VIEWPORT_BIT
);
4041 glDisable(GL_CULL_FACE
);
4042 glDisable(GL_BLEND
);
4043 glDisable(GL_ALPHA_TEST
);
4044 glDisable(GL_SCISSOR_TEST
);
4045 glDisable(GL_STENCIL_TEST
);
4046 glEnable(GL_DEPTH_TEST
);
4047 glDepthFunc(GL_ALWAYS
);
4048 glDepthMask(GL_TRUE
);
4049 glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
4050 glViewport(0, 0, w
, h
);
4052 surface_get_blt_info(target
, NULL
, w
, h
, &info
);
4053 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
4054 glGetIntegerv(info
.binding
, &old_binding
);
4055 glBindTexture(info
.bind_target
, texture
);
4056 if (gl_info
->supported
[ARB_SHADOW
])
4058 glGetTexParameteriv(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, &compare_mode
);
4059 if (compare_mode
!= GL_NONE
) glTexParameteri(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
4062 device
->shader_backend
->shader_select_depth_blt((IWineD3DDevice
*)device
,
4063 info
.tex_type
, &This
->ds_current_size
);
4065 glBegin(GL_TRIANGLE_STRIP
);
4066 glTexCoord3fv(info
.coords
[0]);
4067 glVertex2f(-1.0f
, -1.0f
);
4068 glTexCoord3fv(info
.coords
[1]);
4069 glVertex2f(1.0f
, -1.0f
);
4070 glTexCoord3fv(info
.coords
[2]);
4071 glVertex2f(-1.0f
, 1.0f
);
4072 glTexCoord3fv(info
.coords
[3]);
4073 glVertex2f(1.0f
, 1.0f
);
4076 if (compare_mode
!= GL_NONE
) glTexParameteri(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, compare_mode
);
4077 glBindTexture(info
.bind_target
, old_binding
);
4081 device
->shader_backend
->shader_deselect_depth_blt((IWineD3DDevice
*)device
);
4084 void surface_modify_ds_location(IWineD3DSurfaceImpl
*surface
,
4085 DWORD location
, UINT w
, UINT h
)
4087 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface
, location
, w
, h
);
4089 if (location
& ~SFLAG_DS_LOCATIONS
)
4090 FIXME("Invalid location (%#x) specified.\n", location
);
4092 surface
->ds_current_size
.cx
= w
;
4093 surface
->ds_current_size
.cy
= h
;
4094 surface
->Flags
&= ~SFLAG_DS_LOCATIONS
;
4095 surface
->Flags
|= location
;
4098 /* Context activation is done by the caller. */
4099 void surface_load_ds_location(IWineD3DSurfaceImpl
*surface
, struct wined3d_context
*context
, DWORD location
)
4101 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
4102 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4104 TRACE("surface %p, new location %#x.\n", surface
, location
);
4106 /* TODO: Make this work for modes other than FBO */
4107 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return;
4109 if (!(surface
->Flags
& location
))
4111 surface
->ds_current_size
.cx
= 0;
4112 surface
->ds_current_size
.cy
= 0;
4115 if (surface
->ds_current_size
.cx
== surface
->currentDesc
.Width
4116 && surface
->ds_current_size
.cy
== surface
->currentDesc
.Height
)
4118 TRACE("Location (%#x) is already up to date.\n", location
);
4122 if (surface
->current_renderbuffer
)
4124 FIXME("Not supported with fixed up depth stencil.\n");
4128 if (!(surface
->Flags
& SFLAG_LOCATIONS
))
4130 FIXME("No up to date depth stencil location.\n");
4131 surface
->Flags
|= location
;
4135 if (location
== SFLAG_DS_OFFSCREEN
)
4137 GLint old_binding
= 0;
4140 TRACE("Copying onscreen depth buffer to depth texture.\n");
4144 if (!device
->depth_blt_texture
)
4146 glGenTextures(1, &device
->depth_blt_texture
);
4149 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4150 * directly on the FBO texture. That's because we need to flip. */
4151 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4152 if (surface
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
4154 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
4155 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
4159 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
4160 bind_target
= GL_TEXTURE_2D
;
4162 glBindTexture(bind_target
, device
->depth_blt_texture
);
4163 glCopyTexImage2D(bind_target
, surface
->texture_level
, surface
->resource
.format_desc
->glInternal
,
4164 0, 0, surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, 0);
4165 glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4166 glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4167 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
4168 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
4169 glTexParameteri(bind_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
4170 glTexParameteri(bind_target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
4171 glBindTexture(bind_target
, old_binding
);
4173 /* Setup the destination */
4174 if (!device
->depth_blt_rb
)
4176 gl_info
->fbo_ops
.glGenRenderbuffers(1, &device
->depth_blt_rb
);
4177 checkGLcall("glGenRenderbuffersEXT");
4179 if (device
->depth_blt_rb_w
!= surface
->currentDesc
.Width
4180 || device
->depth_blt_rb_h
!= surface
->currentDesc
.Height
)
4182 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, device
->depth_blt_rb
);
4183 checkGLcall("glBindRenderbufferEXT");
4184 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, GL_RGBA8
,
4185 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
);
4186 checkGLcall("glRenderbufferStorageEXT");
4187 device
->depth_blt_rb_w
= surface
->currentDesc
.Width
;
4188 device
->depth_blt_rb_h
= surface
->currentDesc
.Height
;
4191 context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->dst_fbo
);
4192 gl_info
->fbo_ops
.glFramebufferRenderbuffer(GL_FRAMEBUFFER
,
4193 GL_COLOR_ATTACHMENT0
, GL_RENDERBUFFER
, device
->depth_blt_rb
);
4194 checkGLcall("glFramebufferRenderbufferEXT");
4195 context_attach_depth_stencil_fbo(context
, GL_FRAMEBUFFER
, surface
, FALSE
);
4197 /* Do the actual blit */
4198 surface_depth_blt(surface
, gl_info
, device
->depth_blt_texture
,
4199 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, bind_target
);
4200 checkGLcall("depth_blt");
4202 if (context
->current_fbo
) context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->current_fbo
->id
);
4203 else context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4207 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
4209 else if (location
== SFLAG_DS_ONSCREEN
)
4211 TRACE("Copying depth texture to onscreen depth buffer.\n");
4215 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
4216 surface_depth_blt(surface
, gl_info
, surface
->texture_name
,
4217 surface
->currentDesc
.Width
, surface
->currentDesc
.Height
, surface
->texture_target
);
4218 checkGLcall("depth_blt");
4220 if (context
->current_fbo
) context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->current_fbo
->id
);
4224 if (wined3d_settings
.strict_draw_ordering
) wglFlush(); /* Flush to ensure ordering across contexts. */
4228 ERR("Invalid location (%#x) specified.\n", location
);
4231 surface
->Flags
|= location
;
4232 surface
->ds_current_size
.cx
= surface
->currentDesc
.Width
;
4233 surface
->ds_current_size
.cy
= surface
->currentDesc
.Height
;
4236 void surface_modify_location(IWineD3DSurfaceImpl
*surface
, DWORD flag
, BOOL persistent
)
4238 IWineD3DBaseTexture
*texture
;
4239 IWineD3DSurfaceImpl
*overlay
;
4241 TRACE("surface %p, location %s, persitent %#x.\n",
4242 surface
, debug_surflocation(flag
), persistent
);
4244 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4246 if (surface_is_offscreen(surface
))
4248 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4249 if (flag
& (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
)) flag
|= (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
);
4253 TRACE("Surface %p is an onscreen surface.\n", surface
);
4259 if (((surface
->Flags
& SFLAG_INTEXTURE
) && !(flag
& SFLAG_INTEXTURE
))
4260 || ((surface
->Flags
& SFLAG_INSRGBTEX
) && !(flag
& SFLAG_INSRGBTEX
)))
4262 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
4263 &IID_IWineD3DBaseTexture
, (void **)&texture
)))
4265 TRACE("Passing to container.\n");
4266 IWineD3DBaseTexture_SetDirty(texture
, TRUE
);
4267 IWineD3DBaseTexture_Release(texture
);
4270 surface
->Flags
&= ~SFLAG_LOCATIONS
;
4271 surface
->Flags
|= flag
;
4273 /* Redraw emulated overlays, if any */
4274 if (flag
& SFLAG_INDRAWABLE
&& !list_empty(&surface
->overlays
))
4276 LIST_FOR_EACH_ENTRY(overlay
, &surface
->overlays
, IWineD3DSurfaceImpl
, overlay_entry
)
4278 IWineD3DSurface_DrawOverlay((IWineD3DSurface
*)overlay
);
4284 if ((surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
)) && (flag
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
)))
4286 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
4287 &IID_IWineD3DBaseTexture
, (void **)&texture
)))
4289 TRACE("Passing to container\n");
4290 IWineD3DBaseTexture_SetDirty(texture
, TRUE
);
4291 IWineD3DBaseTexture_Release(texture
);
4294 surface
->Flags
&= ~flag
;
4297 if (!(surface
->Flags
& SFLAG_LOCATIONS
))
4299 ERR("Surface %p does not have any up to date location.\n", surface
);
4303 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl
*This
, const RECT
*rect_in
)
4305 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
4306 IWineD3DSwapChainImpl
*swapchain
;
4307 struct wined3d_context
*context
;
4308 RECT src_rect
, dst_rect
;
4310 surface_get_rect(This
, rect_in
, &src_rect
);
4312 context
= context_acquire(device
, This
);
4313 context_apply_blit_state(context
, device
);
4314 if (context
->render_offscreen
)
4316 dst_rect
.left
= src_rect
.left
;
4317 dst_rect
.right
= src_rect
.right
;
4318 dst_rect
.top
= src_rect
.bottom
;
4319 dst_rect
.bottom
= src_rect
.top
;
4323 dst_rect
= src_rect
;
4326 if ((This
->Flags
& SFLAG_SWAPCHAIN
) && This
== ((IWineD3DSwapChainImpl
*)This
->container
)->front_buffer
)
4327 surface_translate_frontbuffer_coords(This
, context
->win_handle
, &dst_rect
);
4329 device
->blitter
->set_shader((IWineD3DDevice
*) device
, This
);
4332 draw_textured_quad(This
, &src_rect
, &dst_rect
, WINED3DTEXF_POINT
);
4335 device
->blitter
->unset_shader((IWineD3DDevice
*) device
);
4337 swapchain
= (This
->Flags
& SFLAG_SWAPCHAIN
) ? (IWineD3DSwapChainImpl
*)This
->container
: NULL
;
4338 if (wined3d_settings
.strict_draw_ordering
|| (swapchain
4339 && (This
== swapchain
->front_buffer
|| swapchain
->num_contexts
> 1)))
4340 wglFlush(); /* Flush to ensure ordering across contexts. */
4342 context_release(context
);
4345 HRESULT
surface_load_location(IWineD3DSurfaceImpl
*surface
, DWORD flag
, const RECT
*rect
)
4347 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
4348 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4349 BOOL drawable_read_ok
= surface_is_offscreen(surface
);
4350 struct wined3d_format_desc desc
;
4351 CONVERT_TYPES convert
;
4352 int width
, pitch
, outpitch
;
4354 BOOL in_fbo
= FALSE
;
4356 TRACE("surface %p, location %s, rect %s.\n", surface
, debug_surflocation(flag
), wine_dbgstr_rect(rect
));
4358 if (surface
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
)
4360 if (flag
== SFLAG_INTEXTURE
)
4362 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4363 surface_load_ds_location(surface
, context
, SFLAG_DS_OFFSCREEN
);
4364 context_release(context
);
4369 FIXME("Unimplemented location %#x for depth/stencil buffers.\n", flag
);
4370 return WINED3DERR_INVALIDCALL
;
4374 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4376 if (surface_is_offscreen(surface
))
4378 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4379 * Prefer SFLAG_INTEXTURE. */
4380 if (flag
== SFLAG_INDRAWABLE
) flag
= SFLAG_INTEXTURE
;
4381 drawable_read_ok
= FALSE
;
4386 TRACE("Surface %p is an onscreen surface.\n", surface
);
4390 if (surface
->Flags
& flag
)
4392 TRACE("Location already up to date\n");
4396 if (!(surface
->Flags
& SFLAG_LOCATIONS
))
4398 ERR("Surface %p does not have any up to date location.\n", surface
);
4399 surface
->Flags
|= SFLAG_LOST
;
4400 return WINED3DERR_DEVICELOST
;
4403 if (flag
== SFLAG_INSYSMEM
)
4405 surface_prepare_system_memory(surface
);
4407 /* Download the surface to system memory */
4408 if (surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSRGBTEX
))
4410 struct wined3d_context
*context
= NULL
;
4412 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
4414 surface_bind_and_dirtify(surface
, !(surface
->Flags
& SFLAG_INTEXTURE
));
4415 surface_download_data(surface
, gl_info
);
4417 if (context
) context_release(context
);
4421 /* Note: It might be faster to download into a texture first. */
4422 read_from_framebuffer(surface
, rect
, surface
->resource
.allocatedMemory
,
4423 IWineD3DSurface_GetPitch((IWineD3DSurface
*)surface
));
4426 else if (flag
== SFLAG_INDRAWABLE
)
4428 if (surface
->Flags
& SFLAG_INTEXTURE
)
4430 surface_blt_to_drawable(surface
, rect
);
4435 if ((surface
->Flags
& SFLAG_LOCATIONS
) == SFLAG_INSRGBTEX
)
4437 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4438 * values, otherwise we get incorrect values in the target. For now go the slow way
4439 * via a system memory copy
4441 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4444 d3dfmt_get_conv(surface
, FALSE
/* We need color keying */,
4445 FALSE
/* We won't use textures */, &desc
, &convert
);
4447 /* The width is in 'length' not in bytes */
4448 width
= surface
->currentDesc
.Width
;
4449 pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*)surface
);
4451 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4452 * but it isn't set (yet) in all cases it is getting called. */
4453 if ((convert
!= NO_CONVERSION
) && (surface
->Flags
& SFLAG_PBO
))
4455 struct wined3d_context
*context
= NULL
;
4457 TRACE("Removing the pbo attached to surface %p.\n", surface
);
4459 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
4460 surface_remove_pbo(surface
, gl_info
);
4461 if (context
) context_release(context
);
4464 if ((convert
!= NO_CONVERSION
) && surface
->resource
.allocatedMemory
)
4466 int height
= surface
->currentDesc
.Height
;
4467 byte_count
= desc
.conv_byte_count
;
4469 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4470 outpitch
= width
* byte_count
;
4471 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4473 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4475 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4476 return WINED3DERR_OUTOFVIDEOMEMORY
;
4478 d3dfmt_convert_surface(surface
->resource
.allocatedMemory
, mem
, pitch
,
4479 width
, height
, outpitch
, convert
, surface
);
4481 surface
->Flags
|= SFLAG_CONVERTED
;
4485 surface
->Flags
&= ~SFLAG_CONVERTED
;
4486 mem
= surface
->resource
.allocatedMemory
;
4487 byte_count
= desc
.byte_count
;
4490 flush_to_framebuffer_drawpixels(surface
, desc
.glFormat
, desc
.glType
, byte_count
, mem
);
4492 /* Don't delete PBO memory */
4493 if ((mem
!= surface
->resource
.allocatedMemory
) && !(surface
->Flags
& SFLAG_PBO
))
4494 HeapFree(GetProcessHeap(), 0, mem
);
4497 else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4499 const DWORD attach_flags
= WINED3DFMT_FLAG_FBO_ATTACHABLE
| WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB
;
4501 if (drawable_read_ok
&& (surface
->Flags
& SFLAG_INDRAWABLE
))
4503 read_from_framebuffer_texture(surface
, flag
== SFLAG_INSRGBTEX
);
4505 else if (surface
->Flags
& (SFLAG_INSRGBTEX
| SFLAG_INTEXTURE
)
4506 && (surface
->resource
.format_desc
->Flags
& attach_flags
) == attach_flags
4507 && fbo_blit_supported(gl_info
, BLIT_OP_BLIT
,
4508 NULL
, surface
->resource
.usage
, surface
->resource
.pool
, surface
->resource
.format_desc
,
4509 NULL
, surface
->resource
.usage
, surface
->resource
.pool
, surface
->resource
.format_desc
))
4511 DWORD src_location
= flag
== SFLAG_INSRGBTEX
? SFLAG_INTEXTURE
: SFLAG_INSRGBTEX
;
4512 RECT rect
= {0, 0, surface
->currentDesc
.Width
, surface
->currentDesc
.Height
};
4514 surface_blt_fbo(surface
->resource
.device
, WINED3DTEXF_POINT
,
4515 surface
, src_location
, &rect
, surface
, flag
, &rect
);
4519 /* Upload from system memory */
4520 BOOL srgb
= flag
== SFLAG_INSRGBTEX
;
4521 struct wined3d_context
*context
= NULL
;
4523 d3dfmt_get_conv(surface
, TRUE
/* We need color keying */,
4524 TRUE
/* We will use textures */, &desc
, &convert
);
4528 if ((surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INSYSMEM
)) == SFLAG_INTEXTURE
)
4530 /* Performance warning... */
4531 FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface
);
4532 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4537 if ((surface
->Flags
& (SFLAG_INSRGBTEX
| SFLAG_INSYSMEM
)) == SFLAG_INSRGBTEX
)
4539 /* Performance warning... */
4540 FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface
);
4541 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4544 if (!(surface
->Flags
& SFLAG_INSYSMEM
))
4546 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4547 /* Lets hope we get it from somewhere... */
4548 surface_load_location(surface
, SFLAG_INSYSMEM
, rect
);
4551 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
4553 surface_prepare_texture(surface
, gl_info
, srgb
);
4554 surface_bind_and_dirtify(surface
, srgb
);
4556 if (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
)
4558 surface
->Flags
|= SFLAG_GLCKEY
;
4559 surface
->glCKey
= surface
->SrcBltCKey
;
4561 else surface
->Flags
&= ~SFLAG_GLCKEY
;
4563 /* The width is in 'length' not in bytes */
4564 width
= surface
->currentDesc
.Width
;
4565 pitch
= IWineD3DSurface_GetPitch((IWineD3DSurface
*)surface
);
4567 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4568 * but it isn't set (yet) in all cases it is getting called. */
4569 if ((convert
!= NO_CONVERSION
|| desc
.convert
) && (surface
->Flags
& SFLAG_PBO
))
4571 TRACE("Removing the pbo attached to surface %p.\n", surface
);
4572 surface_remove_pbo(surface
, gl_info
);
4577 /* This code is entered for texture formats which need a fixup. */
4578 int height
= surface
->currentDesc
.Height
;
4580 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4581 outpitch
= width
* desc
.conv_byte_count
;
4582 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4584 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4586 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4587 if (context
) context_release(context
);
4588 return WINED3DERR_OUTOFVIDEOMEMORY
;
4590 desc
.convert(surface
->resource
.allocatedMemory
, mem
, pitch
, width
, height
);
4592 else if (convert
!= NO_CONVERSION
&& surface
->resource
.allocatedMemory
)
4594 /* This code is only entered for color keying fixups */
4595 int height
= surface
->currentDesc
.Height
;
4597 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4598 outpitch
= width
* desc
.conv_byte_count
;
4599 outpitch
= (outpitch
+ device
->surface_alignment
- 1) & ~(device
->surface_alignment
- 1);
4601 mem
= HeapAlloc(GetProcessHeap(), 0, outpitch
* height
);
4603 ERR("Out of memory %d, %d!\n", outpitch
, height
);
4604 if (context
) context_release(context
);
4605 return WINED3DERR_OUTOFVIDEOMEMORY
;
4607 d3dfmt_convert_surface(surface
->resource
.allocatedMemory
, mem
, pitch
,
4608 width
, height
, outpitch
, convert
, surface
);
4612 mem
= surface
->resource
.allocatedMemory
;
4615 /* Make sure the correct pitch is used */
4617 glPixelStorei(GL_UNPACK_ROW_LENGTH
, width
);
4620 if (mem
|| (surface
->Flags
& SFLAG_PBO
))
4621 surface_upload_data(surface
, gl_info
, &desc
, srgb
, mem
);
4623 /* Restore the default pitch */
4625 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
4628 if (context
) context_release(context
);
4630 /* Don't delete PBO memory */
4631 if ((mem
!= surface
->resource
.allocatedMemory
) && !(surface
->Flags
& SFLAG_PBO
))
4632 HeapFree(GetProcessHeap(), 0, mem
);
4636 if (!rect
) surface
->Flags
|= flag
;
4638 if (in_fbo
&& (surface
->Flags
& (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
)))
4640 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4641 surface
->Flags
|= (SFLAG_INTEXTURE
| SFLAG_INDRAWABLE
);
4647 static HRESULT WINAPI
IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface
*iface
, IWineD3DBase
*container
)
4649 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4650 IWineD3DSwapChain
*swapchain
= NULL
;
4652 /* Update the drawable size method */
4654 IWineD3DBase_QueryInterface(container
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
4657 This
->get_drawable_size
= get_drawable_size_swapchain
;
4658 IWineD3DSwapChain_Release(swapchain
);
4662 switch (wined3d_settings
.offscreen_rendering_mode
)
4665 This
->get_drawable_size
= get_drawable_size_fbo
;
4668 case ORM_BACKBUFFER
:
4669 This
->get_drawable_size
= get_drawable_size_backbuffer
;
4673 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
4674 return WINED3DERR_INVALIDCALL
;
4678 return IWineD3DBaseSurfaceImpl_SetContainer(iface
, container
);
4681 static WINED3DSURFTYPE WINAPI
IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface
*iface
) {
4682 return SURFACE_OPENGL
;
4685 static HRESULT WINAPI
IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface
*iface
) {
4686 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
4689 /* If there's no destination surface there is nothing to do */
4690 if(!This
->overlay_dest
) return WINED3D_OK
;
4692 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4693 * update the overlay. Prevent an endless recursion
4695 if(This
->overlay_dest
->Flags
& SFLAG_INOVERLAYDRAW
) {
4698 This
->overlay_dest
->Flags
|= SFLAG_INOVERLAYDRAW
;
4699 hr
= IWineD3DSurfaceImpl_Blt((IWineD3DSurface
*) This
->overlay_dest
, &This
->overlay_destrect
,
4700 iface
, &This
->overlay_srcrect
, WINEDDBLT_WAIT
,
4701 NULL
, WINED3DTEXF_LINEAR
);
4702 This
->overlay_dest
->Flags
&= ~SFLAG_INOVERLAYDRAW
;
4707 BOOL
surface_is_offscreen(IWineD3DSurfaceImpl
*surface
)
4709 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)surface
->container
;
4711 /* Not on a swapchain - must be offscreen */
4712 if (!(surface
->Flags
& SFLAG_SWAPCHAIN
)) return TRUE
;
4714 /* The front buffer is always onscreen */
4715 if (surface
== swapchain
->front_buffer
) return FALSE
;
4717 /* If the swapchain is rendered to an FBO, the backbuffer is
4718 * offscreen, otherwise onscreen */
4719 return swapchain
->render_to_fbo
;
4722 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl
=
4725 IWineD3DBaseSurfaceImpl_QueryInterface
,
4726 IWineD3DBaseSurfaceImpl_AddRef
,
4727 IWineD3DSurfaceImpl_Release
,
4728 /* IWineD3DResource */
4729 IWineD3DBaseSurfaceImpl_GetParent
,
4730 IWineD3DBaseSurfaceImpl_SetPrivateData
,
4731 IWineD3DBaseSurfaceImpl_GetPrivateData
,
4732 IWineD3DBaseSurfaceImpl_FreePrivateData
,
4733 IWineD3DBaseSurfaceImpl_SetPriority
,
4734 IWineD3DBaseSurfaceImpl_GetPriority
,
4735 IWineD3DSurfaceImpl_PreLoad
,
4736 IWineD3DSurfaceImpl_UnLoad
,
4737 IWineD3DBaseSurfaceImpl_GetType
,
4738 /* IWineD3DSurface */
4739 IWineD3DBaseSurfaceImpl_GetContainer
,
4740 IWineD3DBaseSurfaceImpl_GetDesc
,
4741 IWineD3DSurfaceImpl_LockRect
,
4742 IWineD3DSurfaceImpl_UnlockRect
,
4743 IWineD3DSurfaceImpl_GetDC
,
4744 IWineD3DSurfaceImpl_ReleaseDC
,
4745 IWineD3DSurfaceImpl_Flip
,
4746 IWineD3DSurfaceImpl_Blt
,
4747 IWineD3DBaseSurfaceImpl_GetBltStatus
,
4748 IWineD3DBaseSurfaceImpl_GetFlipStatus
,
4749 IWineD3DBaseSurfaceImpl_IsLost
,
4750 IWineD3DBaseSurfaceImpl_Restore
,
4751 IWineD3DSurfaceImpl_BltFast
,
4752 IWineD3DBaseSurfaceImpl_GetPalette
,
4753 IWineD3DBaseSurfaceImpl_SetPalette
,
4754 IWineD3DSurfaceImpl_RealizePalette
,
4755 IWineD3DBaseSurfaceImpl_SetColorKey
,
4756 IWineD3DBaseSurfaceImpl_GetPitch
,
4757 IWineD3DSurfaceImpl_SetMem
,
4758 IWineD3DBaseSurfaceImpl_SetOverlayPosition
,
4759 IWineD3DBaseSurfaceImpl_GetOverlayPosition
,
4760 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder
,
4761 IWineD3DBaseSurfaceImpl_UpdateOverlay
,
4762 IWineD3DBaseSurfaceImpl_SetClipper
,
4763 IWineD3DBaseSurfaceImpl_GetClipper
,
4765 IWineD3DSurfaceImpl_LoadTexture
,
4766 IWineD3DSurfaceImpl_BindTexture
,
4767 IWineD3DSurfaceImpl_SetContainer
,
4768 IWineD3DBaseSurfaceImpl_GetData
,
4769 IWineD3DSurfaceImpl_SetFormat
,
4770 IWineD3DSurfaceImpl_PrivateSetup
,
4771 IWineD3DSurfaceImpl_GetImplType
,
4772 IWineD3DSurfaceImpl_DrawOverlay
4775 static HRESULT
ffp_blit_alloc(IWineD3DDevice
*iface
) { return WINED3D_OK
; }
4776 /* Context activation is done by the caller. */
4777 static void ffp_blit_free(IWineD3DDevice
*iface
) { }
4779 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4780 /* Context activation is done by the caller. */
4781 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl
*surface
, const struct wined3d_gl_info
*gl_info
)
4784 BOOL colorkey_active
= (surface
->CKeyFlags
& WINEDDSD_CKSRCBLT
) ? TRUE
: FALSE
;
4786 d3dfmt_p8_init_palette(surface
, table
, colorkey_active
);
4788 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4790 GL_EXTCALL(glColorTableEXT(surface
->texture_target
, GL_RGBA
, 256, GL_RGBA
, GL_UNSIGNED_BYTE
, table
));
4794 /* Context activation is done by the caller. */
4795 static HRESULT
ffp_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
4797 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
4798 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4799 enum complex_fixup fixup
= get_complex_fixup(surface
->resource
.format_desc
->color_fixup
);
4801 /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4802 * else the surface is converted in software at upload time in LoadLocation.
4804 if(fixup
== COMPLEX_FIXUP_P8
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
])
4805 ffp_blit_p8_upload_palette(surface
, gl_info
);
4808 glEnable(surface
->texture_target
);
4809 checkGLcall("glEnable(surface->texture_target)");
4814 /* Context activation is done by the caller. */
4815 static void ffp_blit_unset(IWineD3DDevice
*iface
)
4817 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) iface
;
4818 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4821 glDisable(GL_TEXTURE_2D
);
4822 checkGLcall("glDisable(GL_TEXTURE_2D)");
4823 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
4825 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
4826 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4828 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
4830 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
4831 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4836 static BOOL
ffp_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4837 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4838 const struct wined3d_format_desc
*src_format_desc
,
4839 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4840 const struct wined3d_format_desc
*dst_format_desc
)
4842 enum complex_fixup src_fixup
;
4844 if (blit_op
== BLIT_OP_COLOR_FILL
)
4846 if (!(dst_usage
& WINED3DUSAGE_RENDERTARGET
))
4848 TRACE("Color fill not supported\n");
4855 src_fixup
= get_complex_fixup(src_format_desc
->color_fixup
);
4856 if (TRACE_ON(d3d_surface
) && TRACE_ON(d3d
))
4858 TRACE("Checking support for fixup:\n");
4859 dump_color_fixup_desc(src_format_desc
->color_fixup
);
4862 if (blit_op
!= BLIT_OP_BLIT
)
4864 TRACE("Unsupported blit_op=%d\n", blit_op
);
4868 if (!is_identity_fixup(dst_format_desc
->color_fixup
))
4870 TRACE("Destination fixups are not supported\n");
4874 if (src_fixup
== COMPLEX_FIXUP_P8
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
])
4876 TRACE("P8 fixup supported\n");
4880 /* We only support identity conversions. */
4881 if (is_identity_fixup(src_format_desc
->color_fixup
))
4887 TRACE("[FAILED]\n");
4891 static HRESULT
ffp_blit_color_fill(IWineD3DDeviceImpl
*device
,
4892 IWineD3DSurfaceImpl
*dst_surface
, const RECT
*dst_rect
, DWORD color
)
4894 const float c
[] = {D3DCOLOR_R(color
), D3DCOLOR_G(color
), D3DCOLOR_B(color
), D3DCOLOR_A(color
)};
4896 return device_clear_render_targets(device
, 1 /* rt_count */, &dst_surface
, 1 /* rect_count */,
4897 (const WINED3DRECT
*)dst_rect
, WINED3DCLEAR_TARGET
, c
, 0.0f
/* depth */, 0 /* stencil */);
4900 const struct blit_shader ffp_blit
= {
4909 static HRESULT
cpu_blit_alloc(IWineD3DDevice
*iface
)
4914 /* Context activation is done by the caller. */
4915 static void cpu_blit_free(IWineD3DDevice
*iface
)
4919 /* Context activation is done by the caller. */
4920 static HRESULT
cpu_blit_set(IWineD3DDevice
*iface
, IWineD3DSurfaceImpl
*surface
)
4925 /* Context activation is done by the caller. */
4926 static void cpu_blit_unset(IWineD3DDevice
*iface
)
4930 static BOOL
cpu_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4931 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4932 const struct wined3d_format_desc
*src_format_desc
,
4933 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4934 const struct wined3d_format_desc
*dst_format_desc
)
4936 if (blit_op
== BLIT_OP_COLOR_FILL
)
4944 static HRESULT
cpu_blit_color_fill(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*dst_surface
, const RECT
*dst_rect
, DWORD fill_color
)
4947 memset(&BltFx
, 0, sizeof(BltFx
));
4948 BltFx
.dwSize
= sizeof(BltFx
);
4949 BltFx
.u5
.dwFillColor
= color_convert_argb_to_fmt(fill_color
, dst_surface
->resource
.format_desc
->format
);
4950 return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface
*)dst_surface
, dst_rect
, NULL
, NULL
, WINEDDBLT_COLORFILL
, &BltFx
, WINED3DTEXF_POINT
);
4953 const struct blit_shader cpu_blit
= {
4962 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum blit_operation blit_op
,
4963 const RECT
*src_rect
, DWORD src_usage
, WINED3DPOOL src_pool
,
4964 const struct wined3d_format_desc
*src_format_desc
,
4965 const RECT
*dst_rect
, DWORD dst_usage
, WINED3DPOOL dst_pool
,
4966 const struct wined3d_format_desc
*dst_format_desc
)
4968 if ((wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) || !gl_info
->fbo_ops
.glBlitFramebuffer
)
4971 /* We only support blitting. Things like color keying / color fill should
4972 * be handled by other blitters.
4974 if (blit_op
!= BLIT_OP_BLIT
)
4977 /* Source and/or destination need to be on the GL side */
4978 if (src_pool
== WINED3DPOOL_SYSTEMMEM
|| dst_pool
== WINED3DPOOL_SYSTEMMEM
)
4981 if(!((src_format_desc
->Flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE
) || (src_usage
& WINED3DUSAGE_RENDERTARGET
))
4982 && ((dst_format_desc
->Flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE
) || (dst_usage
& WINED3DUSAGE_RENDERTARGET
)))
4985 if (!is_identity_fixup(src_format_desc
->color_fixup
) ||
4986 !is_identity_fixup(dst_format_desc
->color_fixup
))
4989 if (!(src_format_desc
->format
== dst_format_desc
->format
4990 || (is_identity_fixup(src_format_desc
->color_fixup
)
4991 && is_identity_fixup(dst_format_desc
->color_fixup
))))