surface.c 204 KB
Newer Older
1
/*
2 3
 * Copyright 1997-2000 Marcus Meissner
 * Copyright 1998-2000 Lionel Ulmer
4
 * Copyright 2000-2001 TransGaming Technologies Inc.
5 6 7
 * Copyright 2002-2005 Jason Edmeades
 * Copyright 2002-2003 Raphael Junqueira
 * 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
13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
27 28 29
 */

#include "config.h"
30
#include "wine/port.h"
31 32 33
#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34
WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
35
WINE_DECLARE_DEBUG_CHANNEL(d3d);
36

37 38
#define MAXLOCKCOUNT 50 /* After this amount of locks do not free the sysmem copy. */

39
static const DWORD surface_simple_locations =
40 41
        WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
        | WINED3D_LOCATION_DIB | WINED3D_LOCATION_BUFFER;
42

43
static void surface_cleanup(struct wined3d_surface *surface)
44
{
45 46
    struct wined3d_surface *overlay, *cur;

47
    TRACE("surface %p.\n", surface);
48

49
    if (surface->pbo || surface->rb_multisample
50
            || surface->rb_resolved || !list_empty(&surface->renderbuffers))
51
    {
52
        struct wined3d_renderbuffer_entry *entry, *entry2;
53 54
        const struct wined3d_gl_info *gl_info;
        struct wined3d_context *context;
55

56
        context = context_acquire(surface->resource.device, NULL);
57
        gl_info = context->gl_info;
58

59
        if (surface->pbo)
60
        {
61
            TRACE("Deleting PBO %u.\n", surface->pbo);
62
            GL_EXTCALL(glDeleteBuffers(1, &surface->pbo));
63 64
        }

65 66 67 68 69 70 71 72 73 74 75 76
        if (surface->rb_multisample)
        {
            TRACE("Deleting multisample renderbuffer %u.\n", surface->rb_multisample);
            gl_info->fbo_ops.glDeleteRenderbuffers(1, &surface->rb_multisample);
        }

        if (surface->rb_resolved)
        {
            TRACE("Deleting resolved renderbuffer %u.\n", surface->rb_resolved);
            gl_info->fbo_ops.glDeleteRenderbuffers(1, &surface->rb_resolved);
        }

77
        LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
78 79 80 81 82
        {
            TRACE("Deleting renderbuffer %u.\n", entry->id);
            gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
            HeapFree(GetProcessHeap(), 0, entry);
        }
83

84 85
        context_release(context);
    }
86

87
    if (surface->flags & SFLAG_DIBSECTION)
88
    {
89 90 91
        DeleteDC(surface->hDC);
        DeleteObject(surface->dib.DIBsection);
        surface->dib.bitmap_data = NULL;
92 93
    }

94 95
    if (surface->overlay_dest)
        list_remove(&surface->overlay_entry);
96

97 98 99 100 101 102
    LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
    {
        list_remove(&overlay->overlay_entry);
        overlay->overlay_dest = NULL;
    }

103
    resource_cleanup(&surface->resource);
104 105
}

106 107 108 109 110 111 112 113 114
void wined3d_surface_destroy(struct wined3d_surface *surface)
{
    TRACE("surface %p.\n", surface);

    surface_cleanup(surface);
    surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
    HeapFree(GetProcessHeap(), 0, surface);
}

115 116
void surface_get_drawable_size(const struct wined3d_surface *surface, const struct wined3d_context *context,
        unsigned int *width, unsigned int *height)
117
{
118
    if (surface->container->swapchain)
119
    {
120 121 122 123 124
        /* The drawable size of an onscreen drawable is the surface size.
         * (Actually: The window size, but the surface is created in window
         * size.) */
        *width = context->current_rt->resource.width;
        *height = context->current_rt->resource.height;
125
    }
126
    else if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
127
    {
128
        const struct wined3d_swapchain *swapchain = context->swapchain;
129

130 131 132 133 134
        /* The drawable size of a backbuffer / aux buffer offscreen target is
         * the size of the current context's drawable, which is the size of
         * the back buffer of the swapchain the active context belongs to. */
        *width = swapchain->desc.backbuffer_width;
        *height = swapchain->desc.backbuffer_height;
135
    }
136 137 138 139 140 141 142 143 144
    else
    {
        /* The drawable size of an FBO target is the OpenGL texture size,
         * which is the power of two size. */
        *width = context->current_rt->pow2Width;
        *height = context->current_rt->pow2Height;
    }
}

145 146 147 148
struct blt_info
{
    GLenum binding;
    GLenum bind_target;
149
    enum wined3d_gl_resource_type tex_type;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    GLfloat coords[4][3];
};

struct float_rect
{
    float l;
    float t;
    float r;
    float b;
};

static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
{
    f->l = ((r->left * 2.0f) / w) - 1.0f;
    f->t = ((r->top * 2.0f) / h) - 1.0f;
    f->r = ((r->right * 2.0f) / w) - 1.0f;
    f->b = ((r->bottom * 2.0f) / h) - 1.0f;
}

169
static void surface_get_blt_info(GLenum target, const RECT *rect, GLsizei w, GLsizei h, struct blt_info *info)
170 171 172 173 174 175 176 177 178 179 180 181
{
    GLfloat (*coords)[3] = info->coords;
    struct float_rect f;

    switch (target)
    {
        default:
            FIXME("Unsupported texture target %#x\n", target);
            /* Fall back to GL_TEXTURE_2D */
        case GL_TEXTURE_2D:
            info->binding = GL_TEXTURE_BINDING_2D;
            info->bind_target = GL_TEXTURE_2D;
182
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_2D;
183 184
            coords[0][0] = (float)rect->left / w;
            coords[0][1] = (float)rect->top / h;
185 186
            coords[0][2] = 0.0f;

187 188
            coords[1][0] = (float)rect->right / w;
            coords[1][1] = (float)rect->top / h;
189 190
            coords[1][2] = 0.0f;

191 192
            coords[2][0] = (float)rect->left / w;
            coords[2][1] = (float)rect->bottom / h;
193 194
            coords[2][2] = 0.0f;

195 196
            coords[3][0] = (float)rect->right / w;
            coords[3][1] = (float)rect->bottom / h;
197 198 199 200 201 202
            coords[3][2] = 0.0f;
            break;

        case GL_TEXTURE_RECTANGLE_ARB:
            info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
            info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
203
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_RECT;
204 205 206 207
            coords[0][0] = rect->left;  coords[0][1] = rect->top;       coords[0][2] = 0.0f;
            coords[1][0] = rect->right; coords[1][1] = rect->top;       coords[1][2] = 0.0f;
            coords[2][0] = rect->left;  coords[2][1] = rect->bottom;    coords[2][2] = 0.0f;
            coords[3][0] = rect->right; coords[3][1] = rect->bottom;    coords[3][2] = 0.0f;
208 209 210 211 212
            break;

        case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
            info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
            info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
213
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
214
            cube_coords_float(rect, w, h, &f);
215 216 217 218 219 220 221 222 223 224

            coords[0][0] =  1.0f;   coords[0][1] = -f.t;   coords[0][2] = -f.l;
            coords[1][0] =  1.0f;   coords[1][1] = -f.t;   coords[1][2] = -f.r;
            coords[2][0] =  1.0f;   coords[2][1] = -f.b;   coords[2][2] = -f.l;
            coords[3][0] =  1.0f;   coords[3][1] = -f.b;   coords[3][2] = -f.r;
            break;

        case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
            info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
            info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
225
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
226
            cube_coords_float(rect, w, h, &f);
227 228 229 230 231 232 233 234 235 236

            coords[0][0] = -1.0f;   coords[0][1] = -f.t;   coords[0][2] = f.l;
            coords[1][0] = -1.0f;   coords[1][1] = -f.t;   coords[1][2] = f.r;
            coords[2][0] = -1.0f;   coords[2][1] = -f.b;   coords[2][2] = f.l;
            coords[3][0] = -1.0f;   coords[3][1] = -f.b;   coords[3][2] = f.r;
            break;

        case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
            info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
            info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
237
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
238
            cube_coords_float(rect, w, h, &f);
239 240 241 242 243 244 245 246 247 248

            coords[0][0] = f.l;   coords[0][1] =  1.0f;   coords[0][2] = f.t;
            coords[1][0] = f.r;   coords[1][1] =  1.0f;   coords[1][2] = f.t;
            coords[2][0] = f.l;   coords[2][1] =  1.0f;   coords[2][2] = f.b;
            coords[3][0] = f.r;   coords[3][1] =  1.0f;   coords[3][2] = f.b;
            break;

        case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
            info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
            info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
249
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
250
            cube_coords_float(rect, w, h, &f);
251 252 253 254 255 256 257 258 259 260

            coords[0][0] = f.l;   coords[0][1] = -1.0f;   coords[0][2] = -f.t;
            coords[1][0] = f.r;   coords[1][1] = -1.0f;   coords[1][2] = -f.t;
            coords[2][0] = f.l;   coords[2][1] = -1.0f;   coords[2][2] = -f.b;
            coords[3][0] = f.r;   coords[3][1] = -1.0f;   coords[3][2] = -f.b;
            break;

        case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
            info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
            info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
261
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
262
            cube_coords_float(rect, w, h, &f);
263 264 265 266 267 268 269 270 271 272

            coords[0][0] = f.l;   coords[0][1] = -f.t;   coords[0][2] =  1.0f;
            coords[1][0] = f.r;   coords[1][1] = -f.t;   coords[1][2] =  1.0f;
            coords[2][0] = f.l;   coords[2][1] = -f.b;   coords[2][2] =  1.0f;
            coords[3][0] = f.r;   coords[3][1] = -f.b;   coords[3][2] =  1.0f;
            break;

        case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
            info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
            info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
273
            info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
274
            cube_coords_float(rect, w, h, &f);
275 276 277 278 279 280 281 282 283

            coords[0][0] = -f.l;   coords[0][1] = -f.t;   coords[0][2] = -1.0f;
            coords[1][0] = -f.r;   coords[1][1] = -f.t;   coords[1][2] = -1.0f;
            coords[2][0] = -f.l;   coords[2][1] = -f.b;   coords[2][2] = -1.0f;
            coords[3][0] = -f.r;   coords[3][1] = -f.b;   coords[3][2] = -1.0f;
            break;
    }
}

284
static void surface_get_rect(const struct wined3d_surface *surface, const RECT *rect_in, RECT *rect_out)
285 286 287 288 289 290 291
{
    if (rect_in)
        *rect_out = *rect_in;
    else
    {
        rect_out->left = 0;
        rect_out->top = 0;
292 293
        rect_out->right = surface->resource.width;
        rect_out->bottom = surface->resource.height;
294 295 296
    }
}

297
/* Context activation is done by the caller. */
298
void draw_textured_quad(const struct wined3d_surface *src_surface, struct wined3d_context *context,
299
        const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
300
{
301
    const struct wined3d_gl_info *gl_info = context->gl_info;
302
    struct wined3d_texture *texture = src_surface->container;
303 304 305 306
    struct blt_info info;

    surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);

307
    gl_info->gl_ops.gl.p_glEnable(info.bind_target);
308 309
    checkGLcall("glEnable(bind_target)");

310
    context_bind_texture(context, info.bind_target, texture->texture_rgb.name);
311 312

    /* Filtering for StretchRect */
313
    gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
314
    checkGLcall("glTexParameteri");
315
    gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
316
            wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
317
    checkGLcall("glTexParameteri");
318 319
    gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
320
    if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
321 322
        gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
    gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
323 324 325
    checkGLcall("glTexEnvi");

    /* Draw a quad */
326 327 328
    gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[0]);
    gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
329

330 331
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[1]);
    gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
332

333 334
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[2]);
    gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
335

336 337 338
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[3]);
    gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
    gl_info->gl_ops.gl.p_glEnd();
339 340

    /* Unbind the texture */
341
    context_bind_texture(context, info.bind_target, 0);
342 343 344

    /* We changed the filtering settings on the texture. Inform the
     * container about this to get the filters reset properly next draw. */
345 346 347 348
    texture->texture_rgb.sampler_desc.mag_filter = WINED3D_TEXF_POINT;
    texture->texture_rgb.sampler_desc.min_filter = WINED3D_TEXF_POINT;
    texture->texture_rgb.sampler_desc.mip_filter = WINED3D_TEXF_NONE;
    texture->texture_rgb.sampler_desc.srgb_decode = FALSE;
349
}
350

351 352 353 354 355 356 357 358
/* Works correctly only for <= 4 bpp formats. */
static void get_color_masks(const struct wined3d_format *format, DWORD *masks)
{
    masks[0] = ((1 << format->red_size) - 1) << format->red_offset;
    masks[1] = ((1 << format->green_size) - 1) << format->green_offset;
    masks[2] = ((1 << format->blue_size) - 1) << format->blue_offset;
}

359
static HRESULT surface_create_dib_section(struct wined3d_surface *surface)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
{
    const struct wined3d_format *format = surface->resource.format;
    SYSTEM_INFO sysInfo;
    BITMAPINFO *b_info;
    int extraline = 0;
    DWORD *masks;

    TRACE("surface %p.\n", surface);

    if (!(format->flags & WINED3DFMT_FLAG_GETDC))
    {
        WARN("Cannot use GetDC on a %s surface.\n", debug_d3dformat(format->id));
        return WINED3DERR_INVALIDCALL;
    }

    switch (format->byte_count)
    {
        case 2:
        case 4:
            /* Allocate extra space to store the RGB bit masks. */
            b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD));
            break;

        case 3:
            b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER));
            break;

        default:
            /* Allocate extra space for a palette. */
            b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                    sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1 << (format->byte_count * 8)));
            break;
    }

    if (!b_info)
        return E_OUTOFMEMORY;

    /* Some applications access the surface in via DWORDs, and do not take
     * the necessary care at the end of the surface. So we need at least
     * 4 extra bytes at the end of the surface. Check against the page size,
     * if the last page used for the surface has at least 4 spare bytes we're
     * safe, otherwise add an extra line to the DIB section. */
    GetSystemInfo(&sysInfo);
    if( ((surface->resource.size + 3) % sysInfo.dwPageSize) < 4)
    {
        extraline = 1;
        TRACE("Adding an extra line to the DIB section.\n");
    }

    b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    /* TODO: Is there a nicer way to force a specific alignment? (8 byte for ddraw) */
411
    b_info->bmiHeader.biWidth = wined3d_surface_get_pitch(surface) / format->byte_count;
412
    b_info->bmiHeader.biHeight = 0 - surface->resource.height - extraline;
413
    b_info->bmiHeader.biSizeImage = (surface->resource.height + extraline)
414
            * wined3d_surface_get_pitch(surface);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    b_info->bmiHeader.biPlanes = 1;
    b_info->bmiHeader.biBitCount = format->byte_count * 8;

    b_info->bmiHeader.biXPelsPerMeter = 0;
    b_info->bmiHeader.biYPelsPerMeter = 0;
    b_info->bmiHeader.biClrUsed = 0;
    b_info->bmiHeader.biClrImportant = 0;

    /* Get the bit masks */
    masks = (DWORD *)b_info->bmiColors;
    switch (surface->resource.format->id)
    {
        case WINED3DFMT_B8G8R8_UNORM:
            b_info->bmiHeader.biCompression = BI_RGB;
            break;

        case WINED3DFMT_B5G5R5X1_UNORM:
        case WINED3DFMT_B5G5R5A1_UNORM:
        case WINED3DFMT_B4G4R4A4_UNORM:
        case WINED3DFMT_B4G4R4X4_UNORM:
        case WINED3DFMT_B2G3R3_UNORM:
        case WINED3DFMT_B2G3R3A8_UNORM:
        case WINED3DFMT_R10G10B10A2_UNORM:
        case WINED3DFMT_R8G8B8A8_UNORM:
        case WINED3DFMT_R8G8B8X8_UNORM:
        case WINED3DFMT_B10G10R10A2_UNORM:
        case WINED3DFMT_B5G6R5_UNORM:
        case WINED3DFMT_R16G16B16A16_UNORM:
            b_info->bmiHeader.biCompression = BI_BITFIELDS;
444
            get_color_masks(format, masks);
445 446 447 448 449 450 451 452 453 454 455
            break;

        default:
            /* Don't know palette */
            b_info->bmiHeader.biCompression = BI_RGB;
            break;
    }

    TRACE("Creating a DIB section with size %dx%dx%d, size=%d.\n",
            b_info->bmiHeader.biWidth, b_info->bmiHeader.biHeight,
            b_info->bmiHeader.biBitCount, b_info->bmiHeader.biSizeImage);
456
    surface->dib.DIBsection = CreateDIBSection(0, b_info, DIB_RGB_COLORS, &surface->dib.bitmap_data, 0, 0);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

    if (!surface->dib.DIBsection)
    {
        ERR("Failed to create DIB section.\n");
        HeapFree(GetProcessHeap(), 0, b_info);
        return HRESULT_FROM_WIN32(GetLastError());
    }

    TRACE("DIBSection at %p.\n", surface->dib.bitmap_data);
    surface->dib.bitmap_size = b_info->bmiHeader.biSizeImage;

    HeapFree(GetProcessHeap(), 0, b_info);

    /* Now allocate a DC. */
    surface->hDC = CreateCompatibleDC(0);
472
    SelectObject(surface->hDC, surface->dib.DIBsection);
473 474 475 476 477 478

    surface->flags |= SFLAG_DIBSECTION;

    return WINED3D_OK;
}

479 480
static void surface_get_memory(const struct wined3d_surface *surface, struct wined3d_bo_address *data,
        DWORD location)
481
{
482
    if (location & WINED3D_LOCATION_BUFFER)
483 484 485 486 487
    {
        data->addr = NULL;
        data->buffer_object = surface->pbo;
        return;
    }
488
    if (location & WINED3D_LOCATION_USER_MEMORY)
489 490 491 492 493
    {
        data->addr = surface->user_memory;
        data->buffer_object = 0;
        return;
    }
494
    if (location & WINED3D_LOCATION_DIB)
495 496 497 498 499
    {
        data->addr = surface->dib.bitmap_data;
        data->buffer_object = 0;
        return;
    }
500
    if (location & WINED3D_LOCATION_SYSMEM)
501
    {
502
        data->addr = surface->resource.heap_memory;
503
        data->buffer_object = 0;
504
        return;
505
    }
506

507
    ERR("Unexpected locations %s.\n", wined3d_debug_location(location));
508
    data->addr = NULL;
509
    data->buffer_object = 0;
510 511
}

512
static void surface_prepare_buffer(struct wined3d_surface *surface)
513
{
514 515
    struct wined3d_context *context;
    GLenum error;
516 517 518 519
    const struct wined3d_gl_info *gl_info;

    if (surface->pbo)
        return;
520

521
    context = context_acquire(surface->resource.device, NULL);
522
    gl_info = context->gl_info;
523

524
    GL_EXTCALL(glGenBuffers(1, &surface->pbo));
525
    error = gl_info->gl_ops.gl.p_glGetError();
526 527
    if (!surface->pbo || error != GL_NO_ERROR)
        ERR("Failed to create a PBO with error %s (%#x).\n", debug_glerror(error), error);
528

529
    TRACE("Binding PBO %u.\n", surface->pbo);
530

531 532
    GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, surface->pbo));
    checkGLcall("glBindBuffer");
533

534 535 536
    GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, surface->resource.size + 4,
            NULL, GL_STREAM_DRAW));
    checkGLcall("glBufferData");
537

538 539
    GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
    checkGLcall("glBindBuffer");
540

541 542
    context_release(context);
}
543

544
static void surface_prepare_system_memory(struct wined3d_surface *surface)
545 546 547
{
    TRACE("surface %p.\n", surface);

548 549
    if (surface->resource.heap_memory)
        return;
550

551 552 553 554 555
    /* Whatever surface we have, make sure that there is memory allocated
     * for the downloaded copy, or a PBO to map. */
    if (!wined3d_resource_allocate_sysmem(&surface->resource))
        ERR("Failed to allocate system memory.\n");

556 557
    if (surface->locations & WINED3D_LOCATION_SYSMEM)
        ERR("Surface without system memory has WINED3D_LOCATION_SYSMEM set.\n");
558
}
559

560 561
void surface_prepare_map_memory(struct wined3d_surface *surface)
{
562
    switch (surface->resource.map_binding)
563
    {
564 565
        case WINED3D_LOCATION_SYSMEM:
            surface_prepare_system_memory(surface);
566 567
            break;

568 569 570
        case WINED3D_LOCATION_USER_MEMORY:
            if (!surface->user_memory)
                ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
571
            break;
572

573 574 575
        case WINED3D_LOCATION_DIB:
            if (!surface->dib.bitmap_data)
                ERR("Map binding is set to WINED3D_LOCATION_DIB but surface->dib.bitmap_data is NULL.\n");
576 577
            break;

578
        case WINED3D_LOCATION_BUFFER:
579 580 581
            surface_prepare_buffer(surface);
            break;

582
        default:
583
            ERR("Unexpected map binding %s.\n", wined3d_debug_location(surface->resource.map_binding));
584 585 586
    }
}

587
static void surface_evict_sysmem(struct wined3d_surface *surface)
588
{
589
    /* In some conditions the surface memory must not be freed:
590
     * WINED3D_TEXTURE_CONVERTED: Converting the data back would take too long
591
     * WINED3D_TEXTURE_DYNAMIC_MAP: Avoid freeing the data for performance
592
     * SFLAG_CLIENT: OpenGL uses our memory as backup */
593 594 595
    if (surface->resource.map_count || surface->flags & SFLAG_CLIENT
            || surface->container->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM
            | WINED3D_TEXTURE_DYNAMIC_MAP))
596 597
        return;

598
    wined3d_resource_free_sysmem(&surface->resource);
599
    surface_invalidate_location(surface, WINED3D_LOCATION_SYSMEM);
600 601
}

602
static void surface_release_client_storage(struct wined3d_surface *surface)
603 604
{
    struct wined3d_context *context = context_acquire(surface->resource.device, NULL);
605
    const struct wined3d_gl_info *gl_info = context->gl_info;
606

607
    if (surface->container->texture_rgb.name)
608
    {
609
        wined3d_texture_bind_and_dirtify(surface->container, context, FALSE);
610
        gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
611 612
                GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    }
613
    if (surface->container->texture_srgb.name)
614
    {
615
        wined3d_texture_bind_and_dirtify(surface->container, context, TRUE);
616
        gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
617 618
                GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    }
619
    wined3d_texture_force_reload(surface->container);
620 621 622 623

    context_release(context);
}

624 625 626
static BOOL surface_use_pbo(const struct wined3d_surface *surface)
{
    const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
627
    struct wined3d_texture *texture = surface->container;
628

629
    return texture->resource.pool == WINED3D_POOL_DEFAULT
630 631
                && surface->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
                && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
632 633 634
                && !texture->resource.format->convert
                && !(texture->flags & WINED3D_TEXTURE_PIN_SYSMEM)
                && !(surface->flags & SFLAG_NONPOW2);
635 636
}

637
static HRESULT surface_private_setup(struct wined3d_surface *surface)
638 639 640 641 642 643 644 645
{
    /* TODO: Check against the maximum texture sizes supported by the video card. */
    const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
    unsigned int pow2Width, pow2Height;

    TRACE("surface %p.\n", surface);

    /* Non-power2 support */
646 647
    if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
            || gl_info->supported[ARB_TEXTURE_RECTANGLE])
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    {
        pow2Width = surface->resource.width;
        pow2Height = surface->resource.height;
    }
    else
    {
        /* Find the nearest pow2 match */
        pow2Width = pow2Height = 1;
        while (pow2Width < surface->resource.width)
            pow2Width <<= 1;
        while (pow2Height < surface->resource.height)
            pow2Height <<= 1;
    }
    surface->pow2Width = pow2Width;
    surface->pow2Height = pow2Height;

    if (pow2Width > surface->resource.width || pow2Height > surface->resource.height)
    {
        /* TODO: Add support for non power two compressed textures. */
667
        if (surface->resource.format->flags & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
668
        {
669
            FIXME("(%p) Compressed or height scaled non-power-two textures are not supported w(%d) h(%d)\n",
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
                  surface, surface->resource.width, surface->resource.height);
            return WINED3DERR_NOTAVAILABLE;
        }
    }

    if (pow2Width != surface->resource.width
            || pow2Height != surface->resource.height)
    {
        surface->flags |= SFLAG_NONPOW2;
    }

    if ((surface->pow2Width > gl_info->limits.texture_size || surface->pow2Height > gl_info->limits.texture_size)
            && !(surface->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
    {
        /* One of three options:
         * 1: Do the same as we do with NPOT and scale the texture, (any
         *    texture ops would require the texture to be scaled which is
         *    potentially slow)
         * 2: Set the texture to the maximum size (bad idea).
         * 3: WARN and return WINED3DERR_NOTAVAILABLE;
         * 4: Create the surface, but allow it to be used only for DirectDraw
         *    Blts. Some apps (e.g. Swat 3) create textures with a Height of
         *    16 and a Width > 3000 and blt 16x16 letter areas from them to
         *    the render target. */
694
        if (surface->resource.pool == WINED3D_POOL_DEFAULT || surface->resource.pool == WINED3D_POOL_MANAGED)
695 696 697 698 699 700 701 702 703 704
        {
            WARN("Unable to allocate a surface which exceeds the maximum OpenGL texture size.\n");
            return WINED3DERR_NOTAVAILABLE;
        }

        /* We should never use this surface in combination with OpenGL! */
        TRACE("Creating an oversized surface: %ux%u.\n",
                surface->pow2Width, surface->pow2Height);
    }

705
    if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
706
        surface->locations = WINED3D_LOCATION_DISCARDED;
707

708
    if (surface_use_pbo(surface))
709
        surface->resource.map_binding = WINED3D_LOCATION_BUFFER;
710

711 712 713
    return WINED3D_OK;
}

714
static void surface_unmap(struct wined3d_surface *surface)
715
{
716
    struct wined3d_device *device = surface->resource.device;
717 718
    const struct wined3d_gl_info *gl_info;
    struct wined3d_context *context;
719 720 721 722 723

    TRACE("surface %p.\n", surface);

    memset(&surface->lockedRect, 0, sizeof(surface->lockedRect));

724
    switch (surface->resource.map_binding)
725
    {
726 727 728
        case WINED3D_LOCATION_SYSMEM:
        case WINED3D_LOCATION_USER_MEMORY:
        case WINED3D_LOCATION_DIB:
729
            break;
730

731
        case WINED3D_LOCATION_BUFFER:
732 733
            context = context_acquire(device, NULL);
            gl_info = context->gl_info;
734

735 736 737 738
            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, surface->pbo));
            GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
            checkGLcall("glUnmapBuffer");
739
            context_release(context);
740 741 742
            break;

        default:
743
            ERR("Unexpected map binding %s.\n", wined3d_debug_location(surface->resource.map_binding));
744 745
    }

746
    if (surface->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB))
