2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2002-2005 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006-2011, 2013-2014 Stefan Dösinger for CodeWeavers
10 * Copyright 2007-2008 Henri Verbeet
11 * Copyright 2006-2008 Roderick Colenbrander
12 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
36 static const DWORD surface_simple_locations
= WINED3D_LOCATION_SYSMEM
37 | WINED3D_LOCATION_USER_MEMORY
| WINED3D_LOCATION_BUFFER
;
39 void surface_get_drawable_size(const struct wined3d_surface
*surface
, const struct wined3d_context
*context
,
40 unsigned int *width
, unsigned int *height
)
42 if (surface
->container
->swapchain
)
44 /* The drawable size of an onscreen drawable is the surface size.
45 * (Actually: The window size, but the surface is created in window
47 *width
= context
->current_rt
.texture
->resource
.width
;
48 *height
= context
->current_rt
.texture
->resource
.height
;
50 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
52 const struct wined3d_swapchain
*swapchain
= context
->swapchain
;
54 /* The drawable size of a backbuffer / aux buffer offscreen target is
55 * the size of the current context's drawable, which is the size of
56 * the back buffer of the swapchain the active context belongs to. */
57 *width
= swapchain
->desc
.backbuffer_width
;
58 *height
= swapchain
->desc
.backbuffer_height
;
62 struct wined3d_surface
*rt
;
64 /* The drawable size of an FBO target is the OpenGL texture size,
65 * which is the power of two size. */
66 rt
= context
->current_rt
.texture
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
67 *width
= wined3d_texture_get_level_pow2_width(rt
->container
, rt
->texture_level
);
68 *height
= wined3d_texture_get_level_pow2_height(rt
->container
, rt
->texture_level
);
76 enum wined3d_gl_resource_type tex_type
;
88 static inline void cube_coords_float(const RECT
*r
, UINT w
, UINT h
, struct float_rect
*f
)
90 f
->l
= ((r
->left
* 2.0f
) / w
) - 1.0f
;
91 f
->t
= ((r
->top
* 2.0f
) / h
) - 1.0f
;
92 f
->r
= ((r
->right
* 2.0f
) / w
) - 1.0f
;
93 f
->b
= ((r
->bottom
* 2.0f
) / h
) - 1.0f
;
96 static void surface_get_blt_info(GLenum target
, const RECT
*rect
, GLsizei w
, GLsizei h
, struct blt_info
*info
)
98 GLfloat (*coords
)[3] = info
->coords
;
104 FIXME("Unsupported texture target %#x\n", target
);
105 /* Fall back to GL_TEXTURE_2D */
107 info
->binding
= GL_TEXTURE_BINDING_2D
;
108 info
->bind_target
= GL_TEXTURE_2D
;
109 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_2D
;
110 coords
[0][0] = (float)rect
->left
/ w
;
111 coords
[0][1] = (float)rect
->top
/ h
;
114 coords
[1][0] = (float)rect
->right
/ w
;
115 coords
[1][1] = (float)rect
->top
/ h
;
118 coords
[2][0] = (float)rect
->left
/ w
;
119 coords
[2][1] = (float)rect
->bottom
/ h
;
122 coords
[3][0] = (float)rect
->right
/ w
;
123 coords
[3][1] = (float)rect
->bottom
/ h
;
127 case GL_TEXTURE_RECTANGLE_ARB
:
128 info
->binding
= GL_TEXTURE_BINDING_RECTANGLE_ARB
;
129 info
->bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
130 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_RECT
;
131 coords
[0][0] = rect
->left
; coords
[0][1] = rect
->top
; coords
[0][2] = 0.0f
;
132 coords
[1][0] = rect
->right
; coords
[1][1] = rect
->top
; coords
[1][2] = 0.0f
;
133 coords
[2][0] = rect
->left
; coords
[2][1] = rect
->bottom
; coords
[2][2] = 0.0f
;
134 coords
[3][0] = rect
->right
; coords
[3][1] = rect
->bottom
; coords
[3][2] = 0.0f
;
137 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
138 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
139 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
140 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_CUBE
;
141 cube_coords_float(rect
, w
, h
, &f
);
143 coords
[0][0] = 1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = -f
.l
;
144 coords
[1][0] = 1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = -f
.r
;
145 coords
[2][0] = 1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = -f
.l
;
146 coords
[3][0] = 1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = -f
.r
;
149 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
150 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
151 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
152 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_CUBE
;
153 cube_coords_float(rect
, w
, h
, &f
);
155 coords
[0][0] = -1.0f
; coords
[0][1] = -f
.t
; coords
[0][2] = f
.l
;
156 coords
[1][0] = -1.0f
; coords
[1][1] = -f
.t
; coords
[1][2] = f
.r
;
157 coords
[2][0] = -1.0f
; coords
[2][1] = -f
.b
; coords
[2][2] = f
.l
;
158 coords
[3][0] = -1.0f
; coords
[3][1] = -f
.b
; coords
[3][2] = f
.r
;
161 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
162 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
163 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
164 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_CUBE
;
165 cube_coords_float(rect
, w
, h
, &f
);
167 coords
[0][0] = f
.l
; coords
[0][1] = 1.0f
; coords
[0][2] = f
.t
;
168 coords
[1][0] = f
.r
; coords
[1][1] = 1.0f
; coords
[1][2] = f
.t
;
169 coords
[2][0] = f
.l
; coords
[2][1] = 1.0f
; coords
[2][2] = f
.b
;
170 coords
[3][0] = f
.r
; coords
[3][1] = 1.0f
; coords
[3][2] = f
.b
;
173 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
174 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
175 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
176 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_CUBE
;
177 cube_coords_float(rect
, w
, h
, &f
);
179 coords
[0][0] = f
.l
; coords
[0][1] = -1.0f
; coords
[0][2] = -f
.t
;
180 coords
[1][0] = f
.r
; coords
[1][1] = -1.0f
; coords
[1][2] = -f
.t
;
181 coords
[2][0] = f
.l
; coords
[2][1] = -1.0f
; coords
[2][2] = -f
.b
;
182 coords
[3][0] = f
.r
; coords
[3][1] = -1.0f
; coords
[3][2] = -f
.b
;
185 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
186 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
187 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
188 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_CUBE
;
189 cube_coords_float(rect
, w
, h
, &f
);
191 coords
[0][0] = f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = 1.0f
;
192 coords
[1][0] = f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = 1.0f
;
193 coords
[2][0] = f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = 1.0f
;
194 coords
[3][0] = f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = 1.0f
;
197 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
198 info
->binding
= GL_TEXTURE_BINDING_CUBE_MAP_ARB
;
199 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
200 info
->tex_type
= WINED3D_GL_RES_TYPE_TEX_CUBE
;
201 cube_coords_float(rect
, w
, h
, &f
);
203 coords
[0][0] = -f
.l
; coords
[0][1] = -f
.t
; coords
[0][2] = -1.0f
;
204 coords
[1][0] = -f
.r
; coords
[1][1] = -f
.t
; coords
[1][2] = -1.0f
;
205 coords
[2][0] = -f
.l
; coords
[2][1] = -f
.b
; coords
[2][2] = -1.0f
;
206 coords
[3][0] = -f
.r
; coords
[3][1] = -f
.b
; coords
[3][2] = -1.0f
;
211 static void surface_get_rect(const struct wined3d_surface
*surface
, const RECT
*rect_in
, RECT
*rect_out
)
214 *rect_out
= *rect_in
;
217 const struct wined3d_texture
*texture
= surface
->container
;
221 rect_out
->right
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
222 rect_out
->bottom
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
226 /* Context activation is done by the caller. */
227 void draw_textured_quad(const struct wined3d_surface
*src_surface
, struct wined3d_context
*context
,
228 const RECT
*src_rect
, const RECT
*dst_rect
, enum wined3d_texture_filter_type filter
)
230 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
231 struct wined3d_texture
*texture
= src_surface
->container
;
232 struct blt_info info
;
234 surface_get_blt_info(src_surface
->texture_target
, src_rect
,
235 wined3d_texture_get_level_pow2_width(texture
, src_surface
->texture_level
),
236 wined3d_texture_get_level_pow2_height(texture
, src_surface
->texture_level
), &info
);
238 gl_info
->gl_ops
.gl
.p_glEnable(info
.bind_target
);
239 checkGLcall("glEnable(bind_target)");
241 context_bind_texture(context
, info
.bind_target
, texture
->texture_rgb
.name
);
243 /* Filtering for StretchRect */
244 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(filter
));
245 checkGLcall("glTexParameteri");
246 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MIN_FILTER
,
247 wined3d_gl_min_mip_filter(filter
, WINED3D_TEXF_NONE
));
248 checkGLcall("glTexParameteri");
249 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
250 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
251 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
252 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
253 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
254 checkGLcall("glTexEnvi");
257 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
258 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[0]);
259 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->top
);
261 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[1]);
262 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->top
);
264 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[2]);
265 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
267 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[3]);
268 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
269 gl_info
->gl_ops
.gl
.p_glEnd();
271 /* Unbind the texture */
272 context_bind_texture(context
, info
.bind_target
, 0);
274 /* We changed the filtering settings on the texture. Inform the
275 * container about this to get the filters reset properly next draw. */
276 texture
->texture_rgb
.sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
277 texture
->texture_rgb
.sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
278 texture
->texture_rgb
.sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
279 texture
->texture_rgb
.sampler_desc
.srgb_decode
= FALSE
;
282 /* Works correctly only for <= 4 bpp formats. */
283 static void get_color_masks(const struct wined3d_format
*format
, DWORD
*masks
)
285 masks
[0] = ((1u << format
->red_size
) - 1) << format
->red_offset
;
286 masks
[1] = ((1u << format
->green_size
) - 1) << format
->green_offset
;
287 masks
[2] = ((1u << format
->blue_size
) - 1) << format
->blue_offset
;
290 void wined3d_surface_destroy_dc(struct wined3d_surface
*surface
)
292 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
293 struct wined3d_texture
*texture
= surface
->container
;
294 struct wined3d_device
*device
= texture
->resource
.device
;
295 const struct wined3d_gl_info
*gl_info
= NULL
;
296 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
297 struct wined3d_context
*context
= NULL
;
298 struct wined3d_bo_address data
;
303 ERR("Surface %p has no DC.\n", surface
);
307 TRACE("dc %p, bitmap %p.\n", surface
->dc
, surface
->bitmap
);
309 destroy_desc
.hDc
= surface
->dc
;
310 destroy_desc
.hBitmap
= surface
->bitmap
;
311 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
312 ERR("Failed to destroy dc, status %#x.\n", status
);
314 surface
->bitmap
= NULL
;
316 if (device
->d3d_initialized
)
318 context
= context_acquire(device
, NULL
);
319 gl_info
= context
->gl_info
;
322 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
323 wined3d_texture_unmap_bo_address(&data
, gl_info
, GL_PIXEL_UNPACK_BUFFER
);
326 context_release(context
);
329 HRESULT
wined3d_surface_create_dc(struct wined3d_surface
*surface
)
331 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
332 struct wined3d_texture
*texture
= surface
->container
;
333 const struct wined3d_format
*format
= texture
->resource
.format
;
334 struct wined3d_device
*device
= texture
->resource
.device
;
335 const struct wined3d_gl_info
*gl_info
= NULL
;
336 struct wined3d_context
*context
= NULL
;
337 unsigned int row_pitch
, slice_pitch
;
338 struct wined3d_bo_address data
;
339 D3DKMT_CREATEDCFROMMEMORY desc
;
342 TRACE("surface %p.\n", surface
);
344 if (!format
->ddi_format
)
346 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
347 return WINED3DERR_INVALIDCALL
;
350 wined3d_texture_get_pitch(texture
, surface
->texture_level
, &row_pitch
, &slice_pitch
);
352 if (device
->d3d_initialized
)
354 context
= context_acquire(device
, NULL
);
355 gl_info
= context
->gl_info
;
358 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
359 desc
.pMemory
= wined3d_texture_map_bo_address(&data
, texture
->sub_resources
[sub_resource_idx
].size
,
360 gl_info
, GL_PIXEL_UNPACK_BUFFER
, 0);
363 context_release(context
);
365 desc
.Format
= format
->ddi_format
;
366 desc
.Width
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
367 desc
.Height
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
368 desc
.Pitch
= row_pitch
;
369 desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
370 desc
.pColorTable
= NULL
;
372 status
= D3DKMTCreateDCFromMemory(&desc
);
373 DeleteDC(desc
.hDeviceDc
);
376 WARN("Failed to create DC, status %#x.\n", status
);
377 return WINED3DERR_INVALIDCALL
;
380 surface
->dc
= desc
.hDc
;
381 surface
->bitmap
= desc
.hBitmap
;
383 TRACE("Created DC %p, bitmap %p for surface %p.\n", surface
->dc
, surface
->bitmap
, surface
);
388 static BOOL
surface_is_full_rect(const struct wined3d_surface
*surface
, const RECT
*r
)
392 t
= wined3d_texture_get_level_width(surface
->container
, surface
->texture_level
);
393 if ((r
->left
&& r
->right
) || abs(r
->right
- r
->left
) != t
)
395 t
= wined3d_texture_get_level_height(surface
->container
, surface
->texture_level
);
396 if ((r
->top
&& r
->bottom
) || abs(r
->bottom
- r
->top
) != t
)
401 static void surface_depth_blt_fbo(const struct wined3d_device
*device
,
402 struct wined3d_surface
*src_surface
, DWORD src_location
, const RECT
*src_rect
,
403 struct wined3d_surface
*dst_surface
, DWORD dst_location
, const RECT
*dst_rect
)
405 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
406 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
407 struct wined3d_texture
*src_texture
= src_surface
->container
;
408 const struct wined3d_gl_info
*gl_info
;
409 struct wined3d_context
*context
;
410 DWORD src_mask
, dst_mask
;
413 TRACE("device %p\n", device
);
414 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
415 src_surface
, wined3d_debug_location(src_location
), wine_dbgstr_rect(src_rect
));
416 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
417 dst_surface
, wined3d_debug_location(dst_location
), wine_dbgstr_rect(dst_rect
));
419 src_mask
= src_texture
->resource
.format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
);
420 dst_mask
= dst_texture
->resource
.format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
);
422 if (src_mask
!= dst_mask
)
424 ERR("Incompatible formats %s and %s.\n",
425 debug_d3dformat(src_texture
->resource
.format
->id
),
426 debug_d3dformat(dst_texture
->resource
.format
->id
));
432 ERR("Not a depth / stencil format: %s.\n",
433 debug_d3dformat(src_texture
->resource
.format
->id
));
438 if (src_mask
& WINED3DFMT_FLAG_DEPTH
)
439 gl_mask
|= GL_DEPTH_BUFFER_BIT
;
440 if (src_mask
& WINED3DFMT_FLAG_STENCIL
)
441 gl_mask
|= GL_STENCIL_BUFFER_BIT
;
443 context
= context_acquire(device
, NULL
);
446 context_release(context
);
447 WARN("Invalid context, skipping blit.\n");
451 /* Make sure the locations are up-to-date. Loading the destination
452 * surface isn't required if the entire surface is overwritten. */
453 surface_load_location(src_surface
, context
, src_location
);
454 if (!surface_is_full_rect(dst_surface
, dst_rect
))
455 surface_load_location(dst_surface
, context
, dst_location
);
457 wined3d_texture_prepare_location(dst_texture
, dst_sub_resource_idx
, context
, dst_location
);
459 gl_info
= context
->gl_info
;
461 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, NULL
, src_surface
, src_location
);
462 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
464 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, NULL
, dst_surface
, dst_location
);
465 context_set_draw_buffer(context
, GL_NONE
);
466 context_check_fbo_status(context
, GL_DRAW_FRAMEBUFFER
);
467 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
469 if (gl_mask
& GL_DEPTH_BUFFER_BIT
)
471 gl_info
->gl_ops
.gl
.p_glDepthMask(GL_TRUE
);
472 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZWRITEENABLE
));
474 if (gl_mask
& GL_STENCIL_BUFFER_BIT
)
476 if (context
->gl_info
->supported
[EXT_STENCIL_TWO_SIDE
])
478 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT
);
479 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE
));
481 gl_info
->gl_ops
.gl
.p_glStencilMask(~0U);
482 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
485 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
486 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
488 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
,
489 dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, gl_mask
, GL_NEAREST
);
490 checkGLcall("glBlitFramebuffer()");
492 if (wined3d_settings
.strict_draw_ordering
)
493 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
495 context_release(context
);
498 /* Blit between surface locations. Onscreen on different swapchains is not supported.
499 * Depth / stencil is not supported. Context activation is done by the caller. */
500 static void surface_blt_fbo(const struct wined3d_device
*device
,
501 struct wined3d_context
*old_ctx
, enum wined3d_texture_filter_type filter
,
502 struct wined3d_surface
*src_surface
, DWORD src_location
, const RECT
*src_rect_in
,
503 struct wined3d_surface
*dst_surface
, DWORD dst_location
, const RECT
*dst_rect_in
)
505 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
506 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
507 struct wined3d_texture
*src_texture
= src_surface
->container
;
508 const struct wined3d_gl_info
*gl_info
;
509 struct wined3d_context
*context
= old_ctx
;
510 struct wined3d_surface
*required_rt
, *restore_rt
= NULL
;
511 RECT src_rect
, dst_rect
;
515 TRACE("device %p, filter %s,\n", device
, debug_d3dtexturefiltertype(filter
));
516 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
517 src_surface
, wined3d_debug_location(src_location
), wine_dbgstr_rect(src_rect_in
));
518 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
519 dst_surface
, wined3d_debug_location(dst_location
), wine_dbgstr_rect(dst_rect_in
));
521 src_rect
= *src_rect_in
;
522 dst_rect
= *dst_rect_in
;
526 case WINED3D_TEXF_LINEAR
:
527 gl_filter
= GL_LINEAR
;
531 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter
), filter
);
532 case WINED3D_TEXF_NONE
:
533 case WINED3D_TEXF_POINT
:
534 gl_filter
= GL_NEAREST
;
538 /* Resolve the source surface first if needed. */
539 if (src_location
== WINED3D_LOCATION_RB_MULTISAMPLE
540 && (src_texture
->resource
.format
->id
!= dst_texture
->resource
.format
->id
541 || abs(src_rect
.bottom
- src_rect
.top
) != abs(dst_rect
.bottom
- dst_rect
.top
)
542 || abs(src_rect
.right
- src_rect
.left
) != abs(dst_rect
.right
- dst_rect
.left
)))
543 src_location
= WINED3D_LOCATION_RB_RESOLVED
;
545 /* Make sure the locations are up-to-date. Loading the destination
546 * surface isn't required if the entire surface is overwritten. (And is
547 * in fact harmful if we're being called by surface_load_location() with
548 * the purpose of loading the destination surface.) */
549 surface_load_location(src_surface
, old_ctx
, src_location
);
550 if (!surface_is_full_rect(dst_surface
, &dst_rect
))
551 surface_load_location(dst_surface
, old_ctx
, dst_location
);
553 wined3d_texture_prepare_location(dst_texture
, dst_sub_resource_idx
, old_ctx
, dst_location
);
556 if (src_location
== WINED3D_LOCATION_DRAWABLE
) required_rt
= src_surface
;
557 else if (dst_location
== WINED3D_LOCATION_DRAWABLE
) required_rt
= dst_surface
;
558 else required_rt
= NULL
;
560 restore_rt
= context_get_rt_surface(old_ctx
);
561 if (restore_rt
!= required_rt
)
562 context
= context_acquire(device
, required_rt
);
568 context_release(context
);
569 WARN("Invalid context, skipping blit.\n");
573 gl_info
= context
->gl_info
;
575 if (src_location
== WINED3D_LOCATION_DRAWABLE
)
577 TRACE("Source surface %p is onscreen.\n", src_surface
);
578 buffer
= wined3d_texture_get_gl_buffer(src_texture
);
579 surface_translate_drawable_coords(src_surface
, context
->win_handle
, &src_rect
);
583 TRACE("Source surface %p is offscreen.\n", src_surface
);
584 buffer
= GL_COLOR_ATTACHMENT0
;
587 context_apply_fbo_state_blit(context
, GL_READ_FRAMEBUFFER
, src_surface
, NULL
, src_location
);
588 gl_info
->gl_ops
.gl
.p_glReadBuffer(buffer
);
589 checkGLcall("glReadBuffer()");
590 context_check_fbo_status(context
, GL_READ_FRAMEBUFFER
);
592 if (dst_location
== WINED3D_LOCATION_DRAWABLE
)
594 TRACE("Destination surface %p is onscreen.\n", dst_surface
);
595 buffer
= wined3d_texture_get_gl_buffer(dst_texture
);
596 surface_translate_drawable_coords(dst_surface
, context
->win_handle
, &dst_rect
);
600 TRACE("Destination surface %p is offscreen.\n", dst_surface
);
601 buffer
= GL_COLOR_ATTACHMENT0
;
604 context_apply_fbo_state_blit(context
, GL_DRAW_FRAMEBUFFER
, dst_surface
, NULL
, dst_location
);
605 context_set_draw_buffer(context
, buffer
);
606 context_check_fbo_status(context
, GL_DRAW_FRAMEBUFFER
);
607 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
609 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
610 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
611 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
612 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
613 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
615 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
616 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
618 gl_info
->fbo_ops
.glBlitFramebuffer(src_rect
.left
, src_rect
.top
, src_rect
.right
, src_rect
.bottom
,
619 dst_rect
.left
, dst_rect
.top
, dst_rect
.right
, dst_rect
.bottom
, GL_COLOR_BUFFER_BIT
, gl_filter
);
620 checkGLcall("glBlitFramebuffer()");
622 if (wined3d_settings
.strict_draw_ordering
|| (dst_location
== WINED3D_LOCATION_DRAWABLE
623 && dst_texture
->swapchain
->front_buffer
== dst_texture
))
624 gl_info
->gl_ops
.gl
.p_glFlush();
627 context_restore(context
, restore_rt
);
630 static BOOL
fbo_blit_supported(const struct wined3d_gl_info
*gl_info
, enum wined3d_blit_op blit_op
,
631 const RECT
*src_rect
, DWORD src_usage
, enum wined3d_pool src_pool
, const struct wined3d_format
*src_format
,
632 const RECT
*dst_rect
, DWORD dst_usage
, enum wined3d_pool dst_pool
, const struct wined3d_format
*dst_format
)
634 if ((wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) || !gl_info
->fbo_ops
.glBlitFramebuffer
)
637 /* Source and/or destination need to be on the GL side */
638 if (src_pool
== WINED3D_POOL_SYSTEM_MEM
|| dst_pool
== WINED3D_POOL_SYSTEM_MEM
)
643 case WINED3D_BLIT_OP_COLOR_BLIT
:
644 if (!((src_format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FBO_ATTACHABLE
)
645 || (src_usage
& WINED3DUSAGE_RENDERTARGET
)))
647 if (!((dst_format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FBO_ATTACHABLE
)
648 || (dst_usage
& WINED3DUSAGE_RENDERTARGET
)))
650 if (!(src_format
->id
== dst_format
->id
651 || (is_identity_fixup(src_format
->color_fixup
)
652 && is_identity_fixup(dst_format
->color_fixup
))))
656 case WINED3D_BLIT_OP_DEPTH_BLIT
:
657 if (!(src_format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
659 if (!(dst_format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
661 /* Accept pure swizzle fixups for depth formats. In general we
662 * ignore the stencil component (if present) at the moment and the
663 * swizzle is not relevant with just the depth component. */
664 if (is_complex_fixup(src_format
->color_fixup
) || is_complex_fixup(dst_format
->color_fixup
)
665 || is_scaling_fixup(src_format
->color_fixup
) || is_scaling_fixup(dst_format
->color_fixup
))
676 static BOOL
surface_convert_depth_to_float(const struct wined3d_surface
*surface
, DWORD depth
, float *float_depth
)
678 const struct wined3d_format
*format
= surface
->container
->resource
.format
;
682 case WINED3DFMT_S1_UINT_D15_UNORM
:
683 *float_depth
= depth
/ (float)0x00007fff;
686 case WINED3DFMT_D16_UNORM
:
687 *float_depth
= depth
/ (float)0x0000ffff;
690 case WINED3DFMT_D24_UNORM_S8_UINT
:
691 case WINED3DFMT_X8D24_UNORM
:
692 *float_depth
= depth
/ (float)0x00ffffff;
695 case WINED3DFMT_D32_UNORM
:
696 *float_depth
= depth
/ (float)0xffffffff;
700 ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format
->id
));
707 static HRESULT
wined3d_surface_depth_fill(struct wined3d_surface
*surface
, const RECT
*rect
, float depth
)
709 struct wined3d_resource
*resource
= &surface
->container
->resource
;
710 struct wined3d_device
*device
= resource
->device
;
711 struct wined3d_rendertarget_view_desc view_desc
;
712 struct wined3d_rendertarget_view
*view
;
713 const struct blit_shader
*blitter
;
716 if (!(blitter
= wined3d_select_blitter(&device
->adapter
->gl_info
, &device
->adapter
->d3d_info
,
717 WINED3D_BLIT_OP_DEPTH_FILL
, NULL
, 0, 0, NULL
, rect
, resource
->usage
, resource
->pool
, resource
->format
)))
719 FIXME("No blitter is capable of performing the requested depth fill operation.\n");
720 return WINED3DERR_INVALIDCALL
;
723 view_desc
.format_id
= resource
->format
->id
;
724 view_desc
.u
.texture
.level_idx
= surface
->texture_level
;
725 view_desc
.u
.texture
.layer_idx
= surface
->texture_layer
;
726 view_desc
.u
.texture
.layer_count
= 1;
727 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
,
728 resource
, NULL
, &wined3d_null_parent_ops
, &view
)))
730 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
734 hr
= blitter
->depth_fill(device
, view
, rect
, WINED3DCLEAR_ZBUFFER
, depth
, 0);
735 wined3d_rendertarget_view_decref(view
);
740 static HRESULT
wined3d_surface_depth_blt(struct wined3d_surface
*src_surface
, DWORD src_location
, const RECT
*src_rect
,
741 struct wined3d_surface
*dst_surface
, DWORD dst_location
, const RECT
*dst_rect
)
743 struct wined3d_texture
*src_texture
= src_surface
->container
;
744 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
745 struct wined3d_device
*device
= src_texture
->resource
.device
;
747 if (!fbo_blit_supported(&device
->adapter
->gl_info
, WINED3D_BLIT_OP_DEPTH_BLIT
,
748 src_rect
, src_texture
->resource
.usage
, src_texture
->resource
.pool
, src_texture
->resource
.format
,
749 dst_rect
, dst_texture
->resource
.usage
, dst_texture
->resource
.pool
, dst_texture
->resource
.format
))
750 return WINED3DERR_INVALIDCALL
;
752 surface_depth_blt_fbo(device
, src_surface
, src_location
, src_rect
, dst_surface
, dst_location
, dst_rect
);
754 surface_modify_ds_location(dst_surface
, dst_location
,
755 dst_surface
->ds_current_size
.cx
, dst_surface
->ds_current_size
.cy
);
760 /* This call just downloads data, the caller is responsible for binding the
761 * correct texture. */
762 /* Context activation is done by the caller. */
763 static void surface_download_data(struct wined3d_surface
*surface
, const struct wined3d_gl_info
*gl_info
,
766 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
767 struct wined3d_texture
*texture
= surface
->container
;
768 const struct wined3d_format
*format
= texture
->resource
.format
;
769 struct wined3d_texture_sub_resource
*sub_resource
;
770 unsigned int dst_row_pitch
, dst_slice_pitch
;
771 unsigned int src_row_pitch
, src_slice_pitch
;
772 struct wined3d_bo_address data
;
773 BYTE
*temporary_mem
= NULL
;
776 /* Only support read back of converted P8 surfaces. */
777 if (texture
->flags
& WINED3D_TEXTURE_CONVERTED
&& format
->id
!= WINED3DFMT_P8_UINT
)
779 ERR("Trying to read back converted surface %p with format %s.\n", surface
, debug_d3dformat(format
->id
));
783 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
785 if (surface
->texture_target
== GL_TEXTURE_2D_ARRAY
)
787 /* NP2 emulation is not allowed on array textures. */
788 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
789 ERR("Array texture %p uses NP2 emulation.\n", texture
);
791 WARN_(d3d_perf
)("Downloading all miplevel layers to get the surface data for a single sub-resource.\n");
793 if (!(temporary_mem
= wined3d_calloc(texture
->layer_count
, sub_resource
->size
)))
795 ERR("Out of memory.\n");
800 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, dst_location
);
802 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
804 wined3d_texture_get_pitch(texture
, surface
->texture_level
, &dst_row_pitch
, &dst_slice_pitch
);
805 wined3d_format_calculate_pitch(format
, texture
->resource
.device
->surface_alignment
,
806 wined3d_texture_get_level_pow2_width(texture
, surface
->texture_level
),
807 wined3d_texture_get_level_pow2_height(texture
, surface
->texture_level
),
808 &src_row_pitch
, &src_slice_pitch
);
809 if (!(temporary_mem
= HeapAlloc(GetProcessHeap(), 0, src_slice_pitch
)))
811 ERR("Out of memory.\n");
815 if (data
.buffer_object
)
816 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
817 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_COMPRESSED
)
818 ERR("Unexpected compressed format for NP2 emulated texture.\n");
825 else if (data
.buffer_object
)
827 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
.buffer_object
));
828 checkGLcall("glBindBuffer");
836 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_COMPRESSED
)
838 TRACE("Downloading compressed surface %p, level %u, format %#x, type %#x, data %p.\n",
839 surface
, surface
->texture_level
, format
->glFormat
, format
->glType
, mem
);
841 GL_EXTCALL(glGetCompressedTexImage(surface
->texture_target
, surface
->texture_level
, mem
));
842 checkGLcall("glGetCompressedTexImage");
846 TRACE("Downloading surface %p, level %u, format %#x, type %#x, data %p.\n",
847 surface
, surface
->texture_level
, format
->glFormat
, format
->glType
, mem
);
849 gl_info
->gl_ops
.gl
.p_glGetTexImage(surface
->texture_target
, surface
->texture_level
,
850 format
->glFormat
, format
->glType
, mem
);
851 checkGLcall("glGetTexImage");
854 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
856 const BYTE
*src_data
;
860 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
861 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
862 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
864 * We're doing this...
866 * instead of boxing the texture :
867 * |<-texture width ->| -->pow2width| /\
868 * |111111111111111111| | |
869 * |222 Texture 222222| boxed empty | texture height
870 * |3333 Data 33333333| | |
871 * |444444444444444444| | \/
872 * ----------------------------------- |
873 * | boxed empty | boxed empty | pow2height
875 * -----------------------------------
878 * we're repacking the data to the expected texture width
880 * |<-texture width ->| -->pow2width| /\
881 * |111111111111111111222222222222222| |
882 * |222333333333333333333444444444444| texture height
886 * | empty | pow2height
888 * -----------------------------------
892 * |<-texture width ->| /\
893 * |111111111111111111|
894 * |222222222222222222|texture height
895 * |333333333333333333|
896 * |444444444444444444| \/
897 * --------------------
899 * This also means that any references to surface memory should work with the data as if it were a
900 * standard texture with a non-power2 width instead of a texture boxed up to be a power2 texture.
902 * internally the texture is still stored in a boxed format so any references to textureName will
903 * get a boxed texture with width pow2width and not a texture of width resource.width. */
905 dst_data
= data
.addr
;
906 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch
, dst_row_pitch
);
907 h
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
908 for (y
= 0; y
< h
; ++y
)
910 memcpy(dst_data
, src_data
, dst_row_pitch
);
911 src_data
+= src_row_pitch
;
912 dst_data
+= dst_row_pitch
;
915 else if (temporary_mem
)
917 void *src_data
= temporary_mem
+ surface
->texture_layer
* sub_resource
->size
;
918 if (data
.buffer_object
)
920 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
.buffer_object
));
921 checkGLcall("glBindBuffer");
922 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, sub_resource
->size
, src_data
));
923 checkGLcall("glBufferSubData");
927 memcpy(data
.addr
, src_data
, sub_resource
->size
);
931 if (data
.buffer_object
)
933 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
934 checkGLcall("glBindBuffer");
937 HeapFree(GetProcessHeap(), 0, temporary_mem
);
940 /* This call just uploads data, the caller is responsible for binding the
941 * correct texture. */
942 /* Context activation is done by the caller. */
943 void wined3d_surface_upload_data(struct wined3d_surface
*surface
, const struct wined3d_gl_info
*gl_info
,
944 const struct wined3d_format
*format
, const RECT
*src_rect
, UINT src_pitch
, const POINT
*dst_point
,
945 BOOL srgb
, const struct wined3d_const_bo_address
*data
)
947 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
948 struct wined3d_texture
*texture
= surface
->container
;
949 UINT update_w
= src_rect
->right
- src_rect
->left
;
950 UINT update_h
= src_rect
->bottom
- src_rect
->top
;
952 TRACE("surface %p, gl_info %p, format %s, src_rect %s, src_pitch %u, dst_point %s, srgb %#x, data {%#x:%p}.\n",
953 surface
, gl_info
, debug_d3dformat(format
->id
), wine_dbgstr_rect(src_rect
), src_pitch
,
954 wine_dbgstr_point(dst_point
), srgb
, data
->buffer_object
, data
->addr
);
956 if (texture
->sub_resources
[sub_resource_idx
].map_count
)
958 WARN("Uploading a surface that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
959 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
962 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_HEIGHT_SCALE
)
964 update_h
*= format
->height_scale
.numerator
;
965 update_h
/= format
->height_scale
.denominator
;
968 if (data
->buffer_object
)
970 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
971 checkGLcall("glBindBuffer");
974 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_COMPRESSED
)
976 UINT row_length
= wined3d_format_calculate_size(format
, 1, update_w
, 1, 1);
977 UINT row_count
= (update_h
+ format
->block_height
- 1) / format
->block_height
;
978 const BYTE
*addr
= data
->addr
;
981 addr
+= (src_rect
->top
/ format
->block_height
) * src_pitch
;
982 addr
+= (src_rect
->left
/ format
->block_width
) * format
->block_byte_count
;
985 internal
= format
->glGammaInternal
;
986 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
987 && wined3d_resource_is_offscreen(&texture
->resource
))
988 internal
= format
->rtInternal
;
990 internal
= format
->glInternal
;
992 TRACE("Uploading compressed data, target %#x, level %u, layer %u, x %d, y %d, w %u, h %u, "
993 "format %#x, image_size %#x, addr %p.\n",
994 surface
->texture_target
, surface
->texture_level
, surface
->texture_layer
,
995 dst_point
->x
, dst_point
->y
, update_w
, update_h
, internal
, row_count
* row_length
, addr
);
997 if (row_length
== src_pitch
)
999 if (surface
->texture_target
== GL_TEXTURE_2D_ARRAY
)
1001 GL_EXTCALL(glCompressedTexSubImage3D(surface
->texture_target
, surface
->texture_level
,
1002 dst_point
->x
, dst_point
->y
, surface
->texture_layer
, update_w
, update_h
, 1,
1003 internal
, row_count
* row_length
, addr
));
1007 GL_EXTCALL(glCompressedTexSubImage2D(surface
->texture_target
, surface
->texture_level
,
1008 dst_point
->x
, dst_point
->y
, update_w
, update_h
,
1009 internal
, row_count
* row_length
, addr
));
1016 /* glCompressedTexSubImage2D() ignores pixel store state, so we
1017 * can't use the unpack row length like for glTexSubImage2D. */
1018 for (row
= 0, y
= dst_point
->y
; row
< row_count
; ++row
)
1020 if (surface
->texture_target
== GL_TEXTURE_2D_ARRAY
)
1022 GL_EXTCALL(glCompressedTexSubImage3D(surface
->texture_target
, surface
->texture_level
,
1023 dst_point
->x
, y
, surface
->texture_layer
, update_w
, format
->block_height
, 1,
1024 internal
, row_length
, addr
));
1028 GL_EXTCALL(glCompressedTexSubImage2D(surface
->texture_target
, surface
->texture_level
,
1029 dst_point
->x
, y
, update_w
, format
->block_height
, internal
, row_length
, addr
));
1032 y
+= format
->block_height
;
1036 checkGLcall("Upload compressed surface data");
1040 const BYTE
*addr
= data
->addr
;
1042 addr
+= src_rect
->top
* src_pitch
;
1043 addr
+= src_rect
->left
* format
->byte_count
;
1045 TRACE("Uploading data, target %#x, level %u, layer %u, x %d, y %d, w %u, h %u, "
1046 "format %#x, type %#x, addr %p.\n",
1047 surface
->texture_target
, surface
->texture_level
, surface
->texture_layer
,
1048 dst_point
->x
, dst_point
->y
, update_w
, update_h
, format
->glFormat
, format
->glType
, addr
);
1050 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, src_pitch
/ format
->byte_count
);
1051 if (surface
->texture_target
== GL_TEXTURE_2D_ARRAY
)
1053 GL_EXTCALL(glTexSubImage3D(surface
->texture_target
, surface
->texture_level
,
1054 dst_point
->x
, dst_point
->y
, surface
->texture_layer
, update_w
, update_h
, 1,
1055 format
->glFormat
, format
->glType
, addr
));
1059 gl_info
->gl_ops
.gl
.p_glTexSubImage2D(surface
->texture_target
, surface
->texture_level
,
1060 dst_point
->x
, dst_point
->y
, update_w
, update_h
, format
->glFormat
, format
->glType
, addr
);
1062 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1063 checkGLcall("Upload surface data");
1066 if (data
->buffer_object
)
1068 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1069 checkGLcall("glBindBuffer");
1072 if (wined3d_settings
.strict_draw_ordering
)
1073 gl_info
->gl_ops
.gl
.p_glFlush();
1075 if (gl_info
->quirks
& WINED3D_QUIRK_FBO_TEX_UPDATE
)
1077 struct wined3d_device
*device
= texture
->resource
.device
;
1080 for (i
= 0; i
< device
->context_count
; ++i
)
1082 context_surface_update(device
->contexts
[i
], surface
);
1087 static BOOL
surface_check_block_align_rect(struct wined3d_surface
*surface
, const RECT
*rect
)
1089 struct wined3d_box box
= {rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1};
1091 return wined3d_texture_check_block_align(surface
->container
, surface
->texture_level
, &box
);
1094 HRESULT
surface_upload_from_surface(struct wined3d_surface
*dst_surface
, const POINT
*dst_point
,
1095 struct wined3d_surface
*src_surface
, const RECT
*src_rect
)
1097 unsigned int src_sub_resource_idx
= surface_get_sub_resource_idx(src_surface
);
1098 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
1099 struct wined3d_texture
*src_texture
= src_surface
->container
;
1100 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
1101 unsigned int src_row_pitch
, src_slice_pitch
;
1102 const struct wined3d_format
*src_format
;
1103 const struct wined3d_format
*dst_format
;
1104 unsigned int src_fmt_flags
, dst_fmt_flags
;
1105 const struct wined3d_gl_info
*gl_info
;
1106 struct wined3d_context
*context
;
1107 struct wined3d_bo_address data
;
1108 UINT update_w
, update_h
;
1113 TRACE("dst_surface %p, dst_point %s, src_surface %p, src_rect %s.\n",
1114 dst_surface
, wine_dbgstr_point(dst_point
),
1115 src_surface
, wine_dbgstr_rect(src_rect
));
1117 src_format
= src_texture
->resource
.format
;
1118 dst_format
= dst_texture
->resource
.format
;
1119 src_fmt_flags
= src_texture
->resource
.format_flags
;
1120 dst_fmt_flags
= dst_texture
->resource
.format_flags
;
1122 if (src_format
->id
!= dst_format
->id
)
1124 WARN("Source and destination surfaces should have the same format.\n");
1125 return WINED3DERR_INVALIDCALL
;
1134 else if (dst_point
->x
< 0 || dst_point
->y
< 0)
1136 WARN("Invalid destination point.\n");
1137 return WINED3DERR_INVALIDCALL
;
1142 SetRect(&r
, 0, 0, wined3d_texture_get_level_width(src_texture
, src_surface
->texture_level
),
1143 wined3d_texture_get_level_height(src_texture
, src_surface
->texture_level
));
1146 else if (src_rect
->left
< 0 || src_rect
->top
< 0 || IsRectEmpty(src_rect
))
1148 WARN("Invalid source rectangle.\n");
1149 return WINED3DERR_INVALIDCALL
;
1152 dst_w
= wined3d_texture_get_level_width(dst_texture
, dst_surface
->texture_level
);
1153 dst_h
= wined3d_texture_get_level_height(dst_texture
, dst_surface
->texture_level
);
1155 update_w
= src_rect
->right
- src_rect
->left
;
1156 update_h
= src_rect
->bottom
- src_rect
->top
;
1158 if (update_w
> dst_w
|| dst_point
->x
> dst_w
- update_w
1159 || update_h
> dst_h
|| dst_point
->y
> dst_h
- update_h
)
1161 WARN("Destination out of bounds.\n");
1162 return WINED3DERR_INVALIDCALL
;
1165 if ((src_fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !surface_check_block_align_rect(src_surface
, src_rect
))
1167 WARN("Source rectangle not block-aligned.\n");
1168 return WINED3DERR_INVALIDCALL
;
1171 SetRect(&dst_rect
, dst_point
->x
, dst_point
->y
, dst_point
->x
+ update_w
, dst_point
->y
+ update_h
);
1172 if ((dst_fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !surface_check_block_align_rect(dst_surface
, &dst_rect
))
1174 WARN("Destination rectangle not block-aligned.\n");
1175 return WINED3DERR_INVALIDCALL
;
1178 /* Use wined3d_surface_blt() instead of uploading directly if we need conversion. */
1179 if (dst_format
->convert
|| wined3d_format_get_color_key_conversion(dst_texture
, FALSE
))
1180 return wined3d_surface_blt(dst_surface
, &dst_rect
, src_surface
, src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
1182 context
= context_acquire(dst_texture
->resource
.device
, NULL
);
1183 gl_info
= context
->gl_info
;
1185 /* Only load the surface for partial updates. For newly allocated texture
1186 * the texture wouldn't be the current location, and we'd upload zeroes
1187 * just to overwrite them again. */
1188 if (update_w
== dst_w
&& update_h
== dst_h
)
1189 wined3d_texture_prepare_texture(dst_texture
, context
, FALSE
);
1191 surface_load_location(dst_surface
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
1192 wined3d_texture_bind_and_dirtify(dst_texture
, context
, FALSE
);
1194 wined3d_texture_get_memory(src_texture
, src_sub_resource_idx
, &data
,
1195 src_texture
->sub_resources
[src_sub_resource_idx
].locations
);
1196 wined3d_texture_get_pitch(src_texture
, src_surface
->texture_level
, &src_row_pitch
, &src_slice_pitch
);
1198 wined3d_surface_upload_data(dst_surface
, gl_info
, src_format
, src_rect
,
1199 src_row_pitch
, dst_point
, FALSE
, wined3d_const_bo_address(&data
));
1201 context_release(context
);
1203 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
1204 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1209 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1210 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1211 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1212 /* Context activation is done by the caller. */
1213 void surface_set_compatible_renderbuffer(struct wined3d_surface
*surface
, const struct wined3d_surface
*rt
)
1215 const struct wined3d_gl_info
*gl_info
= &surface
->container
->resource
.device
->adapter
->gl_info
;
1216 struct wined3d_renderbuffer_entry
*entry
;
1217 GLuint renderbuffer
= 0;
1218 unsigned int src_width
, src_height
;
1219 unsigned int width
, height
;
1221 if (rt
&& rt
->container
->resource
.format
->id
!= WINED3DFMT_NULL
)
1223 width
= wined3d_texture_get_level_pow2_width(rt
->container
, rt
->texture_level
);
1224 height
= wined3d_texture_get_level_pow2_height(rt
->container
, rt
->texture_level
);
1228 width
= wined3d_texture_get_level_pow2_width(surface
->container
, surface
->texture_level
);
1229 height
= wined3d_texture_get_level_pow2_height(surface
->container
, surface
->texture_level
);
1232 src_width
= wined3d_texture_get_level_pow2_width(surface
->container
, surface
->texture_level
);
1233 src_height
= wined3d_texture_get_level_pow2_height(surface
->container
, surface
->texture_level
);
1235 /* A depth stencil smaller than the render target is not valid */
1236 if (width
> src_width
|| height
> src_height
) return;
1238 /* Remove any renderbuffer set if the sizes match */
1239 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
]
1240 || (width
== src_width
&& height
== src_height
))
1242 surface
->current_renderbuffer
= NULL
;
1246 /* Look if we've already got a renderbuffer of the correct dimensions */
1247 LIST_FOR_EACH_ENTRY(entry
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1249 if (entry
->width
== width
&& entry
->height
== height
)
1251 renderbuffer
= entry
->id
;
1252 surface
->current_renderbuffer
= entry
;
1259 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
1260 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
1261 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
,
1262 surface
->container
->resource
.format
->glInternal
, width
, height
);
1264 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
1265 entry
->width
= width
;
1266 entry
->height
= height
;
1267 entry
->id
= renderbuffer
;
1268 list_add_head(&surface
->renderbuffers
, &entry
->entry
);
1270 surface
->current_renderbuffer
= entry
;
1273 checkGLcall("set_compatible_renderbuffer");
1276 /* See also float_16_to_32() in wined3d_private.h */
1277 static inline unsigned short float_32_to_16(const float *in
)
1280 float tmp
= fabsf(*in
);
1281 unsigned int mantissa
;
1284 /* Deal with special numbers */
1290 return (*in
< 0.0f
? 0xfc00 : 0x7c00);
1292 if (tmp
< (float)(1u << 10))
1298 } while (tmp
< (float)(1u << 10));
1300 else if (tmp
>= (float)(1u << 11))
1306 } while (tmp
>= (float)(1u << 11));
1309 mantissa
= (unsigned int)tmp
;
1310 if (tmp
- mantissa
>= 0.5f
)
1311 ++mantissa
; /* Round to nearest, away from zero. */
1313 exp
+= 10; /* Normalize the mantissa. */
1314 exp
+= 15; /* Exponent is encoded with excess 15. */
1316 if (exp
> 30) /* too big */
1318 ret
= 0x7c00; /* INF */
1322 /* exp == 0: Non-normalized mantissa. Returns 0x0000 (=0.0) for too small numbers. */
1325 mantissa
= mantissa
>> 1;
1328 ret
= mantissa
& 0x3ff;
1332 ret
= (exp
<< 10) | (mantissa
& 0x3ff);
1335 ret
|= ((*in
< 0.0f
? 1 : 0) << 15); /* Add the sign */
1339 static void convert_r32_float_r16_float(const BYTE
*src
, BYTE
*dst
,
1340 DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
)
1342 unsigned short *dst_s
;
1346 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w
, h
, pitch_in
, pitch_out
);
1348 for (y
= 0; y
< h
; ++y
)
1350 src_f
= (const float *)(src
+ y
* pitch_in
);
1351 dst_s
= (unsigned short *) (dst
+ y
* pitch_out
);
1352 for (x
= 0; x
< w
; ++x
)
1354 dst_s
[x
] = float_32_to_16(src_f
+ x
);
1359 static void convert_r5g6b5_x8r8g8b8(const BYTE
*src
, BYTE
*dst
,
1360 DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
)
1362 static const unsigned char convert_5to8
[] =
1364 0x00, 0x08, 0x10, 0x19, 0x21, 0x29, 0x31, 0x3a,
1365 0x42, 0x4a, 0x52, 0x5a, 0x63, 0x6b, 0x73, 0x7b,
1366 0x84, 0x8c, 0x94, 0x9c, 0xa5, 0xad, 0xb5, 0xbd,
1367 0xc5, 0xce, 0xd6, 0xde, 0xe6, 0xef, 0xf7, 0xff,
1369 static const unsigned char convert_6to8
[] =
1371 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
1372 0x20, 0x24, 0x28, 0x2d, 0x31, 0x35, 0x39, 0x3d,
1373 0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d,
1374 0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d,
1375 0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
1376 0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
1377 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd7, 0xdb, 0xdf,
1378 0xe3, 0xe7, 0xeb, 0xef, 0xf3, 0xf7, 0xfb, 0xff,
1382 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w
, h
, pitch_in
, pitch_out
);
1384 for (y
= 0; y
< h
; ++y
)
1386 const WORD
*src_line
= (const WORD
*)(src
+ y
* pitch_in
);
1387 DWORD
*dst_line
= (DWORD
*)(dst
+ y
* pitch_out
);
1388 for (x
= 0; x
< w
; ++x
)
1390 WORD pixel
= src_line
[x
];
1391 dst_line
[x
] = 0xff000000u
1392 | convert_5to8
[(pixel
& 0xf800u
) >> 11] << 16
1393 | convert_6to8
[(pixel
& 0x07e0u
) >> 5] << 8
1394 | convert_5to8
[(pixel
& 0x001fu
)];
1399 /* We use this for both B8G8R8A8 -> B8G8R8X8 and B8G8R8X8 -> B8G8R8A8, since
1400 * in both cases we're just setting the X / Alpha channel to 0xff. */
1401 static void convert_a8r8g8b8_x8r8g8b8(const BYTE
*src
, BYTE
*dst
,
1402 DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
)
1406 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w
, h
, pitch_in
, pitch_out
);
1408 for (y
= 0; y
< h
; ++y
)
1410 const DWORD
*src_line
= (const DWORD
*)(src
+ y
* pitch_in
);
1411 DWORD
*dst_line
= (DWORD
*)(dst
+ y
* pitch_out
);
1413 for (x
= 0; x
< w
; ++x
)
1415 dst_line
[x
] = 0xff000000 | (src_line
[x
] & 0xffffff);
1420 static inline BYTE
cliptobyte(int x
)
1422 return (BYTE
)((x
< 0) ? 0 : ((x
> 255) ? 255 : x
));
1425 static void convert_yuy2_x8r8g8b8(const BYTE
*src
, BYTE
*dst
,
1426 DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
)
1428 int c2
, d
, e
, r2
= 0, g2
= 0, b2
= 0;
1431 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w
, h
, pitch_in
, pitch_out
);
1433 for (y
= 0; y
< h
; ++y
)
1435 const BYTE
*src_line
= src
+ y
* pitch_in
;
1436 DWORD
*dst_line
= (DWORD
*)(dst
+ y
* pitch_out
);
1437 for (x
= 0; x
< w
; ++x
)
1439 /* YUV to RGB conversion formulas from http://en.wikipedia.org/wiki/YUV:
1440 * C = Y - 16; D = U - 128; E = V - 128;
1441 * R = cliptobyte((298 * C + 409 * E + 128) >> 8);
1442 * G = cliptobyte((298 * C - 100 * D - 208 * E + 128) >> 8);
1443 * B = cliptobyte((298 * C + 516 * D + 128) >> 8);
1444 * Two adjacent YUY2 pixels are stored as four bytes: Y0 U Y1 V .
1445 * U and V are shared between the pixels. */
1446 if (!(x
& 1)) /* For every even pixel, read new U and V. */
1448 d
= (int) src_line
[1] - 128;
1449 e
= (int) src_line
[3] - 128;
1451 g2
= - 100 * d
- 208 * e
+ 128;
1454 c2
= 298 * ((int) src_line
[0] - 16);
1455 dst_line
[x
] = 0xff000000
1456 | cliptobyte((c2
+ r2
) >> 8) << 16 /* red */
1457 | cliptobyte((c2
+ g2
) >> 8) << 8 /* green */
1458 | cliptobyte((c2
+ b2
) >> 8); /* blue */
1459 /* Scale RGB values to 0..255 range,
1460 * then clip them if still not in range (may be negative),
1461 * then shift them within DWORD if necessary. */
1467 static void convert_yuy2_r5g6b5(const BYTE
*src
, BYTE
*dst
,
1468 DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
)
1471 int c2
, d
, e
, r2
= 0, g2
= 0, b2
= 0;
1473 TRACE("Converting %ux%u pixels, pitches %u %u\n", w
, h
, pitch_in
, pitch_out
);
1475 for (y
= 0; y
< h
; ++y
)
1477 const BYTE
*src_line
= src
+ y
* pitch_in
;
1478 WORD
*dst_line
= (WORD
*)(dst
+ y
* pitch_out
);
1479 for (x
= 0; x
< w
; ++x
)
1481 /* YUV to RGB conversion formulas from http://en.wikipedia.org/wiki/YUV:
1482 * C = Y - 16; D = U - 128; E = V - 128;
1483 * R = cliptobyte((298 * C + 409 * E + 128) >> 8);
1484 * G = cliptobyte((298 * C - 100 * D - 208 * E + 128) >> 8);
1485 * B = cliptobyte((298 * C + 516 * D + 128) >> 8);
1486 * Two adjacent YUY2 pixels are stored as four bytes: Y0 U Y1 V .
1487 * U and V are shared between the pixels. */
1488 if (!(x
& 1)) /* For every even pixel, read new U and V. */
1490 d
= (int) src_line
[1] - 128;
1491 e
= (int) src_line
[3] - 128;
1493 g2
= - 100 * d
- 208 * e
+ 128;
1496 c2
= 298 * ((int) src_line
[0] - 16);
1497 dst_line
[x
] = (cliptobyte((c2
+ r2
) >> 8) >> 3) << 11 /* red */
1498 | (cliptobyte((c2
+ g2
) >> 8) >> 2) << 5 /* green */
1499 | (cliptobyte((c2
+ b2
) >> 8) >> 3); /* blue */
1500 /* Scale RGB values to 0..255 range,
1501 * then clip them if still not in range (may be negative),
1502 * then shift them within DWORD if necessary. */
1508 struct d3dfmt_converter_desc
1510 enum wined3d_format_id from
, to
;
1511 void (*convert
)(const BYTE
*src
, BYTE
*dst
, DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
);
1514 static const struct d3dfmt_converter_desc converters
[] =
1516 {WINED3DFMT_R32_FLOAT
, WINED3DFMT_R16_FLOAT
, convert_r32_float_r16_float
},
1517 {WINED3DFMT_B5G6R5_UNORM
, WINED3DFMT_B8G8R8X8_UNORM
, convert_r5g6b5_x8r8g8b8
},
1518 {WINED3DFMT_B8G8R8A8_UNORM
, WINED3DFMT_B8G8R8X8_UNORM
, convert_a8r8g8b8_x8r8g8b8
},
1519 {WINED3DFMT_B8G8R8X8_UNORM
, WINED3DFMT_B8G8R8A8_UNORM
, convert_a8r8g8b8_x8r8g8b8
},
1520 {WINED3DFMT_YUY2
, WINED3DFMT_B8G8R8X8_UNORM
, convert_yuy2_x8r8g8b8
},
1521 {WINED3DFMT_YUY2
, WINED3DFMT_B5G6R5_UNORM
, convert_yuy2_r5g6b5
},
1524 static inline const struct d3dfmt_converter_desc
*find_converter(enum wined3d_format_id from
,
1525 enum wined3d_format_id to
)
1529 for (i
= 0; i
< (sizeof(converters
) / sizeof(*converters
)); ++i
)
1531 if (converters
[i
].from
== from
&& converters
[i
].to
== to
)
1532 return &converters
[i
];
1538 static struct wined3d_texture
*surface_convert_format(struct wined3d_texture
*src_texture
,
1539 unsigned int sub_resource_idx
, const struct wined3d_format
*dst_format
)
1541 unsigned int texture_level
= sub_resource_idx
% src_texture
->level_count
;
1542 const struct wined3d_format
*src_format
= src_texture
->resource
.format
;
1543 struct wined3d_device
*device
= src_texture
->resource
.device
;
1544 const struct d3dfmt_converter_desc
*conv
= NULL
;
1545 struct wined3d_texture
*dst_texture
;
1546 struct wined3d_resource_desc desc
;
1547 struct wined3d_map_desc src_map
;
1549 if (!(conv
= find_converter(src_format
->id
, dst_format
->id
)) && (!device
->d3d_initialized
1550 || !is_identity_fixup(src_format
->color_fixup
) || src_format
->convert
1551 || !is_identity_fixup(dst_format
->color_fixup
) || dst_format
->convert
))
1553 FIXME("Cannot find a conversion function from format %s to %s.\n",
1554 debug_d3dformat(src_format
->id
), debug_d3dformat(dst_format
->id
));
1558 /* FIXME: Multisampled conversion? */
1559 desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
1560 desc
.format
= dst_format
->id
;
1561 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
1562 desc
.multisample_quality
= 0;
1564 desc
.pool
= WINED3D_POOL_SCRATCH
;
1565 desc
.width
= wined3d_texture_get_level_width(src_texture
, texture_level
);
1566 desc
.height
= wined3d_texture_get_level_height(src_texture
, texture_level
);
1569 if (FAILED(wined3d_texture_create(device
, &desc
, 1, 1,
1570 WINED3D_TEXTURE_CREATE_MAPPABLE
| WINED3D_TEXTURE_CREATE_DISCARD
,
1571 NULL
, NULL
, &wined3d_null_parent_ops
, &dst_texture
)))
1573 ERR("Failed to create a destination texture for conversion.\n");
1577 memset(&src_map
, 0, sizeof(src_map
));
1578 if (FAILED(wined3d_resource_map(&src_texture
->resource
, sub_resource_idx
,
1579 &src_map
, NULL
, WINED3D_MAP_READONLY
)))
1581 ERR("Failed to map the source texture.\n");
1582 wined3d_texture_decref(dst_texture
);
1587 struct wined3d_map_desc dst_map
;
1589 memset(&dst_map
, 0, sizeof(dst_map
));
1590 if (FAILED(wined3d_resource_map(&dst_texture
->resource
, 0, &dst_map
, NULL
, 0)))
1592 ERR("Failed to map the destination texture.\n");
1593 wined3d_resource_unmap(&src_texture
->resource
, sub_resource_idx
);
1594 wined3d_texture_decref(dst_texture
);
1598 conv
->convert(src_map
.data
, dst_map
.data
, src_map
.row_pitch
, dst_map
.row_pitch
, desc
.width
, desc
.height
);
1600 wined3d_resource_unmap(&dst_texture
->resource
, 0);
1604 struct wined3d_bo_address data
= {0, src_map
.data
};
1605 RECT src_rect
= {0, 0, desc
.width
, desc
.height
};
1606 const struct wined3d_gl_info
*gl_info
;
1607 struct wined3d_context
*context
;
1608 POINT dst_point
= {0, 0};
1610 TRACE("Using upload conversion.\n");
1611 context
= context_acquire(device
, NULL
);
1612 gl_info
= context
->gl_info
;
1614 wined3d_texture_prepare_texture(dst_texture
, context
, FALSE
);
1615 wined3d_texture_bind_and_dirtify(dst_texture
, context
, FALSE
);
1616 wined3d_surface_upload_data(dst_texture
->sub_resources
[0].u
.surface
, gl_info
, src_format
,
1617 &src_rect
, src_map
.row_pitch
, &dst_point
, FALSE
, wined3d_const_bo_address(&data
));
1619 context_release(context
);
1621 wined3d_texture_validate_location(dst_texture
, 0, WINED3D_LOCATION_TEXTURE_RGB
);
1622 wined3d_texture_invalidate_location(dst_texture
, 0, ~WINED3D_LOCATION_TEXTURE_RGB
);
1624 wined3d_resource_unmap(&src_texture
->resource
, sub_resource_idx
);
1629 static HRESULT
_Blt_ColorFill(BYTE
*buf
, unsigned int width
, unsigned int height
,
1630 unsigned int bpp
, UINT pitch
, DWORD color
)
1637 #define COLORFILL_ROW(type) \
1639 type *d = (type *)buf; \
1640 for (x = 0; x < width; ++x) \
1641 d[x] = (type)color; \
1647 COLORFILL_ROW(BYTE
);
1651 COLORFILL_ROW(WORD
);
1657 for (x
= 0; x
< width
; ++x
, d
+= 3)
1659 d
[0] = (color
) & 0xff;
1660 d
[1] = (color
>> 8) & 0xff;
1661 d
[2] = (color
>> 16) & 0xff;
1666 COLORFILL_ROW(DWORD
);
1670 FIXME("Color fill not implemented for bpp %u!\n", bpp
* 8);
1671 return WINED3DERR_NOTAVAILABLE
;
1674 #undef COLORFILL_ROW
1676 /* Now copy first row. */
1678 for (y
= 1; y
< height
; ++y
)
1681 memcpy(buf
, first
, width
* bpp
);
1687 static void read_from_framebuffer(struct wined3d_surface
*surface
,
1688 struct wined3d_context
*old_ctx
, DWORD dst_location
)
1690 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1691 struct wined3d_texture
*texture
= surface
->container
;
1692 struct wined3d_device
*device
= texture
->resource
.device
;
1693 const struct wined3d_gl_info
*gl_info
;
1694 struct wined3d_context
*context
= old_ctx
;
1695 struct wined3d_surface
*restore_rt
= NULL
;
1696 unsigned int row_pitch
, slice_pitch
;
1697 unsigned int width
, height
;
1699 BYTE
*row
, *top
, *bottom
;
1701 BOOL srcIsUpsideDown
;
1702 struct wined3d_bo_address data
;
1704 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, dst_location
);
1706 restore_rt
= context_get_rt_surface(old_ctx
);
1707 if (restore_rt
!= surface
)
1708 context
= context_acquire(device
, surface
);
1712 context_apply_blit_state(context
, device
);
1713 gl_info
= context
->gl_info
;
1715 /* Select the correct read buffer, and give some debug output.
1716 * There is no need to keep track of the current read buffer or reset it, every part of the code
1717 * that reads sets the read buffer as desired.
1719 if (wined3d_resource_is_offscreen(&texture
->resource
))
1721 /* Mapping the primary render target which is not on a swapchain.
1722 * Read from the back buffer. */
1723 TRACE("Mapping offscreen render target.\n");
1724 gl_info
->gl_ops
.gl
.p_glReadBuffer(context_get_offscreen_gl_buffer(context
));
1725 srcIsUpsideDown
= TRUE
;
1729 /* Onscreen surfaces are always part of a swapchain */
1730 GLenum buffer
= wined3d_texture_get_gl_buffer(texture
);
1731 TRACE("Mapping %#x buffer.\n", buffer
);
1732 gl_info
->gl_ops
.gl
.p_glReadBuffer(buffer
);
1733 checkGLcall("glReadBuffer");
1734 srcIsUpsideDown
= FALSE
;
1737 if (data
.buffer_object
)
1739 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
.buffer_object
));
1740 checkGLcall("glBindBuffer");
1743 wined3d_texture_get_pitch(texture
, surface
->texture_level
, &row_pitch
, &slice_pitch
);
1745 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1746 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ROW_LENGTH
, row_pitch
/ texture
->resource
.format
->byte_count
);
1747 checkGLcall("glPixelStorei");
1749 width
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
1750 height
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
1751 gl_info
->gl_ops
.gl
.p_glReadPixels(0, 0, width
, height
,
1752 texture
->resource
.format
->glFormat
,
1753 texture
->resource
.format
->glType
, data
.addr
);
1754 checkGLcall("glReadPixels");
1756 /* Reset previous pixel store pack state */
1757 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ROW_LENGTH
, 0);
1758 checkGLcall("glPixelStorei");
1760 if (!srcIsUpsideDown
)
1762 /* glReadPixels returns the image upside down, and there is no way to
1763 * prevent this. Flip the lines in software. */
1765 if (!(row
= HeapAlloc(GetProcessHeap(), 0, row_pitch
)))
1768 if (data
.buffer_object
)
1770 mem
= GL_EXTCALL(glMapBuffer(GL_PIXEL_PACK_BUFFER
, GL_READ_WRITE
));
1771 checkGLcall("glMapBuffer");
1777 bottom
= mem
+ row_pitch
* (height
- 1);
1778 for (i
= 0; i
< height
/ 2; i
++)
1780 memcpy(row
, top
, row_pitch
);
1781 memcpy(top
, bottom
, row_pitch
);
1782 memcpy(bottom
, row
, row_pitch
);
1784 bottom
-= row_pitch
;
1786 HeapFree(GetProcessHeap(), 0, row
);
1788 if (data
.buffer_object
)
1789 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_PACK_BUFFER
));
1793 if (data
.buffer_object
)
1795 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
1796 checkGLcall("glBindBuffer");
1800 context_restore(context
, restore_rt
);
1803 /* Read the framebuffer contents into a texture. Note that this function
1804 * doesn't do any kind of flipping. Using this on an onscreen surface will
1805 * result in a flipped D3D texture.
1807 * Context activation is done by the caller. This function may temporarily
1808 * switch to a different context and restore the original one before return. */
1809 void surface_load_fb_texture(struct wined3d_surface
*surface
, BOOL srgb
, struct wined3d_context
*old_ctx
)
1811 struct wined3d_texture
*texture
= surface
->container
;
1812 struct wined3d_device
*device
= texture
->resource
.device
;
1813 const struct wined3d_gl_info
*gl_info
;
1814 struct wined3d_context
*context
= old_ctx
;
1815 struct wined3d_surface
*restore_rt
= NULL
;
1817 restore_rt
= context_get_rt_surface(old_ctx
);
1818 if (restore_rt
!= surface
)
1819 context
= context_acquire(device
, surface
);
1823 gl_info
= context
->gl_info
;
1824 device_invalidate_state(device
, STATE_FRAMEBUFFER
);
1826 wined3d_texture_prepare_texture(texture
, context
, srgb
);
1827 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1829 TRACE("Reading back offscreen render target %p.\n", surface
);
1831 if (wined3d_resource_is_offscreen(&texture
->resource
))
1832 gl_info
->gl_ops
.gl
.p_glReadBuffer(context_get_offscreen_gl_buffer(context
));
1834 gl_info
->gl_ops
.gl
.p_glReadBuffer(wined3d_texture_get_gl_buffer(texture
));
1835 checkGLcall("glReadBuffer");
1837 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(surface
->texture_target
, surface
->texture_level
,
1838 0, 0, 0, 0, wined3d_texture_get_level_width(texture
, surface
->texture_level
),
1839 wined3d_texture_get_level_height(texture
, surface
->texture_level
));
1840 checkGLcall("glCopyTexSubImage2D");
1843 context_restore(context
, restore_rt
);
1846 /* Does a direct frame buffer -> texture copy. Stretching is done with single
1847 * pixel copy calls. */
1848 static void fb_copy_to_texture_direct(struct wined3d_surface
*dst_surface
, struct wined3d_surface
*src_surface
,
1849 const RECT
*src_rect
, const RECT
*dst_rect_in
, enum wined3d_texture_filter_type filter
)
1851 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
1852 struct wined3d_texture
*src_texture
= src_surface
->container
;
1853 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
1854 struct wined3d_device
*device
= dst_texture
->resource
.device
;
1855 const struct wined3d_gl_info
*gl_info
;
1857 struct wined3d_context
*context
;
1858 BOOL upsidedown
= FALSE
;
1859 RECT dst_rect
= *dst_rect_in
;
1860 unsigned int src_height
;
1862 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
1863 * glCopyTexSubImage is a bit picky about the parameters we pass to it
1865 if(dst_rect
.top
> dst_rect
.bottom
) {
1866 UINT tmp
= dst_rect
.bottom
;
1867 dst_rect
.bottom
= dst_rect
.top
;
1872 context
= context_acquire(device
, src_surface
);
1873 gl_info
= context
->gl_info
;
1874 context_apply_blit_state(context
, device
);
1875 wined3d_texture_load(dst_texture
, context
, FALSE
);
1877 /* Bind the target texture */
1878 context_bind_texture(context
, dst_texture
->target
, dst_texture
->texture_rgb
.name
);
1879 if (wined3d_resource_is_offscreen(&src_texture
->resource
))
1881 TRACE("Reading from an offscreen target\n");
1882 upsidedown
= !upsidedown
;
1883 gl_info
->gl_ops
.gl
.p_glReadBuffer(context_get_offscreen_gl_buffer(context
));
1887 gl_info
->gl_ops
.gl
.p_glReadBuffer(wined3d_texture_get_gl_buffer(src_texture
));
1889 checkGLcall("glReadBuffer");
1891 xrel
= (float) (src_rect
->right
- src_rect
->left
) / (float) (dst_rect
.right
- dst_rect
.left
);
1892 yrel
= (float) (src_rect
->bottom
- src_rect
->top
) / (float) (dst_rect
.bottom
- dst_rect
.top
);
1894 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
1896 FIXME_(d3d_perf
)("Doing a pixel by pixel copy from the framebuffer to a texture.\n");
1898 if (filter
!= WINED3D_TEXF_NONE
&& filter
!= WINED3D_TEXF_POINT
)
1899 ERR("Texture filtering not supported in direct blit.\n");
1901 else if ((filter
!= WINED3D_TEXF_NONE
&& filter
!= WINED3D_TEXF_POINT
)
1902 && ((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
1904 ERR("Texture filtering not supported in direct blit\n");
1907 src_height
= wined3d_texture_get_level_height(src_texture
, src_surface
->texture_level
);
1909 && !((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
1910 && !((yrel
- 1.0f
< -eps
) || (yrel
- 1.0f
> eps
)))
1912 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do. */
1913 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
1914 dst_rect
.left
/*xoffset */, dst_rect
.top
/* y offset */,
1915 src_rect
->left
, src_height
- src_rect
->bottom
,
1916 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
1921 UINT yoffset
= src_height
- src_rect
->top
+ dst_rect
.top
- 1;
1922 /* I have to process this row by row to swap the image,
1923 * otherwise it would be upside down, so stretching in y direction
1924 * doesn't cost extra time
1926 * However, stretching in x direction can be avoided if not necessary
1928 for(row
= dst_rect
.top
; row
< dst_rect
.bottom
; row
++) {
1929 if ((xrel
- 1.0f
< -eps
) || (xrel
- 1.0f
> eps
))
1931 /* Well, that stuff works, but it's very slow.
1932 * find a better way instead
1936 for (col
= dst_rect
.left
; col
< dst_rect
.right
; ++col
)
1938 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
1939 dst_rect
.left
+ col
/* x offset */, row
/* y offset */,
1940 src_rect
->left
+ col
* xrel
, yoffset
- (int) (row
* yrel
), 1, 1);
1945 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(dst_surface
->texture_target
, dst_surface
->texture_level
,
1946 dst_rect
.left
/* x offset */, row
/* y offset */,
1947 src_rect
->left
, yoffset
- (int) (row
* yrel
), dst_rect
.right
- dst_rect
.left
, 1);
1951 checkGLcall("glCopyTexSubImage2D");
1953 context_release(context
);
1955 /* The texture is now most up to date - If the surface is a render target
1956 * and has a drawable, this path is never entered. */
1957 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
1958 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1961 /* Uses the hardware to stretch and flip the image */
1962 static void fb_copy_to_texture_hwstretch(struct wined3d_surface
*dst_surface
, struct wined3d_surface
*src_surface
,
1963 const RECT
*src_rect
, const RECT
*dst_rect_in
, enum wined3d_texture_filter_type filter
)
1965 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
1966 unsigned int src_width
, src_height
, src_pow2_width
, src_pow2_height
;
1967 struct wined3d_texture
*src_texture
= src_surface
->container
;
1968 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
1969 struct wined3d_device
*device
= dst_texture
->resource
.device
;
1970 GLuint src
, backup
= 0;
1971 float left
, right
, top
, bottom
; /* Texture coordinates */
1972 const struct wined3d_gl_info
*gl_info
;
1973 struct wined3d_context
*context
;
1974 GLenum drawBuffer
= GL_BACK
;
1975 GLenum offscreen_buffer
;
1976 GLenum texture_target
;
1977 BOOL noBackBufferBackup
;
1979 BOOL upsidedown
= FALSE
;
1980 RECT dst_rect
= *dst_rect_in
;
1982 TRACE("Using hwstretch blit\n");
1983 /* Activate the Proper context for reading from the source surface, set it up for blitting */
1984 context
= context_acquire(device
, src_surface
);
1985 gl_info
= context
->gl_info
;
1986 context_apply_blit_state(context
, device
);
1987 wined3d_texture_load(dst_texture
, context
, FALSE
);
1989 offscreen_buffer
= context_get_offscreen_gl_buffer(context
);
1990 src_width
= wined3d_texture_get_level_width(src_texture
, src_surface
->texture_level
);
1991 src_height
= wined3d_texture_get_level_height(src_texture
, src_surface
->texture_level
);
1992 src_pow2_width
= wined3d_texture_get_level_pow2_width(src_texture
, src_surface
->texture_level
);
1993 src_pow2_height
= wined3d_texture_get_level_pow2_height(src_texture
, src_surface
->texture_level
);
1995 src_offscreen
= wined3d_resource_is_offscreen(&src_texture
->resource
);
1996 noBackBufferBackup
= src_offscreen
&& wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
;
1997 if (!noBackBufferBackup
&& !src_texture
->texture_rgb
.name
)
1999 /* Get it a description */
2000 wined3d_texture_load(src_texture
, context
, FALSE
);
2003 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2004 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2006 if (context
->aux_buffers
>= 2)
2008 /* Got more than one aux buffer? Use the 2nd aux buffer */
2009 drawBuffer
= GL_AUX1
;
2011 else if ((!src_offscreen
|| offscreen_buffer
== GL_BACK
) && context
->aux_buffers
>= 1)
2013 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2014 drawBuffer
= GL_AUX0
;
2017 if (noBackBufferBackup
)
2019 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &backup
);
2020 checkGLcall("glGenTextures");
2021 context_bind_texture(context
, GL_TEXTURE_2D
, backup
);
2022 texture_target
= GL_TEXTURE_2D
;
2026 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2027 * we are reading from the back buffer, the backup can be used as source texture
2029 texture_target
= src_surface
->texture_target
;
2030 context_bind_texture(context
, texture_target
, src_texture
->texture_rgb
.name
);
2031 gl_info
->gl_ops
.gl
.p_glEnable(texture_target
);
2032 checkGLcall("glEnable(texture_target)");
2034 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2035 surface_get_sub_resource(src_surface
)->locations
&= ~WINED3D_LOCATION_TEXTURE_RGB
;
2038 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2039 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2041 if(dst_rect
.top
> dst_rect
.bottom
) {
2042 UINT tmp
= dst_rect
.bottom
;
2043 dst_rect
.bottom
= dst_rect
.top
;
2050 TRACE("Reading from an offscreen target\n");
2051 upsidedown
= !upsidedown
;
2052 gl_info
->gl_ops
.gl
.p_glReadBuffer(offscreen_buffer
);
2056 gl_info
->gl_ops
.gl
.p_glReadBuffer(wined3d_texture_get_gl_buffer(src_texture
));
2059 /* TODO: Only back up the part that will be overwritten */
2060 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(texture_target
, 0, 0, 0, 0, 0, src_width
, src_height
);
2062 checkGLcall("glCopyTexSubImage2D");
2064 /* No issue with overriding these - the sampler is dirty due to blit usage */
2065 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(filter
));
2066 checkGLcall("glTexParameteri");
2067 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
,
2068 wined3d_gl_min_mip_filter(filter
, WINED3D_TEXF_NONE
));
2069 checkGLcall("glTexParameteri");
2071 if (!src_texture
->swapchain
|| src_texture
== src_texture
->swapchain
->back_buffers
[0])
2073 src
= backup
? backup
: src_texture
->texture_rgb
.name
;
2077 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_FRONT
);
2078 checkGLcall("glReadBuffer(GL_FRONT)");
2080 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &src
);
2081 checkGLcall("glGenTextures(1, &src)");
2082 context_bind_texture(context
, GL_TEXTURE_2D
, src
);
2084 /* TODO: Only copy the part that will be read. Use src_rect->left,
2085 * src_rect->bottom as origin, but with the width watch out for power
2087 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, src_pow2_width
,
2088 src_pow2_height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2089 checkGLcall("glTexImage2D");
2090 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(GL_TEXTURE_2D
, 0, 0, 0, 0, 0, src_width
, src_height
);
2092 gl_info
->gl_ops
.gl
.p_glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2093 checkGLcall("glTexParameteri");
2094 gl_info
->gl_ops
.gl
.p_glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2095 checkGLcall("glTexParameteri");
2097 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_BACK
);
2098 checkGLcall("glReadBuffer(GL_BACK)");
2100 if (texture_target
!= GL_TEXTURE_2D
)
2102 gl_info
->gl_ops
.gl
.p_glDisable(texture_target
);
2103 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_2D
);
2104 texture_target
= GL_TEXTURE_2D
;
2107 checkGLcall("glEnd and previous");
2109 left
= src_rect
->left
;
2110 right
= src_rect
->right
;
2114 top
= src_height
- src_rect
->top
;
2115 bottom
= src_height
- src_rect
->bottom
;
2119 top
= src_height
- src_rect
->bottom
;
2120 bottom
= src_height
- src_rect
->top
;
2123 if (src_texture
->flags
& WINED3D_TEXTURE_NORMALIZED_COORDS
)
2125 left
/= src_pow2_width
;
2126 right
/= src_pow2_width
;
2127 top
/= src_pow2_height
;
2128 bottom
/= src_pow2_height
;
2131 /* draw the source texture stretched and upside down. The correct surface is bound already */
2132 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2133 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2135 context_set_draw_buffer(context
, drawBuffer
);
2136 gl_info
->gl_ops
.gl
.p_glReadBuffer(drawBuffer
);
2138 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
2140 gl_info
->gl_ops
.gl
.p_glTexCoord2f(left
, bottom
);
2141 gl_info
->gl_ops
.gl
.p_glVertex2i(0, 0);
2144 gl_info
->gl_ops
.gl
.p_glTexCoord2f(left
, top
);
2145 gl_info
->gl_ops
.gl
.p_glVertex2i(0, dst_rect
.bottom
- dst_rect
.top
);
2148 gl_info
->gl_ops
.gl
.p_glTexCoord2f(right
, top
);
2149 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
2152 gl_info
->gl_ops
.gl
.p_glTexCoord2f(right
, bottom
);
2153 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
.right
- dst_rect
.left
, 0);
2154 gl_info
->gl_ops
.gl
.p_glEnd();
2155 checkGLcall("glEnd and previous");
2157 if (texture_target
!= dst_surface
->texture_target
)
2159 gl_info
->gl_ops
.gl
.p_glDisable(texture_target
);
2160 gl_info
->gl_ops
.gl
.p_glEnable(dst_surface
->texture_target
);
2161 texture_target
= dst_surface
->texture_target
;
2164 /* Now read the stretched and upside down image into the destination texture */
2165 context_bind_texture(context
, texture_target
, dst_texture
->texture_rgb
.name
);
2166 gl_info
->gl_ops
.gl
.p_glCopyTexSubImage2D(texture_target
,
2168 dst_rect
.left
, dst_rect
.top
, /* xoffset, yoffset */
2169 0, 0, /* We blitted the image to the origin */
2170 dst_rect
.right
- dst_rect
.left
, dst_rect
.bottom
- dst_rect
.top
);
2171 checkGLcall("glCopyTexSubImage2D");
2173 if (drawBuffer
== GL_BACK
)
2175 /* Write the back buffer backup back. */
2178 if (texture_target
!= GL_TEXTURE_2D
)
2180 gl_info
->gl_ops
.gl
.p_glDisable(texture_target
);
2181 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_2D
);
2182 texture_target
= GL_TEXTURE_2D
;
2184 context_bind_texture(context
, GL_TEXTURE_2D
, backup
);
2188 if (texture_target
!= src_surface
->texture_target
)
2190 gl_info
->gl_ops
.gl
.p_glDisable(texture_target
);
2191 gl_info
->gl_ops
.gl
.p_glEnable(src_surface
->texture_target
);
2192 texture_target
= src_surface
->texture_target
;
2194 context_bind_texture(context
, src_surface
->texture_target
, src_texture
->texture_rgb
.name
);
2197 gl_info
->gl_ops
.gl
.p_glBegin(GL_QUADS
);
2199 gl_info
->gl_ops
.gl
.p_glTexCoord2f(0.0f
, 0.0f
);
2200 gl_info
->gl_ops
.gl
.p_glVertex2i(0, src_height
);
2203 gl_info
->gl_ops
.gl
.p_glTexCoord2f(0.0f
, (float)src_height
/ (float)src_pow2_height
);
2204 gl_info
->gl_ops
.gl
.p_glVertex2i(0, 0);
2207 gl_info
->gl_ops
.gl
.p_glTexCoord2f((float)src_width
/ (float)src_pow2_width
,
2208 (float)src_height
/ (float)src_pow2_height
);
2209 gl_info
->gl_ops
.gl
.p_glVertex2i(src_width
, 0);
2212 gl_info
->gl_ops
.gl
.p_glTexCoord2f((float)src_width
/ (float)src_pow2_width
, 0.0f
);
2213 gl_info
->gl_ops
.gl
.p_glVertex2i(src_width
, src_height
);
2214 gl_info
->gl_ops
.gl
.p_glEnd();
2216 gl_info
->gl_ops
.gl
.p_glDisable(texture_target
);
2217 checkGLcall("glDisable(texture_target)");
2220 if (src
!= src_texture
->texture_rgb
.name
&& src
!= backup
)
2222 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &src
);
2223 checkGLcall("glDeleteTextures(1, &src)");
2227 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &backup
);
2228 checkGLcall("glDeleteTextures(1, &backup)");
2231 if (wined3d_settings
.strict_draw_ordering
)
2232 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
2234 context_release(context
);
2236 /* The texture is now most up to date - If the surface is a render target
2237 * and has a drawable, this path is never entered. */
2238 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
2239 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
2242 /* Front buffer coordinates are always full screen coordinates, but our GL
2243 * drawable is limited to the window's client area. The sysmem and texture
2244 * copies do have the full screen size. Note that GL has a bottom-left
2245 * origin, while D3D has a top-left origin. */
2246 void surface_translate_drawable_coords(const struct wined3d_surface
*surface
, HWND window
, RECT
*rect
)
2248 struct wined3d_texture
*texture
= surface
->container
;
2249 UINT drawable_height
;
2251 if (texture
->swapchain
&& texture
== texture
->swapchain
->front_buffer
)
2253 POINT offset
= {0, 0};
2256 ScreenToClient(window
, &offset
);
2257 OffsetRect(rect
, offset
.x
, offset
.y
);
2259 GetClientRect(window
, &windowsize
);
2260 drawable_height
= windowsize
.bottom
- windowsize
.top
;
2264 drawable_height
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
2267 rect
->top
= drawable_height
- rect
->top
;
2268 rect
->bottom
= drawable_height
- rect
->bottom
;
2271 /* Context activation is done by the caller. */
2272 static void surface_blt_to_drawable(const struct wined3d_device
*device
,
2273 struct wined3d_context
*old_ctx
,
2274 enum wined3d_texture_filter_type filter
, BOOL alpha_test
,
2275 struct wined3d_surface
*src_surface
, const RECT
*src_rect_in
,
2276 struct wined3d_surface
*dst_surface
, const RECT
*dst_rect_in
)
2278 struct wined3d_texture
*src_texture
= src_surface
->container
;
2279 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
2280 const struct wined3d_gl_info
*gl_info
;
2281 struct wined3d_context
*context
= old_ctx
;
2282 struct wined3d_surface
*restore_rt
= NULL
;
2283 RECT src_rect
, dst_rect
;
2285 src_rect
= *src_rect_in
;
2286 dst_rect
= *dst_rect_in
;
2288 restore_rt
= context_get_rt_surface(old_ctx
);
2289 if (restore_rt
!= dst_surface
)
2290 context
= context_acquire(device
, dst_surface
);
2294 gl_info
= context
->gl_info
;
2296 /* Make sure the surface is up-to-date. This should probably use
2297 * surface_load_location() and worry about the destination surface too,
2298 * unless we're overwriting it completely. */
2299 wined3d_texture_load(src_texture
, context
, FALSE
);
2301 /* Activate the destination context, set it up for blitting */
2302 context_apply_blit_state(context
, device
);
2304 if (!wined3d_resource_is_offscreen(&dst_texture
->resource
))
2305 surface_translate_drawable_coords(dst_surface
, context
->win_handle
, &dst_rect
);
2307 device
->blitter
->set_shader(device
->blit_priv
, context
, src_surface
, NULL
);
2311 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
2312 checkGLcall("glEnable(GL_ALPHA_TEST)");
2314 /* For P8 surfaces, the alpha component contains the palette index.
2315 * Which means that the colorkey is one of the palette entries. In
2316 * other cases pixels that should be masked away have alpha set to 0. */
2317 if (src_texture
->resource
.format
->id
== WINED3DFMT_P8_UINT
)
2318 gl_info
->gl_ops
.gl
.p_glAlphaFunc(GL_NOTEQUAL
,
2319 (float)src_texture
->async
.src_blt_color_key
.color_space_low_value
/ 255.0f
);
2321 gl_info
->gl_ops
.gl
.p_glAlphaFunc(GL_NOTEQUAL
, 0.0f
);
2322 checkGLcall("glAlphaFunc");
2326 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2327 checkGLcall("glDisable(GL_ALPHA_TEST)");
2330 draw_textured_quad(src_surface
, context
, &src_rect
, &dst_rect
, filter
);
2334 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2335 checkGLcall("glDisable(GL_ALPHA_TEST)");
2338 /* Leave the opengl state valid for blitting */
2339 device
->blitter
->unset_shader(context
->gl_info
);
2341 if (wined3d_settings
.strict_draw_ordering
2342 || (dst_texture
->swapchain
&& dst_texture
->swapchain
->front_buffer
== dst_texture
))
2343 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
2346 context_restore(context
, restore_rt
);
2349 HRESULT
surface_color_fill(struct wined3d_surface
*s
, const RECT
*rect
, const struct wined3d_color
*color
)
2351 struct wined3d_resource
*resource
= &s
->container
->resource
;
2352 struct wined3d_device
*device
= resource
->device
;
2353 struct wined3d_rendertarget_view_desc view_desc
;
2354 struct wined3d_rendertarget_view
*view
;
2355 const struct blit_shader
*blitter
;
2358 if (!(blitter
= wined3d_select_blitter(&device
->adapter
->gl_info
, &device
->adapter
->d3d_info
,
2359 WINED3D_BLIT_OP_COLOR_FILL
, NULL
, 0, 0, NULL
, rect
, resource
->usage
, resource
->pool
, resource
->format
)))
2361 FIXME("No blitter is capable of performing the requested color fill operation.\n");
2362 return WINED3DERR_INVALIDCALL
;
2365 view_desc
.format_id
= resource
->format
->id
;
2366 view_desc
.u
.texture
.level_idx
= s
->texture_level
;
2367 view_desc
.u
.texture
.layer_idx
= s
->texture_layer
;
2368 view_desc
.u
.texture
.layer_count
= 1;
2369 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
,
2370 resource
, NULL
, &wined3d_null_parent_ops
, &view
)))
2372 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
2376 hr
= blitter
->color_fill(device
, view
, rect
, color
);
2377 wined3d_rendertarget_view_decref(view
);
2382 static HRESULT
surface_blt_special(struct wined3d_surface
*dst_surface
, const RECT
*dst_rect
,
2383 struct wined3d_surface
*src_surface
, const RECT
*src_rect
, DWORD flags
,
2384 const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
2386 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
2387 struct wined3d_device
*device
= dst_texture
->resource
.device
;
2388 const struct wined3d_surface
*rt
= wined3d_rendertarget_view_get_surface(device
->fb
.render_targets
[0]);
2389 struct wined3d_swapchain
*src_swapchain
, *dst_swapchain
;
2390 struct wined3d_texture
*src_texture
;
2392 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
2393 dst_surface
, wine_dbgstr_rect(dst_rect
), src_surface
, wine_dbgstr_rect(src_rect
),
2394 flags
, fx
, debug_d3dtexturefiltertype(filter
));
2396 /* Get the swapchain. One of the surfaces has to be a primary surface */
2397 if (dst_texture
->resource
.pool
== WINED3D_POOL_SYSTEM_MEM
)
2399 WARN("Destination is in sysmem, rejecting gl blt\n");
2400 return WINED3DERR_INVALIDCALL
;
2403 dst_swapchain
= dst_texture
->swapchain
;
2407 src_texture
= src_surface
->container
;
2408 if (src_texture
->resource
.pool
== WINED3D_POOL_SYSTEM_MEM
)
2410 WARN("Src is in sysmem, rejecting gl blt\n");
2411 return WINED3DERR_INVALIDCALL
;
2414 src_swapchain
= src_texture
->swapchain
;
2419 src_swapchain
= NULL
;
2422 /* Early sort out of cases where no render target is used */
2423 if (!dst_swapchain
&& !src_swapchain
&& src_surface
!= rt
&& dst_surface
!= rt
)
2425 TRACE("No surface is render target, not using hardware blit.\n");
2426 return WINED3DERR_INVALIDCALL
;
2429 /* No destination color keying supported */
2430 if (flags
& (WINED3D_BLT_DST_CKEY
| WINED3D_BLT_DST_CKEY_OVERRIDE
))
2432 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2433 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2434 return WINED3DERR_INVALIDCALL
;
2437 if (dst_swapchain
&& dst_swapchain
== src_swapchain
)
2439 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
2440 return WINED3DERR_INVALIDCALL
;
2443 if (dst_swapchain
&& src_swapchain
)
2445 FIXME("Implement hardware blit between two different swapchains\n");
2446 return WINED3DERR_INVALIDCALL
;
2451 /* Handled with regular texture -> swapchain blit */
2452 if (src_surface
== rt
)
2453 TRACE("Blit from active render target to a swapchain\n");
2455 else if (src_swapchain
&& dst_surface
== rt
)
2457 FIXME("Implement blit from a swapchain to the active render target\n");
2458 return WINED3DERR_INVALIDCALL
;
2461 if ((src_swapchain
|| src_surface
== rt
) && !dst_swapchain
)
2463 unsigned int src_width
, src_height
;
2464 /* Blit from render target to texture */
2467 /* P8 read back is not implemented */
2468 if (src_texture
->resource
.format
->id
== WINED3DFMT_P8_UINT
2469 || dst_texture
->resource
.format
->id
== WINED3DFMT_P8_UINT
)
2471 TRACE("P8 read back not supported by frame buffer to texture blit\n");
2472 return WINED3DERR_INVALIDCALL
;
2475 if (flags
& (WINED3D_BLT_SRC_CKEY
| WINED3D_BLT_SRC_CKEY_OVERRIDE
))
2477 TRACE("Color keying not supported by frame buffer to texture blit\n");
2478 return WINED3DERR_INVALIDCALL
;
2479 /* Destination color key is checked above */
2482 if (dst_rect
->right
- dst_rect
->left
!= src_rect
->right
- src_rect
->left
)
2487 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
2488 * flip the image nor scale it.
2490 * -> If the app asks for an unscaled, upside down copy, just perform one glCopyTexSubImage2D call
2491 * -> If the app wants an image width an unscaled width, copy it line per line
2492 * -> If the app wants an image that is scaled on the x axis, and the destination rectangle is smaller
2493 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
2494 * back buffer. This is slower than reading line per line, thus not used for flipping
2495 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
2496 * pixel by pixel. */
2497 src_width
= wined3d_texture_get_level_width(src_texture
, src_surface
->texture_level
);
2498 src_height
= wined3d_texture_get_level_height(src_texture
, src_surface
->texture_level
);
2499 if (!stretchx
|| dst_rect
->right
- dst_rect
->left
> src_width
2500 || dst_rect
->bottom
- dst_rect
->top
> src_height
)
2502 TRACE("No stretching in x direction, using direct framebuffer -> texture copy.\n");
2503 fb_copy_to_texture_direct(dst_surface
, src_surface
, src_rect
, dst_rect
, filter
);
2507 TRACE("Using hardware stretching to flip / stretch the texture.\n");
2508 fb_copy_to_texture_hwstretch(dst_surface
, src_surface
, src_rect
, dst_rect
, filter
);
2514 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
2515 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
2516 return WINED3DERR_INVALIDCALL
;
2519 /* Context activation is done by the caller. */
2520 static void surface_depth_blt(const struct wined3d_surface
*surface
, struct wined3d_context
*context
,
2521 GLuint texture
, GLint x
, GLint y
, GLsizei w
, GLsizei h
, GLenum target
)
2523 struct wined3d_device
*device
= surface
->container
->resource
.device
;
2524 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2525 GLint compare_mode
= GL_NONE
;
2526 struct blt_info info
;
2527 GLint old_binding
= 0;
2530 gl_info
->gl_ops
.gl
.p_glPushAttrib(GL_ENABLE_BIT
| GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
| GL_VIEWPORT_BIT
);
2532 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2533 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2534 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2535 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2536 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2537 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEPTH_TEST
);
2538 gl_info
->gl_ops
.gl
.p_glDepthFunc(GL_ALWAYS
);
2539 gl_info
->gl_ops
.gl
.p_glDepthMask(GL_TRUE
);
2540 gl_info
->gl_ops
.gl
.p_glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2541 gl_info
->gl_ops
.gl
.p_glViewport(x
, y
, w
, h
);
2542 gl_info
->gl_ops
.gl
.p_glDepthRange(0.0, 1.0);
2544 SetRect(&rect
, 0, h
, w
, 0);
2545 surface_get_blt_info(target
, &rect
,
2546 wined3d_texture_get_level_pow2_width(surface
->container
, surface
->texture_level
),
2547 wined3d_texture_get_level_pow2_height(surface
->container
, surface
->texture_level
), &info
);
2548 context_active_texture(context
, context
->gl_info
, 0);
2549 gl_info
->gl_ops
.gl
.p_glGetIntegerv(info
.binding
, &old_binding
);
2550 gl_info
->gl_ops
.gl
.p_glBindTexture(info
.bind_target
, texture
);
2551 if (gl_info
->supported
[ARB_SHADOW
])
2553 gl_info
->gl_ops
.gl
.p_glGetTexParameteriv(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, &compare_mode
);
2554 if (compare_mode
!= GL_NONE
)
2555 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
2558 device
->shader_backend
->shader_select_depth_blt(device
->shader_priv
,
2559 gl_info
, info
.tex_type
, &surface
->ds_current_size
);
2561 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
2562 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[0]);
2563 gl_info
->gl_ops
.gl
.p_glVertex2f(-1.0f
, -1.0f
);
2564 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[1]);
2565 gl_info
->gl_ops
.gl
.p_glVertex2f(1.0f
, -1.0f
);
2566 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[2]);
2567 gl_info
->gl_ops
.gl
.p_glVertex2f(-1.0f
, 1.0f
);
2568 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(info
.coords
[3]);
2569 gl_info
->gl_ops
.gl
.p_glVertex2f(1.0f
, 1.0f
);
2570 gl_info
->gl_ops
.gl
.p_glEnd();
2572 if (compare_mode
!= GL_NONE
)
2573 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_COMPARE_MODE_ARB
, compare_mode
);
2574 gl_info
->gl_ops
.gl
.p_glBindTexture(info
.bind_target
, old_binding
);
2576 gl_info
->gl_ops
.gl
.p_glPopAttrib();
2578 device
->shader_backend
->shader_deselect_depth_blt(device
->shader_priv
, gl_info
);
2581 void surface_modify_ds_location(struct wined3d_surface
*surface
,
2582 DWORD location
, UINT w
, UINT h
)
2584 struct wined3d_texture
*texture
= surface
->container
;
2585 unsigned int sub_resource_idx
;
2587 TRACE("surface %p, new location %#x, w %u, h %u.\n", surface
, location
, w
, h
);
2589 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
2590 surface
->ds_current_size
.cx
= w
;
2591 surface
->ds_current_size
.cy
= h
;
2592 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
2593 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~location
);
2596 /* Context activation is done by the caller. */
2597 static void surface_load_ds_location(struct wined3d_surface
*surface
, struct wined3d_context
*context
, DWORD location
)
2599 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
2600 struct wined3d_texture
*texture
= surface
->container
;
2601 struct wined3d_device
*device
= texture
->resource
.device
;
2602 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2605 TRACE("surface %p, context %p, new location %#x.\n", surface
, context
, location
);
2607 /* TODO: Make this work for modes other than FBO */
2608 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return;
2610 if (!(texture
->sub_resources
[sub_resource_idx
].locations
& location
))
2612 w
= surface
->ds_current_size
.cx
;
2613 h
= surface
->ds_current_size
.cy
;
2614 surface
->ds_current_size
.cx
= 0;
2615 surface
->ds_current_size
.cy
= 0;
2619 w
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
2620 h
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
2623 if (surface
->current_renderbuffer
)
2625 FIXME("Not supported with fixed up depth stencil.\n");
2629 wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
);
2631 if (location
== WINED3D_LOCATION_TEXTURE_RGB
)
2633 GLint old_binding
= 0;
2636 /* The render target is allowed to be smaller than the depth/stencil
2637 * buffer, so the onscreen depth/stencil buffer is potentially smaller
2638 * than the offscreen surface. Don't overwrite the offscreen surface
2639 * with undefined data. */
2640 w
= min(w
, context
->swapchain
->desc
.backbuffer_width
);
2641 h
= min(h
, context
->swapchain
->desc
.backbuffer_height
);
2643 TRACE("Copying onscreen depth buffer to depth texture.\n");
2645 if (!device
->depth_blt_texture
)
2646 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->depth_blt_texture
);
2648 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
2649 * directly on the FBO texture. That's because we need to flip. */
2650 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
,
2651 context
->swapchain
->front_buffer
->sub_resources
[0].u
.surface
,
2652 NULL
, WINED3D_LOCATION_DRAWABLE
);
2653 if (surface
->texture_target
== GL_TEXTURE_RECTANGLE_ARB
)
2655 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
2656 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
2660 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
2661 bind_target
= GL_TEXTURE_2D
;
2663 gl_info
->gl_ops
.gl
.p_glBindTexture(bind_target
, device
->depth_blt_texture
);
2664 /* We use GL_DEPTH_COMPONENT instead of the surface's specific
2665 * internal format, because the internal format might include stencil
2666 * data. In principle we should copy stencil data as well, but unless
2667 * the driver supports stencil export it's hard to do, and doesn't
2668 * seem to be needed in practice. If the hardware doesn't support
2669 * writing stencil data, the glCopyTexImage2D() call might trigger
2670 * software fallbacks. */
2671 gl_info
->gl_ops
.gl
.p_glCopyTexImage2D(bind_target
, 0, GL_DEPTH_COMPONENT
, 0, 0, w
, h
, 0);
2672 gl_info
->gl_ops
.gl
.p_glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2673 gl_info
->gl_ops
.gl
.p_glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2674 gl_info
->gl_ops
.gl
.p_glTexParameteri(bind_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2675 gl_info
->gl_ops
.gl
.p_glTexParameteri(bind_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2676 gl_info
->gl_ops
.gl
.p_glTexParameteri(bind_target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2677 gl_info
->gl_ops
.gl
.p_glBindTexture(bind_target
, old_binding
);
2679 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
,
2680 NULL
, surface
, WINED3D_LOCATION_TEXTURE_RGB
);
2681 context_set_draw_buffer(context
, GL_NONE
);
2683 /* Do the actual blit */
2684 surface_depth_blt(surface
, context
, device
->depth_blt_texture
, 0, 0, w
, h
, bind_target
);
2685 checkGLcall("depth_blt");
2687 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2689 if (wined3d_settings
.strict_draw_ordering
)
2690 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
2692 else if (location
== WINED3D_LOCATION_DRAWABLE
)
2694 TRACE("Copying depth texture to onscreen depth buffer.\n");
2696 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
,
2697 context
->swapchain
->front_buffer
->sub_resources
[0].u
.surface
,
2698 NULL
, WINED3D_LOCATION_DRAWABLE
);
2699 surface_depth_blt(surface
, context
, texture
->texture_rgb
.name
, 0,
2700 wined3d_texture_get_level_pow2_height(texture
, surface
->texture_level
) - h
,
2701 w
, h
, surface
->texture_target
);
2702 checkGLcall("depth_blt");
2704 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2706 if (wined3d_settings
.strict_draw_ordering
)
2707 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
2711 ERR("Invalid location (%#x) specified.\n", location
);
2715 static DWORD
resource_access_from_location(DWORD location
)
2719 case WINED3D_LOCATION_SYSMEM
:
2720 case WINED3D_LOCATION_USER_MEMORY
:
2721 case WINED3D_LOCATION_BUFFER
:
2722 return WINED3D_RESOURCE_ACCESS_CPU
;
2724 case WINED3D_LOCATION_DRAWABLE
:
2725 case WINED3D_LOCATION_TEXTURE_SRGB
:
2726 case WINED3D_LOCATION_TEXTURE_RGB
:
2727 case WINED3D_LOCATION_RB_MULTISAMPLE
:
2728 case WINED3D_LOCATION_RB_RESOLVED
:
2729 return WINED3D_RESOURCE_ACCESS_GPU
;
2732 FIXME("Unhandled location %#x.\n", location
);
2737 static void surface_copy_simple_location(struct wined3d_surface
*surface
, DWORD location
)
2739 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
2740 struct wined3d_texture
*texture
= surface
->container
;
2741 struct wined3d_device
*device
= texture
->resource
.device
;
2742 struct wined3d_texture_sub_resource
*sub_resource
;
2743 struct wined3d_context
*context
;
2744 const struct wined3d_gl_info
*gl_info
;
2745 struct wined3d_bo_address dst
, src
;
2747 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2748 wined3d_texture_get_memory(texture
, sub_resource_idx
, &dst
, location
);
2749 wined3d_texture_get_memory(texture
, sub_resource_idx
, &src
, sub_resource
->locations
);
2751 if (dst
.buffer_object
)
2753 context
= context_acquire(device
, NULL
);
2754 gl_info
= context
->gl_info
;
2755 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, dst
.buffer_object
));
2756 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER
, 0, sub_resource
->size
, src
.addr
));
2757 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2758 checkGLcall("Upload PBO");
2759 context_release(context
);
2762 if (src
.buffer_object
)
2764 context
= context_acquire(device
, NULL
);
2765 gl_info
= context
->gl_info
;
2766 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, src
.buffer_object
));
2767 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, sub_resource
->size
, dst
.addr
));
2768 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2769 checkGLcall("Download PBO");
2770 context_release(context
);
2773 memcpy(dst
.addr
, src
.addr
, sub_resource
->size
);
2776 /* Context activation is done by the caller. */
2777 static void surface_load_sysmem(struct wined3d_surface
*surface
,
2778 struct wined3d_context
*context
, DWORD dst_location
)
2780 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
2781 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2782 struct wined3d_texture
*texture
= surface
->container
;
2783 struct wined3d_texture_sub_resource
*sub_resource
;
2785 wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, dst_location
);
2787 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2788 if (sub_resource
->locations
& surface_simple_locations
)
2790 surface_copy_simple_location(surface
, dst_location
);
2794 if (sub_resource
->locations
& (WINED3D_LOCATION_RB_MULTISAMPLE
| WINED3D_LOCATION_RB_RESOLVED
))
2795 surface_load_location(surface
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2797 /* Download the surface to system memory. */
2798 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2800 wined3d_texture_bind_and_dirtify(texture
, context
,
2801 !(sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
));
2802 surface_download_data(surface
, gl_info
, dst_location
);
2803 ++texture
->download_count
;
2808 if (sub_resource
->locations
& WINED3D_LOCATION_DRAWABLE
)
2810 read_from_framebuffer(surface
, context
, dst_location
);
2814 FIXME("Can't load surface %p with location flags %s into sysmem.\n",
2815 surface
, wined3d_debug_location(sub_resource
->locations
));
2818 /* Context activation is done by the caller. */
2819 static HRESULT
surface_load_drawable(struct wined3d_surface
*surface
,
2820 struct wined3d_context
*context
)
2822 struct wined3d_texture
*texture
= surface
->container
;
2825 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2826 && wined3d_resource_is_offscreen(&texture
->resource
))
2828 ERR("Trying to load offscreen surface into WINED3D_LOCATION_DRAWABLE.\n");
2829 return WINED3DERR_INVALIDCALL
;
2832 surface_get_rect(surface
, NULL
, &r
);
2833 surface_load_location(surface
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2834 surface_blt_to_drawable(texture
->resource
.device
, context
,
2835 WINED3D_TEXF_POINT
, FALSE
, surface
, &r
, surface
, &r
);
2840 static HRESULT
surface_load_texture(struct wined3d_surface
*surface
,
2841 struct wined3d_context
*context
, BOOL srgb
)
2843 unsigned int width
, height
, src_row_pitch
, src_slice_pitch
, dst_row_pitch
, dst_slice_pitch
;
2844 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
2845 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2846 struct wined3d_texture
*texture
= surface
->container
;
2847 struct wined3d_device
*device
= texture
->resource
.device
;
2848 const struct wined3d_color_key_conversion
*conversion
;
2849 struct wined3d_texture_sub_resource
*sub_resource
;
2850 struct wined3d_bo_address data
;
2851 BYTE
*src_mem
, *dst_mem
= NULL
;
2852 struct wined3d_format format
;
2853 POINT dst_point
= {0, 0};
2856 sub_resource
= surface_get_sub_resource(surface
);
2857 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2858 && wined3d_resource_is_offscreen(&texture
->resource
)
2859 && (sub_resource
->locations
& WINED3D_LOCATION_DRAWABLE
))
2861 surface_load_fb_texture(surface
, srgb
, context
);
2866 width
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
2867 height
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
2868 SetRect(&src_rect
, 0, 0, width
, height
);
2870 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_SRGB
| WINED3D_LOCATION_TEXTURE_RGB
)
2871 && (texture
->resource
.format_flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB
)
2872 && fbo_blit_supported(gl_info
, WINED3D_BLIT_OP_COLOR_BLIT
,
2873 NULL
, texture
->resource
.usage
, texture
->resource
.pool
, texture
->resource
.format
,
2874 NULL
, texture
->resource
.usage
, texture
->resource
.pool
, texture
->resource
.format
))
2877 surface_blt_fbo(device
, context
, WINED3D_TEXF_POINT
, surface
, WINED3D_LOCATION_TEXTURE_RGB
,
2878 &src_rect
, surface
, WINED3D_LOCATION_TEXTURE_SRGB
, &src_rect
);
2880 surface_blt_fbo(device
, context
, WINED3D_TEXF_POINT
, surface
, WINED3D_LOCATION_TEXTURE_SRGB
,
2881 &src_rect
, surface
, WINED3D_LOCATION_TEXTURE_RGB
, &src_rect
);
2886 if (sub_resource
->locations
& (WINED3D_LOCATION_RB_MULTISAMPLE
| WINED3D_LOCATION_RB_RESOLVED
)
2887 && (!srgb
|| (texture
->resource
.format_flags
& WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB
))
2888 && fbo_blit_supported(gl_info
, WINED3D_BLIT_OP_COLOR_BLIT
,
2889 NULL
, texture
->resource
.usage
, texture
->resource
.pool
, texture
->resource
.format
,
2890 NULL
, texture
->resource
.usage
, texture
->resource
.pool
, texture
->resource
.format
))
2892 DWORD src_location
= sub_resource
->locations
& WINED3D_LOCATION_RB_RESOLVED
?
2893 WINED3D_LOCATION_RB_RESOLVED
: WINED3D_LOCATION_RB_MULTISAMPLE
;
2894 DWORD dst_location
= srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
;
2896 surface_blt_fbo(device
, context
, WINED3D_TEXF_POINT
, surface
, src_location
,
2897 &src_rect
, surface
, dst_location
, &src_rect
);
2902 /* Upload from system memory */
2906 if ((sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| texture
->resource
.map_binding
))
2907 == WINED3D_LOCATION_TEXTURE_RGB
)
2909 FIXME_(d3d_perf
)("Downloading RGB surface %p to reload it as sRGB.\n", surface
);
2910 surface_load_location(surface
, context
, texture
->resource
.map_binding
);
2915 if ((sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_SRGB
| texture
->resource
.map_binding
))
2916 == WINED3D_LOCATION_TEXTURE_SRGB
)
2918 FIXME_(d3d_perf
)("Downloading sRGB surface %p to reload it as RGB.\n", surface
);
2919 surface_load_location(surface
, context
, texture
->resource
.map_binding
);
2923 if (!(sub_resource
->locations
& surface_simple_locations
))
2925 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
2926 /* Lets hope we get it from somewhere... */
2927 surface_load_location(surface
, context
, WINED3D_LOCATION_SYSMEM
);
2930 wined3d_texture_prepare_texture(texture
, context
, srgb
);
2931 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
2932 wined3d_texture_get_pitch(texture
, surface
->texture_level
, &src_row_pitch
, &src_slice_pitch
);
2934 format
= *texture
->resource
.format
;
2935 if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
2936 format
= *wined3d_get_format(gl_info
, conversion
->dst_format
);
2938 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
2939 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
2940 * getting called. */
2941 if ((format
.convert
|| conversion
) && texture
->sub_resources
[sub_resource_idx
].buffer_object
)
2943 TRACE("Removing the pbo attached to surface %p.\n", surface
);
2945 surface_load_location(surface
, context
, WINED3D_LOCATION_SYSMEM
);
2946 wined3d_texture_set_map_binding(texture
, WINED3D_LOCATION_SYSMEM
);
2949 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, sub_resource
->locations
);
2952 /* This code is entered for texture formats which need a fixup. */
2953 format
.byte_count
= format
.conv_byte_count
;
2954 wined3d_format_calculate_pitch(&format
, 1, width
, height
, &dst_row_pitch
, &dst_slice_pitch
);
2956 src_mem
= wined3d_texture_map_bo_address(&data
, src_slice_pitch
,
2957 gl_info
, GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READONLY
);
2958 if (!(dst_mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
)))
2960 ERR("Out of memory (%u).\n", dst_slice_pitch
);
2961 context_release(context
);
2962 return E_OUTOFMEMORY
;
2964 format
.convert(src_mem
, dst_mem
, src_row_pitch
, src_slice_pitch
,
2965 dst_row_pitch
, dst_slice_pitch
, width
, height
, 1);
2966 src_row_pitch
= dst_row_pitch
;
2967 wined3d_texture_unmap_bo_address(&data
, gl_info
, GL_PIXEL_UNPACK_BUFFER
);
2969 data
.buffer_object
= 0;
2970 data
.addr
= dst_mem
;
2972 else if (conversion
)
2974 /* This code is only entered for color keying fixups */
2975 struct wined3d_palette
*palette
= NULL
;
2977 wined3d_format_calculate_pitch(&format
, device
->surface_alignment
,
2978 width
, height
, &dst_row_pitch
, &dst_slice_pitch
);
2980 src_mem
= wined3d_texture_map_bo_address(&data
, src_slice_pitch
,
2981 gl_info
, GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READONLY
);
2982 if (!(dst_mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
)))
2984 ERR("Out of memory (%u).\n", dst_slice_pitch
);
2985 context_release(context
);
2986 return E_OUTOFMEMORY
;
2988 if (texture
->swapchain
&& texture
->swapchain
->palette
)
2989 palette
= texture
->swapchain
->palette
;
2990 conversion
->convert(src_mem
, src_row_pitch
, dst_mem
, dst_row_pitch
,
2991 width
, height
, palette
, &texture
->async
.gl_color_key
);
2992 src_row_pitch
= dst_row_pitch
;
2993 wined3d_texture_unmap_bo_address(&data
, gl_info
, GL_PIXEL_UNPACK_BUFFER
);
2995 data
.buffer_object
= 0;
2996 data
.addr
= dst_mem
;
2999 wined3d_surface_upload_data(surface
, gl_info
, &format
, &src_rect
,
3000 src_row_pitch
, &dst_point
, srgb
, wined3d_const_bo_address(&data
));
3002 HeapFree(GetProcessHeap(), 0, dst_mem
);
3007 /* Context activation is done by the caller. */
3008 static void surface_load_renderbuffer(struct wined3d_surface
*surface
, struct wined3d_context
*context
,
3011 struct wined3d_texture
*texture
= surface
->container
;
3012 const RECT rect
= {0, 0,
3013 wined3d_texture_get_level_width(texture
, surface
->texture_level
),
3014 wined3d_texture_get_level_height(texture
, surface
->texture_level
)};
3015 DWORD locations
= surface_get_sub_resource(surface
)->locations
;
3018 if (locations
& WINED3D_LOCATION_RB_MULTISAMPLE
)
3019 src_location
= WINED3D_LOCATION_RB_MULTISAMPLE
;
3020 else if (locations
& WINED3D_LOCATION_RB_RESOLVED
)
3021 src_location
= WINED3D_LOCATION_RB_RESOLVED
;
3022 else if (locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
3023 src_location
= WINED3D_LOCATION_TEXTURE_SRGB
;
3024 else /* surface_blt_fbo will load the source location if necessary. */
3025 src_location
= WINED3D_LOCATION_TEXTURE_RGB
;
3027 surface_blt_fbo(texture
->resource
.device
, context
, WINED3D_TEXF_POINT
,
3028 surface
, src_location
, &rect
, surface
, dst_location
, &rect
);
3031 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
3032 HRESULT
surface_load_location(struct wined3d_surface
*surface
, struct wined3d_context
*context
, DWORD location
)
3034 unsigned int sub_resource_idx
= surface_get_sub_resource_idx(surface
);
3035 struct wined3d_texture
*texture
= surface
->container
;
3036 struct wined3d_texture_sub_resource
*sub_resource
;
3037 unsigned int surface_w
, surface_h
;
3040 TRACE("surface %p, location %s.\n", surface
, wined3d_debug_location(location
));
3042 surface_w
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
3043 surface_h
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
3045 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
3046 if (sub_resource
->locations
& location
&& (!(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
)
3047 || (surface
->ds_current_size
.cx
== surface_w
&& surface
->ds_current_size
.cy
== surface_h
)))
3049 TRACE("Location (%#x) is already up to date.\n", location
);
3055 DWORD required_access
= resource_access_from_location(location
);
3056 if ((texture
->resource
.access_flags
& required_access
) != required_access
)
3057 WARN("Operation requires %#x access, but surface only has %#x.\n",
3058 required_access
, texture
->resource
.access_flags
);
3061 if (sub_resource
->locations
& WINED3D_LOCATION_DISCARDED
)
3063 TRACE("Surface previously discarded, nothing to do.\n");
3064 wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
);
3065 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
3066 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
3070 if (!sub_resource
->locations
)
3072 ERR("Surface %p does not have any up to date location.\n", surface
);
3073 wined3d_texture_validate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
3074 return surface_load_location(surface
, context
, location
);
3077 if (texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
)
3079 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
&& sub_resource
->locations
& WINED3D_LOCATION_DRAWABLE
)
3080 || (location
== WINED3D_LOCATION_DRAWABLE
&& sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
))
3082 surface_load_ds_location(surface
, context
, location
);
3086 FIXME("Unimplemented copy from %s to %s for depth/stencil buffers.\n",
3087 wined3d_debug_location(sub_resource
->locations
), wined3d_debug_location(location
));
3088 return WINED3DERR_INVALIDCALL
;
3093 case WINED3D_LOCATION_USER_MEMORY
:
3094 case WINED3D_LOCATION_SYSMEM
:
3095 case WINED3D_LOCATION_BUFFER
:
3096 surface_load_sysmem(surface
, context
, location
);
3099 case WINED3D_LOCATION_DRAWABLE
:
3100 if (FAILED(hr
= surface_load_drawable(surface
, context
)))
3104 case WINED3D_LOCATION_RB_RESOLVED
:
3105 case WINED3D_LOCATION_RB_MULTISAMPLE
:
3106 surface_load_renderbuffer(surface
, context
, location
);
3109 case WINED3D_LOCATION_TEXTURE_RGB
:
3110 case WINED3D_LOCATION_TEXTURE_SRGB
:
3111 if (FAILED(hr
= surface_load_texture(surface
, context
,
3112 location
== WINED3D_LOCATION_TEXTURE_SRGB
)))
3117 ERR("Don't know how to handle location %#x.\n", location
);
3122 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
3124 if (texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
)
3126 surface
->ds_current_size
.cx
= surface_w
;
3127 surface
->ds_current_size
.cy
= surface_h
;
3133 static HRESULT
ffp_blit_alloc(struct wined3d_device
*device
) { return WINED3D_OK
; }
3134 /* Context activation is done by the caller. */
3135 static void ffp_blit_free(struct wined3d_device
*device
) { }
3137 /* Context activation is done by the caller. */
3138 static HRESULT
ffp_blit_set(void *blit_priv
, struct wined3d_context
*context
, const struct wined3d_surface
*surface
,
3139 const struct wined3d_color_key
*color_key
)
3141 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3143 gl_info
->gl_ops
.gl
.p_glEnable(surface
->container
->target
);
3144 checkGLcall("glEnable(target)");
3149 /* Context activation is done by the caller. */
3150 static void ffp_blit_unset(const struct wined3d_gl_info
*gl_info
)
3152 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3153 checkGLcall("glDisable(GL_TEXTURE_2D)");
3154 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3156 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3157 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3159 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
3161 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
3162 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
3166 static BOOL
ffp_blit_supported(const struct wined3d_gl_info
*gl_info
,
3167 const struct wined3d_d3d_info
*d3d_info
, enum wined3d_blit_op blit_op
,
3168 const RECT
*src_rect
, DWORD src_usage
, enum wined3d_pool src_pool
, const struct wined3d_format
*src_format
,
3169 const RECT
*dst_rect
, DWORD dst_usage
, enum wined3d_pool dst_pool
, const struct wined3d_format
*dst_format
)
3171 if (src_pool
== WINED3D_POOL_SYSTEM_MEM
|| dst_pool
== WINED3D_POOL_SYSTEM_MEM
)
3173 TRACE("Source or destination is in system memory.\n");
3179 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY
:
3180 if (d3d_info
->shader_color_key
)
3182 TRACE("Color keying requires converted textures.\n");
3185 case WINED3D_BLIT_OP_COLOR_BLIT
:
3186 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST
:
3189 TRACE("Checking support for fixup:\n");
3190 dump_color_fixup_desc(src_format
->color_fixup
);
3193 /* We only support identity conversions. */
3194 if (!is_identity_fixup(src_format
->color_fixup
)
3195 || !is_identity_fixup(dst_format
->color_fixup
))
3197 TRACE("Fixups are not supported.\n");
3201 if (!(dst_usage
& WINED3DUSAGE_RENDERTARGET
))
3203 TRACE("Can only blit to render targets.\n");
3208 case WINED3D_BLIT_OP_COLOR_FILL
:
3209 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3211 if (!((dst_format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FBO_ATTACHABLE
)
3212 || (dst_usage
& WINED3DUSAGE_RENDERTARGET
)))
3215 else if (!(dst_usage
& WINED3DUSAGE_RENDERTARGET
))
3217 TRACE("Color fill not supported\n");
3221 /* FIXME: We should reject color fills on formats with fixups,
3222 * but this would break P8 color fills for example. */
3226 case WINED3D_BLIT_OP_DEPTH_FILL
:
3230 TRACE("Unsupported blit_op=%d\n", blit_op
);
3235 static HRESULT
ffp_blit_color_fill(struct wined3d_device
*device
, struct wined3d_rendertarget_view
*view
,
3236 const RECT
*rect
, const struct wined3d_color
*color
)
3238 const RECT draw_rect
= {0, 0, view
->width
, view
->height
};
3239 struct wined3d_fb_state fb
= {&view
, NULL
};
3241 device_clear_render_targets(device
, 1, &fb
, 1, rect
, &draw_rect
, WINED3DCLEAR_TARGET
, color
, 0.0f
, 0);
3246 static HRESULT
ffp_blit_depth_fill(struct wined3d_device
*device
,
3247 struct wined3d_rendertarget_view
*view
, const RECT
*rect
, DWORD clear_flags
,
3248 float depth
, DWORD stencil
)
3250 const RECT draw_rect
= {0, 0, view
->width
, view
->height
};
3251 struct wined3d_fb_state fb
= {NULL
, view
};
3253 device_clear_render_targets(device
, 0, &fb
, 1, rect
, &draw_rect
, clear_flags
, NULL
, depth
, stencil
);
3258 static void ffp_blit_blit_surface(struct wined3d_device
*device
, enum wined3d_blit_op op
, DWORD filter
,
3259 struct wined3d_surface
*src_surface
, const RECT
*src_rect
,
3260 struct wined3d_surface
*dst_surface
, const RECT
*dst_rect
,
3261 const struct wined3d_color_key
*color_key
)
3263 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
3264 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
3265 struct wined3d_texture
*src_texture
= src_surface
->container
;
3266 struct wined3d_context
*context
;
3268 /* Blit from offscreen surface to render target */
3269 struct wined3d_color_key old_blt_key
= src_texture
->async
.src_blt_color_key
;
3270 DWORD old_color_key_flags
= src_texture
->async
.color_key_flags
;
3272 TRACE("Blt from surface %p to rendertarget %p\n", src_surface
, dst_surface
);
3274 wined3d_texture_set_color_key(src_texture
, WINED3D_CKEY_SRC_BLT
, color_key
);
3276 context
= context_acquire(device
, dst_surface
);
3278 if (op
== WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST
)
3279 glEnable(GL_ALPHA_TEST
);
3281 surface_blt_to_drawable(device
, context
, filter
,
3282 !!color_key
, src_surface
, src_rect
, dst_surface
, dst_rect
);
3284 if (op
== WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST
)
3285 glDisable(GL_ALPHA_TEST
);
3287 context_release(context
);
3289 /* Restore the color key parameters */
3290 wined3d_texture_set_color_key(src_texture
, WINED3D_CKEY_SRC_BLT
,
3291 (old_color_key_flags
& WINED3D_CKEY_SRC_BLT
) ? &old_blt_key
: NULL
);
3293 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
, dst_texture
->resource
.draw_binding
);
3294 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
, ~dst_texture
->resource
.draw_binding
);
3297 const struct blit_shader ffp_blit
= {
3303 ffp_blit_color_fill
,
3304 ffp_blit_depth_fill
,
3305 ffp_blit_blit_surface
,
3308 static HRESULT
cpu_blit_alloc(struct wined3d_device
*device
)
3313 /* Context activation is done by the caller. */
3314 static void cpu_blit_free(struct wined3d_device
*device
)
3318 /* Context activation is done by the caller. */
3319 static HRESULT
cpu_blit_set(void *blit_priv
, struct wined3d_context
*context
, const struct wined3d_surface
*surface
,
3320 const struct wined3d_color_key
*color_key
)
3325 /* Context activation is done by the caller. */
3326 static void cpu_blit_unset(const struct wined3d_gl_info
*gl_info
)
3330 static BOOL
cpu_blit_supported(const struct wined3d_gl_info
*gl_info
,
3331 const struct wined3d_d3d_info
*d3d_info
, enum wined3d_blit_op blit_op
,
3332 const RECT
*src_rect
, DWORD src_usage
, enum wined3d_pool src_pool
, const struct wined3d_format
*src_format
,
3333 const RECT
*dst_rect
, DWORD dst_usage
, enum wined3d_pool dst_pool
, const struct wined3d_format
*dst_format
)
3335 if (blit_op
== WINED3D_BLIT_OP_COLOR_FILL
)
3343 static HRESULT
surface_cpu_blt_compressed(const BYTE
*src_data
, BYTE
*dst_data
,
3344 UINT src_pitch
, UINT dst_pitch
, UINT update_w
, UINT update_h
,
3345 const struct wined3d_format
*format
, DWORD flags
, const struct wined3d_blt_fx
*fx
)
3347 UINT row_block_count
;
3348 const BYTE
*src_row
;
3355 row_block_count
= (update_w
+ format
->block_width
- 1) / format
->block_width
;
3359 for (y
= 0; y
< update_h
; y
+= format
->block_height
)
3361 memcpy(dst_row
, src_row
, row_block_count
* format
->block_byte_count
);
3362 src_row
+= src_pitch
;
3363 dst_row
+= dst_pitch
;
3369 if (flags
== WINED3D_BLT_FX
&& fx
->fx
== WINEDDBLTFX_MIRRORUPDOWN
)
3371 src_row
+= (((update_h
/ format
->block_height
) - 1) * src_pitch
);
3375 case WINED3DFMT_DXT1
:
3376 for (y
= 0; y
< update_h
; y
+= format
->block_height
)
3381 BYTE control_row
[4];
3384 const struct block
*s
= (const struct block
*)src_row
;
3385 struct block
*d
= (struct block
*)dst_row
;
3387 for (x
= 0; x
< row_block_count
; ++x
)
3389 d
[x
].color
[0] = s
[x
].color
[0];
3390 d
[x
].color
[1] = s
[x
].color
[1];
3391 d
[x
].control_row
[0] = s
[x
].control_row
[3];
3392 d
[x
].control_row
[1] = s
[x
].control_row
[2];
3393 d
[x
].control_row
[2] = s
[x
].control_row
[1];
3394 d
[x
].control_row
[3] = s
[x
].control_row
[0];
3396 src_row
-= src_pitch
;
3397 dst_row
+= dst_pitch
;
3401 case WINED3DFMT_DXT2
:
3402 case WINED3DFMT_DXT3
:
3403 for (y
= 0; y
< update_h
; y
+= format
->block_height
)
3409 BYTE control_row
[4];
3412 const struct block
*s
= (const struct block
*)src_row
;
3413 struct block
*d
= (struct block
*)dst_row
;
3415 for (x
= 0; x
< row_block_count
; ++x
)
3417 d
[x
].alpha_row
[0] = s
[x
].alpha_row
[3];
3418 d
[x
].alpha_row
[1] = s
[x
].alpha_row
[2];
3419 d
[x
].alpha_row
[2] = s
[x
].alpha_row
[1];
3420 d
[x
].alpha_row
[3] = s
[x
].alpha_row
[0];
3421 d
[x
].color
[0] = s
[x
].color
[0];
3422 d
[x
].color
[1] = s
[x
].color
[1];
3423 d
[x
].control_row
[0] = s
[x
].control_row
[3];
3424 d
[x
].control_row
[1] = s
[x
].control_row
[2];
3425 d
[x
].control_row
[2] = s
[x
].control_row
[1];
3426 d
[x
].control_row
[3] = s
[x
].control_row
[0];
3428 src_row
-= src_pitch
;
3429 dst_row
+= dst_pitch
;
3434 FIXME("Compressed flip not implemented for format %s.\n",
3435 debug_d3dformat(format
->id
));
3440 FIXME("Unsupported blit on compressed surface (format %s, flags %#x, DDFX %#x).\n",
3441 debug_d3dformat(format
->id
), flags
, flags
& WINED3D_BLT_FX
? fx
->fx
: 0);
3446 static HRESULT
surface_cpu_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3447 const struct wined3d_box
*dst_box
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
3448 const struct wined3d_box
*src_box
, DWORD flags
, const struct wined3d_blt_fx
*fx
,
3449 enum wined3d_texture_filter_type filter
)
3451 unsigned int bpp
, src_height
, src_width
, dst_height
, dst_width
, row_byte_count
;
3452 const struct wined3d_format
*src_format
, *dst_format
;
3453 struct wined3d_texture
*converted_texture
= NULL
;
3454 unsigned int src_fmt_flags
, dst_fmt_flags
;
3455 struct wined3d_map_desc dst_map
, src_map
;
3456 const BYTE
*sbase
= NULL
;
3457 HRESULT hr
= WINED3D_OK
;
3458 BOOL same_sub_resource
;
3463 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_box %s, src_texture %p, "
3464 "src_sub_resource_idx %u, src_box %s, flags %#x, fx %p, filter %s.\n",
3465 dst_texture
, dst_sub_resource_idx
, debug_box(dst_box
), src_texture
,
3466 src_sub_resource_idx
, debug_box(src_box
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
3468 if (src_texture
== dst_texture
&& src_sub_resource_idx
== dst_sub_resource_idx
)
3470 same_sub_resource
= TRUE
;
3471 wined3d_resource_map(&dst_texture
->resource
, dst_sub_resource_idx
, &dst_map
, NULL
, 0);
3473 src_format
= dst_texture
->resource
.format
;
3474 dst_format
= src_format
;
3475 dst_fmt_flags
= dst_texture
->resource
.format_flags
;
3476 src_fmt_flags
= dst_fmt_flags
;
3480 same_sub_resource
= FALSE
;
3481 dst_format
= dst_texture
->resource
.format
;
3482 dst_fmt_flags
= dst_texture
->resource
.format_flags
;
3485 if (dst_texture
->resource
.format
->id
!= src_texture
->resource
.format
->id
)
3487 if (!(converted_texture
= surface_convert_format(src_texture
, src_sub_resource_idx
, dst_format
)))
3489 FIXME("Cannot convert %s to %s.\n", debug_d3dformat(src_texture
->resource
.format
->id
),
3490 debug_d3dformat(dst_texture
->resource
.format
->id
));
3491 return WINED3DERR_NOTAVAILABLE
;
3493 src_texture
= converted_texture
;
3494 src_sub_resource_idx
= 0;
3496 wined3d_resource_map(&src_texture
->resource
, src_sub_resource_idx
, &src_map
, NULL
, WINED3D_MAP_READONLY
);
3497 src_format
= src_texture
->resource
.format
;
3498 src_fmt_flags
= src_texture
->resource
.format_flags
;
3502 src_format
= dst_format
;
3503 src_fmt_flags
= dst_fmt_flags
;
3506 wined3d_resource_map(&dst_texture
->resource
, dst_sub_resource_idx
, &dst_map
, dst_box
, 0);
3509 bpp
= dst_format
->byte_count
;
3510 src_height
= src_box
->bottom
- src_box
->top
;
3511 src_width
= src_box
->right
- src_box
->left
;
3512 dst_height
= dst_box
->bottom
- dst_box
->top
;
3513 dst_width
= dst_box
->right
- dst_box
->left
;
3514 row_byte_count
= dst_width
* bpp
;
3517 sbase
= (BYTE
*)src_map
.data
3518 + ((src_box
->top
/ src_format
->block_height
) * src_map
.row_pitch
)
3519 + ((src_box
->left
/ src_format
->block_width
) * src_format
->block_byte_count
);
3520 if (same_sub_resource
)
3521 dbuf
= (BYTE
*)dst_map
.data
3522 + ((dst_box
->top
/ dst_format
->block_height
) * dst_map
.row_pitch
)
3523 + ((dst_box
->left
/ dst_format
->block_width
) * dst_format
->block_byte_count
);
3525 dbuf
= dst_map
.data
;
3527 if (src_fmt_flags
& dst_fmt_flags
& WINED3DFMT_FLAG_BLOCKS
)
3529 TRACE("%s -> %s copy.\n", debug_d3dformat(src_format
->id
), debug_d3dformat(dst_format
->id
));
3531 if (same_sub_resource
)
3533 FIXME("Only plain blits supported on compressed surfaces.\n");
3538 if (src_height
!= dst_height
|| src_width
!= dst_width
)
3540 WARN("Stretching not supported on compressed surfaces.\n");
3541 hr
= WINED3DERR_INVALIDCALL
;
3545 if (!wined3d_texture_check_block_align(src_texture
,
3546 src_sub_resource_idx
% src_texture
->level_count
, src_box
))
3548 WARN("Source rectangle not block-aligned.\n");
3549 hr
= WINED3DERR_INVALIDCALL
;
3553 if (!wined3d_texture_check_block_align(dst_texture
,
3554 dst_sub_resource_idx
% dst_texture
->level_count
, dst_box
))
3556 WARN("Destination rectangle not block-aligned.\n");
3557 hr
= WINED3DERR_INVALIDCALL
;
3561 hr
= surface_cpu_blt_compressed(sbase
, dbuf
,
3562 src_map
.row_pitch
, dst_map
.row_pitch
, dst_width
, dst_height
,
3563 src_format
, flags
, fx
);
3567 /* First, all the 'source-less' blits */
3568 if (flags
& WINED3D_BLT_COLOR_FILL
)
3570 hr
= _Blt_ColorFill(dbuf
, dst_width
, dst_height
, bpp
, dst_map
.row_pitch
, fx
->fill_color
);
3571 flags
&= ~WINED3D_BLT_COLOR_FILL
;
3574 if (flags
& WINED3D_BLT_DEPTH_FILL
)
3575 FIXME("WINED3D_BLT_DEPTH_FILL needs to be implemented!\n");
3577 /* Now the 'with source' blits. */
3580 int sx
, xinc
, sy
, yinc
;
3582 if (!dst_width
|| !dst_height
) /* Hmm... stupid program? */
3585 if (filter
!= WINED3D_TEXF_NONE
&& filter
!= WINED3D_TEXF_POINT
3586 && (src_width
!= dst_width
|| src_height
!= dst_height
))
3588 /* Can happen when d3d9 apps do a StretchRect() call which isn't handled in GL. */
3589 FIXME("Filter %s not supported in software blit.\n", debug_d3dtexturefiltertype(filter
));
3592 xinc
= (src_width
<< 16) / dst_width
;
3593 yinc
= (src_height
<< 16) / dst_height
;
3597 /* No effects, we can cheat here. */
3598 if (dst_width
== src_width
)
3600 if (dst_height
== src_height
)
3602 /* No stretching in either direction. This needs to be as
3603 * fast as possible. */
3606 /* Check for overlapping surfaces. */
3607 if (!same_sub_resource
|| dst_box
->top
< src_box
->top
3608 || dst_box
->right
<= src_box
->left
|| src_box
->right
<= dst_box
->left
)
3610 /* No overlap, or dst above src, so copy from top downwards. */
3611 for (y
= 0; y
< dst_height
; ++y
)
3613 memcpy(dbuf
, sbuf
, row_byte_count
);
3614 sbuf
+= src_map
.row_pitch
;
3615 dbuf
+= dst_map
.row_pitch
;
3618 else if (dst_box
->top
> src_box
->top
)
3620 /* Copy from bottom upwards. */
3621 sbuf
+= src_map
.row_pitch
* dst_height
;
3622 dbuf
+= dst_map
.row_pitch
* dst_height
;
3623 for (y
= 0; y
< dst_height
; ++y
)
3625 sbuf
-= src_map
.row_pitch
;
3626 dbuf
-= dst_map
.row_pitch
;
3627 memcpy(dbuf
, sbuf
, row_byte_count
);
3632 /* Src and dst overlapping on the same line, use memmove. */
3633 for (y
= 0; y
< dst_height
; ++y
)
3635 memmove(dbuf
, sbuf
, row_byte_count
);
3636 sbuf
+= src_map
.row_pitch
;
3637 dbuf
+= dst_map
.row_pitch
;
3643 /* Stretching in y direction only. */
3644 for (y
= sy
= 0; y
< dst_height
; ++y
, sy
+= yinc
)
3646 sbuf
= sbase
+ (sy
>> 16) * src_map
.row_pitch
;
3647 memcpy(dbuf
, sbuf
, row_byte_count
);
3648 dbuf
+= dst_map
.row_pitch
;
3654 /* Stretching in X direction. */
3656 for (y
= sy
= 0; y
< dst_height
; ++y
, sy
+= yinc
)
3658 sbuf
= sbase
+ (sy
>> 16) * src_map
.row_pitch
;
3660 if ((sy
>> 16) == (last_sy
>> 16))
3662 /* This source row is the same as last source row -
3663 * Copy the already stretched row. */
3664 memcpy(dbuf
, dbuf
- dst_map
.row_pitch
, row_byte_count
);
3668 #define STRETCH_ROW(type) \
3670 const type *s = (const type *)sbuf; \
3671 type *d = (type *)dbuf; \
3672 for (x = sx = 0; x < dst_width; ++x, sx += xinc) \
3673 d[x] = s[sx >> 16]; \
3691 for (x
= sx
= 0; x
< dst_width
; x
++, sx
+= xinc
)
3695 s
= sbuf
+ 3 * (sx
>> 16);
3696 pixel
= s
[0] | (s
[1] << 8) | (s
[2] << 16);
3697 d
[0] = (pixel
) & 0xff;
3698 d
[1] = (pixel
>> 8) & 0xff;
3699 d
[2] = (pixel
>> 16) & 0xff;
3705 FIXME("Stretched blit not implemented for bpp %u!\n", bpp
* 8);
3706 hr
= WINED3DERR_NOTAVAILABLE
;
3711 dbuf
+= dst_map
.row_pitch
;
3718 LONG dstyinc
= dst_map
.row_pitch
, dstxinc
= bpp
;
3719 DWORD keylow
= 0xffffffff, keyhigh
= 0, keymask
= 0xffffffff;
3720 DWORD destkeylow
= 0x0, destkeyhigh
= 0xffffffff, destkeymask
= 0xffffffff;
3721 if (flags
& (WINED3D_BLT_SRC_CKEY
| WINED3D_BLT_DST_CKEY
3722 | WINED3D_BLT_SRC_CKEY_OVERRIDE
| WINED3D_BLT_DST_CKEY_OVERRIDE
))
3724 /* The color keying flags are checked for correctness in ddraw */
3725 if (flags
& WINED3D_BLT_SRC_CKEY
)
3727 keylow
= src_texture
->async
.src_blt_color_key
.color_space_low_value
;
3728 keyhigh
= src_texture
->async
.src_blt_color_key
.color_space_high_value
;
3730 else if (flags
& WINED3D_BLT_SRC_CKEY_OVERRIDE
)
3732 keylow
= fx
->src_color_key
.color_space_low_value
;
3733 keyhigh
= fx
->src_color_key
.color_space_high_value
;
3736 if (flags
& WINED3D_BLT_DST_CKEY
)
3738 /* Destination color keys are taken from the source surface! */
3739 destkeylow
= src_texture
->async
.dst_blt_color_key
.color_space_low_value
;
3740 destkeyhigh
= src_texture
->async
.dst_blt_color_key
.color_space_high_value
;
3742 else if (flags
& WINED3D_BLT_DST_CKEY_OVERRIDE
)
3744 destkeylow
= fx
->dst_color_key
.color_space_low_value
;
3745 destkeyhigh
= fx
->dst_color_key
.color_space_high_value
;
3755 get_color_masks(src_format
, masks
);
3760 flags
&= ~(WINED3D_BLT_SRC_CKEY
| WINED3D_BLT_DST_CKEY
3761 | WINED3D_BLT_SRC_CKEY_OVERRIDE
| WINED3D_BLT_DST_CKEY_OVERRIDE
);
3764 if (flags
& WINED3D_BLT_FX
)
3766 BYTE
*dTopLeft
, *dTopRight
, *dBottomLeft
, *dBottomRight
, *tmp
;
3769 dTopRight
= dbuf
+ ((dst_width
- 1) * bpp
);
3770 dBottomLeft
= dTopLeft
+ ((dst_height
- 1) * dst_map
.row_pitch
);
3771 dBottomRight
= dBottomLeft
+ ((dst_width
- 1) * bpp
);
3773 if (fx
->fx
& WINEDDBLTFX_ARITHSTRETCHY
)
3775 /* I don't think we need to do anything about this flag */
3776 WARN("Nothing done for WINEDDBLTFX_ARITHSTRETCHY.\n");
3778 if (fx
->fx
& WINEDDBLTFX_MIRRORLEFTRIGHT
)
3781 dTopRight
= dTopLeft
;
3784 dBottomRight
= dBottomLeft
;
3786 dstxinc
= dstxinc
* -1;
3788 if (fx
->fx
& WINEDDBLTFX_MIRRORUPDOWN
)
3791 dTopLeft
= dBottomLeft
;
3794 dTopRight
= dBottomRight
;
3796 dstyinc
= dstyinc
* -1;
3798 if (fx
->fx
& WINEDDBLTFX_NOTEARING
)
3800 /* I don't think we need to do anything about this flag */
3801 WARN("Nothing done for WINEDDBLTFX_NOTEARING.\n");
3803 if (fx
->fx
& WINEDDBLTFX_ROTATE180
)
3806 dBottomRight
= dTopLeft
;
3809 dBottomLeft
= dTopRight
;
3811 dstxinc
= dstxinc
* -1;
3812 dstyinc
= dstyinc
* -1;
3814 if (fx
->fx
& WINEDDBLTFX_ROTATE270
)
3817 dTopLeft
= dBottomLeft
;
3818 dBottomLeft
= dBottomRight
;
3819 dBottomRight
= dTopRight
;
3824 dstxinc
= dstxinc
* -1;
3826 if (fx
->fx
& WINEDDBLTFX_ROTATE90
)
3829 dTopLeft
= dTopRight
;
3830 dTopRight
= dBottomRight
;
3831 dBottomRight
= dBottomLeft
;
3836 dstyinc
= dstyinc
* -1;
3838 if (fx
->fx
& WINEDDBLTFX_ZBUFFERBASEDEST
)
3840 /* I don't think we need to do anything about this flag */
3841 WARN("Nothing done for WINEDDBLTFX_ZBUFFERBASEDEST.\n");
3844 flags
&= ~(WINED3D_BLT_FX
);
3847 #define COPY_COLORKEY_FX(type) \
3850 type *d = (type *)dbuf, *dx, tmp; \
3851 for (y = sy = 0; y < dst_height; ++y, sy += yinc) \
3853 s = (const type *)(sbase + (sy >> 16) * src_map.row_pitch); \
3855 for (x = sx = 0; x < dst_width; ++x, sx += xinc) \
3857 tmp = s[sx >> 16]; \
3858 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) \
3859 && ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) \
3863 dx = (type *)(((BYTE *)dx) + dstxinc); \
3865 d = (type *)(((BYTE *)d) + dstyinc); \
3872 COPY_COLORKEY_FX(BYTE
);
3875 COPY_COLORKEY_FX(WORD
);
3878 COPY_COLORKEY_FX(DWORD
);
3883 BYTE
*d
= dbuf
, *dx
;
3884 for (y
= sy
= 0; y
< dst_height
; ++y
, sy
+= yinc
)
3886 sbuf
= sbase
+ (sy
>> 16) * src_map
.row_pitch
;
3888 for (x
= sx
= 0; x
< dst_width
; ++x
, sx
+= xinc
)
3890 DWORD pixel
, dpixel
= 0;
3891 s
= sbuf
+ 3 * (sx
>>16);
3892 pixel
= s
[0] | (s
[1] << 8) | (s
[2] << 16);
3893 dpixel
= dx
[0] | (dx
[1] << 8 ) | (dx
[2] << 16);
3894 if (((pixel
& keymask
) < keylow
|| (pixel
& keymask
) > keyhigh
)
3895 && ((dpixel
& keymask
) >= destkeylow
|| (dpixel
& keymask
) <= keyhigh
))
3897 dx
[0] = (pixel
) & 0xff;
3898 dx
[1] = (pixel
>> 8) & 0xff;
3899 dx
[2] = (pixel
>> 16) & 0xff;
3908 FIXME("%s color-keyed blit not implemented for bpp %u!\n",
3909 (flags
& WINED3D_BLT_SRC_CKEY
) ? "Source" : "Destination", bpp
* 8);
3910 hr
= WINED3DERR_NOTAVAILABLE
;
3912 #undef COPY_COLORKEY_FX
3919 FIXME(" Unsupported flags %#x.\n", flags
);
3922 wined3d_resource_unmap(&dst_texture
->resource
, dst_sub_resource_idx
);
3923 if (src_texture
&& !same_sub_resource
)
3924 wined3d_resource_unmap(&src_texture
->resource
, src_sub_resource_idx
);
3925 if (converted_texture
)
3926 wined3d_texture_decref(converted_texture
);
3931 static HRESULT
cpu_blit_color_fill(struct wined3d_device
*device
, struct wined3d_rendertarget_view
*view
,
3932 const RECT
*rect
, const struct wined3d_color
*color
)
3934 const struct wined3d_box box
= {rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1};
3935 static const struct wined3d_box src_box
;
3936 struct wined3d_blt_fx fx
;
3938 fx
.fill_color
= wined3d_format_convert_from_float(view
->format
, color
);
3939 return surface_cpu_blt(texture_from_resource(view
->resource
), view
->sub_resource_idx
,
3940 &box
, NULL
, 0, &src_box
, WINED3D_BLT_COLOR_FILL
, &fx
, WINED3D_TEXF_POINT
);
3943 static HRESULT
cpu_blit_depth_fill(struct wined3d_device
*device
,
3944 struct wined3d_rendertarget_view
*view
, const RECT
*rect
, DWORD clear_flags
,
3945 float depth
, DWORD stencil
)
3947 FIXME("Depth/stencil filling not implemented by cpu_blit.\n");
3948 return WINED3DERR_INVALIDCALL
;
3951 static void cpu_blit_blit_surface(struct wined3d_device
*device
, enum wined3d_blit_op op
, DWORD filter
,
3952 struct wined3d_surface
*src_surface
, const RECT
*src_rect
,
3953 struct wined3d_surface
*dst_surface
, const RECT
*dst_rect
,
3954 const struct wined3d_color_key
*color_key
)
3956 /* FIXME: Remove error returns from surface_blt_cpu. */
3957 ERR("Blit method not implemented by cpu_blit.\n");
3960 const struct blit_shader cpu_blit
= {
3966 cpu_blit_color_fill
,
3967 cpu_blit_depth_fill
,
3968 cpu_blit_blit_surface
,
3971 HRESULT
wined3d_surface_blt(struct wined3d_surface
*dst_surface
, const RECT
*dst_rect
,
3972 struct wined3d_surface
*src_surface
, const RECT
*src_rect
, DWORD flags
,
3973 const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
3975 struct wined3d_box dst_box
= {dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, 0, 1};
3976 struct wined3d_box src_box
= {src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
, 0, 1};
3977 unsigned int dst_sub_resource_idx
= surface_get_sub_resource_idx(dst_surface
);
3978 struct wined3d_texture
*dst_texture
= dst_surface
->container
;
3979 struct wined3d_device
*device
= dst_texture
->resource
.device
;
3980 struct wined3d_swapchain
*src_swapchain
, *dst_swapchain
;
3981 struct wined3d_texture
*src_texture
= NULL
;
3982 unsigned int dst_w
, dst_h
, src_w
, src_h
;
3983 unsigned int src_sub_resource_idx
= 0;
3984 DWORD src_ds_flags
, dst_ds_flags
;
3985 BOOL scale
, convert
;
3987 static const DWORD simple_blit
= WINED3D_BLT_ASYNC
3988 | WINED3D_BLT_COLOR_FILL
3989 | WINED3D_BLT_SRC_CKEY
3990 | WINED3D_BLT_SRC_CKEY_OVERRIDE
3992 | WINED3D_BLT_DEPTH_FILL
3993 | WINED3D_BLT_DO_NOT_WAIT
3994 | WINED3D_BLT_ALPHA_TEST
;
3996 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3997 dst_surface
, wine_dbgstr_rect(dst_rect
), src_surface
, wine_dbgstr_rect(src_rect
),
3998 flags
, fx
, debug_d3dtexturefiltertype(filter
));
3999 TRACE("Usage is %s.\n", debug_d3dusage(dst_texture
->resource
.usage
));
4003 TRACE("fx %#x.\n", fx
->fx
);
4004 TRACE("fill_color 0x%08x.\n", fx
->fill_color
);
4005 TRACE("dst_color_key {0x%08x, 0x%08x}.\n",
4006 fx
->dst_color_key
.color_space_low_value
,
4007 fx
->dst_color_key
.color_space_high_value
);
4008 TRACE("src_color_key {0x%08x, 0x%08x}.\n",
4009 fx
->src_color_key
.color_space_low_value
,
4010 fx
->src_color_key
.color_space_high_value
);
4015 src_texture
= src_surface
->container
;
4016 src_sub_resource_idx
= surface_get_sub_resource_idx(src_surface
);
4019 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
4020 || (src_texture
&& src_texture
->sub_resources
[src_sub_resource_idx
].map_count
))
4022 WARN("Surface is busy, returning WINEDDERR_SURFACEBUSY.\n");
4023 return WINEDDERR_SURFACEBUSY
;
4026 dst_w
= wined3d_texture_get_level_width(dst_texture
, dst_surface
->texture_level
);
4027 dst_h
= wined3d_texture_get_level_height(dst_texture
, dst_surface
->texture_level
);
4028 if (IsRectEmpty(dst_rect
) || dst_rect
->left
> dst_w
|| dst_rect
->left
< 0
4029 || dst_rect
->top
> dst_h
|| dst_rect
->top
< 0
4030 || dst_rect
->right
> dst_w
|| dst_rect
->right
< 0
4031 || dst_rect
->bottom
> dst_h
|| dst_rect
->bottom
< 0)
4033 WARN("The application gave us a bad destination rectangle.\n");
4034 return WINEDDERR_INVALIDRECT
;
4039 src_w
= wined3d_texture_get_level_width(src_texture
, src_surface
->texture_level
);
4040 src_h
= wined3d_texture_get_level_height(src_texture
, src_surface
->texture_level
);
4041 if (IsRectEmpty(src_rect
) || src_rect
->left
> src_w
|| src_rect
->left
< 0
4042 || src_rect
->top
> src_h
|| src_rect
->top
< 0
4043 || src_rect
->right
> src_w
|| src_rect
->right
< 0
4044 || src_rect
->bottom
> src_h
|| src_rect
->bottom
< 0)
4046 WARN("The application gave us a bad source rectangle.\n");
4047 return WINEDDERR_INVALIDRECT
;
4051 if (!fx
|| !(fx
->fx
))
4052 flags
&= ~WINED3D_BLT_FX
;
4054 if (flags
& WINED3D_BLT_WAIT
)
4055 flags
&= ~WINED3D_BLT_WAIT
;
4057 if (flags
& WINED3D_BLT_ASYNC
)
4059 static unsigned int once
;
4062 FIXME("Can't handle WINED3D_BLT_ASYNC flag.\n");
4063 flags
&= ~WINED3D_BLT_ASYNC
;
4066 /* WINED3D_BLT_DO_NOT_WAIT appeared in DX7. */
4067 if (flags
& WINED3D_BLT_DO_NOT_WAIT
)
4069 static unsigned int once
;
4072 FIXME("Can't handle WINED3D_BLT_DO_NOT_WAIT flag.\n");
4073 flags
&= ~WINED3D_BLT_DO_NOT_WAIT
;
4076 if (!device
->d3d_initialized
)
4078 WARN("D3D not initialized, using fallback.\n");
4082 /* We want to avoid invalidating the sysmem location for converted
4083 * surfaces, since otherwise we'd have to convert the data back when
4085 if (dst_texture
->flags
& WINED3D_TEXTURE_CONVERTED
|| dst_texture
->resource
.format
->convert
4086 || wined3d_format_get_color_key_conversion(dst_texture
, TRUE
))
4088 WARN_(d3d_perf
)("Converted surface, using CPU blit.\n");
4092 if (flags
& ~simple_blit
)
4094 WARN_(d3d_perf
)("Using fallback for complex blit (%#x).\n", flags
);
4099 src_swapchain
= src_texture
->swapchain
;
4101 src_swapchain
= NULL
;
4103 dst_swapchain
= dst_texture
->swapchain
;
4105 /* This isn't strictly needed. FBO blits for example could deal with
4106 * cross-swapchain blits by first downloading the source to a texture
4107 * before switching to the destination context. We just have this here to
4108 * not have to deal with the issue, since cross-swapchain blits should be
4110 if (src_swapchain
&& dst_swapchain
&& src_swapchain
!= dst_swapchain
)
4112 FIXME("Using fallback for cross-swapchain blit.\n");
4117 && (src_rect
->right
- src_rect
->left
!= dst_rect
->right
- dst_rect
->left
4118 || src_rect
->bottom
- src_rect
->top
!= dst_rect
->bottom
- dst_rect
->top
);
4119 convert
= src_texture
&& src_texture
->resource
.format
->id
!= dst_texture
->resource
.format
->id
;
4121 dst_ds_flags
= dst_texture
->resource
.format_flags
4122 & (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
);
4124 src_ds_flags
= src_texture
->resource
.format_flags
4125 & (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
);
4129 if (src_ds_flags
|| dst_ds_flags
)
4131 if (flags
& WINED3D_BLT_DEPTH_FILL
)
4135 TRACE("Depth fill.\n");
4137 if (!surface_convert_depth_to_float(dst_surface
, fx
->fill_color
, &depth
))
4138 return WINED3DERR_INVALIDCALL
;
4140 if (SUCCEEDED(wined3d_surface_depth_fill(dst_surface
, dst_rect
, depth
)))
4145 if (src_ds_flags
!= dst_ds_flags
)
4147 WARN("Rejecting depth / stencil blit between incompatible formats.\n");
4148 return WINED3DERR_INVALIDCALL
;
4151 if (SUCCEEDED(wined3d_surface_depth_blt(src_surface
, src_texture
->resource
.draw_binding
,
4152 src_rect
, dst_surface
, dst_texture
->resource
.draw_binding
, dst_rect
)))
4158 struct wined3d_texture_sub_resource
*src_sub_resource
, *dst_sub_resource
;
4159 const struct blit_shader
*blitter
;
4161 dst_sub_resource
= surface_get_sub_resource(dst_surface
);
4162 src_sub_resource
= src_texture
? &src_texture
->sub_resources
[src_sub_resource_idx
] : NULL
;
4164 /* In principle this would apply to depth blits as well, but we don't
4165 * implement those in the CPU blitter at the moment. */
4166 if ((dst_sub_resource
->locations
& dst_texture
->resource
.map_binding
)
4167 && (!src_texture
|| (src_sub_resource
->locations
& src_texture
->resource
.map_binding
)))
4170 TRACE("Not doing sysmem blit because of scaling.\n");
4172 TRACE("Not doing sysmem blit because of format conversion.\n");
4177 if (flags
& WINED3D_BLT_COLOR_FILL
)
4179 struct wined3d_color color
;
4180 const struct wined3d_palette
*palette
= dst_swapchain
? dst_swapchain
->palette
: NULL
;
4182 TRACE("Color fill.\n");
4184 if (!wined3d_format_convert_color_to_float(dst_texture
->resource
.format
,
4185 palette
, fx
->fill_color
, &color
))
4188 if (SUCCEEDED(surface_color_fill(dst_surface
, dst_rect
, &color
)))
4193 enum wined3d_blit_op blit_op
= WINED3D_BLIT_OP_COLOR_BLIT
;
4194 const struct wined3d_color_key
*color_key
= NULL
;
4196 TRACE("Color blit.\n");
4197 if (flags
& WINED3D_BLT_SRC_CKEY_OVERRIDE
)
4199 color_key
= &fx
->src_color_key
;
4200 blit_op
= WINED3D_BLIT_OP_COLOR_BLIT_CKEY
;
4202 else if (flags
& WINED3D_BLT_SRC_CKEY
)
4204 color_key
= &src_texture
->async
.src_blt_color_key
;
4205 blit_op
= WINED3D_BLIT_OP_COLOR_BLIT_CKEY
;
4207 else if (flags
& WINED3D_BLT_ALPHA_TEST
)
4209 blit_op
= WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST
;
4211 else if ((src_sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
4212 && !(dst_sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
))
4216 TRACE("Not doing upload because of scaling.\n");
4218 TRACE("Not doing upload because of format conversion.\n");
4221 POINT dst_point
= {dst_rect
->left
, dst_rect
->top
};
4223 if (SUCCEEDED(surface_upload_from_surface(dst_surface
, &dst_point
, src_surface
, src_rect
)))
4225 if (!wined3d_resource_is_offscreen(&dst_texture
->resource
))
4227 struct wined3d_context
*context
= context_acquire(device
, dst_surface
);
4228 surface_load_location(dst_surface
, context
, dst_texture
->resource
.draw_binding
);
4229 context_release(context
);
4235 else if (dst_swapchain
&& dst_swapchain
->back_buffers
4236 && dst_texture
== dst_swapchain
->front_buffer
4237 && src_texture
== dst_swapchain
->back_buffers
[0])
4239 /* Use present for back -> front blits. The idea behind this is
4240 * that present is potentially faster than a blit, in particular
4241 * when FBO blits aren't available. Some ddraw applications like
4242 * Half-Life and Prince of Persia 3D use Blt() from the backbuffer
4243 * to the frontbuffer instead of doing a Flip(). D3D8 and D3D9
4244 * applications can't blit directly to the frontbuffer. */
4245 enum wined3d_swap_effect swap_effect
= dst_swapchain
->desc
.swap_effect
;
4247 TRACE("Using present for backbuffer -> frontbuffer blit.\n");
4249 /* Set the swap effect to COPY, we don't want the backbuffer
4250 * to become undefined. */
4251 dst_swapchain
->desc
.swap_effect
= WINED3D_SWAP_EFFECT_COPY
;
4252 wined3d_swapchain_present(dst_swapchain
, NULL
, NULL
, dst_swapchain
->win_handle
, 0);
4253 dst_swapchain
->desc
.swap_effect
= swap_effect
;
4258 if (fbo_blit_supported(&device
->adapter
->gl_info
, blit_op
,
4259 src_rect
, src_texture
->resource
.usage
, src_texture
->resource
.pool
, src_texture
->resource
.format
,
4260 dst_rect
, dst_texture
->resource
.usage
, dst_texture
->resource
.pool
, dst_texture
->resource
.format
))
4262 struct wined3d_context
*context
;
4263 TRACE("Using FBO blit.\n");
4265 context
= context_acquire(device
, NULL
);
4266 surface_blt_fbo(device
, context
, filter
,
4267 src_surface
, src_texture
->resource
.draw_binding
, src_rect
,
4268 dst_surface
, dst_texture
->resource
.draw_binding
, dst_rect
);
4269 context_release(context
);
4271 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
,
4272 dst_texture
->resource
.draw_binding
);
4273 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
,
4274 ~dst_texture
->resource
.draw_binding
);
4279 blitter
= wined3d_select_blitter(&device
->adapter
->gl_info
, &device
->adapter
->d3d_info
, blit_op
,
4280 src_rect
, src_texture
->resource
.usage
, src_texture
->resource
.pool
, src_texture
->resource
.format
,
4281 dst_rect
, dst_texture
->resource
.usage
, dst_texture
->resource
.pool
, dst_texture
->resource
.format
);
4284 blitter
->blit_surface(device
, blit_op
, filter
, src_surface
,
4285 src_rect
, dst_surface
, dst_rect
, color_key
);
4292 /* Special cases for render targets. */
4293 if (SUCCEEDED(surface_blt_special(dst_surface
, dst_rect
, src_surface
, src_rect
, flags
, fx
, filter
)))
4297 return surface_cpu_blt(dst_texture
, dst_sub_resource_idx
, &dst_box
,
4298 src_texture
, src_sub_resource_idx
, &src_box
, flags
, fx
, filter
);