747 748
    {
        TRACE("Not dirtified, nothing to do.\n");
749
        return;
750 751
    }

752
    if (surface->container->swapchain && surface->container->swapchain->front_buffer == surface->container)
753
        surface_load_location(surface, surface->container->resource.draw_binding);
754 755 756 757
    else if (surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
        FIXME("Depth / stencil buffer locking is not implemented.\n");
}

758
static BOOL surface_is_full_rect(const struct wined3d_surface *surface, const RECT *r)
759
{
760 761 762 763 764 765
    if ((r->left && r->right) || abs(r->right - r->left) != surface->resource.width)
        return FALSE;
    if ((r->top && r->bottom) || abs(r->bottom - r->top) != surface->resource.height)
        return FALSE;
    return TRUE;
}
766

767 768 769
static void surface_depth_blt_fbo(const struct wined3d_device *device,
        struct wined3d_surface *src_surface, DWORD src_location, const RECT *src_rect,
        struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect)
770 771 772 773 774
{
    const struct wined3d_gl_info *gl_info;
    struct wined3d_context *context;
    DWORD src_mask, dst_mask;
    GLbitfield gl_mask;
775

776 777
    TRACE("device %p\n", device);
    TRACE("src_surface %p, src_location %s, src_rect %s,\n",
778
            src_surface, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect));
779
    TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
780
            dst_surface, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
781 782 783 784 785

    src_mask = src_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
    dst_mask = dst_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);

    if (src_mask != dst_mask)
786
    {
787 788 789 790
        ERR("Incompatible formats %s and %s.\n",
                debug_d3dformat(src_surface->resource.format->id),
                debug_d3dformat(dst_surface->resource.format->id));
        return;
791 792
    }

793
    if (!src_mask)
794
    {
795 796 797
        ERR("Not a depth / stencil format: %s.\n",
                debug_d3dformat(src_surface->resource.format->id));
        return;
798 799
    }

800 801 802 803 804 805 806 807
    gl_mask = 0;
    if (src_mask & WINED3DFMT_FLAG_DEPTH)
        gl_mask |= GL_DEPTH_BUFFER_BIT;
    if (src_mask & WINED3DFMT_FLAG_STENCIL)
        gl_mask |= GL_STENCIL_BUFFER_BIT;

    /* Make sure the locations are up-to-date. Loading the destination
     * surface isn't required if the entire surface is overwritten. */
808
    surface_load_location(src_surface, src_location);
809
    if (!surface_is_full_rect(dst_surface, dst_rect))
810
        surface_load_location(dst_surface, dst_location);
811 812 813

    context = context_acquire(device, NULL);
    if (!context->valid)
814
    {
815 816 817 818
        context_release(context);
        WARN("Invalid context, skipping blit.\n");
        return;
    }
819

820
    gl_info = context->gl_info;
821

822
    context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, NULL, src_surface, src_location);
823
    context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
824

825
    context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, NULL, dst_surface, dst_location);
826 827
    context_set_draw_buffer(context, GL_NONE);
    context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
828
    context_invalidate_state(context, STATE_FRAMEBUFFER);
829

830 831
    if (gl_mask & GL_DEPTH_BUFFER_BIT)
    {
832
        gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
833
        context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
834 835 836 837 838
    }
    if (gl_mask & GL_STENCIL_BUFFER_BIT)
    {
        if (context->gl_info->supported[EXT_STENCIL_TWO_SIDE])
        {
839
            gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
840
            context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
841
        }
842
        gl_info->gl_ops.gl.p_glStencilMask(~0U);
843
        context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
844 845
    }

846
    gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
847
    context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
848

849 850 851
    gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
            dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
    checkGLcall("glBlitFramebuffer()");
852

853
    if (wined3d_settings.strict_draw_ordering)
854
        gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
855

856 857
    context_release(context);
}
858

859 860
/* Blit between surface locations. Onscreen on different swapchains is not supported.
 * Depth / stencil is not supported. */
861
static void surface_blt_fbo(const struct wined3d_device *device, enum wined3d_texture_filter_type filter,
862 863 864 865 866 867 868 869 870 871 872
        struct wined3d_surface *src_surface, DWORD src_location, const RECT *src_rect_in,
        struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
{
    const struct wined3d_gl_info *gl_info;
    struct wined3d_context *context;
    RECT src_rect, dst_rect;
    GLenum gl_filter;
    GLenum buffer;

    TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
    TRACE("src_surface %p, src_location %s, src_rect %s,\n",
873
            src_surface, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect_in));
874
    TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
875
            dst_surface, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect_in));
876 877 878 879 880 881

    src_rect = *src_rect_in;
    dst_rect = *dst_rect_in;

    switch (filter)
    {
882
        case WINED3D_TEXF_LINEAR:
883 884 885 886 887
            gl_filter = GL_LINEAR;
            break;

        default:
            FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
888 889
        case WINED3D_TEXF_NONE:
        case WINED3D_TEXF_POINT:
890 891 892 893
            gl_filter = GL_NEAREST;
            break;
    }

894
    /* Resolve the source surface first if needed. */
895
    if (src_location == WINED3D_LOCATION_RB_MULTISAMPLE
896 897 898
            && (src_surface->resource.format->id != dst_surface->resource.format->id
                || abs(src_rect.bottom - src_rect.top) != abs(dst_rect.bottom - dst_rect.top)
                || abs(src_rect.right - src_rect.left) != abs(dst_rect.right - dst_rect.left)))
899
        src_location = WINED3D_LOCATION_RB_RESOLVED;
900

901 902 903 904
    /* Make sure the locations are up-to-date. Loading the destination
     * surface isn't required if the entire surface is overwritten. (And is
     * in fact harmful if we're being called by surface_load_location() with
     * the purpose of loading the destination surface.) */
905
    surface_load_location(src_surface, src_location);
906
    if (!surface_is_full_rect(dst_surface, &dst_rect))
907
        surface_load_location(dst_surface, dst_location);
908

909 910
    if (src_location == WINED3D_LOCATION_DRAWABLE) context = context_acquire(device, src_surface);
    else if (dst_location == WINED3D_LOCATION_DRAWABLE) context = context_acquire(device, dst_surface);
911 912 913 914 915 916 917 918 919 920 921
    else context = context_acquire(device, NULL);

    if (!context->valid)
    {
        context_release(context);
        WARN("Invalid context, skipping blit.\n");
        return;
    }

    gl_info = context->gl_info;

922
    if (src_location == WINED3D_LOCATION_DRAWABLE)
923 924 925 926 927 928 929 930 931 932 933 934
    {
        TRACE("Source surface %p is onscreen.\n", src_surface);
        buffer = surface_get_gl_buffer(src_surface);
        surface_translate_drawable_coords(src_surface, context->win_handle, &src_rect);
    }
    else
    {
        TRACE("Source surface %p is offscreen.\n", src_surface);
        buffer = GL_COLOR_ATTACHMENT0;
    }

    context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
935
    gl_info->gl_ops.gl.p_glReadBuffer(buffer);
936 937 938
    checkGLcall("glReadBuffer()");
    context_check_fbo_status(context, GL_READ_FRAMEBUFFER);

939
    if (dst_location == WINED3D_LOCATION_DRAWABLE)
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
    {
        TRACE("Destination surface %p is onscreen.\n", dst_surface);
        buffer = surface_get_gl_buffer(dst_surface);
        surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
    }
    else
    {
        TRACE("Destination surface %p is offscreen.\n", dst_surface);
        buffer = GL_COLOR_ATTACHMENT0;
    }

    context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
    context_set_draw_buffer(context, buffer);
    context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
    context_invalidate_state(context, STATE_FRAMEBUFFER);

956
    gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
957 958 959 960
    context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
    context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
    context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
    context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
961

962
    gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
963
    context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
964 965 966 967 968 969

    gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
            dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
    checkGLcall("glBlitFramebuffer()");

    if (wined3d_settings.strict_draw_ordering
970
            || (dst_location == WINED3D_LOCATION_DRAWABLE
971
            && dst_surface->container->swapchain->front_buffer == dst_surface->container))
972
        gl_info->gl_ops.gl.p_glFlush();
973 974 975 976

    context_release(context);
}

977
static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
978 979
        const RECT *src_rect, DWORD src_usage, enum wined3d_pool src_pool, const struct wined3d_format *src_format,
        const RECT *dst_rect, DWORD dst_usage, enum wined3d_pool dst_pool, const struct wined3d_format *dst_format)
980 981 982
{
    if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
        return FALSE;
983

984
    /* Source and/or destination need to be on the GL side */
985
    if (src_pool == WINED3D_POOL_SYSTEM_MEM || dst_pool == WINED3D_POOL_SYSTEM_MEM)
986 987 988
        return FALSE;

    switch (blit_op)
989
    {
990 991 992 993 994 995
        case WINED3D_BLIT_OP_COLOR_BLIT:
            if (!((src_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET)))
                return FALSE;
            if (!((dst_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
                return FALSE;
            break;
996

997 998 999 1000 1001 1002
        case WINED3D_BLIT_OP_DEPTH_BLIT:
            if (!(src_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
                return FALSE;
            if (!(dst_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
                return FALSE;
            break;
1003

1004 1005
        default:
            return FALSE;
1006 1007
    }

1008 1009 1010 1011 1012 1013
    if (!(src_format->id == dst_format->id
            || (is_identity_fixup(src_format->color_fixup)
            && is_identity_fixup(dst_format->color_fixup))))
        return FALSE;

    return TRUE;
1014 1015
}

1016
static BOOL surface_convert_color_to_float(const struct wined3d_surface *surface,
1017
        DWORD color, struct wined3d_color *float_color)
1018 1019
{
    const struct wined3d_format *format = surface->resource.format;
1020
    const struct wined3d_palette *palette;
1021 1022 1023 1024

    switch (format->id)
    {
        case WINED3DFMT_P8_UINT:
1025
            palette = surface->container->swapchain ? surface->container->swapchain->palette : NULL;
1026 1027

            if (palette)
1028
            {
1029 1030 1031
                float_color->r = palette->colors[color].rgbRed / 255.0f;
                float_color->g = palette->colors[color].rgbGreen / 255.0f;
                float_color->b = palette->colors[color].rgbBlue / 255.0f;
1032 1033 1034 1035 1036 1037 1038
            }
            else
            {
                float_color->r = 0.0f;
                float_color->g = 0.0f;
                float_color->b = 0.0f;
            }
1039
            float_color->a = color / 255.0f;
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
            break;

        case WINED3DFMT_B5G6R5_UNORM:
            float_color->r = ((color >> 11) & 0x1f) / 31.0f;
            float_color->g = ((color >> 5) & 0x3f) / 63.0f;
            float_color->b = (color & 0x1f) / 31.0f;
            float_color->a = 1.0f;
            break;

        case WINED3DFMT_B8G8R8_UNORM:
        case WINED3DFMT_B8G8R8X8_UNORM:
            float_color->r = D3DCOLOR_R(color);
            float_color->g = D3DCOLOR_G(color);
            float_color->b = D3DCOLOR_B(color);
            float_color->a = 1.0f;
            break;

        case WINED3DFMT_B8G8R8A8_UNORM:
            float_color->r = D3DCOLOR_R(color);
            float_color->g = D3DCOLOR_G(color);
            float_color->b = D3DCOLOR_B(color);
            float_color->a = D3DCOLOR_A(color);
            break;

        default:
            ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
            return FALSE;
    }

    return TRUE;
}

1072
static BOOL surface_convert_depth_to_float(const struct wined3d_surface *surface, DWORD depth, float *float_depth)
1073
{
1074 1075 1076
    const struct wined3d_format *format = surface->resource.format;

    switch (format->id)
1077
    {
1078 1079 1080
        case WINED3DFMT_S1_UINT_D15_UNORM:
            *float_depth = depth / (float)0x00007fff;
            break;
1081

1082 1083 1084
        case WINED3DFMT_D16_UNORM:
            *float_depth = depth / (float)0x0000ffff;
            break;
1085

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        case WINED3DFMT_D24_UNORM_S8_UINT:
        case WINED3DFMT_X8D24_UNORM:
            *float_depth = depth / (float)0x00ffffff;
            break;

        case WINED3DFMT_D32_UNORM:
            *float_depth = depth / (float)0xffffffff;
            break;

        default:
            ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
            return FALSE;
    }

    return TRUE;
1101 1102
}

1103
static HRESULT wined3d_surface_depth_fill(struct wined3d_surface *surface, const RECT *rect, float depth)
1104
{
1105
    const struct wined3d_resource *resource = &surface->container->resource;
1106
    struct wined3d_device *device = resource->device;
1107
    const struct blit_shader *blitter;
1108

1109 1110 1111
    blitter = wined3d_select_blitter(&device->adapter->gl_info, WINED3D_BLIT_OP_DEPTH_FILL,
            NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format);
    if (!blitter)
1112
    {
1113 1114 1115 1116 1117 1118 1119
        FIXME("No blitter is capable of performing the requested depth fill operation.\n");
        return WINED3DERR_INVALIDCALL;
    }

    return blitter->depth_fill(device, surface, rect, depth);
}

1120 1121
static HRESULT wined3d_surface_depth_blt(struct wined3d_surface *src_surface, DWORD src_location, const RECT *src_rect,
        struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect)
1122
{
1123
    struct wined3d_device *device = src_surface->resource.device;
1124 1125 1126 1127 1128 1129

    if (!fbo_blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_DEPTH_BLIT,
            src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
            dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
        return WINED3DERR_INVALIDCALL;

1130
    surface_depth_blt_fbo(device, src_surface, src_location, src_rect, dst_surface, dst_location, dst_rect);
1131

1132
    surface_modify_ds_location(dst_surface, dst_location,
1133 1134 1135 1136 1137
            dst_surface->ds_current_size.cx, dst_surface->ds_current_size.cy);

    return WINED3D_OK;
}

1138 1139
HRESULT CDECL wined3d_surface_get_render_target_data(struct wined3d_surface *surface,
        struct wined3d_surface *render_target)
1140
{
1141
    TRACE("surface %p, render_target %p.\n", surface, render_target);
1142

1143
    /* TODO: Check surface sizes, pools, etc. */
1144

1145 1146
    if (render_target->resource.multisample_type)
        return WINED3DERR_INVALIDCALL;
1147

1148 1149 1150 1151 1152 1153
    return wined3d_surface_blt(surface, NULL, render_target, NULL, 0, NULL, WINED3D_TEXF_POINT);
}

/* Context activation is done by the caller. */
static void surface_remove_pbo(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info)
{
1154 1155
    GL_EXTCALL(glDeleteBuffers(1, &surface->pbo));
    checkGLcall("glDeleteBuffers(1, &surface->pbo)");
1156

1157
    surface->pbo = 0;
1158
    surface_invalidate_location(surface, WINED3D_LOCATION_BUFFER);
1159
}
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
static ULONG surface_resource_incref(struct wined3d_resource *resource)
{
    return wined3d_surface_incref(surface_from_resource(resource));
}

static ULONG surface_resource_decref(struct wined3d_resource *resource)
{
    return wined3d_surface_decref(surface_from_resource(resource));
}

1171 1172 1173 1174 1175 1176 1177
static void surface_unload(struct wined3d_resource *resource)
{
    struct wined3d_surface *surface = surface_from_resource(resource);
    struct wined3d_renderbuffer_entry *entry, *entry2;
    struct wined3d_device *device = resource->device;
    const struct wined3d_gl_info *gl_info;
    struct wined3d_context *context;
1178

1179
    TRACE("surface %p.\n", surface);
1180

1181
    if (resource->pool == WINED3D_POOL_DEFAULT)
1182
    {
1183 1184 1185 1186 1187 1188 1189 1190
        /* Default pool resources are supposed to be destroyed before Reset is called.
         * Implicit resources stay however. So this means we have an implicit render target
         * or depth stencil. The content may be destroyed, but we still have to tear down
         * opengl resources, so we cannot leave early.
         *
         * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
         * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
         * or the depth stencil into an FBO the texture or render buffer will be removed
1191
         * and all flags get lost */
1192 1193
        surface_prepare_system_memory(surface);
        memset(surface->resource.heap_memory, 0, surface->resource.size);
1194 1195
        surface_validate_location(surface, WINED3D_LOCATION_SYSMEM);
        surface_invalidate_location(surface, ~WINED3D_LOCATION_SYSMEM);
1196

1197 1198 1199 1200 1201
        /* We also get here when the ddraw swapchain is destroyed, for example
         * for a mode switch. In this case this surface won't necessarily be
         * an implicit surface. We have to mark it lost so that the
         * application can restore it after the mode switch. */
        surface->flags |= SFLAG_LOST;
1202
    }
1203
    else
1204
    {
1205
        surface_prepare_map_memory(surface);
1206 1207
        surface_load_location(surface, surface->resource.map_binding);
        surface_invalidate_location(surface, ~surface->resource.map_binding);
1208 1209
    }

1210 1211 1212 1213
    context = context_acquire(device, NULL);
    gl_info = context->gl_info;

    /* Destroy PBOs, but load them into real sysmem before */
1214
    if (surface->pbo)
1215 1216 1217 1218 1219 1220 1221
        surface_remove_pbo(surface, gl_info);

    /* Destroy fbo render buffers. This is needed for implicit render targets, for
     * all application-created targets the application has to release the surface
     * before calling _Reset
     */
    LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1222
    {
1223 1224 1225
        gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
        list_remove(&entry->entry);
        HeapFree(GetProcessHeap(), 0, entry);
1226
    }
1227 1228
    list_init(&surface->renderbuffers);
    surface->current_renderbuffer = NULL;
1229

1230
    if (surface->rb_multisample)
1231
    {
1232 1233
        gl_info->fbo_ops.glDeleteRenderbuffers(1, &surface->rb_multisample);
        surface->rb_multisample = 0;
1234
    }
1235
    if (surface->rb_resolved)
1236
    {
1237 1238 1239
        gl_info->fbo_ops.glDeleteRenderbuffers(1, &surface->rb_resolved);
        surface->rb_resolved = 0;
    }
1240

1241
    context_release(context);
1242

1243 1244
    resource_unload(resource);
}
1245

1246 1247
static const struct wined3d_resource_ops surface_resource_ops =
{
1248 1249
    surface_resource_incref,
    surface_resource_decref,
1250 1251
    surface_unload,
};
1252

1253 1254 1255 1256 1257
static const struct wined3d_surface_ops surface_ops =
{
    surface_private_setup,
    surface_unmap,
};
1258

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
/*****************************************************************************
 * Initializes the GDI surface, aka creates the DIB section we render to
 * The DIB section creation is done by calling GetDC, which will create the
 * section and releasing the dc to allow the app to use it. The dib section
 * will stay until the surface is released
 *
 * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
 * are set to the real sizes to save memory. The NONPOW2 flag is unset to
 * avoid confusion in the shared surface code.
 *
 * Returns:
 *  WINED3D_OK on success
 *  The return values of called methods on failure
 *
 *****************************************************************************/
static HRESULT gdi_surface_private_setup(struct wined3d_surface *surface)
{
    HRESULT hr;
1277

1278
    TRACE("surface %p.\n", surface);
1279

1280 1281 1282 1283 1284
    if (surface->resource.usage & WINED3DUSAGE_OVERLAY)
    {
        ERR("Overlays not yet supported by GDI surfaces.\n");
        return WINED3DERR_INVALIDCALL;
    }
1285

1286 1287 1288
    /* Sysmem textures have memory already allocated - release it,
     * this avoids an unnecessary memcpy. */
    hr = surface_create_dib_section(surface);
1289 1290
    if (FAILED(hr))
        return hr;
1291
    surface->resource.map_binding = WINED3D_LOCATION_DIB;
1292

1293 1294 1295
    /* We don't mind the nonpow2 stuff in GDI. */
    surface->pow2Width = surface->resource.width;
    surface->pow2Height = surface->resource.height;
1296

1297 1298
    return WINED3D_OK;
}
1299

1300
static void gdi_surface_unmap(struct wined3d_surface *surface)
1301
{
1302
    TRACE("surface %p.\n", surface);
1303

1304
    /* Tell the swapchain to update the screen. */
1305
    if (surface->container->swapchain && surface->container == surface->container->swapchain->front_buffer)
1306
        x11_copy_to_screen(surface->container->swapchain, &surface->lockedRect);
1307

1308
    memset(&surface->lockedRect, 0, sizeof(RECT));
1309 1310
}

1311
static const struct wined3d_surface_ops gdi_surface_ops =
1312
{
1313 1314 1315 1316 1317 1318 1319
    gdi_surface_private_setup,
    gdi_surface_unmap,
};

/* This call just downloads data, the caller is responsible for binding the
 * correct texture. */
/* Context activation is done by the caller. */
1320 1321
static void surface_download_data(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info,
        DWORD dst_location)
1322
{
1323
    const struct wined3d_format *format = surface->resource.format;
1324
    struct wined3d_bo_address data;
1325

1326
    /* Only support read back of converted P8 surfaces. */
1327
    if (surface->container->flags & WINED3D_TEXTURE_CONVERTED && format->id != WINED3DFMT_P8_UINT)
1328
    {
1329 1330
        ERR("Trying to read back converted surface %p with format %s.\n", surface, debug_d3dformat(format->id));
        return;
1331 1332
    }

1333
    surface_get_memory(surface, &data, dst_location);
1334

1335
    if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
1336
    {
1337
        TRACE("(%p) : Calling glGetCompressedTexImage level %d, format %#x, type %#x, data %p.\n",
1338
                surface, surface->texture_level, format->glFormat, format->glType, data.addr);
1339

1340
        if (data.buffer_object)
1341
        {
1342 1343
            GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
            checkGLcall("glBindBuffer");
1344 1345
            GL_EXTCALL(glGetCompressedTexImage(surface->texture_target, surface->texture_level, NULL));
            checkGLcall("glGetCompressedTexImage");
1346 1347
            GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
            checkGLcall("glBindBuffer");
1348 1349 1350
        }
        else
        {
1351
            GL_EXTCALL(glGetCompressedTexImage(surface->texture_target,
1352
                    surface->texture_level, data.addr));
1353
            checkGLcall("glGetCompressedTexImage");
1354
        }
1355
    }
1356
    else
1357
    {
1358 1359 1360 1361 1362
        void *mem;
        GLenum gl_format = format->glFormat;
        GLenum gl_type = format->glType;
        int src_pitch = 0;
        int dst_pitch = 0;
1363

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        if (surface->flags & SFLAG_NONPOW2)
        {
            unsigned char alignment = surface->resource.device->surface_alignment;
            src_pitch = format->byte_count * surface->pow2Width;
            dst_pitch = wined3d_surface_get_pitch(surface);
            src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
            mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * surface->pow2Height);
        }
        else
        {
1374
            mem = data.addr;
1375
        }
1376

1377 1378
        TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
                surface, surface->texture_level, gl_format, gl_type, mem);
1379

1380
        if (data.buffer_object)
1381
        {
1382 1383
            GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
            checkGLcall("glBindBuffer");
1384

1385 1386 1387
            gl_info->gl_ops.gl.p_glGetTexImage(surface->texture_target, surface->texture_level,
                    gl_format, gl_type, NULL);
            checkGLcall("glGetTexImage");
1388

1389 1390
            GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
            checkGLcall("glBindBuffer");
1391 1392
        }
        else
1393
        {
1394 1395
            gl_info->gl_ops.gl.p_glGetTexImage(surface->texture_target, surface->texture_level,
                    gl_format, gl_type, mem);
1396
            checkGLcall("glGetTexImage");
1397
        }
1398

1399
        if (surface->flags & SFLAG_NONPOW2)
1400
        {
1401 1402
            const BYTE *src_data;
            BYTE *dst_data;
1403
            UINT y;
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
            /*
             * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
             * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
             * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
             *
             * We're doing this...
             *
             * instead of boxing the texture :
             * |<-texture width ->|  -->pow2width|   /\
             * |111111111111111111|              |   |
             * |222 Texture 222222| boxed empty  | texture height
             * |3333 Data 33333333|              |   |
             * |444444444444444444|              |   \/
             * -----------------------------------   |
             * |     boxed  empty | boxed empty  | pow2height
             * |                  |              |   \/
             * -----------------------------------
             *
             *
             * we're repacking the data to the expected texture width
             *
             * |<-texture width ->|  -->pow2width|   /\
             * |111111111111111111222222222222222|   |
             * |222333333333333333333444444444444| texture height
             * |444444                           |   |
             * |                                 |   \/
             * |                                 |   |
             * |            empty                | pow2height
             * |                                 |   \/
             * -----------------------------------
             *
             * == is the same as
             *
             * |<-texture width ->|    /\
             * |111111111111111111|
             * |222222222222222222|texture height
             * |333333333333333333|
             * |444444444444444444|    \/
             * --------------------
             *
1444 1445
             * This also means that any references to surface memory should work with the data as if it were a
             * standard texture with a non-power2 width instead of a texture boxed up to be a power2 texture.
1446 1447
             *
             * internally the texture is still stored in a boxed format so any references to textureName will
1448
             * get a boxed texture with width pow2width and not a texture of width resource.width.
1449 1450
             *
             * Performance should not be an issue, because applications normally do not lock the surfaces when
1451 1452
             * rendering. If an app does, the WINED3D_TEXTURE_DYNAMIC_MAP flag will kick in and the memory copy
             * won't be released, and doesn't have to be re-read. */
1453
            src_data = mem;
1454
            dst_data = data.addr;
1455
            TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", surface, src_pitch, dst_pitch);
1456
            for (y = 0; y < surface->resource.height; ++y)
1457
            {
1458
                memcpy(dst_data, src_data, dst_pitch);
1459 1460
                src_data += src_pitch;
                dst_data += dst_pitch;
1461
            }
1462 1463

            HeapFree(GetProcessHeap(), 0, mem);
1464 1465 1466 1467
        }
    }
}

1468 1469 1470
/* This call just uploads data, the caller is responsible for binding the
 * correct texture. */
/* Context activation is done by the caller. */
1471
void wined3d_surface_upload_data(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info,
1472
        const struct wined3d_format *format, const RECT *src_rect, UINT src_pitch, const POINT *dst_point,
1473
        BOOL srgb, const struct wined3d_const_bo_address *data)
1474
{
1475 1476
    UINT update_w = src_rect->right - src_rect->left;
    UINT update_h = src_rect->bottom - src_rect->top;
1477

1478 1479
    TRACE("surface %p, gl_info %p, format %s, src_rect %s, src_pitch %u, dst_point %s, srgb %#x, data {%#x:%p}.\n",
            surface, gl_info, debug_d3dformat(format->id), wine_dbgstr_rect(src_rect), src_pitch,
1480
            wine_dbgstr_point(dst_point), srgb, data->buffer_object, data->addr);
1481

1482
    if (surface->resource.map_count)
1483
    {
1484 1485
        WARN("Uploading a surface that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
        surface->container->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
1486 1487
    }

1488
    if (format->flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
1489 1490 1491 1492
    {
        update_h *= format->height_scale.numerator;
        update_h /= format->height_scale.denominator;
    }
1493

1494
    if (data->buffer_object)
1495
    {
1496 1497
        GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
        checkGLcall("glBindBuffer");
1498
    }
1499

1500
    if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
1501
    {
1502
        UINT row_length = wined3d_format_calculate_size(format, 1, update_w, 1, 1);
1503 1504 1505 1506 1507 1508
        UINT row_count = (update_h + format->block_height - 1) / format->block_height;
        const BYTE *addr = data->addr;
        GLenum internal;

        addr += (src_rect->top / format->block_height) * src_pitch;
        addr += (src_rect->left / format->block_width) * format->block_byte_count;
1509

1510 1511
        if (srgb)
            internal = format->glGammaInternal;
1512
        else if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET
1513
                && wined3d_resource_is_offscreen(&surface->container->resource))
1514 1515 1516 1517
            internal = format->rtInternal;
        else
            internal = format->glInternal;

1518
        TRACE("glCompressedTexSubImage2D, target %#x, level %d, x %d, y %d, w %d, h %d, "
1519 1520 1521 1522 1523
                "format %#x, image_size %#x, addr %p.\n", surface->texture_target, surface->texture_level,
                dst_point->x, dst_point->y, update_w, update_h, internal, row_count * row_length, addr);

        if (row_length == src_pitch)
        {
1524
            GL_EXTCALL(glCompressedTexSubImage2D(surface->texture_target, surface->texture_level,
1525 1526 1527 1528 1529 1530
                    dst_point->x, dst_point->y, update_w, update_h, internal, row_count * row_length, addr));
        }
        else
        {
            UINT row, y;

1531 1532
            /* glCompressedTexSubImage2D() ignores pixel store state, so we
             * can't use the unpack row length like for glTexSubImage2D. */
1533 1534
            for (row = 0, y = dst_point->y; row < row_count; ++row)
            {
1535
                GL_EXTCALL(glCompressedTexSubImage2D(surface->texture_target, surface->texture_level,
1536 1537 1538 1539 1540
                        dst_point->x, y, update_w, format->block_height, internal, row_length, addr));
                y += format->block_height;
                addr += src_pitch;
            }
        }
1541
        checkGLcall("glCompressedTexSubImage2D");
1542 1543 1544
    }
    else
    {
1545
        const BYTE *addr = data->addr;
1546

1547
        addr += src_rect->top * src_pitch;
1548 1549 1550 1551 1552 1553
        addr += src_rect->left * format->byte_count;

        TRACE("glTexSubImage2D, target %#x, level %d, x %d, y %d, w %d, h %d, format %#x, type %#x, addr %p.\n",
                surface->texture_target, surface->texture_level, dst_point->x, dst_point->y,
                update_w, update_h, format->glFormat, format->glType, addr);

1554 1555 1556 1557
        gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_pitch / format->byte_count);
        gl_info->gl_ops.gl.p_glTexSubImage2D(surface->texture_target, surface->texture_level,
                dst_point->x, dst_point->y, update_w, update_h, format->glFormat, format->glType, addr);
        gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1558 1559
        checkGLcall("glTexSubImage2D");
    }
1560

1561
    if (data->buffer_object)
1562
    {
1563 1564
        GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
        checkGLcall("glBindBuffer");
1565
    }
1566

1567
    if (wined3d_settings.strict_draw_ordering)
1568
        gl_info->gl_ops.gl.p_glFlush();
1569

1570 1571
    if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
    {
1572
        struct wined3d_device *device = surface->resource.device;
1573 1574
        unsigned int i;

1575
        for (i = 0; i < device->context_count; ++i)
1576
        {
1577
            context_surface_update(device->contexts[i], surface);
1578 1579
        }
    }
1580 1581
}

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
static BOOL surface_check_block_align(struct wined3d_surface *surface, const RECT *rect)
{
    UINT width_mask, height_mask;

    if (!rect->left && !rect->top
            && rect->right == surface->resource.width
            && rect->bottom == surface->resource.height)
        return TRUE;

    /* This assumes power of two block sizes, but NPOT block sizes would be
     * silly anyway. */
    width_mask = surface->resource.format->block_width - 1;
    height_mask = surface->resource.format->block_height - 1;

    if (!(rect->left & width_mask) && !(rect->top & height_mask)
            && !(rect->right & width_mask) && !(rect->bottom & height_mask))
        return TRUE;

    return FALSE;
}

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
HRESULT surface_upload_from_surface(struct wined3d_surface *dst_surface, const POINT *dst_point,
        struct wined3d_surface *src_surface, const RECT *src_rect)
{
    const struct wined3d_format *src_format;
    const struct wined3d_format *dst_format;
    const struct wined3d_gl_info *gl_info;
    struct wined3d_context *context;
    struct wined3d_bo_address data;
    UINT update_w, update_h;
    UINT dst_w, dst_h;
1613
    RECT r, dst_rect;
1614
    UINT src_pitch;
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
    POINT p;

    TRACE("dst_surface %p, dst_point %s, src_surface %p, src_rect %s.\n",
            dst_surface, wine_dbgstr_point(dst_point),
            src_surface, wine_dbgstr_rect(src_rect));

    src_format = src_surface->resource.format;
    dst_format = dst_surface->resource.format;

    if (src_format->id != dst_format->id)
    {
        WARN("Source and destination surfaces should have the same format.\n");
        return WINED3DERR_INVALIDCALL;
    }

    if (!dst_point)
    {
        p.x = 0;
        p.y = 0;
        dst_point = &p;
    }
    else if (dst_point->x < 0 || dst_point->y < 0)
    {
        WARN("Invalid destination point.\n");
        return WINED3DERR_INVALIDCALL;
    }

    if (!src_rect)
    {
        r.left = 0;
        r.top = 0;
        r.right = src_surface->resource.width;
        r.bottom = src_surface->resource.height;
        src_rect = &r;
    }
    else if (src_rect->left < 0 || src_rect->left >= src_rect->right
            || src_rect->top < 0 || src_rect->top >= src_rect->bottom)
    {
        WARN("Invalid source rectangle.\n");
        return WINED3DERR_INVALIDCALL;
    }

    dst_w = dst_surface->resource.width;
    dst_h = dst_surface->resource.height;

    update_w = src_rect->right - src_rect->left;
    update_h = src_rect->bottom - src_rect->top;

    if (update_w > dst_w || dst_point->x > dst_w - update_w
            || update_h > dst_h || dst_point->y > dst_h - update_h)
    {
        WARN("Destination out of bounds.\n");
        return WINED3DERR_INVALIDCALL;
    }

1670 1671 1672 1673 1674 1675 1676 1677
    if ((src_format->flags & WINED3DFMT_FLAG_BLOCKS) && !surface_check_block_align(src_surface, src_rect))
    {
        WARN("Source rectangle not block-aligned.\n");
        return WINED3DERR_INVALIDCALL;
    }

    SetRect(&dst_rect, dst_point->x, dst_point->y, dst_point->x + update_w, dst_point->y + update_h);
    if ((dst_format->flags & WINED3DFMT_FLAG_BLOCKS) && !surface_check_block_align(dst_surface, &dst_rect))
1678
    {
1679
        WARN("Destination rectangle not block-aligned.\n");
1680 1681 1682
        return WINED3DERR_INVALIDCALL;
    }

1683
    /* Use wined3d_surface_blt() instead of uploading directly if we need conversion. */
1684
    if (dst_format->convert || wined3d_format_get_color_key_conversion(dst_surface->container, FALSE))
1685
        return wined3d_surface_blt(dst_surface, &dst_rect, src_surface, src_rect, 0, NULL, WINED3D_TEXF_POINT);
1686 1687 1688 1689 1690 1691 1692 1693

    context = context_acquire(dst_surface->resource.device, NULL);
    gl_info = context->gl_info;

    /* Only load the surface for partial updates. For newly allocated texture
     * the texture wouldn't be the current location, and we'd upload zeroes
     * just to overwrite them again. */
    if (update_w == dst_w && update_h == dst_h)
1694
        wined3d_texture_prepare_texture(dst_surface->container, context, FALSE);
1695
    else
1696
        surface_load_location(dst_surface, WINED3D_LOCATION_TEXTURE_RGB);
1697
    wined3d_texture_bind(dst_surface->container, context, FALSE);
1698

1699
    surface_get_memory(src_surface, &data, src_surface->locations);
1700
    src_pitch = wined3d_surface_get_pitch(src_surface);
1701

1702 1703
    wined3d_surface_upload_data(dst_surface, gl_info, src_format, src_rect,
            src_pitch, dst_point, FALSE, wined3d_const_bo_address(&data));
1704

1705
    context_invalidate_active_texture(context);
1706 1707 1708

    context_release(context);

1709 1710
    surface_validate_location(dst_surface, WINED3D_LOCATION_TEXTURE_RGB);
    surface_invalidate_location(dst_surface, ~WINED3D_LOCATION_TEXTURE_RGB);
1711

1712 1713 1714
    return WINED3D_OK;
}

1715 1716 1717
/* In D3D the depth stencil dimensions have to be greater than or equal to the
 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
/* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1718
/* Context activation is done by the caller. */
1719
void surface_set_compatible_renderbuffer(struct wined3d_surface *surface, const struct wined3d_surface *rt)
1720 1721
{
    const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
1722
    struct wined3d_renderbuffer_entry *entry;
1723 1724
    GLuint renderbuffer = 0;
    unsigned int src_width, src_height;
1725 1726
    unsigned int width, height;

1727
    if (rt && rt->resource.format->id != WINED3DFMT_NULL)
1728 1729 1730 1731 1732 1733 1734 1735 1736
    {
        width = rt->pow2Width;
        height = rt->pow2Height;
    }
    else
    {
        width = surface->pow2Width;
        height = surface->pow2Height;
    }
1737

1738 1739
    src_width = surface->pow2Width;
    src_height = surface->pow2Height;
1740 1741 1742 1743 1744

    /* A depth stencil smaller than the render target is not valid */
    if (width > src_width || height > src_height) return;

    /* Remove any renderbuffer set if the sizes match */
1745 1746 1747
    if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
            || (width == src_width && height == src_height))
    {
1748
        surface->current_renderbuffer = NULL;
1749 1750 1751 1752
        return;
    }

    /* Look if we've already got a renderbuffer of the correct dimensions */
1753
    LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1754 1755 1756
    {
        if (entry->width == width && entry->height == height)
        {
1757
            renderbuffer = entry->id;
1758
            surface->current_renderbuffer = entry;
1759 1760 1761 1762
            break;
        }
    }

1763 1764
    if (!renderbuffer)
    {
1765 1766 1767
        gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
        gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
        gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
1768
                surface->resource.format->glInternal, width, height);
1769

1770
        entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
1771 1772 1773
        entry->width = width;
        entry->height = height;
        entry->id = renderbuffer;
1774
        list_add_head(&surface->renderbuffers, &entry->entry);
1775

1776
        surface->current_renderbuffer = entry;
1777 1778 1779 1780 1781
    }

    checkGLcall("set_compatible_renderbuffer");
}

1782
GLenum surface_get_gl_buffer(const struct wined3d_surface *surface)
1783
{
1784
    const struct wined3d_swapchain *swapchain = surface->container->swapchain;
1785

1786
    TRACE("surface %p.\n", surface);
1787

1788
    if (!swapchain)
1789
    {
1790
        ERR("Surface %p is not on a swapchain.\n", surface);
1791 1792 1793
        return GL_NONE;
    }

1794
    if (swapchain->back_buffers && swapchain->back_buffers[0] == surface->container)
1795 1796 1797
    {
        if (swapchain->render_to_fbo)
        {
1798 1799 1800
            TRACE("Returning GL_COLOR_ATTACHMENT0\n");
            return GL_COLOR_ATTACHMENT0;
        }
1801 1802
        TRACE("Returning GL_BACK\n");
        return GL_BACK;
1803
    }
1804
    else if (surface->container == swapchain->front_buffer)
1805
    {
1806 1807 1808 1809 1810 1811 1812
        TRACE("Returning GL_FRONT\n");
        return GL_FRONT;
    }

    FIXME("Higher back buffer, returning GL_BACK\n");
    return GL_BACK;
}
1813

1814
void surface_load(struct wined3d_surface *surface, BOOL srgb)
1815
{
1816
    DWORD location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
1817 1818 1819

    TRACE("surface %p, srgb %#x.\n", surface, srgb);

1820
    if (surface->resource.pool == WINED3D_POOL_SCRATCH)
1821 1822
        ERR("Not supported on scratch surfaces.\n");

1823
    if (surface->locations & location)
1824 1825
    {
        TRACE("surface is already in texture\n");
1826
        return;
1827
    }
1828
    TRACE("Reloading because surface is dirty.\n");
1829

1830
    surface_load_location(surface, location);
1831
    surface_evict_sysmem(surface);
1832 1833
}

1834 1835 1836 1837
/* See also float_16_to_32() in wined3d_private.h */
static inline unsigned short float_32_to_16(const float *in)
{
    int exp = 0;
1838
    float tmp = fabsf(*in);
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
    unsigned int mantissa;
    unsigned short ret;

    /* Deal with special numbers */
    if (*in == 0.0f)
        return 0x0000;
    if (isnan(*in))
        return 0x7c01;
    if (isinf(*in))
        return (*in < 0.0f ? 0xfc00 : 0x7c00);

    if (tmp < powf(2, 10))
    {
        do
        {
            tmp = tmp * 2.0f;
            exp--;
        } while (tmp < powf(2, 10));
    }
    else if (tmp >= powf(2, 11))
    {
        do
        {
            tmp /= 2.0f;
            exp++;
        } while (tmp >= powf(2, 11));
    }

    mantissa = (unsigned int)tmp;
    if (tmp - mantissa >= 0.5f)
        ++mantissa; /* Round to nearest, away from zero. */

    exp += 10;  /* Normalize the mantissa. */
    exp += 15;  /* Exponent is encoded with excess 15. */

    if (exp > 30) /* too big */
    {
        ret = 0x7c00; /* INF */
    }
    else if (exp <= 0)
    {
        /* exp == 0: Non-normalized mantissa. Returns 0x0000 (=0.0) for too small numbers. */
        while (exp <= 0)
        {
            mantissa = mantissa >> 1;
            ++exp;
        }
        ret = mantissa & 0x3ff;
    }
    else
    {
        ret = (exp << 10) | (mantissa & 0x3ff);
    }

    ret |= ((*in < 0.0f ? 1 : 0) << 15); /* Add the sign */
    return ret;
}

1897
ULONG CDECL wined3d_surface_incref(struct wined3d_surface *surface)
1898
{
1899
    TRACE("surface %p, container %p.\n", surface, surface->container);
1900

1901
    return wined3d_texture_incref(surface->container);
1902 1903
}

1904
ULONG CDECL wined3d_surface_decref(struct wined3d_surface *surface)
1905
{
1906
    TRACE("surface %p, container %p.\n", surface, surface->container);
1907

1908
    return wined3d_texture_decref(surface->container);
1909 1910
}

1911
void CDECL wined3d_surface_preload(struct wined3d_surface *surface)
1912
{
1913
    TRACE("surface %p.\n", surface);
1914

1915 1916 1917 1918 1919 1920
    if (!surface->resource.device->d3d_initialized)
    {
        ERR("D3D not initialized.\n");
        return;
    }

1921
    wined3d_texture_preload(surface->container);
1922 1923
}

1924
void * CDECL wined3d_surface_get_parent(const struct wined3d_surface *surface)
1925
{
1926
    TRACE("surface %p.\n", surface);
1927

1928
    return surface->resource.parent;
1929 1930
}

1931
struct wined3d_resource * CDECL wined3d_surface_get_resource(struct wined3d_surface *surface)
1932
{
1933
    TRACE("surface %p.\n", surface);
1934

1935
    return &surface->resource;
1936 1937
}

1938
HRESULT CDECL wined3d_surface_get_blt_status(const struct wined3d_surface *surface, DWORD flags)
1939
{
1940
    TRACE("surface %p, flags %#x.\n", surface, flags);
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952

    switch (flags)
    {
        case WINEDDGBS_CANBLT:
        case WINEDDGBS_ISBLTDONE:
            return WINED3D_OK;

        default:
            return WINED3DERR_INVALIDCALL;
    }
}

1953
HRESULT CDECL wined3d_surface_get_flip_status(const struct wined3d_surface *surface, DWORD flags)
1954
{
1955
    TRACE("surface %p, flags %#x.\n", surface, flags);
1956

1957
    /* XXX: DDERR_INVALIDSURFACETYPE */
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969

    switch (flags)
    {
        case WINEDDGFS_CANFLIP:
        case WINEDDGFS_ISFLIPDONE:
            return WINED3D_OK;

        default:
            return WINED3DERR_INVALIDCALL;
    }
}

1970
HRESULT CDECL wined3d_surface_is_lost(const struct wined3d_surface *surface)
1971
{
1972
    TRACE("surface %p.\n", surface);
1973 1974 1975 1976 1977

    /* D3D8 and 9 loose full devices, ddraw only surfaces. */
    return surface->flags & SFLAG_LOST ? WINED3DERR_DEVICELOST : WINED3D_OK;
}

1978
HRESULT CDECL wined3d_surface_restore(struct wined3d_surface *surface)
1979
{
1980
    TRACE("surface %p.\n", surface);
1981 1982 1983 1984 1985

    surface->flags &= ~SFLAG_LOST;
    return WINED3D_OK;
}

1986
DWORD CDECL wined3d_surface_get_pitch(const struct wined3d_surface *surface)
1987
{
1988
    unsigned int alignment;
1989 1990
    DWORD pitch;

1991
    TRACE("surface %p.\n", surface);
1992

1993 1994 1995
    if (surface->pitch)
        return surface->pitch;

1996 1997 1998
    alignment = surface->resource.device->surface_alignment;
    pitch = wined3d_format_calculate_pitch(surface->resource.format, surface->resource.width);
    pitch = (pitch + alignment - 1) & ~(alignment - 1);
1999 2000 2001 2002 2003 2004

    TRACE("Returning %u.\n", pitch);

    return pitch;
}

2005
HRESULT CDECL wined3d_surface_set_overlay_position(struct wined3d_surface *surface, LONG x, LONG y)
2006 2007 2008
{
    LONG w, h;

2009
    TRACE("surface %p, x %d, y %d.\n", surface, x, y);
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

    if (!(surface->resource.usage & WINED3DUSAGE_OVERLAY))
    {
        WARN("Not an overlay surface.\n");
        return WINEDDERR_NOTAOVERLAYSURFACE;
    }

    w = surface->overlay_destrect.right - surface->overlay_destrect.left;
    h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
    surface->overlay_destrect.left = x;
    surface->overlay_destrect.top = y;
    surface->overlay_destrect.right = x + w;
    surface->overlay_destrect.bottom = y + h;

    return WINED3D_OK;
}

2027
HRESULT CDECL wined3d_surface_get_overlay_position(const struct wined3d_surface *surface, LONG *x, LONG *y)
2028
{
2029
    TRACE("surface %p, x %p, y %p.\n", surface, x, y);
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052

    if (!(surface->resource.usage & WINED3DUSAGE_OVERLAY))
    {
        TRACE("Not an overlay surface.\n");
        return WINEDDERR_NOTAOVERLAYSURFACE;
    }

    if (!surface->overlay_dest)
    {
        TRACE("Overlay not visible.\n");
        *x = 0;
        *y = 0;
        return WINEDDERR_OVERLAYNOTVISIBLE;
    }

    *x = surface->overlay_destrect.left;
    *y = surface->overlay_destrect.top;

    TRACE("Returning position %d, %d.\n", *x, *y);

    return WINED3D_OK;
}

2053 2054
HRESULT CDECL wined3d_surface_update_overlay_z_order(struct wined3d_surface *surface,
        DWORD flags, struct wined3d_surface *ref)
2055
{
2056
    FIXME("surface %p, flags %#x, ref %p stub!\n", surface, flags, ref);
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066

    if (!(surface->resource.usage & WINED3DUSAGE_OVERLAY))
    {
        TRACE("Not an overlay surface.\n");
        return WINEDDERR_NOTAOVERLAYSURFACE;
    }

    return WINED3D_OK;
}

2067
HRESULT CDECL wined3d_surface_update_overlay(struct wined3d_surface *surface, const RECT *src_rect,
2068
        struct wined3d_surface *dst_surface, const RECT *dst_rect, DWORD flags, const WINEDDOVERLAYFX *fx)
2069
{
2070 2071
    TRACE("surface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
            surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103

    if (!(surface->resource.usage & WINED3DUSAGE_OVERLAY))
    {
        WARN("Not an overlay surface.\n");
        return WINEDDERR_NOTAOVERLAYSURFACE;
    }
    else if (!dst_surface)
    {
        WARN("Dest surface is NULL.\n");
        return WINED3DERR_INVALIDCALL;
    }

    if (src_rect)
    {
        surface->overlay_srcrect = *src_rect;
    }
    else
    {
        surface->overlay_srcrect.left = 0;
        surface->overlay_srcrect.top = 0;
        surface->overlay_srcrect.right = surface->resource.width;
        surface->overlay_srcrect.bottom = surface->resource.height;
    }

    if (dst_rect)
    {
        surface->overlay_destrect = *dst_rect;
    }
    else
    {
        surface->overlay_destrect.left = 0;
        surface->overlay_destrect.top = 0;
2104 2105
        surface->overlay_destrect.right = dst_surface ? dst_surface->resource.width : 0;
        surface->overlay_destrect.bottom = dst_surface ? dst_surface->resource.height : 0;
2106 2107
    }

2108
    if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2109
    {
2110
        surface->overlay_dest = NULL;
2111 2112 2113 2114 2115
        list_remove(&surface->overlay_entry);
    }

    if (flags & WINEDDOVER_SHOW)
    {
2116
        if (surface->overlay_dest != dst_surface)
2117
        {
2118 2119
            surface->overlay_dest = dst_surface;
            list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
        }
    }
    else if (flags & WINEDDOVER_HIDE)
    {
        /* tests show that the rectangles are erased on hide */
        surface->overlay_srcrect.left = 0; surface->overlay_srcrect.top = 0;
        surface->overlay_srcrect.right = 0; surface->overlay_srcrect.bottom = 0;
        surface->overlay_destrect.left = 0; surface->overlay_destrect.top = 0;
        surface->overlay_destrect.right = 0; surface->overlay_destrect.bottom = 0;
        surface->overlay_dest = NULL;
    }

    return WINED3D_OK;
}

2135 2136
HRESULT wined3d_surface_update_desc(struct wined3d_surface *surface,
        const struct wined3d_gl_info *gl_info, void *mem, unsigned int pitch)
2137
{
2138 2139
    struct wined3d_resource *texture_resource = &surface->container->resource;
    unsigned int width, height;
2140
    BOOL create_dib = FALSE;
2141
    DWORD valid_location = 0;
2142
    HRESULT hr;
2143 2144 2145 2146 2147 2148 2149

    if (surface->flags & SFLAG_DIBSECTION)
    {
        DeleteDC(surface->hDC);
        DeleteObject(surface->dib.DIBsection);
        surface->dib.bitmap_data = NULL;
        surface->flags &= ~SFLAG_DIBSECTION;
2150
        create_dib = TRUE;
2151 2152
    }

2153
    surface->locations = 0;
2154
    wined3d_resource_free_sysmem(&surface->resource);
2155

2156 2157
    width = texture_resource->width;
    height = texture_resource->height;
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
    surface->resource.width = width;
    surface->resource.height = height;
    if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[ARB_TEXTURE_RECTANGLE]
            || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
    {
        surface->pow2Width = width;
        surface->pow2Height = height;
    }
    else
    {
        surface->pow2Width = surface->pow2Height = 1;
        while (surface->pow2Width < width)
            surface->pow2Width <<= 1;
        while (surface->pow2Height < height)
            surface->pow2Height <<= 1;
    }

    if (surface->pow2Width != width || surface->pow2Height != height)
        surface->flags |= SFLAG_NONPOW2;
    else
        surface->flags &= ~SFLAG_NONPOW2;

2180
    if ((surface->user_memory = mem))
2181
    {
2182
        surface->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
2183
        valid_location = WINED3D_LOCATION_USER_MEMORY;
2184
    }
2185
    surface->pitch = pitch;
2186 2187 2188
    surface->resource.format = texture_resource->format;
    surface->resource.multisample_type = texture_resource->multisample_type;
    surface->resource.multisample_quality = texture_resource->multisample_quality;
2189
    if (surface->pitch)
2190
    {
2191
        surface->resource.size = height * surface->pitch;
2192
    }
2193
    else
2194 2195
    {
        /* User memory surfaces don't have the regular surface alignment. */
2196
        surface->resource.size = wined3d_format_calculate_size(texture_resource->format,
2197 2198 2199
                1, width, height, 1);
        surface->pitch = wined3d_format_calculate_pitch(texture_resource->format, width);
    }
2200

2201 2202 2203 2204
    /* The format might be changed to a format that needs conversion.
     * If the surface didn't use PBOs previously but could now, don't
     * change it - whatever made us not use PBOs might come back, e.g.
     * color keys. */
2205 2206
    if (surface->resource.map_binding == WINED3D_LOCATION_BUFFER && !surface_use_pbo(surface))
        surface->resource.map_binding = create_dib ? WINED3D_LOCATION_DIB : WINED3D_LOCATION_SYSMEM;
2207

2208 2209 2210 2211 2212 2213 2214
    if (create_dib)
    {
        if (FAILED(hr = surface_create_dib_section(surface)))
        {
            ERR("Failed to create dib section, hr %#x.\n", hr);
            return hr;
        }
2215
        if (!valid_location)
2216
            valid_location = WINED3D_LOCATION_DIB;
2217
    }
2218

2219 2220 2221
    if (!valid_location)
    {
        surface_prepare_system_memory(surface);
2222
        valid_location = WINED3D_LOCATION_SYSMEM;
2223 2224 2225
    }

    surface_validate_location(surface, valid_location);
2226

2227 2228 2229
    return WINED3D_OK;
}

2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
static void convert_r32_float_r16_float(const BYTE *src, BYTE *dst,
        DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
{
    unsigned short *dst_s;
    const float *src_f;
    unsigned int x, y;

    TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);

    for (y = 0; y < h; ++y)
    {
        src_f = (const float *)(src + y * pitch_in);
        dst_s = (unsigned short *) (dst + y * pitch_out);
        for (x = 0; x < w; ++x)
        {
            dst_s[x] = float_32_to_16(src_f + x);
        }
    }
}

static void convert_r5g6b5_x8r8g8b8(const BYTE *src, BYTE *dst,
        DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
{
    static const unsigned char convert_5to8[] =
    {
        0x00, 0x08, 0x10, 0x19, 0x21, 0x29, 0x31, 0x3a,
        0x42, 0x4a, 0x52, 0x5a, 0x63, 0x6b, 0x73, 0x7b,
        0x84, 0x8c, 0x94, 0x9c, 0xa5, 0xad, 0xb5, 0xbd,
        0xc5, 0xce, 0xd6, 0xde, 0xe6, 0xef, 0xf7, 0xff,
    };
    static const unsigned char convert_6to8[] =
    {
        0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
        0x20, 0x24, 0x28, 0x2d, 0x31, 0x35, 0x39, 0x3d,
        0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d,
        0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d,
        0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
        0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
        0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd7, 0xdb, 0xdf,
        0xe3, 0xe7, 0xeb, 0xef, 0xf3, 0xf7, 0xfb, 0xff,
    };
    unsigned int x, y;

    TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);

    for (y = 0; y < h; ++y)
    {
        const WORD *src_line = (const WORD *)(src + y * pitch_in);
        DWORD *dst_line = (DWORD *)(dst + y * pitch_out);
        for (x = 0; x < w; ++x)
        {
            WORD pixel = src_line[x];
            dst_line[x] = 0xff000000
                    | convert_5to8[(pixel & 0xf800) >> 11] << 16
                    | convert_6to8[(pixel & 0x07e0) >> 5] << 8
                    | convert_5to8[(pixel & 0x001f)];
        }
    }
}

2290 2291
/* We use this for both B8G8R8A8 -> B8G8R8X8 and B8G8R8X8 -> B8G8R8A8, since
 * in both cases we're just setting the X / Alpha channel to 0xff. */
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
static void convert_a8r8g8b8_x8r8g8b8(const BYTE *src, BYTE *dst,
        DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
{
    unsigned int x, y;

    TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);

    for (y = 0; y < h; ++y)
    {
        const DWORD *src_line = (const DWORD *)(src + y * pitch_in);
        DWORD *dst_line = (DWORD *)(dst + y * pitch_out);

        for (x = 0; x < w; ++x)
        {
            dst_line[x] = 0xff000000 | (src_line[x] & 0xffffff);
        }
    }
}

static inline BYTE cliptobyte(int x)
{
    return (BYTE)((x < 0) ? 0 : ((x > 255) ? 255 : x));
}

static void convert_yuy2_x8r8g8b8(const BYTE *src, BYTE *dst,
        DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
{
    int c2, d, e, r2 = 0, g2 = 0, b2 = 0;
    unsigned int x, y;

    TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);

    for (y = 0; y < h; ++y)
    {
        const BYTE *src_line = src + y * pitch_in;
        DWORD *dst_line = (DWORD *)(dst + y * pitch_out);
        for (x = 0; x < w; ++x)
        {
            /* YUV to RGB conversion formulas from http://en.wikipedia.org/wiki/YUV:
             *     C = Y - 16; D = U - 128; E = V - 128;
             *     R = cliptobyte((298 * C + 409 * E + 128) >> 8);
             *     G = cliptobyte((298 * C - 100 * D - 208 * E + 128) >> 8);
             *     B = cliptobyte((298 * C + 516 * D + 128) >> 8);
             * Two adjacent YUY2 pixels are stored as four bytes: Y0 U Y1 V .
             * U and V are shared between the pixels. */
            if (!(x & 1)) /* For every even pixel, read new U and V. */
            {
                d = (int) src_line[1] - 128;
                e = (int) src_line[3] - 128;
                r2 = 409 * e + 128;
                g2 = - 100 * d - 208 * e + 128;
                b2 = 516 * d + 128;
            }
            c2 = 298 * ((int) src_line[0] - 16);
            dst_line[x] = 0xff000000
                | cliptobyte((c2 + r2) >> 8) << 16    /* red   */
                | cliptobyte((c2 + g2) >> 8) << 8     /* green */
                | cliptobyte((c2 + b2) >> 8);         /* blue  */
                /* Scale RGB values to 0..255 range,
                 * then clip them if still not in range (may be negative),
                 * then shift them within DWORD if necessary. */
            src_line += 2;
        }
    }
}

static void convert_yuy2_r5g6b5(const BYTE *src, BYTE *dst,
        DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
{
    unsigned int x, y;
    int c2, d, e, r2 = 0, g2 = 0, b2 = 0;

    TRACE("Converting %ux%u pixels, pitches %u %u\n", w, h, pitch_in, pitch_out);

    for (y = 0; y < h; ++y)
    {
        const BYTE *src_line = src + y * pitch_in;
        WORD *dst_line = (WORD *)(dst + y * pitch_out);
        for (x = 0; x < w; ++x)
        {
            /* YUV to RGB conversion formulas from http://en.wikipedia.org/wiki/YUV:
             *     C = Y - 16; D = U - 128; E = V - 128;
             *     R = cliptobyte((298 * C + 409 * E + 128) >> 8);
             *     G = cliptobyte((298 * C - 100 * D - 208 * E + 128) >> 8);
             *     B = cliptobyte((298 * C + 516 * D + 128) >> 8);
             * Two adjacent YUY2 pixels are stored as four bytes: Y0 U Y1 V .
             * U and V are shared between the pixels. */
            if (!(x & 1)) /* For every even pixel, read new U and V. */
            {
                d = (int) src_line[1] - 128;
                e = (int) src_line[3] - 128;
                r2 = 409 * e + 128;
                g2 = - 100 * d - 208 * e + 128;
                b2 = 516 * d + 128;
            }
            c2 = 298 * ((int) src_line[0] - 16);
            dst_line[x] = (cliptobyte((c2 + r2) >> 8) >> 3) << 11   /* red   */
                | (cliptobyte((c2 + g2) >> 8) >> 2) << 5            /* green */
                | (cliptobyte((c2 + b2) >> 8) >> 3);                /* blue  */
                /* Scale RGB values to 0..255 range,
                 * then clip them if still not in range (may be negative),
                 * then shift them within DWORD if necessary. */
            src_line += 2;
        }
    }
}

2399
struct d3dfmt_converter_desc
2400 2401 2402 2403 2404
{
    enum wined3d_format_id from, to;
    void (*convert)(const BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h);
};

2405
static const struct d3dfmt_converter_desc converters[] =
2406 2407 2408 2409
{
    {WINED3DFMT_R32_FLOAT,      WINED3DFMT_R16_FLOAT,       convert_r32_float_r16_float},
    {WINED3DFMT_B5G6R5_UNORM,   WINED3DFMT_B8G8R8X8_UNORM,  convert_r5g6b5_x8r8g8b8},
    {WINED3DFMT_B8G8R8A8_UNORM, WINED3DFMT_B8G8R8X8_UNORM,  convert_a8r8g8b8_x8r8g8b8},
2410
    {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM,  convert_a8r8g8b8_x8r8g8b8},
2411 2412 2413 2414
    {WINED3DFMT_YUY2,           WINED3DFMT_B8G8R8X8_UNORM,  convert_yuy2_x8r8g8b8},
    {WINED3DFMT_YUY2,           WINED3DFMT_B5G6R5_UNORM,    convert_yuy2_r5g6b5},
};

2415
static inline const struct d3dfmt_converter_desc *find_converter(enum wined3d_format_id from,
2416 2417 2418 2419
        enum wined3d_format_id to)
{
    unsigned int i;

2420
    for (i = 0; i < (sizeof(converters) / sizeof(*converters)); ++i)
2421
    {
2422 2423
        if (converters[i].from == from && converters[i].to == to)
            return &converters[i];
2424 2425 2426 2427 2428
    }

    return NULL;
}

2429
static struct wined3d_texture *surface_convert_format(struct wined3d_surface *source, enum wined3d_format_id to_fmt)
2430
{
2431
    struct wined3d_map_desc src_map, dst_map;
2432
    const struct d3dfmt_converter_desc *conv;
2433 2434 2435
    struct wined3d_texture *ret = NULL;
    struct wined3d_resource_desc desc;
    struct wined3d_surface *dst;
2436

2437
    conv = find_converter(source->resource.format->id, to_fmt);
2438 2439 2440 2441 2442 2443 2444
    if (!conv)
    {
        FIXME("Cannot find a conversion function from format %s to %s.\n",
                debug_d3dformat(source->resource.format->id), debug_d3dformat(to_fmt));
        return NULL;
    }

2445
    /* FIXME: Multisampled conversion? */
2446
    wined3d_resource_get_desc(&source->resource, &desc);
2447
    desc.resource_type = WINED3D_RTYPE_TEXTURE;
2448 2449 2450
    desc.format = to_fmt;
    desc.usage = 0;
    desc.pool = WINED3D_POOL_SCRATCH;
2451
    if (FAILED(wined3d_texture_create(source->resource.device, &desc, 1,
2452
            WINED3D_SURFACE_MAPPABLE | WINED3D_SURFACE_DISCARD, NULL, NULL, &wined3d_null_parent_ops, &ret)))
2453 2454 2455 2456
    {
        ERR("Failed to create a destination surface for conversion.\n");
        return NULL;
    }
2457
    dst = surface_from_resource(wined3d_texture_get_sub_resource(ret, 0));
2458

2459 2460
    memset(&src_map, 0, sizeof(src_map));
    memset(&dst_map, 0, sizeof(dst_map));
2461

2462
    if (FAILED(wined3d_surface_map(source, &src_map, NULL, WINED3D_MAP_READONLY)))
2463 2464
    {
        ERR("Failed to lock the source surface.\n");
2465
        wined3d_texture_decref(ret);
2466 2467
        return NULL;
    }
2468
    if (FAILED(wined3d_surface_map(dst, &dst_map, NULL, 0)))
2469 2470
    {
        ERR("Failed to lock the destination surface.\n");
2471
        wined3d_surface_unmap(source);
2472
        wined3d_texture_decref(ret);
2473 2474 2475
        return NULL;
    }

2476
    conv->convert(src_map.data, dst_map.data, src_map.row_pitch, dst_map.row_pitch,
2477 2478
            source->resource.width, source->resource.height);

2479
    wined3d_surface_unmap(dst);
2480
    wined3d_surface_unmap(source);
2481

2482
    return ret;
2483 2484 2485 2486 2487 2488
}

static HRESULT _Blt_ColorFill(BYTE *buf, unsigned int width, unsigned int height,
        unsigned int bpp, UINT pitch, DWORD color)
{
    BYTE *first;
2489
    unsigned int x, y;
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514

    /* Do first row */

#define COLORFILL_ROW(type) \
do { \
    type *d = (type *)buf; \
    for (x = 0; x < width; ++x) \
        d[x] = (type)color; \
} while(0)

    switch (bpp)
    {
        case 1:
            COLORFILL_ROW(BYTE);
            break;

        case 2:
            COLORFILL_ROW(WORD);
            break;

        case 3:
        {
            BYTE *d = buf;
            for (x = 0; x < width; ++x, d += 3)
            {
2515 2516 2517
                d[0] = (color      ) & 0xff;
                d[1] = (color >>  8) & 0xff;
                d[2] = (color >> 16) & 0xff;
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
            }
            break;
        }
        case 4:
            COLORFILL_ROW(DWORD);
            break;

        default:
            FIXME("Color fill not implemented for bpp %u!\n", bpp * 8);
            return WINED3DERR_NOTAVAILABLE;
    }

#undef COLORFILL_ROW

    /* Now copy first row. */
    first = buf;
    for (y = 1; y < height; ++y)
    {
        buf += pitch;
        memcpy(buf, first, width * bpp);
    }

    return WINED3D_OK;
}

2543 2544 2545 2546 2547
struct wined3d_surface * CDECL wined3d_surface_from_resource(struct wined3d_resource *resource)
{
    return surface_from_resource(resource);
}

2548
HRESULT CDECL wined3d_surface_unmap(struct wined3d_surface *surface)
2549
{
2550
    TRACE("surface %p.\n", surface);
2551

2552
    if (!surface->resource.map_count)
2553 2554 2555 2556
    {
        WARN("Trying to unmap unmapped surface.\n");
        return WINEDDERR_NOTLOCKED;
    }
2557
    --surface->resource.map_count;
2558

2559
    surface->surface_ops->surface_unmap(surface);
2560

2561 2562
    return WINED3D_OK;
}
2563

2564
HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
2565
        struct wined3d_map_desc *map_desc, const RECT *rect, DWORD flags)
2566
{
2567
    const struct wined3d_format *format = surface->resource.format;
2568 2569 2570
    struct wined3d_device *device = surface->resource.device;
    struct wined3d_context *context;
    const struct wined3d_gl_info *gl_info;
2571
    BYTE *base_memory;
2572

2573 2574
    TRACE("surface %p, map_desc %p, rect %s, flags %#x.\n",
            surface, map_desc, wine_dbgstr_rect(rect), flags);
2575

2576
    if (surface->resource.map_count)
2577
    {
2578 2579
        WARN("Surface is already mapped.\n");
        return WINED3DERR_INVALIDCALL;
2580
    }
2581

2582 2583 2584 2585 2586
    if ((format->flags & WINED3DFMT_FLAG_BLOCKS) && rect
            && !surface_check_block_align(surface, rect))
    {
        WARN("Map rect %s is misaligned for %ux%u blocks.\n",
                wine_dbgstr_rect(rect), format->block_width, format->block_height);
2587

2588 2589
        if (surface->resource.pool == WINED3D_POOL_DEFAULT)
            return WINED3DERR_INVALIDCALL;
2590 2591
    }

2592
    ++surface->resource.map_count;
2593

2594
    if (!(surface->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
2595 2596
        WARN("Trying to lock unlockable surface.\n");

2597 2598 2599
    /* Performance optimization: Count how often a surface is mapped, if it is
     * mapped regularly do not throw away the system memory copy. This avoids
     * the need to download the surface from OpenGL all the time. The surface
2600 2601 2602 2603
     * is still downloaded if the OpenGL texture is changed. Note that this
     * only really makes sense for managed textures.*/
    if (!(surface->container->flags & WINED3D_TEXTURE_DYNAMIC_MAP)
            && surface->resource.map_binding == WINED3D_LOCATION_SYSMEM)
2604 2605 2606 2607
    {
        if (++surface->lockCount > MAXLOCKCOUNT)
        {
            TRACE("Surface is mapped regularly, not freeing the system memory copy any more.\n");
2608
            surface->container->flags |= WINED3D_TEXTURE_DYNAMIC_MAP;
2609 2610 2611
        }
    }

2612 2613 2614 2615
    surface_prepare_map_memory(surface);
    if (flags & WINED3D_MAP_DISCARD)
    {
        TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2616 2617
                wined3d_debug_location(surface->resource.map_binding));
        surface_validate_location(surface, surface->resource.map_binding);
2618 2619 2620 2621 2622 2623
    }
    else
    {
        if (surface->resource.usage & WINED3DUSAGE_DYNAMIC)
            WARN_(d3d_perf)("Mapping a dynamic surface without WINED3D_MAP_DISCARD.\n");

2624
        surface_load_location(surface, surface->resource.map_binding);
2625 2626 2627
    }

    if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
2628
        surface_invalidate_location(surface, ~surface->resource.map_binding);
2629

2630
    switch (surface->resource.map_binding)
2631
    {
2632 2633 2634 2635 2636
        case WINED3D_LOCATION_SYSMEM:
            base_memory = surface->resource.heap_memory;
            break;

        case WINED3D_LOCATION_USER_MEMORY:
2637 2638 2639
            base_memory = surface->user_memory;
            break;

2640
        case WINED3D_LOCATION_DIB:
2641 2642 2643
            base_memory = surface->dib.bitmap_data;
            break;

2644
        case WINED3D_LOCATION_BUFFER:
2645 2646 2647
            context = context_acquire(device, NULL);
            gl_info = context->gl_info;

2648 2649 2650
            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, surface->pbo));
            base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE));
            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2651 2652 2653 2654 2655 2656
            checkGLcall("map PBO");

            context_release(context);
            break;

        default:
2657
            ERR("Unexpected map binding %s.\n", wined3d_debug_location(surface->resource.map_binding));
2658 2659
            base_memory = NULL;
    }
2660

2661
    if (format->flags & WINED3DFMT_FLAG_BROKEN_PITCH)
2662
        map_desc->row_pitch = surface->resource.width * format->byte_count;
2663
    else
2664 2665
        map_desc->row_pitch = wined3d_surface_get_pitch(surface);
    map_desc->slice_pitch = 0;
2666 2667

    if (!rect)
2668
    {
2669
        map_desc->data = base_memory;
2670 2671 2672 2673
        surface->lockedRect.left = 0;
        surface->lockedRect.top = 0;
        surface->lockedRect.right = surface->resource.width;
        surface->lockedRect.bottom = surface->resource.height;
2674 2675 2676
    }
    else
    {
2677
        if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
2678
        {
2679 2680
            /* Compressed textures are block based, so calculate the offset of
             * the block that contains the top-left pixel of the locked rectangle. */
2681
            map_desc->data = base_memory
2682
                    + ((rect->top / format->block_height) * map_desc->row_pitch)
2683
                    + ((rect->left / format->block_width) * format->block_byte_count);
2684 2685 2686
        }
        else
        {
2687
            map_desc->data = base_memory
2688
                    + (map_desc->row_pitch * rect->top)
2689
                    + (rect->left * format->byte_count);
2690
        }
2691 2692 2693 2694
        surface->lockedRect.left = rect->left;
        surface->lockedRect.top = rect->top;
        surface->lockedRect.right = rect->right;
        surface->lockedRect.bottom = rect->bottom;
2695 2696
    }

2697
    TRACE("Locked rect %s.\n", wine_dbgstr_rect(&surface->lockedRect));
2698
    TRACE("Returning memory %p, pitch %u.\n", map_desc->data, map_desc->row_pitch);
2699

2700 2701
    return WINED3D_OK;
}
2702

2703
HRESULT CDECL wined3d_surface_getdc(struct wined3d_surface *surface, HDC *dc)
2704 2705
{
    HRESULT hr;
2706

2707
    TRACE("surface %p, dc %p.\n", surface, dc);
2708

2709 2710 2711 2712 2713
    /* Give more detailed info for ddraw. */
    if (surface->flags & SFLAG_DCINUSE)
        return WINEDDERR_DCALREADYCREATED;

    /* Can't GetDC if the surface is locked. */
2714
    if (surface->resource.map_count)
2715 2716
        return WINED3DERR_INVALIDCALL;

2717 2718 2719 2720 2721
    /* Create a DIB section if there isn't a dc yet. */
    if (!surface->hDC)
    {
        if (surface->flags & SFLAG_CLIENT)
        {
2722
            surface_load_location(surface, WINED3D_LOCATION_SYSMEM);
2723 2724 2725 2726 2727
            surface_release_client_storage(surface);
        }
        hr = surface_create_dib_section(surface);
        if (FAILED(hr))
            return WINED3DERR_INVALIDCALL;
2728
        if (!(surface->resource.map_binding == WINED3D_LOCATION_USER_MEMORY
2729
                || surface->container->flags & WINED3D_TEXTURE_PIN_SYSMEM
2730
                || surface->pbo))
2731
            surface->resource.map_binding = WINED3D_LOCATION_DIB;
2732 2733
    }

2734 2735
    surface_load_location(surface, WINED3D_LOCATION_DIB);
    surface_invalidate_location(surface, ~WINED3D_LOCATION_DIB);
2736 2737

    surface->flags |= SFLAG_DCINUSE;
2738
    surface->resource.map_count++;
2739 2740 2741 2742 2743 2744 2745

    *dc = surface->hDC;
    TRACE("Returning dc %p.\n", *dc);

    return WINED3D_OK;
}

2746
HRESULT CDECL wined3d_surface_releasedc(struct wined3d_surface *surface, HDC dc)
2747
{
2748
    TRACE("surface %p, dc %p.\n", surface, dc);
2749 2750 2751 2752 2753

    if (!(surface->flags & SFLAG_DCINUSE))
        return WINEDDERR_NODC;

    if (surface->hDC != dc)
2754
    {
2755 2756 2757
        WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
                dc, surface->hDC);
        return WINEDDERR_NODC;
2758 2759
    }

2760
    surface->resource.map_count--;
2761 2762
    surface->flags &= ~SFLAG_DCINUSE;

2763 2764
    if (surface->resource.map_binding == WINED3D_LOCATION_USER_MEMORY
            || (surface->container->flags & WINED3D_TEXTURE_PIN_SYSMEM
2765
            && surface->resource.map_binding != WINED3D_LOCATION_DIB))
2766 2767 2768 2769 2770 2771 2772 2773
    {
        /* The game Salammbo modifies the surface contents without mapping the surface between
         * a GetDC/ReleaseDC operation and flipping the surface. If the DIB remains the active
         * copy and is copied to the screen, this update, which draws the mouse pointer, is lost.
         * Do not only copy the DIB to the map location, but also make sure the map location is
         * copied back to the DIB in the next getdc call.
         *
         * The same consideration applies to user memory surfaces. */
2774
        surface_load_location(surface, surface->resource.map_binding);
2775 2776
        surface_invalidate_location(surface, WINED3D_LOCATION_DIB);
    }
2777

2778 2779 2780
    return WINED3D_OK;
}

2781
static void read_from_framebuffer(struct wined3d_surface *surface, DWORD dst_location)
2782
{
2783
    struct wined3d_device *device = surface->resource.device;
2784
    const struct wined3d_gl_info *gl_info;
2785
    struct wined3d_context *context;
2786 2787 2788
    BYTE *mem;
    BYTE *row, *top, *bottom;
    int i;
2789
    BOOL srcIsUpsideDown;
2790 2791
    struct wined3d_bo_address data;

2792
    surface_get_memory(surface, &data, dst_location);
2793

2794
    context = context_acquire(device, surface);
2795
    context_apply_blit_state(context, device);
2796 2797
    gl_info = context->gl_info;

2798 2799 2800 2801
    /* Select the correct read buffer, and give some debug output.
     * There is no need to keep track of the current read buffer or reset it, every part of the code
     * that reads sets the read buffer as desired.
     */
2802
    if (wined3d_resource_is_offscreen(&surface->container->resource))
2803
    {
2804 2805 2806
        /* Mapping the primary render target which is not on a swapchain.
         * Read from the back buffer. */
        TRACE("Mapping offscreen render target.\n");
2807
        gl_info->gl_ops.gl.p_glReadBuffer(device->offscreenBuffer);
2808
        srcIsUpsideDown = TRUE;
2809
    }
2810 2811 2812
    else
    {
        /* Onscreen surfaces are always part of a swapchain */
2813
        GLenum buffer = surface_get_gl_buffer(surface);
2814
        TRACE("Mapping %#x buffer.\n", buffer);
2815
        gl_info->gl_ops.gl.p_glReadBuffer(buffer);
2816 2817 2818
        checkGLcall("glReadBuffer");
        srcIsUpsideDown = FALSE;
    }
2819

2820
    if (data.buffer_object)
2821
    {
2822 2823
        GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
        checkGLcall("glBindBuffer");
2824 2825
    }

2826
    /* Setup pixel store pack state -- to glReadPixels into the correct place */
2827 2828
    gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH,
            wined3d_surface_get_pitch(surface) / surface->resource.format->byte_count);
2829 2830
    checkGLcall("glPixelStorei");

2831 2832
    gl_info->gl_ops.gl.p_glReadPixels(0, 0,
            surface->resource.width, surface->resource.height,
2833 2834
            surface->resource.format->glFormat,
            surface->resource.format->glType, data.addr);
2835
    checkGLcall("glReadPixels");
2836

2837
    /* Reset previous pixel store pack state */
2838
    gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, 0);
2839 2840
    checkGLcall("glPixelStorei");

2841 2842
    if (!srcIsUpsideDown)
    {
2843
        /* glReadPixels returns the image upside down, and there is no way to prevent this.
2844 2845 2846 2847 2848
         * Flip the lines in software. */
        UINT pitch = wined3d_surface_get_pitch(surface);

        if (!(row = HeapAlloc(GetProcessHeap(), 0, pitch)))
            goto error;
2849

2850
        if (data.buffer_object)
2851
        {
2852 2853
            mem = GL_EXTCALL(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_WRITE));
            checkGLcall("glMapBuffer");
2854
        }
2855 2856
        else
            mem = data.addr;
2857

2858 2859 2860 2861
        top = mem;
        bottom = mem + pitch * (surface->resource.height - 1);
        for (i = 0; i < surface->resource.height / 2; i++)
        {
2862 2863 2864
            memcpy(row, top, pitch);
            memcpy(top, bottom, pitch);
            memcpy(bottom, row, pitch);
2865 2866 2867 2868
            top += pitch;
            bottom -= pitch;
        }
        HeapFree(GetProcessHeap(), 0, row);
2869 2870

        if (data.buffer_object)
2871
            GL_EXTCALL(glUnmapBuffer(GL_PIXEL_PACK_BUFFER));
2872
    }
2873

2874
error:
2875 2876
    if (data.buffer_object)
    {
2877 2878
        GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
        checkGLcall("glBindBuffer");
2879 2880
    }

2881
    context_release(context);
2882 2883
}

2884 2885 2886 2887
/* Read the framebuffer contents into a texture. Note that this function
 * doesn't do any kind of flipping. Using this on an onscreen surface will
 * result in a flipped D3D texture. */
void surface_load_fb_texture(struct wined3d_surface *surface, BOOL srgb)
2888
{
2889
    struct wined3d_device *device = surface->resource.device;
2890
    const struct wined3d_gl_info *gl_info;
2891
    struct wined3d_context *context;
2892

2893
    context = context_acquire(device, surface);
2894
    gl_info = context->gl_info;
2895
    device_invalidate_state(device, STATE_FRAMEBUFFER);
2896

2897
    wined3d_texture_prepare_texture(surface->container, context, srgb);
2898
    wined3d_texture_bind_and_dirtify(surface->container, context, srgb);
2899

2900
    TRACE("Reading back offscreen render target %p.\n", surface);
2901

2902
    if (wined3d_resource_is_offscreen(&surface->container->resource))
2903
        gl_info->gl_ops.gl.p_glReadBuffer(device->offscreenBuffer);
2904
    else
2905
        gl_info->gl_ops.gl.p_glReadBuffer(surface_get_gl_buffer(surface));
2906
    checkGLcall("glReadBuffer");
2907

2908
    gl_info->gl_ops.gl.p_glCopyTexSubImage2D(surface->texture_target, surface->texture_level,
2909
            0, 0, 0, 0, surface->resource.width, surface->resource.height);
2910 2911
    checkGLcall("glCopyTexSubImage2D");

2912
    context_release(context);
2913 2914
}

2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925
void surface_prepare_rb(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info, BOOL multisample)
{
    if (multisample)
    {
        if (surface->rb_multisample)
            return;

        gl_info->fbo_ops.glGenRenderbuffers(1, &surface->rb_multisample);
        gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, surface->rb_multisample);
        gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, surface->resource.multisample_type,
                surface->resource.format->glInternal, surface->pow2Width, surface->pow2Height);
2926
        checkGLcall("glRenderbufferStorageMultisample()");
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
        TRACE("Created multisample rb %u.\n", surface->rb_multisample);
    }
    else
    {
        if (surface->rb_resolved)
            return;

        gl_info->fbo_ops.glGenRenderbuffers(1, &surface->rb_resolved);
        gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, surface->rb_resolved);
        gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, surface->resource.format->glInternal,
                surface->pow2Width, surface->pow2Height);
2938
        checkGLcall("glRenderbufferStorage()");
2939 2940 2941 2942
        TRACE("Created resolved rb %u.\n", surface->rb_resolved);
    }
}

2943 2944
void flip_surface(struct wined3d_surface *front, struct wined3d_surface *back)
{
2945 2946 2947 2948
    if (front->container->level_count != 1 || front->container->layer_count != 1
            || back->container->level_count != 1 || back->container->layer_count != 1)
        ERR("Flip between surfaces %p and %p not supported.\n", front, back);

2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
    /* Flip the surface contents */
    /* Flip the DC */
    {
        HDC tmp;
        tmp = front->hDC;
        front->hDC = back->hDC;
        back->hDC = tmp;
    }

    /* Flip the DIBsection */
    {
2960
        HBITMAP tmp = front->dib.DIBsection;
2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
        front->dib.DIBsection = back->dib.DIBsection;
        back->dib.DIBsection = tmp;
    }

    /* Flip the surface data */
    {
        void* tmp;

        tmp = front->dib.bitmap_data;
        front->dib.bitmap_data = back->dib.bitmap_data;
        back->dib.bitmap_data = tmp;

2973 2974 2975
        tmp = front->resource.heap_memory;
        front->resource.heap_memory = back->resource.heap_memory;
        back->resource.heap_memory = tmp;
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
    }

    /* Flip the PBO */
    {
        GLuint tmp_pbo = front->pbo;
        front->pbo = back->pbo;
        back->pbo = tmp_pbo;
    }

    /* Flip the opengl texture */
    {
2987 2988
        GLuint tmp;

2989 2990 2991 2992 2993 2994 2995 2996
        tmp = back->container->texture_rgb.name;
        back->container->texture_rgb.name = front->container->texture_rgb.name;
        front->container->texture_rgb.name = tmp;

        tmp = back->container->texture_srgb.name;
        back->container->texture_srgb.name = front->container->texture_srgb.name;
        front->container->texture_srgb.name = tmp;

2997 2998 2999 3000 3001 3002 3003 3004
        tmp = back->rb_multisample;
        back->rb_multisample = front->rb_multisample;
        front->rb_multisample = tmp;

        tmp = back->rb_resolved;
        back->rb_resolved = front->rb_resolved;
        front->rb_resolved = tmp;

3005 3006
        resource_unload(&back->resource);
        resource_unload(&front->resource);
3007 3008 3009
    }

    {
3010 3011 3012
        DWORD tmp_flags = back->flags;
        back->flags = front->flags;
        front->flags = tmp_flags;
3013 3014 3015 3016

        tmp_flags = back->locations;
        back->locations = front->locations;
        front->locations = tmp_flags;
3017 3018 3019
    }
}

3020 3021 3022
/* Does a direct frame buffer -> texture copy. Stretching is done with single
 * pixel copy calls. */
static void fb_copy_to_texture_direct(struct wined3d_surface *dst_surface, struct wined3d_surface *src_surface,
3023
        const RECT *src_rect, const RECT *dst_rect_in, enum wined3d_texture_filter_type filter)
3024
{
3025
    struct wined3d_device *device = dst_surface->resource.device;
3026
    const struct wined3d_gl_info *gl_info;
3027
    float xrel, yrel;
3028
    struct wined3d_context *context;
3029 3030
    BOOL upsidedown = FALSE;
    RECT dst_rect = *dst_rect_in;
3031

3032 3033 3034 3035 3036 3037 3038 3039 3040
    /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
     * glCopyTexSubImage is a bit picky about the parameters we pass to it
     */
    if(dst_rect.top > dst_rect.bottom) {
        UINT tmp = dst_rect.bottom;
        dst_rect.bottom = dst_rect.top;
        dst_rect.top = tmp;
        upsidedown = TRUE;
    }
3041

3042
    context = context_acquire(device, src_surface);
3043
    gl_info = context->gl_info;
3044
    context_apply_blit_state(context, device);
3045
    wined3d_texture_load(dst_surface->container, context, FALSE);
3046 3047

    /* Bind the target texture */
3048
    context_bind_texture(context, dst_surface->container->target, dst_surface->container->texture_rgb.name);
3049
    if (wined3d_resource_is_offscreen(&src_surface->container->resource))
3050
    {
3051 3052
        TRACE("Reading from an offscreen target\n");
        upsidedown = !upsidedown;
3053
        gl_info->gl_ops.gl.p_glReadBuffer(device->offscreenBuffer);
3054 3055 3056
    }
    else
    {
3057
        gl_info->gl_ops.gl.p_glReadBuffer(surface_get_gl_buffer(src_surface));
3058 3059 3060
    }
    checkGLcall("glReadBuffer");

3061 3062
    xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
    yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
3063

3064 3065
    if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
    {
3066
        FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
3067

3068 3069
        if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT)
            ERR("Texture filtering not supported in direct blit.\n");
3070
    }
3071
    else if ((filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT)
3072 3073
            && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
    {
3074
        ERR("Texture filtering not supported in direct blit\n");
3075 3076
    }

3077 3078 3079 3080
    if (upsidedown
            && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
            && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
    {
3081 3082
        /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do. */
        gl_info->gl_ops.gl.p_glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
3083
                dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
3084
                src_rect->left, src_surface->resource.height - src_rect->bottom,
3085
                dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3086 3087 3088
    }
    else
    {
3089
        LONG row;
3090
        UINT yoffset = src_surface->resource.height - src_rect->top + dst_rect.top - 1;
3091
        /* I have to process this row by row to swap the image,
3092
         * otherwise it would be upside down, so stretching in y direction
3093 3094
         * doesn't cost extra time
         *
3095
         * However, stretching in x direction can be avoided if not necessary
3096
         */
3097
        for(row = dst_rect.top; row < dst_rect.bottom; row++) {
3098 3099
            if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
            {
3100 3101 3102
                /* Well, that stuff works, but it's very slow.
                 * find a better way instead
                 */
3103
                LONG col;
3104

3105 3106
                for (col = dst_rect.left; col < dst_rect.right; ++col)
                {
3107
                    gl_info->gl_ops.gl.p_glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
3108
                            dst_rect.left + col /* x offset */, row /* y offset */,
3109
                            src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
3110
                }
3111 3112 3113
            }
            else
            {
3114
                gl_info->gl_ops.gl.p_glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
3115 3116
                        dst_rect.left /* x offset */, row /* y offset */,
                        src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
3117 3118 3119
            }
        }
    }
3120
    checkGLcall("glCopyTexSubImage2D");
3121

3122
    context_release(context);
3123

3124 3125
    /* The texture is now most up to date - If the surface is a render target
     * and has a drawable, this path is never entered. */
3126 3127
    surface_validate_location(dst_surface, WINED3D_LOCATION_TEXTURE_RGB);
    surface_invalidate_location(dst_surface, ~WINED3D_LOCATION_TEXTURE_RGB);
3128 3129 3130
}

/* Uses the hardware to stretch and flip the image */
3131
static void fb_copy_to_texture_hwstretch(struct wined3d_surface *dst_surface, struct wined3d_surface *src_surface,
3132
        const RECT *src_rect, const RECT *dst_rect_in, enum wined3d_texture_filter_type filter)
3133
{
3134
    struct wined3d_device *device = dst_surface->resource.device;
3135
    GLuint src, backup = 0;
3136
    float left, right, top, bottom; /* Texture coordinates */
3137 3138
    UINT fbwidth = src_surface->resource.width;
    UINT fbheight = src_surface->resource.height;
3139
    const struct wined3d_gl_info *gl_info;
3140
    struct wined3d_context *context;
3141
    GLenum drawBuffer = GL_BACK;
3142
    GLenum texture_target;
3143
    BOOL noBackBufferBackup;
3144
    BOOL src_offscreen;
3145 3146
    BOOL upsidedown = FALSE;
    RECT dst_rect = *dst_rect_in;
3147 3148 3149

    TRACE("Using hwstretch blit\n");
    /* Activate the Proper context for reading from the source surface, set it up for blitting */
3150
    context = context_acquire(device, src_surface);
3151
    gl_info = context->gl_info;
3152
    context_apply_blit_state(context, device);
3153
    wined3d_texture_load(dst_surface->container, context, FALSE);
3154

3155
    src_offscreen = wined3d_resource_is_offscreen(&src_surface->container->resource);
3156
    noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
3157
    if (!noBackBufferBackup && !src_surface->container->texture_rgb.name)
3158
    {
3159
        /* Get it a description */
3160
        wined3d_texture_load(src_surface->container, context, FALSE);
3161 3162
    }

3163 3164 3165
    /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
     * This way we don't have to wait for the 2nd readback to finish to leave this function.
     */
3166 3167
    if (context->aux_buffers >= 2)
    {
3168 3169
        /* Got more than one aux buffer? Use the 2nd aux buffer */
        drawBuffer = GL_AUX1;
3170
    }
3171
    else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
3172
    {
3173 3174 3175 3176
        /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
        drawBuffer = GL_AUX0;
    }

3177 3178 3179
    if (noBackBufferBackup)
    {
        gl_info->gl_ops.gl.p_glGenTextures(1, &backup);
3180
        checkGLcall("glGenTextures");
3181
        context_bind_texture(context, GL_TEXTURE_2D, backup);
3182
        texture_target = GL_TEXTURE_2D;
3183 3184 3185
    }
    else
    {
3186 3187 3188
        /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
         * we are reading from the back buffer, the backup can be used as source texture
         */
3189
        texture_target = src_surface->texture_target;
3190
        context_bind_texture(context, texture_target, src_surface->container->texture_rgb.name);
3191
        gl_info->gl_ops.gl.p_glEnable(texture_target);
3192
        checkGLcall("glEnable(texture_target)");
3193 3194

        /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3195
        src_surface->locations &= ~WINED3D_LOCATION_TEXTURE_RGB;
3196
    }
3197

3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
    /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
     * glCopyTexSubImage is a bit picky about the parameters we pass to it
     */
    if(dst_rect.top > dst_rect.bottom) {
        UINT tmp = dst_rect.bottom;
        dst_rect.bottom = dst_rect.top;
        dst_rect.top = tmp;
        upsidedown = TRUE;
    }

3208 3209
    if (src_offscreen)
    {
3210 3211
        TRACE("Reading from an offscreen target\n");
        upsidedown = !upsidedown;
3212
        gl_info->gl_ops.gl.p_glReadBuffer(device->offscreenBuffer);
3213 3214 3215
    }
    else
    {
3216
        gl_info->gl_ops.gl.p_glReadBuffer(surface_get_gl_buffer(src_surface));
3217
    }
3218 3219

    /* TODO: Only back up the part that will be overwritten */
3220
    gl_info->gl_ops.gl.p_glCopyTexSubImage2D(texture_target, 0, 0, 0, 0, 0, fbwidth, fbheight);
3221 3222 3223

    checkGLcall("glCopyTexSubImage2D");

3224
    /* No issue with overriding these - the sampler is dirty due to blit usage */
3225
    gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
3226
    checkGLcall("glTexParameteri");
3227
    gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3228
            wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
3229 3230
    checkGLcall("glTexParameteri");

3231 3232
    if (!src_surface->container->swapchain
            || src_surface->container == src_surface->container->swapchain->back_buffers[0])
3233
    {
3234
        src = backup ? backup : src_surface->container->texture_rgb.name;
3235 3236 3237
    }
    else
    {
3238
        gl_info->gl_ops.gl.p_glReadBuffer(GL_FRONT);
3239 3240
        checkGLcall("glReadBuffer(GL_FRONT)");

3241
        gl_info->gl_ops.gl.p_glGenTextures(1, &src);
3242
        checkGLcall("glGenTextures(1, &src)");
3243
        context_bind_texture(context, GL_TEXTURE_2D, src);
3244

3245
        /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3246 3247
         * out for power of 2 sizes
         */
3248
        gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3249
                src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3250
        checkGLcall("glTexImage2D");
3251
        gl_info->gl_ops.gl.p_glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, fbwidth, fbheight);
3252

3253
        gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3254
        checkGLcall("glTexParameteri");
3255
        gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3256 3257
        checkGLcall("glTexParameteri");

3258
        gl_info->gl_ops.gl.p_glReadBuffer(GL_BACK);
3259
        checkGLcall("glReadBuffer(GL_BACK)");
3260

3261 3262 3263 3264
        if (texture_target != GL_TEXTURE_2D)
        {
            gl_info->gl_ops.gl.p_glDisable(texture_target);
            gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
3265 3266
            texture_target = GL_TEXTURE_2D;
        }
3267 3268 3269
    }
    checkGLcall("glEnd and previous");

3270 3271
    left = src_rect->left;
    right = src_rect->right;
3272

3273
    if (!upsidedown)
3274
    {
3275 3276
        top = src_surface->resource.height - src_rect->top;
        bottom = src_surface->resource.height - src_rect->bottom;
3277 3278 3279
    }
    else
    {
3280 3281
        top = src_surface->resource.height - src_rect->bottom;
        bottom = src_surface->resource.height - src_rect->top;
3282 3283
    }

3284
    if (src_surface->container->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
3285 3286 3287 3288 3289
    {
        left /= src_surface->pow2Width;
        right /= src_surface->pow2Width;
        top /= src_surface->pow2Height;
        bottom /= src_surface->pow2Height;
3290 3291 3292
    }

    /* draw the source texture stretched and upside down. The correct surface is bound already */
3293 3294
    gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3295

3296
    context_set_draw_buffer(context, drawBuffer);
3297
    gl_info->gl_ops.gl.p_glReadBuffer(drawBuffer);
3298

3299
    gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
3300
        /* bottom left */
3301 3302
        gl_info->gl_ops.gl.p_glTexCoord2f(left, bottom);
        gl_info->gl_ops.gl.p_glVertex2i(0, 0);
3303 3304

        /* top left */
3305 3306
        gl_info->gl_ops.gl.p_glTexCoord2f(left, top);
        gl_info->gl_ops.gl.p_glVertex2i(0, dst_rect.bottom - dst_rect.top);
3307 3308

        /* top right */
3309 3310
        gl_info->gl_ops.gl.p_glTexCoord2f(right, top);
        gl_info->gl_ops.gl.p_glVertex2i(dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3311 3312

        /* bottom right */
3313 3314 3315
        gl_info->gl_ops.gl.p_glTexCoord2f(right, bottom);
        gl_info->gl_ops.gl.p_glVertex2i(dst_rect.right - dst_rect.left, 0);
    gl_info->gl_ops.gl.p_glEnd();
3316 3317
    checkGLcall("glEnd and previous");

3318
    if (texture_target != dst_surface->texture_target)
3319
    {
3320 3321
        gl_info->gl_ops.gl.p_glDisable(texture_target);
        gl_info->gl_ops.gl.p_glEnable(dst_surface->texture_target);
3322
        texture_target = dst_surface->texture_target;
3323 3324
    }

3325
    /* Now read the stretched and upside down image into the destination texture */
3326
    context_bind_texture(context, texture_target, dst_surface->container->texture_rgb.name);
3327
    gl_info->gl_ops.gl.p_glCopyTexSubImage2D(texture_target,
3328
                        0,
3329
                        dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3330
                        0, 0, /* We blitted the image to the origin */
3331
                        dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3332 3333
    checkGLcall("glCopyTexSubImage2D");

3334 3335 3336 3337 3338 3339 3340 3341 3342
    if (drawBuffer == GL_BACK)
    {
        /* Write the back buffer backup back. */
        if (backup)
        {
            if (texture_target != GL_TEXTURE_2D)
            {
                gl_info->gl_ops.gl.p_glDisable(texture_target);
                gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
3343 3344
                texture_target = GL_TEXTURE_2D;
            }
3345
            context_bind_texture(context, GL_TEXTURE_2D, backup);
3346 3347 3348 3349
        }
        else
        {
            if (texture_target != src_surface->texture_target)
3350
            {
3351 3352
                gl_info->gl_ops.gl.p_glDisable(texture_target);
                gl_info->gl_ops.gl.p_glEnable(src_surface->texture_target);
3353
                texture_target = src_surface->texture_target;
3354
            }
3355
            context_bind_texture(context, src_surface->texture_target, src_surface->container->texture_rgb.name);
3356 3357
        }

3358
        gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
3359
            /* top left */
3360 3361
            gl_info->gl_ops.gl.p_glTexCoord2f(0.0f, 0.0f);
            gl_info->gl_ops.gl.p_glVertex2i(0, fbheight);
3362

3363
            /* bottom left */
3364 3365
            gl_info->gl_ops.gl.p_glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
            gl_info->gl_ops.gl.p_glVertex2i(0, 0);
3366

3367
            /* bottom right */
3368
            gl_info->gl_ops.gl.p_glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3369
                    (float)fbheight / (float)src_surface->pow2Height);
3370
            gl_info->gl_ops.gl.p_glVertex2i(fbwidth, 0);
3371 3372

            /* top right */
3373 3374 3375
            gl_info->gl_ops.gl.p_glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
            gl_info->gl_ops.gl.p_glVertex2i(fbwidth, fbheight);
        gl_info->gl_ops.gl.p_glEnd();
3376
    }
3377
    gl_info->gl_ops.gl.p_glDisable(texture_target);
3378
    checkGLcall("glDisable(texture_target)");
3379 3380

    /* Cleanup */
3381
    if (src != src_surface->container->texture_rgb.name && src != backup)
3382
    {
3383
        gl_info->gl_ops.gl.p_glDeleteTextures(1, &src);
3384 3385
        checkGLcall("glDeleteTextures(1, &src)");
    }
3386 3387 3388
    if (backup)
    {
        gl_info->gl_ops.gl.p_glDeleteTextures(1, &backup);
3389 3390
        checkGLcall("glDeleteTextures(1, &backup)");
    }
3391

3392 3393
    if (wined3d_settings.strict_draw_ordering)
        gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
3394

3395
    context_release(context);
3396

3397 3398
    /* The texture is now most up to date - If the surface is a render target
     * and has a drawable, this path is never entered. */
3399 3400
    surface_validate_location(dst_surface, WINED3D_LOCATION_TEXTURE_RGB);
    surface_invalidate_location(dst_surface, ~WINED3D_LOCATION_TEXTURE_RGB);
3401 3402
}

3403 3404 3405 3406
/* Front buffer coordinates are always full screen coordinates, but our GL
 * drawable is limited to the window's client area. The sysmem and texture
 * copies do have the full screen size. Note that GL has a bottom-left
 * origin, while D3D has a top-left origin. */
3407
void surface_translate_drawable_coords(const struct wined3d_surface *surface, HWND window, RECT *rect)
3408
{
3409
    UINT drawable_height;
3410

3411
    if (surface->container->swapchain && surface->container == surface->container->swapchain->front_buffer)
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
    {
        POINT offset = {0, 0};
        RECT windowsize;

        ScreenToClient(window, &offset);
        OffsetRect(rect, offset.x, offset.y);

        GetClientRect(window, &windowsize);
        drawable_height = windowsize.bottom - windowsize.top;
    }
    else
    {
3424
        drawable_height = surface->resource.height;
3425 3426 3427 3428
    }

    rect->top = drawable_height - rect->top;
    rect->bottom = drawable_height - rect->bottom;
3429 3430
}

3431
static void surface_blt_to_drawable(const struct wined3d_device *device,
3432
        enum wined3d_texture_filter_type filter, BOOL alpha_test,
3433 3434
        struct wined3d_surface *src_surface, const RECT *src_rect_in,
        struct wined3d_surface *dst_surface, const RECT *dst_rect_in)
3435
{
3436
    const struct wined3d_gl_info *gl_info;
3437 3438 3439 3440 3441 3442
    struct wined3d_context *context;
    RECT src_rect, dst_rect;

    src_rect = *src_rect_in;
    dst_rect = *dst_rect_in;

3443 3444 3445
    context = context_acquire(device, dst_surface);
    gl_info = context->gl_info;

3446 3447 3448
    /* Make sure the surface is up-to-date. This should probably use
     * surface_load_location() and worry about the destination surface too,
     * unless we're overwriting it completely. */
3449
    wined3d_texture_load(src_surface->container, context, FALSE);
3450 3451 3452 3453

    /* Activate the destination context, set it up for blitting */
    context_apply_blit_state(context, device);

3454
    if (!wined3d_resource_is_offscreen(&dst_surface->container->resource))
3455
        surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3456

3457
    device->blitter->set_shader(device->blit_priv, context, src_surface);
3458

3459
    if (alpha_test)
3460
    {
3461
        gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
3462 3463
        checkGLcall("glEnable(GL_ALPHA_TEST)");

3464 3465 3466 3467
        /* For P8 surfaces, the alpha component contains the palette index.
         * Which means that the colorkey is one of the palette entries. In
         * other cases pixels that should be masked away have alpha set to 0. */
        if (src_surface->resource.format->id == WINED3DFMT_P8_UINT)
3468
            gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
3469
                    (float)src_surface->container->src_blt_color_key.color_space_low_value / 255.0f);
3470
        else
3471
            gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
3472 3473 3474 3475
        checkGLcall("glAlphaFunc");
    }
    else
    {
3476
        gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
3477 3478 3479
        checkGLcall("glDisable(GL_ALPHA_TEST)");
    }

3480
    draw_textured_quad(src_surface, context, &src_rect, &dst_rect, filter);
3481

3482
    if (alpha_test)
3483
    {
3484
        gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
3485 3486 3487 3488
        checkGLcall("glDisable(GL_ALPHA_TEST)");
    }

    /* Leave the opengl state valid for blitting */
3489
    device->blitter->unset_shader(context->gl_info);
3490

3491
    if (wined3d_settings.strict_draw_ordering
3492 3493
            || (dst_surface->container->swapchain
            && dst_surface->container->swapchain->front_buffer == dst_surface->container))
3494
        gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
3495 3496 3497 3498

    context_release(context);
}

3499
HRESULT surface_color_fill(struct wined3d_surface *s, const RECT *rect, const struct wined3d_color *color)
3500
{
3501
    struct wined3d_device *device = s->resource.device;
3502 3503
    const struct blit_shader *blitter;

3504
    blitter = wined3d_select_blitter(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_FILL,
3505
            NULL, 0, 0, NULL, rect, s->resource.usage, s->resource.pool, s->resource.format);
3506 3507 3508 3509 3510 3511 3512 3513 3514
    if (!blitter)
    {
        FIXME("No blitter is capable of performing the requested color fill operation.\n");
        return WINED3DERR_INVALIDCALL;
    }

    return blitter->color_fill(device, s, rect, color);
}

3515
static HRESULT surface_blt_special(struct wined3d_surface *dst_surface, const RECT *dst_rect,
3516
        struct wined3d_surface *src_surface, const RECT *src_rect, DWORD flags, const WINEDDBLTFX *DDBltFx,
3517
        enum wined3d_texture_filter_type filter)
3518
{
3519
    struct wined3d_device *device = dst_surface->resource.device;
3520
    const struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(device->fb.render_targets[0]);
3521
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3522
    struct wined3d_swapchain *src_swapchain, *dst_swapchain;
3523

3524
    TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3525
            dst_surface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect),
3526
            flags, DDBltFx, debug_d3dtexturefiltertype(filter));
3527 3528

    /* Get the swapchain. One of the surfaces has to be a primary surface */
3529
    if (dst_surface->resource.pool == WINED3D_POOL_SYSTEM_MEM)
3530
    {
3531 3532 3533
        WARN("Destination is in sysmem, rejecting gl blt\n");
        return WINED3DERR_INVALIDCALL;
    }
3534

3535
    dst_swapchain = dst_surface->container->swapchain;
3536

3537 3538
    if (src_surface)
    {
3539
        if (src_surface->resource.pool == WINED3D_POOL_SYSTEM_MEM)
3540
        {
3541 3542 3543
            WARN("Src is in sysmem, rejecting gl blt\n");
            return WINED3DERR_INVALIDCALL;
        }
3544

3545
        src_swapchain = src_surface->container->swapchain;
3546 3547 3548 3549
    }
    else
    {
        src_swapchain = NULL;
3550 3551 3552
    }

    /* Early sort out of cases where no render target is used */
3553
    if (!dst_swapchain && !src_swapchain && src_surface != rt && dst_surface != rt)
3554
    {
3555
        TRACE("No surface is render target, not using hardware blit.\n");
3556
        return WINED3DERR_INVALIDCALL;
3557 3558
    }

3559
    /* No destination color keying supported */
3560 3561
    if (flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE))
    {
3562 3563 3564 3565 3566
        /* Can we support that with glBlendFunc if blitting to the frame buffer? */
        TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
        return WINED3DERR_INVALIDCALL;
    }

3567
    if (dst_swapchain && dst_swapchain == src_swapchain)
3568
    {
3569 3570
        FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
        return WINED3DERR_INVALIDCALL;
3571 3572
    }

3573
    if (dst_swapchain && src_swapchain)
3574
    {
3575 3576
        FIXME("Implement hardware blit between two different swapchains\n");
        return WINED3DERR_INVALIDCALL;
3577
    }
3578

3579
    if (dst_swapchain)
3580
    {
3581
        /* Handled with regular texture -> swapchain blit */
3582
        if (src_surface == rt)
3583
            TRACE("Blit from active render target to a swapchain\n");
3584
    }
3585
    else if (src_swapchain && dst_surface == rt)
3586
    {
3587
        FIXME("Implement blit from a swapchain to the active render target\n");
3588 3589
        return WINED3DERR_INVALIDCALL;
    }
3590

3591
    if ((src_swapchain || src_surface == rt) && !dst_swapchain)
3592
    {
3593
        /* Blit from render target to texture */
3594
        BOOL stretchx;
3595 3596

        /* P8 read back is not implemented */
3597 3598
        if (src_surface->resource.format->id == WINED3DFMT_P8_UINT
                || dst_surface->resource.format->id == WINED3DFMT_P8_UINT)
3599 3600 3601 3602
        {
            TRACE("P8 read back not supported by frame buffer to texture blit\n");
            return WINED3DERR_INVALIDCALL;
        }
3603

3604 3605
        if (flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
        {
3606 3607 3608 3609 3610
            TRACE("Color keying not supported by frame buffer to texture blit\n");
            return WINED3DERR_INVALIDCALL;
            /* Destination color key is checked above */
        }

3611
        if (dst_rect->right - dst_rect->left != src_rect->right - src_rect->left)
3612
            stretchx = TRUE;
3613
        else
3614
            stretchx = FALSE;
3615

3616
        /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3617
         * flip the image nor scale it.
3618
         *
3619 3620 3621
         * -> If the app asks for an unscaled, upside down copy, just perform one glCopyTexSubImage2D call
         * -> If the app wants an image width an unscaled width, copy it line per line
         * -> If the app wants an image that is scaled on the x axis, and the destination rectangle is smaller
3622 3623 3624
         *    than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
         *    back buffer. This is slower than reading line per line, thus not used for flipping
         * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3625 3626
         *    pixel by pixel. */
        if (!stretchx || dst_rect->right - dst_rect->left > src_surface->resource.width
3627
                || dst_rect->bottom - dst_rect->top > src_surface->resource.height)
3628
        {
3629 3630 3631 3632 3633 3634 3635
            TRACE("No stretching in x direction, using direct framebuffer -> texture copy.\n");
            fb_copy_to_texture_direct(dst_surface, src_surface, src_rect, dst_rect, filter);
        }
        else
        {
            TRACE("Using hardware stretching to flip / stretch the texture.\n");
            fb_copy_to_texture_hwstretch(dst_surface, src_surface, src_rect, dst_rect, filter);
3636
        }
3637

3638
        surface_evict_sysmem(dst_surface);
3639

3640 3641 3642 3643 3644
        return WINED3D_OK;
    }
    else if (src_surface)
    {
        /* Blit from offscreen surface to render target */
3645 3646
        struct wined3d_color_key old_blt_key = src_surface->container->src_blt_color_key;
        DWORD old_color_key_flags = src_surface->container->color_key_flags;
3647 3648 3649 3650

        TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);

        if (!device->blitter->blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3651 3652
                src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
                dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
        {
            FIXME("Unsupported blit operation falling back to software\n");
            return WINED3DERR_INVALIDCALL;
        }

        /* Color keying: Check if we have to do a color keyed blt,
         * and if not check if a color key is activated.
         *
         * Just modify the color keying parameters in the surface and restore them afterwards
         * The surface keeps track of the color key last used to load the opengl surface.
         * PreLoad will catch the change to the flags and color key and reload if necessary.
         */
        if (flags & WINEDDBLT_KEYSRC)
        {
            /* Use color key from surface */
        }
        else if (flags & WINEDDBLT_KEYSRCOVERRIDE)
        {
            /* Use color key from DDBltFx */
3672
            wined3d_texture_set_color_key(src_surface->container, WINED3D_CKEY_SRC_BLT, &DDBltFx->ddckSrcColorkey);
3673 3674 3675 3676
        }
        else
        {
            /* Do not use color key */
3677
            wined3d_texture_set_color_key(src_surface->container, WINED3D_CKEY_SRC_BLT, NULL);
3678 3679
        }

3680 3681
        surface_blt_to_drawable(device, filter,
                flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_ALPHATEST),
3682
                src_surface, src_rect, dst_surface, dst_rect);
3683 3684

        /* Restore the color key parameters */
3685 3686
        wined3d_texture_set_color_key(src_surface->container, WINED3D_CKEY_SRC_BLT,
                (old_color_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
3687

3688 3689
        surface_validate_location(dst_surface, dst_surface->container->resource.draw_binding);
        surface_invalidate_location(dst_surface, ~dst_surface->container->resource.draw_binding);
3690 3691 3692 3693 3694 3695 3696 3697 3698

        return WINED3D_OK;
    }

    /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
    TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
    return WINED3DERR_INVALIDCALL;
}

3699
/* Context activation is done by the caller. */
3700
static void surface_depth_blt(const struct wined3d_surface *surface, struct wined3d_context *context,
3701
        GLuint texture, GLint x, GLint y, GLsizei w, GLsizei h, GLenum target)
3702
{
3703
    struct wined3d_device *device = surface->resource.device;
3704
    const struct wined3d_gl_info *gl_info = context->gl_info;
3705 3706 3707 3708 3709
    GLint compare_mode = GL_NONE;
    struct blt_info info;
    GLint old_binding = 0;
    RECT rect;

3710
    gl_info->gl_ops.gl.p_glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
3711

3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
    gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
    gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
    gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
    gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
    gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
    gl_info->gl_ops.gl.p_glEnable(GL_DEPTH_TEST);
    gl_info->gl_ops.gl.p_glDepthFunc(GL_ALWAYS);
    gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
    gl_info->gl_ops.gl.p_glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    gl_info->gl_ops.gl.p_glViewport(x, y, w, h);
    gl_info->gl_ops.gl.p_glDepthRange(0.0, 1.0);
3723 3724

    SetRect(&rect, 0, h, w, 0);
3725
    surface_get_blt_info(target, &rect, surface->pow2Width, surface->pow2Height, &info);
3726
    context_active_texture(context, context->gl_info, 0);
3727 3728
    gl_info->gl_ops.gl.p_glGetIntegerv(info.binding, &old_binding);
    gl_info->gl_ops.gl.p_glBindTexture(info.bind_target, texture);
3729 3730
    if (gl_info->supported[ARB_SHADOW])
    {
3731 3732 3733
        gl_info->gl_ops.gl.p_glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
        if (compare_mode != GL_NONE)
            gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
3734 3735 3736
    }

    device->shader_backend->shader_select_depth_blt(device->shader_priv,
3737
            gl_info, info.tex_type, &surface->ds_current_size);
3738

3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
    gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[0]);
    gl_info->gl_ops.gl.p_glVertex2f(-1.0f, -1.0f);
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[1]);
    gl_info->gl_ops.gl.p_glVertex2f(1.0f, -1.0f);
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[2]);
    gl_info->gl_ops.gl.p_glVertex2f(-1.0f, 1.0f);
    gl_info->gl_ops.gl.p_glTexCoord3fv(info.coords[3]);
    gl_info->gl_ops.gl.p_glVertex2f(1.0f, 1.0f);
    gl_info->gl_ops.gl.p_glEnd();
3749

3750 3751 3752
    if (compare_mode != GL_NONE)
        gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
    gl_info->gl_ops.gl.p_glBindTexture(info.bind_target, old_binding);
3753

3754
    gl_info->gl_ops.gl.p_glPopAttrib();
3755 3756 3757 3758

    device->shader_backend->shader_deselect_depth_blt(device->shader_priv, gl_info);
}

3759
void surface_modify_ds_location(struct wined3d_surface *surface,
3760 3761 3762 3763
        DWORD location, UINT w, UINT h)
{
    TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);

3764 3765
    if (((surface->locations & WINED3D_LOCATION_TEXTURE_RGB) && !(location & WINED3D_LOCATION_TEXTURE_RGB))
            || (!(surface->locations & WINED3D_LOCATION_TEXTURE_RGB) && (location & WINED3D_LOCATION_TEXTURE_RGB)))
3766
        wined3d_texture_set_dirty(surface->container);
3767

3768 3769
    surface->ds_current_size.cx = w;
    surface->ds_current_size.cy = h;
3770
    surface->locations = location;
3771 3772 3773
}

/* Context activation is done by the caller. */
3774
void surface_load_ds_location(struct wined3d_surface *surface, struct wined3d_context *context, DWORD location)
3775
{
3776
    const struct wined3d_gl_info *gl_info = context->gl_info;
3777
    struct wined3d_device *device = surface->resource.device;
3778 3779 3780 3781 3782 3783 3784
    GLsizei w, h;

    TRACE("surface %p, new location %#x.\n", surface, location);

    /* TODO: Make this work for modes other than FBO */
    if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;

3785
    if (!(surface->locations & location))
3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810
    {
        w = surface->ds_current_size.cx;
        h = surface->ds_current_size.cy;
        surface->ds_current_size.cx = 0;
        surface->ds_current_size.cy = 0;
    }
    else
    {
        w = surface->resource.width;
        h = surface->resource.height;
    }

    if (surface->ds_current_size.cx == surface->resource.width
            && surface->ds_current_size.cy == surface->resource.height)
    {
        TRACE("Location (%#x) is already up to date.\n", location);
        return;
    }

    if (surface->current_renderbuffer)
    {
        FIXME("Not supported with fixed up depth stencil.\n");
        return;
    }

3811
    if (surface->locations & WINED3D_LOCATION_DISCARDED)
3812 3813 3814 3815
    {
        TRACE("Surface was discarded, no need copy data.\n");
        switch (location)
        {
3816
            case WINED3D_LOCATION_TEXTURE_RGB:
3817
                wined3d_texture_prepare_texture(surface->container, context, FALSE);
3818
                break;
3819
            case WINED3D_LOCATION_RB_MULTISAMPLE:
3820
                surface_prepare_rb(surface, gl_info, TRUE);
3821
                break;
3822
            case WINED3D_LOCATION_DRAWABLE:
3823 3824 3825
                /* Nothing to do */
                break;
            default:
3826
                FIXME("Unhandled location %#x\n", location);
3827
        }
3828
        surface->locations &= ~WINED3D_LOCATION_DISCARDED;
3829
        surface->locations |= location;
3830 3831 3832 3833 3834
        surface->ds_current_size.cx = surface->resource.width;
        surface->ds_current_size.cy = surface->resource.height;
        return;
    }

3835
    if (!surface->locations)
3836 3837
    {
        FIXME("No up to date depth stencil location.\n");
3838
        surface->locations |= location;
3839 3840 3841
        surface->ds_current_size.cx = surface->resource.width;
        surface->ds_current_size.cy = surface->resource.height;
        return;
3842
    }
3843

3844
    if (location == WINED3D_LOCATION_TEXTURE_RGB)
3845
    {
3846 3847
        GLint old_binding = 0;
        GLenum bind_target;
3848

3849 3850 3851 3852
        /* The render target is allowed to be smaller than the depth/stencil
         * buffer, so the onscreen depth/stencil buffer is potentially smaller
         * than the offscreen surface. Don't overwrite the offscreen surface
         * with undefined data. */
3853 3854
        w = min(w, context->swapchain->desc.backbuffer_width);
        h = min(h, context->swapchain->desc.backbuffer_height);
3855

3856
        TRACE("Copying onscreen depth buffer to depth texture.\n");
3857

3858
        if (!device->depth_blt_texture)
3859
            gl_info->gl_ops.gl.p_glGenTextures(1, &device->depth_blt_texture);
3860

3861 3862
        /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
         * directly on the FBO texture. That's because we need to flip. */
3863
        context_apply_fbo_state_blit(context, GL_FRAMEBUFFER,
3864 3865
                surface_from_resource(wined3d_texture_get_sub_resource(context->swapchain->front_buffer, 0)),
                NULL, WINED3D_LOCATION_DRAWABLE);
3866
        if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
3867
        {
3868
            gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
3869
            bind_target = GL_TEXTURE_RECTANGLE_ARB;
3870
        }
3871
        else
3872
        {
3873
            gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
3874
            bind_target = GL_TEXTURE_2D;
3875
        }
3876
        gl_info->gl_ops.gl.p_glBindTexture(bind_target, device->depth_blt_texture);
3877 3878 3879 3880 3881 3882 3883
        /* We use GL_DEPTH_COMPONENT instead of the surface's specific
         * internal format, because the internal format might include stencil
         * data. In principle we should copy stencil data as well, but unless
         * the driver supports stencil export it's hard to do, and doesn't
         * seem to be needed in practice. If the hardware doesn't support
         * writing stencil data, the glCopyTexImage2D() call might trigger
         * software fallbacks. */
3884 3885 3886 3887 3888 3889 3890 3891
        gl_info->gl_ops.gl.p_glCopyTexImage2D(bind_target, 0, GL_DEPTH_COMPONENT, 0, 0, w, h, 0);
        gl_info->gl_ops.gl.p_glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        gl_info->gl_ops.gl.p_glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        gl_info->gl_ops.gl.p_glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        gl_info->gl_ops.gl.p_glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        gl_info->gl_ops.gl.p_glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
        gl_info->gl_ops.gl.p_glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
        gl_info->gl_ops.gl.p_glBindTexture(bind_target, old_binding);
3892

3893
        context_apply_fbo_state_blit(context, GL_FRAMEBUFFER,
3894
                NULL, surface, WINED3D_LOCATION_TEXTURE_RGB);
3895
        context_set_draw_buffer(context, GL_NONE);
3896

3897
        /* Do the actual blit */
3898
        surface_depth_blt(surface, context, device->depth_blt_texture, 0, 0, w, h, bind_target);
3899
        checkGLcall("depth_blt");
3900

3901
        context_invalidate_state(context, STATE_FRAMEBUFFER);
3902

3903 3904
        if (wined3d_settings.strict_draw_ordering)
            gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
3905
    }
3906
    else if (location == WINED3D_LOCATION_DRAWABLE)
3907
    {
3908
        TRACE("Copying depth texture to onscreen depth buffer.\n");
3909

3910
        context_apply_fbo_state_blit(context, GL_FRAMEBUFFER,
3911 3912
                surface_from_resource(wined3d_texture_get_sub_resource(context->swapchain->front_buffer, 0)),
                NULL, WINED3D_LOCATION_DRAWABLE);
3913
        surface_depth_blt(surface, context, surface->container->texture_rgb.name,
3914
                0, surface->pow2Height - h, w, h, surface->texture_target);
3915
        checkGLcall("depth_blt");
3916

3917
        context_invalidate_state(context, STATE_FRAMEBUFFER);
3918

3919 3920
        if (wined3d_settings.strict_draw_ordering)
            gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
3921 3922 3923 3924
    }
    else
    {
        ERR("Invalid location (%#x) specified.\n", location);
3925 3926
    }

3927
    surface->locations |= location;
3928 3929
    surface->ds_current_size.cx = surface->resource.width;
    surface->ds_current_size.cy = surface->resource.height;
3930 3931
}

3932
void surface_validate_location(struct wined3d_surface *surface, DWORD location)
3933
{
3934
    TRACE("surface %p, location %s.\n", surface, wined3d_debug_location(location));
3935

3936
    surface->locations |= location;
3937 3938
}

3939
void surface_invalidate_location(struct wined3d_surface *surface, DWORD location)
3940
{
3941
    TRACE("surface %p, location %s.\n", surface, wined3d_debug_location(location));
3942

3943
    if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3944
        wined3d_texture_set_dirty(surface->container);
3945
    surface->locations &= ~location;
3946

3947
    if (!surface->locations)
3948 3949 3950
        ERR("Surface %p does not have any up to date location.\n", surface);
}

3951 3952 3953 3954
static DWORD resource_access_from_location(DWORD location)
{
    switch (location)
    {
3955 3956 3957 3958
        case WINED3D_LOCATION_SYSMEM:
        case WINED3D_LOCATION_USER_MEMORY:
        case WINED3D_LOCATION_DIB:
        case WINED3D_LOCATION_BUFFER:
3959 3960
            return WINED3D_RESOURCE_ACCESS_CPU;

3961 3962 3963 3964 3965
        case WINED3D_LOCATION_DRAWABLE:
        case WINED3D_LOCATION_TEXTURE_SRGB:
        case WINED3D_LOCATION_TEXTURE_RGB:
        case WINED3D_LOCATION_RB_MULTISAMPLE:
        case WINED3D_LOCATION_RB_RESOLVED:
3966 3967 3968 3969 3970 3971 3972 3973
            return WINED3D_RESOURCE_ACCESS_GPU;

        default:
            FIXME("Unhandled location %#x.\n", location);
            return 0;
    }
}

3974 3975 3976 3977 3978 3979 3980 3981 3982
static void surface_copy_simple_location(struct wined3d_surface *surface, DWORD location)
{
    struct wined3d_device *device = surface->resource.device;
    struct wined3d_context *context;
    const struct wined3d_gl_info *gl_info;
    struct wined3d_bo_address dst, src;
    UINT size = surface->resource.size;

    surface_get_memory(surface, &dst, location);
3983
    surface_get_memory(surface, &src, surface->locations);
3984 3985 3986 3987 3988

    if (dst.buffer_object)
    {
        context = context_acquire(device, NULL);
        gl_info = context->gl_info;
3989 3990 3991
        GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
        GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
        GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
3992 3993 3994 3995 3996 3997 3998 3999
        checkGLcall("Upload PBO");
        context_release(context);
        return;
    }
    if (src.buffer_object)
    {
        context = context_acquire(device, NULL);
        gl_info = context->gl_info;
4000 4001 4002
        GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
        GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
        GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
4003 4004 4005 4006 4007 4008 4009
        checkGLcall("Download PBO");
        context_release(context);
        return;
    }
    memcpy(dst.addr, src.addr, size);
}

4010
static void surface_load_sysmem(struct wined3d_surface *surface,
4011
        const struct wined3d_gl_info *gl_info, DWORD dst_location)
4012
{
4013
    if (surface->locations & surface_simple_locations)
4014 4015 4016 4017 4018
    {
        surface_copy_simple_location(surface, dst_location);
        return;
    }

4019 4020
    if (surface->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
        surface_load_location(surface, WINED3D_LOCATION_TEXTURE_RGB);
4021

4022
    /* Download the surface to system memory. */
4023
    if (surface->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
4024 4025
    {
        struct wined3d_device *device = surface->resource.device;
4026
        struct wined3d_context *context;
4027

4028 4029
        /* TODO: Use already acquired context when possible. */
        context = context_acquire(device, NULL);
4030

4031 4032
        wined3d_texture_bind_and_dirtify(surface->container, context,
                !(surface->locations & WINED3D_LOCATION_TEXTURE_RGB));
4033
        surface_download_data(surface, gl_info, dst_location);
4034

4035
        context_release(context);
4036 4037 4038 4039

        return;
    }

4040
    if (surface->locations & WINED3D_LOCATION_DRAWABLE)
4041
    {
4042
        read_from_framebuffer(surface, dst_location);
4043 4044 4045
        return;
    }

4046
    FIXME("Can't load surface %p with location flags %s into sysmem.\n",
4047
            surface, wined3d_debug_location(surface->locations));
4048 4049
}

4050
static HRESULT surface_load_drawable(struct wined3d_surface *surface,
4051
        const struct wined3d_gl_info *gl_info)
4052
{
4053
    RECT r;
4054

4055
    if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
4056
            && wined3d_resource_is_offscreen(&surface->container->resource))
4057
    {
4058
        ERR("Trying to load offscreen surface into WINED3D_LOCATION_DRAWABLE.\n");
4059 4060 4061
        return WINED3DERR_INVALIDCALL;
    }

4062
    surface_get_rect(surface, NULL, &r);
4063
    surface_load_location(surface, WINED3D_LOCATION_TEXTURE_RGB);
4064 4065
    surface_blt_to_drawable(surface->resource.device,
            WINED3D_TEXF_POINT, FALSE, surface, &r, surface, &r);
4066 4067 4068 4069

    return WINED3D_OK;
}

4070
static HRESULT surface_load_texture(struct wined3d_surface *surface,
4071
        const struct wined3d_gl_info *gl_info, BOOL srgb)
4072
{
4073
    RECT src_rect = {0, 0, surface->resource.width, surface->resource.height};
4074
    struct wined3d_device *device = surface->resource.device;
4075
    const struct wined3d_color_key_conversion *conversion;
4076
    struct wined3d_texture *texture = surface->container;
4077
    struct wined3d_context *context;
4078 4079
    UINT width, src_pitch, dst_pitch;
    struct wined3d_bo_address data;
4080
    struct wined3d_format format;
4081
    POINT dst_point = {0, 0};
4082
    BYTE *mem = NULL;
4083 4084

    if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4085
            && wined3d_resource_is_offscreen(&texture->resource)
4086
            && (surface->locations & WINED3D_LOCATION_DRAWABLE))
4087
    {
4088
        surface_load_fb_texture(surface, srgb);
4089 4090 4091 4092

        return WINED3D_OK;
    }

4093
    if (surface->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
4094
            && (surface->resource.format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
4095 4096 4097 4098 4099
            && fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
                NULL, surface->resource.usage, surface->resource.pool, surface->resource.format,
                NULL, surface->resource.usage, surface->resource.pool, surface->resource.format))
    {
        if (srgb)
4100 4101
            surface_blt_fbo(device, WINED3D_TEXF_POINT, surface, WINED3D_LOCATION_TEXTURE_RGB,
                    &src_rect, surface, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
4102
        else
4103 4104
            surface_blt_fbo(device, WINED3D_TEXF_POINT, surface, WINED3D_LOCATION_TEXTURE_SRGB,
                    &src_rect, surface, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
4105 4106 4107 4108

        return WINED3D_OK;
    }

4109
    if (surface->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
4110
            && (!srgb || (surface->resource.format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB))
4111 4112 4113 4114
            && fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
                NULL, surface->resource.usage, surface->resource.pool, surface->resource.format,
                NULL, surface->resource.usage, surface->resource.pool, surface->resource.format))
    {
4115 4116 4117
        DWORD src_location = surface->locations & WINED3D_LOCATION_RB_RESOLVED ?
                WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
        DWORD dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
4118 4119
        RECT rect = {0, 0, surface->resource.width, surface->resource.height};

4120
        surface_blt_fbo(device, WINED3D_TEXF_POINT, surface, src_location,
4121 4122 4123 4124 4125
                &rect, surface, dst_location, &rect);

        return WINED3D_OK;
    }

4126 4127 4128 4129
    /* Upload from system memory */

    if (srgb)
    {
4130
        if ((surface->locations & (WINED3D_LOCATION_TEXTURE_RGB | surface->resource.map_binding))
4131
                == WINED3D_LOCATION_TEXTURE_RGB)
4132 4133 4134
        {
            /* Performance warning... */
            FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface);
4135
            surface_prepare_map_memory(surface);
4136
            surface_load_location(surface, surface->resource.map_binding);
4137 4138 4139 4140
        }
    }
    else
    {
4141
        if ((surface->locations & (WINED3D_LOCATION_TEXTURE_SRGB | surface->resource.map_binding))
4142
                == WINED3D_LOCATION_TEXTURE_SRGB)
4143 4144 4145
        {
            /* Performance warning... */
            FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface);
4146
            surface_prepare_map_memory(surface);
4147
            surface_load_location(surface, surface->resource.map_binding);
4148 4149 4150
        }
    }

4151
    if (!(surface->locations & surface_simple_locations))
4152
    {
4153
        WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
4154
        /* Lets hope we get it from somewhere... */
4155
        surface_prepare_system_memory(surface);
4156
        surface_load_location(surface, WINED3D_LOCATION_SYSMEM);
4157 4158
    }

4159 4160
    /* TODO: Use already acquired context when possible. */
    context = context_acquire(device, NULL);
4161

4162
    wined3d_texture_prepare_texture(texture, context, srgb);
4163
    wined3d_texture_bind_and_dirtify(texture, context, srgb);
4164 4165 4166 4167

    width = surface->resource.width;
    src_pitch = wined3d_surface_get_pitch(surface);

4168
    format = *texture->resource.format;
4169
    if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
4170
        format = *wined3d_get_format(gl_info, conversion->dst_format);
4171

4172
    /* Don't use PBOs for converted surfaces. During PBO conversion we look at
4173 4174
     * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
     * getting called. */
4175
    if ((format.convert || conversion) && surface->pbo)
4176 4177
    {
        TRACE("Removing the pbo attached to surface %p.\n", surface);
4178 4179

        if (surface->flags & SFLAG_DIBSECTION)
4180
            surface->resource.map_binding = WINED3D_LOCATION_DIB;
4181
        else
4182
            surface->resource.map_binding = WINED3D_LOCATION_SYSMEM;
4183 4184

        surface_prepare_map_memory(surface);
4185
        surface_load_location(surface, surface->resource.map_binding);
4186 4187 4188
        surface_remove_pbo(surface, gl_info);
    }

4189
    surface_get_memory(surface, &data, surface->locations);
4190 4191 4192 4193 4194
    if (format.convert)
    {
        /* This code is entered for texture formats which need a fixup. */
        UINT height = surface->resource.height;

4195 4196
        format.byte_count = format.conv_byte_count;
        dst_pitch = wined3d_format_calculate_pitch(&format, width);
4197 4198 4199 4200

        if (!(mem = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height)))
        {
            ERR("Out of memory (%u).\n", dst_pitch * height);
4201
            context_release(context);
4202 4203
            return E_OUTOFMEMORY;
        }
4204
        format.convert(data.addr, mem, src_pitch, src_pitch * height,
4205
                dst_pitch, dst_pitch * height, width, height, 1);
4206
        src_pitch = dst_pitch;
4207
        data.addr = mem;
4208
    }
4209
    else if (conversion)
4210 4211
    {
        /* This code is only entered for color keying fixups */
4212
        struct wined3d_palette *palette = NULL;
4213 4214
        UINT height = surface->resource.height;

4215
        dst_pitch = wined3d_format_calculate_pitch(&format, width);
4216 4217 4218 4219 4220
        dst_pitch = (dst_pitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);

        if (!(mem = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height)))
        {
            ERR("Out of memory (%u).\n", dst_pitch * height);
4221
            context_release(context);
4222 4223
            return E_OUTOFMEMORY;
        }
4224 4225 4226
        if (texture->swapchain && texture->swapchain->palette)
            palette = texture->swapchain->palette;
        conversion->convert(data.addr, src_pitch, mem, dst_pitch,
4227
                width, height, palette, &texture->gl_color_key);
4228
        src_pitch = dst_pitch;
4229
        data.addr = mem;
4230 4231
    }

4232 4233
    wined3d_surface_upload_data(surface, gl_info, &format, &src_rect,
            src_pitch, &dst_point, srgb, wined3d_const_bo_address(&data));
4234

4235
    context_release(context);
4236

4237
    HeapFree(GetProcessHeap(), 0, mem);
4238 4239 4240 4241

    return WINED3D_OK;
}

4242 4243 4244 4245
static void surface_multisample_resolve(struct wined3d_surface *surface)
{
    RECT rect = {0, 0, surface->resource.width, surface->resource.height};

4246 4247 4248
    if (!(surface->locations & WINED3D_LOCATION_RB_MULTISAMPLE))
        ERR("Trying to resolve multisampled surface %p, but location WINED3D_LOCATION_RB_MULTISAMPLE not current.\n",
                surface);
4249

4250
    surface_blt_fbo(surface->resource.device, WINED3D_TEXF_POINT,
4251
            surface, WINED3D_LOCATION_RB_MULTISAMPLE, &rect, surface, WINED3D_LOCATION_RB_RESOLVED, &rect);
4252 4253
}

4254
HRESULT surface_load_location(struct wined3d_surface *surface, DWORD location)
4255 4256 4257
{
    struct wined3d_device *device = surface->resource.device;
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4258
    HRESULT hr;
4259

4260
    TRACE("surface %p, location %s.\n", surface, wined3d_debug_location(location));
4261

4262 4263
    if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
    {
4264 4265
        if (location == WINED3D_LOCATION_TEXTURE_RGB
                && surface->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_DISCARDED))
4266 4267
        {
            struct wined3d_context *context = context_acquire(device, NULL);
4268
            surface_load_ds_location(surface, context, location);
4269 4270 4271
            context_release(context);
            return WINED3D_OK;
        }
4272 4273
        else if (location & surface->locations
                && surface->container->resource.draw_binding != WINED3D_LOCATION_DRAWABLE)
4274 4275 4276 4277
        {
            /* Already up to date, nothing to do. */
            return WINED3D_OK;
        }
4278 4279
        else
        {
4280
            FIXME("Unimplemented copy from %s to %s for depth/stencil buffers.\n",
4281
                    wined3d_debug_location(surface->locations), wined3d_debug_location(location));
4282 4283 4284
            return WINED3DERR_INVALIDCALL;
        }
    }
4285

4286
    if (surface->locations & location)
4287
    {
4288
        TRACE("Location already up to date.\n");
4289
        return WINED3D_OK;
4290 4291
    }

4292 4293
    if (WARN_ON(d3d_surface))
    {
4294
        DWORD required_access = resource_access_from_location(location);
4295 4296 4297 4298 4299
        if ((surface->resource.access_flags & required_access) != required_access)
            WARN("Operation requires %#x access, but surface only has %#x.\n",
                    required_access, surface->resource.access_flags);
    }

4300
    if (!surface->locations)
4301 4302 4303 4304 4305
    {
        ERR("Surface %p does not have any up to date location.\n", surface);
        surface->flags |= SFLAG_LOST;
        return WINED3DERR_DEVICELOST;
    }
4306

4307
    switch (location)
4308
    {
4309 4310 4311 4312
        case WINED3D_LOCATION_DIB:
        case WINED3D_LOCATION_USER_MEMORY:
        case WINED3D_LOCATION_SYSMEM:
        case WINED3D_LOCATION_BUFFER:
4313
            surface_load_sysmem(surface, gl_info, location);
4314 4315
            break;

4316
        case WINED3D_LOCATION_DRAWABLE:
4317
            if (FAILED(hr = surface_load_drawable(surface, gl_info)))
4318 4319
                return hr;
            break;
4320

4321
        case WINED3D_LOCATION_RB_RESOLVED:
4322 4323
            surface_multisample_resolve(surface);
            break;
4324

4325 4326 4327
        case WINED3D_LOCATION_TEXTURE_RGB:
        case WINED3D_LOCATION_TEXTURE_SRGB:
            if (FAILED(hr = surface_load_texture(surface, gl_info, location == WINED3D_LOCATION_TEXTURE_SRGB)))
4328 4329 4330 4331
                return hr;
            break;

        default:
4332
            ERR("Don't know how to handle location %#x.\n", location);
4333
            break;
4334
    }
4335

4336
    surface_validate_location(surface, location);
4337

4338
    if (location != WINED3D_LOCATION_SYSMEM && (surface->locations & WINED3D_LOCATION_SYSMEM))
4339
        surface_evict_sysmem(surface);
4340

4341 4342
    return WINED3D_OK;
}
4343

4344
static HRESULT ffp_blit_alloc(struct wined3d_device *device) { return WINED3D_OK; }
4345
/* Context activation is done by the caller. */
4346
static void ffp_blit_free(struct wined3d_device *device) { }
4347

4348
/* Context activation is done by the caller. */
4349
static HRESULT ffp_blit_set(void *blit_priv, struct wined3d_context *context, const struct wined3d_surface *surface)
4350
{
4351
    const struct wined3d_gl_info *gl_info = context->gl_info;
4352

4353
    gl_info->gl_ops.gl.p_glEnable(surface->container->target);
4354
    checkGLcall("glEnable(target)");
4355

4356
    return WINED3D_OK;
4357 4358
}

4359 4360
/* Context activation is done by the caller. */
static void ffp_blit_unset(const struct wined3d_gl_info *gl_info)
4361
{
4362
    gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
4363 4364
    checkGLcall("glDisable(GL_TEXTURE_2D)");
    if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4365
    {
4366
        gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4367
        checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4368
    }
4369
    if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4370
    {
4371
        gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
4372
        checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4373
    }
4374
}
4375

4376
static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
4377 4378
        const RECT *src_rect, DWORD src_usage, enum wined3d_pool src_pool, const struct wined3d_format *src_format,
        const RECT *dst_rect, DWORD dst_usage, enum wined3d_pool dst_pool, const struct wined3d_format *dst_format)
4379 4380
{
    switch (blit_op)
4381
    {
4382
        case WINED3D_BLIT_OP_COLOR_BLIT:
4383
            if (src_pool == WINED3D_POOL_SYSTEM_MEM || dst_pool == WINED3D_POOL_SYSTEM_MEM)
4384 4385
                return FALSE;

4386
            if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4387
            {
4388 4389
                TRACE("Checking support for fixup:\n");
                dump_color_fixup_desc(src_format->color_fixup);
4390
            }
4391

4392
            /* We only support identity conversions. */
4393 4394
            if (!is_identity_fixup(src_format->color_fixup)
                    || !is_identity_fixup(dst_format->color_fixup))
4395
            {
4396 4397
                TRACE("Fixups are not supported.\n");
                return FALSE;
4398
            }
4399

4400
            return TRUE;
4401

4402
        case WINED3D_BLIT_OP_COLOR_FILL:
4403
            if (dst_pool == WINED3D_POOL_SYSTEM_MEM)
4404 4405
                return FALSE;

4406 4407 4408 4409 4410 4411
            if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
            {
                if (!((dst_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
                    return FALSE;
            }
            else if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
4412 4413 4414 4415
            {
                TRACE("Color fill not supported\n");
                return FALSE;
            }
4416

4417 4418 4419
            /* FIXME: We should reject color fills on formats with fixups,
             * but this would break P8 color fills for example. */

4420
            return TRUE;
4421

4422 4423
        case WINED3D_BLIT_OP_DEPTH_FILL:
            return TRUE;
4424

4425 4426 4427
        default:
            TRACE("Unsupported blit_op=%d\n", blit_op);
            return FALSE;
4428
    }
4429
}
4430

4431
static HRESULT ffp_blit_color_fill(struct wined3d_device *device, struct wined3d_surface *dst_surface,
4432
        const RECT *dst_rect, const struct wined3d_color *color)
4433 4434
{
    const RECT draw_rect = {0, 0, dst_surface->resource.width, dst_surface->resource.height};
4435 4436 4437 4438 4439 4440 4441 4442 4443 4444
    struct wined3d_rendertarget_view *view;
    struct wined3d_fb_state fb = {&view, NULL};
    HRESULT hr;

    if (FAILED(hr = wined3d_rendertarget_view_create_from_surface(dst_surface,
            NULL, &wined3d_null_parent_ops, &view)))
    {
        ERR("Failed to create rendertarget view, hr %#x.\n", hr);
        return hr;
    }
4445

4446
    device_clear_render_targets(device, 1, &fb, 1, dst_rect, &draw_rect, WINED3DCLEAR_TARGET, color, 0.0f, 0);
4447
    wined3d_rendertarget_view_decref(view);
4448 4449

    return WINED3D_OK;
4450
}
4451

4452 4453
static HRESULT ffp_blit_depth_fill(struct wined3d_device *device, struct wined3d_surface *dst_surface,
        const RECT *dst_rect, float depth)
4454
{
4455 4456 4457 4458 4459 4460 4461 4462 4463 4464
    const RECT draw_rect = {0, 0, dst_surface->resource.width, dst_surface->resource.height};
    struct wined3d_fb_state fb = {NULL, NULL};
    HRESULT hr;

    if (FAILED(hr = wined3d_rendertarget_view_create_from_surface(dst_surface,
            NULL, &wined3d_null_parent_ops, &fb.depth_stencil)))
    {
        ERR("Failed to create rendertarget view, hr %#x.\n", hr);
        return hr;
    }
4465

4466 4467
    device_clear_render_targets(device, 0, &fb, 1, dst_rect, &draw_rect, WINED3DCLEAR_ZBUFFER, 0, depth, 0);
    wined3d_rendertarget_view_decref(fb.depth_stencil);
4468 4469

    return WINED3D_OK;
4470
}
4471

4472 4473 4474 4475 4476 4477 4478 4479 4480
const struct blit_shader ffp_blit =  {
    ffp_blit_alloc,
    ffp_blit_free,
    ffp_blit_set,
    ffp_blit_unset,
    ffp_blit_supported,
    ffp_blit_color_fill,
    ffp_blit_depth_fill,
};
4481

4482
static HRESULT cpu_blit_alloc(struct wined3d_device *device)
4483 4484 4485
{
    return WINED3D_OK;
}
4486

4487
/* Context activation is done by the caller. */
4488
static void cpu_blit_free(struct wined3d_device *device)
4489 4490
{
}
4491

4492
/* Context activation is done by the caller. */
4493
static HRESULT cpu_blit_set(void *blit_priv, struct wined3d_context *context, const struct wined3d_surface *surface)
4494 4495 4496
{
    return WINED3D_OK;
}
4497

4498 4499 4500 4501
/* Context activation is done by the caller. */
static void cpu_blit_unset(const struct wined3d_gl_info *gl_info)
{
}
4502

4503
static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
4504 4505
        const RECT *src_rect, DWORD src_usage, enum wined3d_pool src_pool, const struct wined3d_format *src_format,
        const RECT *dst_rect, DWORD dst_usage, enum wined3d_pool dst_pool, const struct wined3d_format *dst_format)
4506 4507 4508 4509 4510
{
    if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
    {
        return TRUE;
    }
4511

4512 4513
    return FALSE;
}
4514

4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572
static HRESULT surface_cpu_blt_compressed(const BYTE *src_data, BYTE *dst_data,
        UINT src_pitch, UINT dst_pitch, UINT update_w, UINT update_h,
        const struct wined3d_format *format, DWORD flags, const WINEDDBLTFX *fx)
{
    UINT row_block_count;
    const BYTE *src_row;
    BYTE *dst_row;
    UINT x, y;

    src_row = src_data;
    dst_row = dst_data;

    row_block_count = (update_w + format->block_width - 1) / format->block_width;

    if (!flags)
    {
        for (y = 0; y < update_h; y += format->block_height)
        {
            memcpy(dst_row, src_row, row_block_count * format->block_byte_count);
            src_row += src_pitch;
            dst_row += dst_pitch;
        }

        return WINED3D_OK;
    }

    if (flags == WINEDDBLT_DDFX && fx->dwDDFX == WINEDDBLTFX_MIRRORUPDOWN)
    {
        src_row += (((update_h / format->block_height) - 1) * src_pitch);

        switch (format->id)
        {
            case WINED3DFMT_DXT1:
                for (y = 0; y < update_h; y += format->block_height)
                {
                    struct block
                    {
                        WORD color[2];
                        BYTE control_row[4];
                    };

                    const struct block *s = (const struct block *)src_row;
                    struct block *d = (struct block *)dst_row;

                    for (x = 0; x < row_block_count; ++x)
                    {
                        d[x].color[0] = s[x].color[0];
                        d[x].color[1] = s[x].color[1];
                        d[x].control_row[0] = s[x].control_row[3];
                        d[x].control_row[1] = s[x].control_row[2];
                        d[x].control_row[2] = s[x].control_row[1];
                        d[x].control_row[3] = s[x].control_row[0];
                    }
                    src_row -= src_pitch;
                    dst_row += dst_pitch;
                }
                return WINED3D_OK;

4573
            case WINED3DFMT_DXT2:
4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617
            case WINED3DFMT_DXT3:
                for (y = 0; y < update_h; y += format->block_height)
                {
                    struct block
                    {
                        WORD alpha_row[4];
                        WORD color[2];
                        BYTE control_row[4];
                    };

                    const struct block *s = (const struct block *)src_row;
                    struct block *d = (struct block *)dst_row;

                    for (x = 0; x < row_block_count; ++x)
                    {
                        d[x].alpha_row[0] = s[x].alpha_row[3];
                        d[x].alpha_row[1] = s[x].alpha_row[2];
                        d[x].alpha_row[2] = s[x].alpha_row[1];
                        d[x].alpha_row[3] = s[x].alpha_row[0];
                        d[x].color[0] = s[x].color[0];
                        d[x].color[1] = s[x].color[1];
                        d[x].control_row[0] = s[x].control_row[3];
                        d[x].control_row[1] = s[x].control_row[2];
                        d[x].control_row[2] = s[x].control_row[1];
                        d[x].control_row[3] = s[x].control_row[0];
                    }
                    src_row -= src_pitch;
                    dst_row += dst_pitch;
                }
                return WINED3D_OK;

            default:
                FIXME("Compressed flip not implemented for format %s.\n",
                        debug_d3dformat(format->id));
                return E_NOTIMPL;
        }
    }

    FIXME("Unsupported blit on compressed surface (format %s, flags %#x, DDFX %#x).\n",
            debug_d3dformat(format->id), flags, flags & WINEDDBLT_DDFX ? fx->dwDDFX : 0);

    return E_NOTIMPL;
}

4618 4619
static HRESULT surface_cpu_blt(struct wined3d_surface *dst_surface, const RECT *dst_rect,
        struct wined3d_surface *src_surface, const RECT *src_rect, DWORD flags,
4620
        const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
4621 4622 4623
{
    int bpp, srcheight, srcwidth, dstheight, dstwidth, width;
    const struct wined3d_format *src_format, *dst_format;
4624
    struct wined3d_texture *src_texture = NULL;
4625
    struct wined3d_map_desc dst_map, src_map;
4626
    const BYTE *sbase = NULL;
4627 4628 4629 4630
    HRESULT hr = WINED3D_OK;
    const BYTE *sbuf;
    BYTE *dbuf;
    int x, y;
4631

4632 4633 4634
    TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
            dst_surface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect),
            flags, fx, debug_d3dtexturefiltertype(filter));
4635

4636
    if (src_surface == dst_surface)
4637
    {
4638 4639
        wined3d_surface_map(dst_surface, &dst_map, NULL, 0);
        src_map = dst_map;
4640 4641
        src_format = dst_surface->resource.format;
        dst_format = src_format;
4642
    }
4643
    else
4644
    {
4645 4646 4647 4648 4649
        dst_format = dst_surface->resource.format;
        if (src_surface)
        {
            if (dst_surface->resource.format->id != src_surface->resource.format->id)
            {
4650
                if (!(src_texture = surface_convert_format(src_surface, dst_format->id)))
4651 4652 4653 4654 4655
                {
                    /* The conv function writes a FIXME */
                    WARN("Cannot convert source surface format to dest format.\n");
                    goto release;
                }
4656
                src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, 0));
4657
            }
4658
            wined3d_surface_map(src_surface, &src_map, NULL, WINED3D_MAP_READONLY);
4659 4660 4661 4662 4663 4664
            src_format = src_surface->resource.format;
        }
        else
        {
            src_format = dst_format;
        }
4665 4666

        wined3d_surface_map(dst_surface, &dst_map, dst_rect, 0);
4667 4668
    }

4669
    bpp = dst_surface->resource.format->byte_count;
4670 4671 4672 4673 4674
    srcheight = src_rect->bottom - src_rect->top;
    srcwidth = src_rect->right - src_rect->left;
    dstheight = dst_rect->bottom - dst_rect->top;
    dstwidth = dst_rect->right - dst_rect->left;
    width = (dst_rect->right - dst_rect->left) * bpp;
4675

4676 4677
    if (src_surface)
        sbase = (BYTE *)src_map.data
4678 4679 4680
                + ((src_rect->top / src_format->block_height) * src_map.row_pitch)
                + ((src_rect->left / src_format->block_width) * src_format->block_byte_count);
    if (src_surface != dst_surface)
4681 4682 4683
        dbuf = dst_map.data;
    else
        dbuf = (BYTE *)dst_map.data
4684 4685
                + ((dst_rect->top / dst_format->block_height) * dst_map.row_pitch)
                + ((dst_rect->left / dst_format->block_width) * dst_format->block_byte_count);
4686

4687
    if (src_format->flags & dst_format->flags & WINED3DFMT_FLAG_BLOCKS)
4688
    {
4689
        TRACE("%s -> %s copy.\n", debug_d3dformat(src_format->id), debug_d3dformat(dst_format->id));
4690

4691
        if (src_surface == dst_surface)
4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
        {
            FIXME("Only plain blits supported on compressed surfaces.\n");
            hr = E_NOTIMPL;
            goto release;
        }

        if (srcheight != dstheight || srcwidth != dstwidth)
        {
            WARN("Stretching not supported on compressed surfaces.\n");
            hr = WINED3DERR_INVALIDCALL;
            goto release;
        }

4705
        if (!surface_check_block_align(src_surface, src_rect))
4706
        {
4707 4708 4709 4710 4711 4712 4713 4714
            WARN("Source rectangle not block-aligned.\n");
            hr = WINED3DERR_INVALIDCALL;
            goto release;
        }

        if (!surface_check_block_align(dst_surface, dst_rect))
        {
            WARN("Destination rectangle not block-aligned.\n");
4715 4716
            hr = WINED3DERR_INVALIDCALL;
            goto release;
4717 4718
        }

4719
        hr = surface_cpu_blt_compressed(sbase, dbuf,
4720
                src_map.row_pitch, dst_map.row_pitch, dstwidth, dstheight,
4721
                src_format, flags, fx);
4722 4723 4724
        goto release;
    }

4725 4726 4727
    /* First, all the 'source-less' blits */
    if (flags & WINEDDBLT_COLORFILL)
    {
4728
        hr = _Blt_ColorFill(dbuf, dstwidth, dstheight, bpp, dst_map.row_pitch, fx->u5.dwFillColor);
4729 4730
        flags &= ~WINEDDBLT_COLORFILL;
    }
4731

4732
    if (flags & WINEDDBLT_DEPTHFILL)
4733
    {
4734 4735 4736
        FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
    }
    if (flags & WINEDDBLT_DDROPS)
4737
    {
4738
        FIXME("\tDdraw Raster Ops: %08x Pattern: %p\n", fx->dwDDROP, fx->u5.lpDDSPattern);
4739
    }
4740 4741 4742 4743
    /* Now the 'with source' blits. */
    if (src_surface)
    {
        int sx, xinc, sy, yinc;
4744

4745 4746
        if (!dstwidth || !dstheight) /* Hmm... stupid program? */
            goto release;
4747

4748
        if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
4749 4750 4751 4752 4753
                && (srcwidth != dstwidth || srcheight != dstheight))
        {
            /* Can happen when d3d9 apps do a StretchRect() call which isn't handled in GL. */
            FIXME("Filter %s not supported in software blit.\n", debug_d3dtexturefiltertype(filter));
        }
4754

4755 4756 4757 4758 4759 4760 4761
        xinc = (srcwidth << 16) / dstwidth;
        yinc = (srcheight << 16) / dstheight;

        if (!flags)
        {
            /* No effects, we can cheat here. */
            if (dstwidth == srcwidth)
4762
            {
4763 4764 4765 4766 4767 4768 4769
                if (dstheight == srcheight)
                {
                    /* No stretching in either direction. This needs to be as
                     * fast as possible. */
                    sbuf = sbase;

                    /* Check for overlapping surfaces. */
4770 4771
                    if (src_surface != dst_surface || dst_rect->top < src_rect->top
                            || dst_rect->right <= src_rect->left || src_rect->right <= dst_rect->left)
4772 4773 4774 4775 4776
                    {
                        /* No overlap, or dst above src, so copy from top downwards. */
                        for (y = 0; y < dstheight; ++y)
                        {
                            memcpy(dbuf, sbuf, width);
4777 4778
                            sbuf += src_map.row_pitch;
                            dbuf += dst_map.row_pitch;
4779 4780
                        }
                    }
4781
                    else if (dst_rect->top > src_rect->top)
4782 4783
                    {
                        /* Copy from bottom upwards. */
4784 4785
                        sbuf += src_map.row_pitch * dstheight;
                        dbuf += dst_map.row_pitch * dstheight;
4786 4787
                        for (y = 0; y < dstheight; ++y)
                        {
4788 4789
                            sbuf -= src_map.row_pitch;
                            dbuf -= dst_map.row_pitch;
4790 4791 4792 4793 4794 4795 4796 4797 4798
                            memcpy(dbuf, sbuf, width);
                        }
                    }
                    else
                    {
                        /* Src and dst overlapping on the same line, use memmove. */
                        for (y = 0; y < dstheight; ++y)
                        {
                            memmove(dbuf, sbuf, width);
4799 4800
                            sbuf += src_map.row_pitch;
                            dbuf += dst_map.row_pitch;
4801 4802 4803 4804 4805 4806 4807 4808
                        }
                    }
                }
                else
                {
                    /* Stretching in y direction only. */
                    for (y = sy = 0; y < dstheight; ++y, sy += yinc)
                    {
4809
                        sbuf = sbase + (sy >> 16) * src_map.row_pitch;
4810
                        memcpy(dbuf, sbuf, width);
4811
                        dbuf += dst_map.row_pitch;
4812 4813
                    }
                }
4814
            }
4815 4816 4817 4818 4819 4820
            else
            {
                /* Stretching in X direction. */
                int last_sy = -1;
                for (y = sy = 0; y < dstheight; ++y, sy += yinc)
                {
4821
                    sbuf = sbase + (sy >> 16) * src_map.row_pitch;
4822

4823 4824 4825 4826
                    if ((sy >> 16) == (last_sy >> 16))
                    {
                        /* This source row is the same as last source row -
                         * Copy the already stretched row. */
4827
                        memcpy(dbuf, dbuf - dst_map.row_pitch, width);
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873
                    }
                    else
                    {
#define STRETCH_ROW(type) \
do { \
    const type *s = (const type *)sbuf; \
    type *d = (type *)dbuf; \
    for (x = sx = 0; x < dstwidth; ++x, sx += xinc) \
        d[x] = s[sx >> 16]; \
} while(0)

                        switch(bpp)
                        {
                            case 1:
                                STRETCH_ROW(BYTE);
                                break;
                            case 2:
                                STRETCH_ROW(WORD);
                                break;
                            case 4:
                                STRETCH_ROW(DWORD);
                                break;
                            case 3:
                            {
                                const BYTE *s;
                                BYTE *d = dbuf;
                                for (x = sx = 0; x < dstwidth; x++, sx+= xinc)
                                {
                                    DWORD pixel;

                                    s = sbuf + 3 * (sx >> 16);
                                    pixel = s[0] | (s[1] << 8) | (s[2] << 16);
                                    d[0] = (pixel      ) & 0xff;
                                    d[1] = (pixel >>  8) & 0xff;
                                    d[2] = (pixel >> 16) & 0xff;
                                    d += 3;
                                }
                                break;
                            }
                            default:
                                FIXME("Stretched blit not implemented for bpp %u!\n", bpp * 8);
                                hr = WINED3DERR_NOTAVAILABLE;
                                goto error;
                        }
#undef STRETCH_ROW
                    }
4874
                    dbuf += dst_map.row_pitch;
4875 4876 4877 4878 4879 4880
                    last_sy = sy;
                }
            }
        }
        else
        {
4881
            LONG dstyinc = dst_map.row_pitch, dstxinc = bpp;
4882 4883
            DWORD keylow = 0xffffffff, keyhigh = 0, keymask = 0xffffffff;
            DWORD destkeylow = 0x0, destkeyhigh = 0xffffffff, destkeymask = 0xffffffff;
4884
            if (flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE))
4885
            {
4886 4887 4888
                /* The color keying flags are checked for correctness in ddraw */
                if (flags & WINEDDBLT_KEYSRC)
                {
4889 4890
                    keylow  = src_surface->container->src_blt_color_key.color_space_low_value;
                    keyhigh = src_surface->container->src_blt_color_key.color_space_high_value;
4891 4892 4893
                }
                else if (flags & WINEDDBLT_KEYSRCOVERRIDE)
                {
4894 4895
                    keylow = fx->ddckSrcColorkey.color_space_low_value;
                    keyhigh = fx->ddckSrcColorkey.color_space_high_value;
4896 4897 4898 4899 4900
                }

                if (flags & WINEDDBLT_KEYDEST)
                {
                    /* Destination color keys are taken from the source surface! */
4901 4902
                    destkeylow = src_surface->container->dst_blt_color_key.color_space_low_value;
                    destkeyhigh = src_surface->container->dst_blt_color_key.color_space_high_value;
4903 4904 4905
                }
                else if (flags & WINEDDBLT_KEYDESTOVERRIDE)
                {
4906 4907
                    destkeylow = fx->ddckDestColorkey.color_space_low_value;
                    destkeyhigh = fx->ddckDestColorkey.color_space_high_value;
4908 4909 4910 4911 4912 4913 4914 4915
                }

                if (bpp == 1)
                {
                    keymask = 0xff;
                }
                else
                {
4916 4917 4918 4919 4920
                    DWORD masks[3];
                    get_color_masks(src_format, masks);
                    keymask = masks[0]
                            | masks[1]
                            | masks[2];
4921 4922
                }
                flags &= ~(WINEDDBLT_KEYSRC | WINEDDBLT_KEYDEST | WINEDDBLT_KEYSRCOVERRIDE | WINEDDBLT_KEYDESTOVERRIDE);
4923
            }
4924

4925
            if (flags & WINEDDBLT_DDFX)
4926
            {
4927 4928 4929 4930
                BYTE *dTopLeft, *dTopRight, *dBottomLeft, *dBottomRight, *tmp;
                LONG tmpxy;
                dTopLeft     = dbuf;
                dTopRight    = dbuf + ((dstwidth - 1) * bpp);
4931
                dBottomLeft  = dTopLeft + ((dstheight - 1) * dst_map.row_pitch);
4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005
                dBottomRight = dBottomLeft + ((dstwidth - 1) * bpp);

                if (fx->dwDDFX & WINEDDBLTFX_ARITHSTRETCHY)
                {
                    /* I don't think we need to do anything about this flag */
                    WARN("flags=DDBLT_DDFX nothing done for WINEDDBLTFX_ARITHSTRETCHY\n");
                }
                if (fx->dwDDFX & WINEDDBLTFX_MIRRORLEFTRIGHT)
                {
                    tmp          = dTopRight;
                    dTopRight    = dTopLeft;
                    dTopLeft     = tmp;
                    tmp          = dBottomRight;
                    dBottomRight = dBottomLeft;
                    dBottomLeft  = tmp;
                    dstxinc = dstxinc * -1;
                }
                if (fx->dwDDFX & WINEDDBLTFX_MIRRORUPDOWN)
                {
                    tmp          = dTopLeft;
                    dTopLeft     = dBottomLeft;
                    dBottomLeft  = tmp;
                    tmp          = dTopRight;
                    dTopRight    = dBottomRight;
                    dBottomRight = tmp;
                    dstyinc = dstyinc * -1;
                }
                if (fx->dwDDFX & WINEDDBLTFX_NOTEARING)
                {
                    /* I don't think we need to do anything about this flag */
                    WARN("flags=DDBLT_DDFX nothing done for WINEDDBLTFX_NOTEARING\n");
                }
                if (fx->dwDDFX & WINEDDBLTFX_ROTATE180)
                {
                    tmp          = dBottomRight;
                    dBottomRight = dTopLeft;
                    dTopLeft     = tmp;
                    tmp          = dBottomLeft;
                    dBottomLeft  = dTopRight;
                    dTopRight    = tmp;
                    dstxinc = dstxinc * -1;
                    dstyinc = dstyinc * -1;
                }
                if (fx->dwDDFX & WINEDDBLTFX_ROTATE270)
                {
                    tmp          = dTopLeft;
                    dTopLeft     = dBottomLeft;
                    dBottomLeft  = dBottomRight;
                    dBottomRight = dTopRight;
                    dTopRight    = tmp;
                    tmpxy   = dstxinc;
                    dstxinc = dstyinc;
                    dstyinc = tmpxy;
                    dstxinc = dstxinc * -1;
                }
                if (fx->dwDDFX & WINEDDBLTFX_ROTATE90)
                {
                    tmp          = dTopLeft;
                    dTopLeft     = dTopRight;
                    dTopRight    = dBottomRight;
                    dBottomRight = dBottomLeft;
                    dBottomLeft  = tmp;
                    tmpxy   = dstxinc;
                    dstxinc = dstyinc;
                    dstyinc = tmpxy;
                    dstyinc = dstyinc * -1;
                }
                if (fx->dwDDFX & WINEDDBLTFX_ZBUFFERBASEDEST)
                {
                    /* I don't think we need to do anything about this flag */
                    WARN("flags=WINEDDBLT_DDFX nothing done for WINEDDBLTFX_ZBUFFERBASEDEST\n");
                }
                dbuf = dTopLeft;
                flags &= ~(WINEDDBLT_DDFX);
5006
            }
5007

5008 5009 5010 5011 5012 5013
#define COPY_COLORKEY_FX(type) \
do { \
    const type *s; \
    type *d = (type *)dbuf, *dx, tmp; \
    for (y = sy = 0; y < dstheight; ++y, sy += yinc) \
    { \
5014
        s = (const type *)(sbase + (sy >> 16) * src_map.row_pitch); \
5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028
        dx = d; \
        for (x = sx = 0; x < dstwidth; ++x, sx += xinc) \
        { \
            tmp = s[sx >> 16]; \
            if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) \
                    && ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) \
            { \
                dx[0] = tmp; \
            } \
            dx = (type *)(((BYTE *)dx) + dstxinc); \
        } \
        d = (type *)(((BYTE *)d) + dstyinc); \
    } \
} while(0)
5029

5030
            switch (bpp)
5031
            {
5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
                case 1:
                    COPY_COLORKEY_FX(BYTE);
                    break;
                case 2:
                    COPY_COLORKEY_FX(WORD);
                    break;
                case 4:
                    COPY_COLORKEY_FX(DWORD);
                    break;
                case 3:
                {
                    const BYTE *s;
                    BYTE *d = dbuf, *dx;
                    for (y = sy = 0; y < dstheight; ++y, sy += yinc)
                    {
5047
                        sbuf = sbase + (sy >> 16) * src_map.row_pitch;
5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073
                        dx = d;
                        for (x = sx = 0; x < dstwidth; ++x, sx+= xinc)
                        {
                            DWORD pixel, dpixel = 0;
                            s = sbuf + 3 * (sx>>16);
                            pixel = s[0] | (s[1] << 8) | (s[2] << 16);
                            dpixel = dx[0] | (dx[1] << 8 ) | (dx[2] << 16);
                            if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh)
                                    && ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
                            {
                                dx[0] = (pixel      ) & 0xff;
                                dx[1] = (pixel >>  8) & 0xff;
                                dx[2] = (pixel >> 16) & 0xff;
                            }
                            dx += dstxinc;
                        }
                        d += dstyinc;
                    }
                    break;
                }
                default:
                    FIXME("%s color-keyed blit not implemented for bpp %u!\n",
                          (flags & WINEDDBLT_KEYSRC) ? "Source" : "Destination", bpp * 8);
                    hr = WINED3DERR_NOTAVAILABLE;
                    goto error;
#undef COPY_COLORKEY_FX
5074
            }
5075
        }
5076
    }
5077

5078 5079
error:
    if (flags && FIXME_ON(d3d_surface))
5080
    {
5081
        FIXME("\tUnsupported flags: %#x.\n", flags);
5082 5083
    }

5084
release:
5085
    wined3d_surface_unmap(dst_surface);
5086
    if (src_surface && src_surface != dst_surface)
5087
        wined3d_surface_unmap(src_surface);
5088
    /* Release the converted surface, if any. */
5089 5090
    if (src_texture)
        wined3d_texture_decref(src_texture);
5091 5092

    return hr;
5093 5094
}

5095
static HRESULT cpu_blit_color_fill(struct wined3d_device *device, struct wined3d_surface *dst_surface,
5096
        const RECT *dst_rect, const struct wined3d_color *color)
5097
{
5098
    static const RECT src_rect;
5099
    WINEDDBLTFX BltFx;
5100

5101 5102
    memset(&BltFx, 0, sizeof(BltFx));
    BltFx.dwSize = sizeof(BltFx);
5103
    BltFx.u5.dwFillColor = wined3d_format_convert_from_float(dst_surface, color);
5104
    return surface_cpu_blt(dst_surface, dst_rect, NULL, &src_rect,
5105
            WINEDDBLT_COLORFILL, &BltFx, WINED3D_TEXF_POINT);
5106 5107
}

5108
static HRESULT cpu_blit_depth_fill(struct wined3d_device *device,
5109
        struct wined3d_surface *surface, const RECT *rect, float depth)
5110 5111 5112 5113 5114
{
    FIXME("Depth filling not implemented by cpu_blit.\n");
    return WINED3DERR_INVALIDCALL;
}

5115 5116 5117 5118 5119
const struct blit_shader cpu_blit =  {
    cpu_blit_alloc,
    cpu_blit_free,
    cpu_blit_set,
    cpu_blit_unset,
5120
    cpu_blit_supported,
5121 5122
    cpu_blit_color_fill,
    cpu_blit_depth_fill,
5123
};
5124

5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247
HRESULT CDECL wined3d_surface_blt(struct wined3d_surface *dst_surface, const RECT *dst_rect_in,
        struct wined3d_surface *src_surface, const RECT *src_rect_in, DWORD flags,
        const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
{
    struct wined3d_swapchain *src_swapchain, *dst_swapchain;
    struct wined3d_device *device = dst_surface->resource.device;
    DWORD src_ds_flags, dst_ds_flags;
    RECT src_rect, dst_rect;
    BOOL scale, convert;

    static const DWORD simple_blit = WINEDDBLT_ASYNC
            | WINEDDBLT_COLORFILL
            | WINEDDBLT_WAIT
            | WINEDDBLT_DEPTHFILL
            | WINEDDBLT_DONOTWAIT;

    TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
            dst_surface, wine_dbgstr_rect(dst_rect_in), src_surface, wine_dbgstr_rect(src_rect_in),
            flags, fx, debug_d3dtexturefiltertype(filter));
    TRACE("Usage is %s.\n", debug_d3dusage(dst_surface->resource.usage));

    if (fx)
    {
        TRACE("dwSize %#x.\n", fx->dwSize);
        TRACE("dwDDFX %#x.\n", fx->dwDDFX);
        TRACE("dwROP %#x.\n", fx->dwROP);
        TRACE("dwDDROP %#x.\n", fx->dwDDROP);
        TRACE("dwRotationAngle %#x.\n", fx->dwRotationAngle);
        TRACE("dwZBufferOpCode %#x.\n", fx->dwZBufferOpCode);
        TRACE("dwZBufferLow %#x.\n", fx->dwZBufferLow);
        TRACE("dwZBufferHigh %#x.\n", fx->dwZBufferHigh);
        TRACE("dwZBufferBaseDest %#x.\n", fx->dwZBufferBaseDest);
        TRACE("dwZDestConstBitDepth %#x.\n", fx->dwZDestConstBitDepth);
        TRACE("lpDDSZBufferDest %p.\n", fx->u1.lpDDSZBufferDest);
        TRACE("dwZSrcConstBitDepth %#x.\n", fx->dwZSrcConstBitDepth);
        TRACE("lpDDSZBufferSrc %p.\n", fx->u2.lpDDSZBufferSrc);
        TRACE("dwAlphaEdgeBlendBitDepth %#x.\n", fx->dwAlphaEdgeBlendBitDepth);
        TRACE("dwAlphaEdgeBlend %#x.\n", fx->dwAlphaEdgeBlend);
        TRACE("dwReserved %#x.\n", fx->dwReserved);
        TRACE("dwAlphaDestConstBitDepth %#x.\n", fx->dwAlphaDestConstBitDepth);
        TRACE("lpDDSAlphaDest %p.\n", fx->u3.lpDDSAlphaDest);
        TRACE("dwAlphaSrcConstBitDepth %#x.\n", fx->dwAlphaSrcConstBitDepth);
        TRACE("lpDDSAlphaSrc %p.\n", fx->u4.lpDDSAlphaSrc);
        TRACE("lpDDSPattern %p.\n", fx->u5.lpDDSPattern);
        TRACE("ddckDestColorkey {%#x, %#x}.\n",
                fx->ddckDestColorkey.color_space_low_value,
                fx->ddckDestColorkey.color_space_high_value);
        TRACE("ddckSrcColorkey {%#x, %#x}.\n",
                fx->ddckSrcColorkey.color_space_low_value,
                fx->ddckSrcColorkey.color_space_high_value);
    }

    if (dst_surface->resource.map_count || (src_surface && src_surface->resource.map_count))
    {
        WARN("Surface is busy, returning WINEDDERR_SURFACEBUSY.\n");
        return WINEDDERR_SURFACEBUSY;
    }

    surface_get_rect(dst_surface, dst_rect_in, &dst_rect);

    if (dst_rect.left >= dst_rect.right || dst_rect.top >= dst_rect.bottom
            || dst_rect.left > dst_surface->resource.width || dst_rect.left < 0
            || dst_rect.top > dst_surface->resource.height || dst_rect.top < 0
            || dst_rect.right > dst_surface->resource.width || dst_rect.right < 0
            || dst_rect.bottom > dst_surface->resource.height || dst_rect.bottom < 0)
    {
        WARN("The application gave us a bad destination rectangle.\n");
        return WINEDDERR_INVALIDRECT;
    }

    if (src_surface)
    {
        surface_get_rect(src_surface, src_rect_in, &src_rect);

        if (src_rect.left >= src_rect.right || src_rect.top >= src_rect.bottom
                || src_rect.left > src_surface->resource.width || src_rect.left < 0
                || src_rect.top > src_surface->resource.height || src_rect.top < 0
                || src_rect.right > src_surface->resource.width || src_rect.right < 0
                || src_rect.bottom > src_surface->resource.height || src_rect.bottom < 0)
        {
            WARN("Application gave us bad source rectangle for Blt.\n");
            return WINEDDERR_INVALIDRECT;
        }
    }
    else
    {
        memset(&src_rect, 0, sizeof(src_rect));
    }

    if (!fx || !(fx->dwDDFX))
        flags &= ~WINEDDBLT_DDFX;

    if (flags & WINEDDBLT_WAIT)
        flags &= ~WINEDDBLT_WAIT;

    if (flags & WINEDDBLT_ASYNC)
    {
        static unsigned int once;

        if (!once++)
            FIXME("Can't handle WINEDDBLT_ASYNC flag.\n");
        flags &= ~WINEDDBLT_ASYNC;
    }

    /* WINEDDBLT_DONOTWAIT appeared in DX7. */
    if (flags & WINEDDBLT_DONOTWAIT)
    {
        static unsigned int once;

        if (!once++)
            FIXME("Can't handle WINEDDBLT_DONOTWAIT flag.\n");
        flags &= ~WINEDDBLT_DONOTWAIT;
    }

    if (!device->d3d_initialized)
    {
        WARN("D3D not initialized, using fallback.\n");
        goto cpu;
    }

    /* We want to avoid invalidating the sysmem location for converted
     * surfaces, since otherwise we'd have to convert the data back when
     * locking them. */
5248 5249
    if (dst_surface->container->flags & WINED3D_TEXTURE_CONVERTED
            || dst_surface->container->resource.format->convert
5250
            || wined3d_format_get_color_key_conversion(dst_surface->container, TRUE))
5251 5252
    {
        WARN_(d3d_perf)("Converted surface, using CPU blit.\n");
5253
        goto cpu;
5254 5255 5256 5257 5258 5259 5260 5261 5262
    }

    if (flags & ~simple_blit)
    {
        WARN_(d3d_perf)("Using fallback for complex blit (%#x).\n", flags);
        goto fallback;
    }

    if (src_surface)
5263
        src_swapchain = src_surface->container->swapchain;
5264 5265 5266
    else
        src_swapchain = NULL;

5267
    dst_swapchain = dst_surface->container->swapchain;
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312

    /* This isn't strictly needed. FBO blits for example could deal with
     * cross-swapchain blits by first downloading the source to a texture
     * before switching to the destination context. We just have this here to
     * not have to deal with the issue, since cross-swapchain blits should be
     * rare. */
    if (src_swapchain && dst_swapchain && src_swapchain != dst_swapchain)
    {
        FIXME("Using fallback for cross-swapchain blit.\n");
        goto fallback;
    }

    scale = src_surface
            && (src_rect.right - src_rect.left != dst_rect.right - dst_rect.left
            || src_rect.bottom - src_rect.top != dst_rect.bottom - dst_rect.top);
    convert = src_surface && src_surface->resource.format->id != dst_surface->resource.format->id;

    dst_ds_flags = dst_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
    if (src_surface)
        src_ds_flags = src_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
    else
        src_ds_flags = 0;

    if (src_ds_flags || dst_ds_flags)
    {
        if (flags & WINEDDBLT_DEPTHFILL)
        {
            float depth;

            TRACE("Depth fill.\n");

            if (!surface_convert_depth_to_float(dst_surface, fx->u5.dwFillDepth, &depth))
                return WINED3DERR_INVALIDCALL;

            if (SUCCEEDED(wined3d_surface_depth_fill(dst_surface, &dst_rect, depth)))
                return WINED3D_OK;
        }
        else
        {
            if (src_ds_flags != dst_ds_flags)
            {
                WARN("Rejecting depth / stencil blit between incompatible formats.\n");
                return WINED3DERR_INVALIDCALL;
            }

5313 5314
            if (SUCCEEDED(wined3d_surface_depth_blt(src_surface, src_surface->container->resource.draw_binding,
                    &src_rect, dst_surface, dst_surface->container->resource.draw_binding, &dst_rect)))
5315 5316 5317 5318 5319 5320 5321
                return WINED3D_OK;
        }
    }
    else
    {
        /* In principle this would apply to depth blits as well, but we don't
         * implement those in the CPU blitter at the moment. */
5322 5323
        if ((dst_surface->locations & dst_surface->resource.map_binding)
                && (!src_surface || (src_surface->locations & src_surface->resource.map_binding)))
5324 5325 5326 5327 5328 5329
        {
            if (scale)
                TRACE("Not doing sysmem blit because of scaling.\n");
            else if (convert)
                TRACE("Not doing sysmem blit because of format conversion.\n");
            else
5330
                goto cpu;
5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349
        }

        if (flags & WINEDDBLT_COLORFILL)
        {
            struct wined3d_color color;

            TRACE("Color fill.\n");

            if (!surface_convert_color_to_float(dst_surface, fx->u5.dwFillColor, &color))
                goto fallback;

            if (SUCCEEDED(surface_color_fill(dst_surface, &dst_rect, &color)))
                return WINED3D_OK;
        }
        else
        {
            TRACE("Color blit.\n");

            /* Upload */
5350 5351
            if ((src_surface->locations & WINED3D_LOCATION_SYSMEM)
                    && !(dst_surface->locations & WINED3D_LOCATION_SYSMEM))
5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362
            {
                if (scale)
                    TRACE("Not doing upload because of scaling.\n");
                else if (convert)
                    TRACE("Not doing upload because of format conversion.\n");
                else
                {
                    POINT dst_point = {dst_rect.left, dst_rect.top};

                    if (SUCCEEDED(surface_upload_from_surface(dst_surface, &dst_point, src_surface, &src_rect)))
                    {
5363
                        if (!wined3d_resource_is_offscreen(&dst_surface->container->resource))
5364
                            surface_load_location(dst_surface, dst_surface->container->resource.draw_binding);
5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376
                        return WINED3D_OK;
                    }
                }
            }

            /* Use present for back -> front blits. The idea behind this is
             * that present is potentially faster than a blit, in particular
             * when FBO blits aren't available. Some ddraw applications like
             * Half-Life and Prince of Persia 3D use Blt() from the backbuffer
             * to the frontbuffer instead of doing a Flip(). D3D8 and D3D9
             * applications can't blit directly to the frontbuffer. */
            if (dst_swapchain && dst_swapchain->back_buffers
5377
                    && dst_surface->container == dst_swapchain->front_buffer
5378
                    && src_surface->container == dst_swapchain->back_buffers[0])
5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399
            {
                enum wined3d_swap_effect swap_effect = dst_swapchain->desc.swap_effect;

                TRACE("Using present for backbuffer -> frontbuffer blit.\n");

                /* Set the swap effect to COPY, we don't want the backbuffer
                 * to become undefined. */
                dst_swapchain->desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
                wined3d_swapchain_present(dst_swapchain, NULL, NULL, dst_swapchain->win_handle, NULL, 0);
                dst_swapchain->desc.swap_effect = swap_effect;

                return WINED3D_OK;
            }

            if (fbo_blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
                    &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
                    &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
            {
                TRACE("Using FBO blit.\n");

                surface_blt_fbo(device, filter,
5400 5401 5402 5403
                        src_surface, src_surface->container->resource.draw_binding, &src_rect,
                        dst_surface, dst_surface->container->resource.draw_binding, &dst_rect);
                surface_validate_location(dst_surface, dst_surface->container->resource.draw_binding);
                surface_invalidate_location(dst_surface, ~dst_surface->container->resource.draw_binding);
5404

5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421
                return WINED3D_OK;
            }

            if (arbfp_blit.blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
                    &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
                    &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
            {
                TRACE("Using arbfp blit.\n");

                if (SUCCEEDED(arbfp_blit_surface(device, filter, src_surface, &src_rect, dst_surface, &dst_rect)))
                    return WINED3D_OK;
            }
        }
    }

fallback:
    /* Special cases for render targets. */
5422 5423
    if (SUCCEEDED(surface_blt_special(dst_surface, &dst_rect, src_surface, &src_rect, flags, fx, filter)))
        return WINED3D_OK;
5424 5425 5426 5427

cpu:

    /* For the rest call the X11 surface implementation. For render targets
5428 5429
     * this should be implemented OpenGL accelerated in surface_blt_special(),
     * other blits are rather rare. */
5430 5431 5432
    return surface_cpu_blt(dst_surface, &dst_rect, src_surface, &src_rect, flags, fx, filter);
}

5433
static HRESULT surface_init(struct wined3d_surface *surface, struct wined3d_texture *container,
5434
        const struct wined3d_resource_desc *desc, GLenum target, unsigned int level, unsigned int layer, DWORD flags)
5435
{
5436
    struct wined3d_device *device = container->resource.device;
5437
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5438 5439
    const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
    UINT multisample_quality = desc->multisample_quality;
5440
    BOOL lockable = flags & WINED3D_SURFACE_MAPPABLE;
5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453
    unsigned int resource_size;
    HRESULT hr;

    if (multisample_quality > 0)
    {
        FIXME("multisample_quality set to %u, substituting 0.\n", multisample_quality);
        multisample_quality = 0;
    }

    /* Quick lockable sanity check.
     * TODO: remove this after surfaces, usage and lockability have been debugged properly
     * this function is too deep to need to care about things like this.
     * Levels need to be checked too, since they all affect what can be done. */
5454
    switch (desc->pool)
5455
    {
5456
        case WINED3D_POOL_MANAGED:
5457
            if (desc->usage & WINED3DUSAGE_DYNAMIC)
5458 5459 5460
                FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
            break;

5461
        case WINED3D_POOL_DEFAULT:
5462 5463
            if (lockable && !(desc->usage & (WINED3DUSAGE_DYNAMIC
                    | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
5464 5465 5466
                WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
            break;

5467 5468 5469 5470
        case WINED3D_POOL_SCRATCH:
        case WINED3D_POOL_SYSTEM_MEM:
            break;

5471
        default:
5472
            FIXME("Unknown pool %#x.\n", desc->pool);
5473 5474 5475
            break;
    };

5476
    if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
5477 5478 5479 5480
        FIXME("Trying to create a render target that isn't in the default pool.\n");

    /* FIXME: Check that the format is supported by the device. */

5481
    resource_size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, 1);
5482 5483 5484
    if (!resource_size)
        return WINED3DERR_INVALIDCALL;

5485 5486 5487 5488
    if (device->wined3d->flags & WINED3D_NO3D)
        surface->surface_ops = &gdi_surface_ops;
    else
        surface->surface_ops = &surface_ops;
5489

5490
    if (FAILED(hr = resource_init(&surface->resource, device, WINED3D_RTYPE_SURFACE, format,
5491
            desc->multisample_type, multisample_quality, desc->usage, desc->pool, desc->width, desc->height, 1,
5492
            resource_size, NULL, &wined3d_null_parent_ops, &surface_resource_ops)))
5493 5494 5495 5496 5497
    {
        WARN("Failed to initialize resource, returning %#x.\n", hr);
        return hr;
    }

5498
    surface->container = container;
5499
    surface_validate_location(surface, WINED3D_LOCATION_SYSMEM);
5500
    list_init(&surface->renderbuffers);
5501 5502 5503
    list_init(&surface->overlays);

    /* Flags */
5504
    if (flags & WINED3D_SURFACE_DISCARD)
5505
        surface->flags |= SFLAG_DISCARD;
5506
    if (lockable || desc->format == WINED3DFMT_D16_LOCKABLE)
5507 5508
        surface->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;

5509 5510
    surface->texture_target = target;
    surface->texture_level = level;
5511
    surface->texture_layer = layer;
5512

5513
    /* Call the private setup routine */
5514
    if (FAILED(hr = surface->surface_ops->surface_private_setup(surface)))
5515
    {
5516
        ERR("Private setup failed, hr %#x.\n", hr);
5517
        surface_cleanup(surface);
5518 5519 5520
        return hr;
    }

5521 5522 5523 5524 5525
    /* Similar to lockable rendertargets above, creating the DIB section
     * during surface initialization prevents the sysmem pointer from changing
     * after a wined3d_surface_getdc() call. */
    if ((desc->usage & WINED3DUSAGE_OWNDC) && !surface->hDC
            && SUCCEEDED(surface_create_dib_section(surface)))
5526
        surface->resource.map_binding = WINED3D_LOCATION_DIB;
5527

5528
    if (surface->resource.map_binding == WINED3D_LOCATION_DIB)
5529 5530
    {
        wined3d_resource_free_sysmem(&surface->resource);
5531 5532
        surface_validate_location(surface, WINED3D_LOCATION_DIB);
        surface_invalidate_location(surface, WINED3D_LOCATION_SYSMEM);
5533
    }
5534

5535 5536
    return hr;
}
5537

5538
HRESULT wined3d_surface_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
5539
        GLenum target, unsigned int level, unsigned int layer, DWORD flags, struct wined3d_surface **surface)
5540
{
5541
    struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
5542
    const struct wined3d_parent_ops *parent_ops;
5543
    struct wined3d_surface *object;
5544
    void *parent;
5545 5546
    HRESULT hr;

5547
    TRACE("container %p, width %u, height %u, format %s, usage %s (%#x), pool %s, "
5548
            "multisample_type %#x, multisample_quality %u, target %#x, level %u, layer %u, flags %#x, surface %p.\n",
5549
            container, desc->width, desc->height, debug_d3dformat(desc->format),
5550
            debug_d3dusage(desc->usage), desc->usage, debug_d3dpool(desc->pool),
5551
            desc->multisample_type, desc->multisample_quality, target, level, layer, flags, surface);
5552

5553 5554
    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
        return E_OUTOFMEMORY;
5555

5556
    if (FAILED(hr = surface_init(object, container, desc, target, level, layer, flags)))
5557 5558 5559 5560 5561 5562
    {
        WARN("Failed to initialize surface, returning %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

5563 5564
    if (FAILED(hr = device_parent->ops->surface_created(device_parent,
            wined3d_texture_get_parent(container), object, &parent, &parent_ops)))
5565 5566
    {
        WARN("Failed to create surface parent, hr %#x.\n", hr);
5567
        wined3d_surface_destroy(object);
5568 5569 5570 5571 5572 5573 5574
        return hr;
    }

    TRACE("Created surface %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);

    object->resource.parent = parent;
    object->resource.parent_ops = parent_ops;
5575 5576 5577 5578
    *surface = object;

    return hr;
}