[D3D8][D3D9][DDRAW][WINED3D] Sync with Wine Staging 2.9. This work couldn't have...
[reactos.git] / reactos / dll / directx / wine / wined3d / resource.c
index c142041..8b310d2 100644 (file)
@@ -55,6 +55,7 @@ static void resource_check_usage(DWORD usage)
             | WINED3DUSAGE_AUTOGENMIPMAP
             | WINED3DUSAGE_STATICDECL
             | WINED3DUSAGE_OVERLAY
+            | WINED3DUSAGE_PRIVATE
             | WINED3DUSAGE_LEGACY_CUBEMAP
             | WINED3DUSAGE_TEXTURE;
 
@@ -95,6 +96,7 @@ HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *
     resource_types[] =
     {
         {WINED3D_RTYPE_BUFFER,      0,                              WINED3D_GL_RES_TYPE_BUFFER},
+        {WINED3D_RTYPE_TEXTURE_1D,  0,                              WINED3D_GL_RES_TYPE_TEX_1D},
         {WINED3D_RTYPE_TEXTURE_2D,  0,                              WINED3D_GL_RES_TYPE_TEX_2D},
         {WINED3D_RTYPE_TEXTURE_2D,  0,                              WINED3D_GL_RES_TYPE_TEX_RECT},
         {WINED3D_RTYPE_TEXTURE_2D,  0,                              WINED3D_GL_RES_TYPE_RB},
@@ -211,92 +213,65 @@ HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *
             ERR("Failed to allocate system memory.\n");
             return E_OUTOFMEMORY;
         }
-#if defined(STAGING_CSMT)
-        resource->heap_memory = resource->map_heap_memory;
-#endif /* STAGING_CSMT */
     }
     else
     {
         resource->heap_memory = NULL;
     }
 
-    /* Check that we have enough video ram left */
-    if (pool == WINED3D_POOL_DEFAULT && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
+    if (!(usage & WINED3DUSAGE_PRIVATE))
     {
-        if (size > wined3d_device_get_available_texture_mem(device))
+        /* Check that we have enough video ram left */
+        if (pool == WINED3D_POOL_DEFAULT && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
         {
-            ERR("Out of adapter memory\n");
-            wined3d_resource_free_sysmem(resource);
-            return WINED3DERR_OUTOFVIDEOMEMORY;
+            if (size > wined3d_device_get_available_texture_mem(device))
+            {
+                ERR("Out of adapter memory\n");
+                wined3d_resource_free_sysmem(resource);
+                return WINED3DERR_OUTOFVIDEOMEMORY;
+            }
+            adapter_adjust_memory(device->adapter, size);
         }
-        adapter_adjust_memory(device->adapter, size);
-    }
 
-    device_resource_add(device, resource);
+        device_resource_add(device, resource);
+    }
 
     return WINED3D_OK;
 }
 
-#if defined(STAGING_CSMT)
-void wined3d_resource_free_bo(struct wined3d_resource *resource)
+static void wined3d_resource_destroy_object(void *object)
 {
-    struct wined3d_context *context = context_acquire(resource->device, NULL);
-
-    if (resource->buffer != resource->map_buffer)
-        ERR("Releasing resource buffer with buffer != map_buffer.\n");
-
-    wined3d_device_release_bo(resource->device, resource->buffer, context);
-    resource->buffer = NULL;
-    resource->map_buffer = NULL;
-
-    context_release(context);
-}
-
-void wined3d_resource_cleanup_cs(struct wined3d_resource *resource)
-{
-    context_resource_released(resource->device, resource, resource->type);
-
-    if (resource->buffer)
-        wined3d_resource_free_bo(resource);
+    struct wined3d_resource *resource = object;
 
     wined3d_resource_free_sysmem(resource);
-    resource->map_heap_memory = NULL;
+    context_resource_released(resource->device, resource, resource->type);
+    wined3d_resource_release(resource);
 }
 
-#endif /* STAGING_CSMT */
 void resource_cleanup(struct wined3d_resource *resource)
 {
     const struct wined3d *d3d = resource->device->wined3d;
 
     TRACE("Cleaning up resource %p.\n", resource);
 
-    if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
+    if (!(resource->usage & WINED3DUSAGE_PRIVATE))
     {
-        TRACE("Decrementing device memory pool by %u.\n", resource->size);
-        adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
-    }
-
-#if defined(STAGING_CSMT)
-    wined3d_cs_emit_resource_cleanup(resource->device->cs, resource);
-#else  /* STAGING_CSMT */
-    wined3d_resource_free_sysmem(resource);
-#endif /* STAGING_CSMT */
+        if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
+        {
+            TRACE("Decrementing device memory pool by %u.\n", resource->size);
+            adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
+        }
 
-    device_resource_released(resource->device, resource);
+        device_resource_released(resource->device, resource);
+    }
+    wined3d_resource_acquire(resource);
+    wined3d_cs_destroy_object(resource->device->cs, wined3d_resource_destroy_object, resource);
 }
 
 void resource_unload(struct wined3d_resource *resource)
 {
     if (resource->map_count)
         ERR("Resource %p is being unloaded while mapped.\n", resource);
-
-#if defined(STAGING_CSMT)
-    if (resource->buffer)
-        wined3d_resource_free_bo(resource);
-
-#endif /* STAGING_CSMT */
-    context_resource_unloaded(resource->device,
-            resource, resource->type);
 }
 
 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
@@ -345,20 +320,69 @@ void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, st
     desc->size = resource->size;
 }
 
-HRESULT CDECL wined3d_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
+static DWORD wined3d_resource_sanitise_map_flags(const struct wined3d_resource *resource, DWORD flags)
+{
+    /* Not all flags make sense together, but Windows never returns an error.
+     * Catch the cases that could cause issues. */
+    if (flags & WINED3D_MAP_READONLY)
+    {
+        if (flags & WINED3D_MAP_DISCARD)
+        {
+            WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
+            return 0;
+        }
+        if (flags & WINED3D_MAP_NOOVERWRITE)
+        {
+            WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
+            return 0;
+        }
+    }
+    else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
+            == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
+    {
+        WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
+        return 0;
+    }
+    else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
+            && !(resource->usage & WINED3DUSAGE_DYNAMIC))
+    {
+        WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
+        return 0;
+    }
+
+    return flags;
+}
+
+HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
         struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
 {
     TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
             resource, sub_resource_idx, map_desc, debug_box(box), flags);
 
-    return resource->resource_ops->resource_sub_resource_map(resource, sub_resource_idx, map_desc, box, flags);
+    flags = wined3d_resource_sanitise_map_flags(resource, flags);
+    wined3d_resource_wait_idle(resource);
+
+    return wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
+}
+
+HRESULT CDECL wined3d_resource_map_info(struct wined3d_resource *resource, unsigned int sub_resource_idx,
+        struct wined3d_map_info *info, DWORD flags)
+{
+    TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
+
+    return resource->resource_ops->resource_map_info(resource, sub_resource_idx, info, flags);
 }
 
-HRESULT CDECL wined3d_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
+HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
 {
     TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
 
-    return resource->resource_ops->resource_sub_resource_unmap(resource, sub_resource_idx);
+    return wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
+}
+
+void CDECL wined3d_resource_preload(struct wined3d_resource *resource)
+{
+    wined3d_cs_emit_preload_resource(resource->device->cs, resource);
 }
 
 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
@@ -373,11 +397,7 @@ BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
     p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
     *p = mem;
 
-#if defined(STAGING_CSMT)
-    resource->map_heap_memory = ++p;
-#else  /* STAGING_CSMT */
     resource->heap_memory = ++p;
-#endif /* STAGING_CSMT */
 
     return TRUE;
 }
@@ -393,39 +413,6 @@ void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
     resource->heap_memory = NULL;
 }
 
-DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
-{
-    /* Not all flags make sense together, but Windows never returns an error.
-     * Catch the cases that could cause issues. */
-    if (flags & WINED3D_MAP_READONLY)
-    {
-        if (flags & WINED3D_MAP_DISCARD)
-        {
-            WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
-            return 0;
-        }
-        if (flags & WINED3D_MAP_NOOVERWRITE)
-        {
-            WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
-            return 0;
-        }
-    }
-    else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
-            == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
-    {
-        WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
-        return 0;
-    }
-    else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
-            && !(resource->usage & WINED3DUSAGE_DYNAMIC))
-    {
-        WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
-        return 0;
-    }
-
-    return flags;
-}
-
 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
 {
     GLbitfield ret = 0;
@@ -443,11 +430,7 @@ GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
     return ret;
 }
 
-#if defined(STAGING_CSMT)
-static GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
-#else  /* STAGING_CSMT */
 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
-#endif /* STAGING_CSMT */
 {
     if (d3d_flags & WINED3D_MAP_READONLY)
         return GL_READ_ONLY_ARB;
@@ -465,7 +448,7 @@ BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
         return TRUE;
 
     /* Not on a swapchain - must be offscreen */
-    if (!(swapchain = wined3d_texture_from_resource(resource)->swapchain))
+    if (!(swapchain = texture_from_resource(resource)->swapchain))
         return TRUE;
 
     /* The front buffer is always onscreen */
@@ -488,579 +471,3 @@ void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
     else
         resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;
 }
-#if defined(STAGING_CSMT)
-
-void wined3d_resource_get_pitch(const struct wined3d_resource *resource, UINT *row_pitch,
-        UINT *slice_pitch)
-{
-    unsigned int alignment;
-    const struct wined3d_format *format = resource->format;
-
-    if (resource->custom_row_pitch)
-    {
-        *row_pitch = resource->custom_row_pitch;
-        *slice_pitch = resource->custom_slice_pitch;
-        return;
-    }
-
-    alignment = resource->device->surface_alignment;
-    *row_pitch = wined3d_format_calculate_pitch(resource->format, resource->width);
-    *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
-    if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BLOCKS)
-    {
-        /* Since compressed formats are block based, pitch means the amount of
-         * bytes to the next row of block rather than the next row of pixels. */
-        UINT slice_block_count = (resource->height + format->block_height - 1) / format->block_height;
-        *slice_pitch = *row_pitch * slice_block_count;
-    }
-    else
-    {
-        *slice_pitch = *row_pitch * resource->height;
-    }
-
-    TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
-}
-
-void wined3d_resource_validate_location(struct wined3d_resource *resource, DWORD location)
-{
-    TRACE("Resource %p, setting %s.\n", resource, wined3d_debug_location(location));
-    resource->locations |= location;
-    TRACE("new location flags are %s.\n", wined3d_debug_location(resource->locations));
-}
-
-void wined3d_resource_invalidate_location(struct wined3d_resource *resource, DWORD location)
-{
-    TRACE("Resource %p, setting %s.\n", resource, wined3d_debug_location(location));
-    resource->locations &= ~location;
-    TRACE("new location flags are %s.\n", wined3d_debug_location(resource->locations));
-
-    resource->resource_ops->resource_location_invalidated(resource, location);
-}
-
-DWORD wined3d_resource_access_from_location(DWORD location)
-{
-    switch (location)
-    {
-        case WINED3D_LOCATION_DISCARDED:
-            return 0;
-
-        case WINED3D_LOCATION_SYSMEM:
-        case WINED3D_LOCATION_USER_MEMORY:
-        case WINED3D_LOCATION_DIB:
-            return WINED3D_RESOURCE_ACCESS_CPU;
-
-        case WINED3D_LOCATION_BUFFER:
-        case WINED3D_LOCATION_TEXTURE_RGB:
-        case WINED3D_LOCATION_TEXTURE_SRGB:
-        case WINED3D_LOCATION_DRAWABLE:
-        case WINED3D_LOCATION_RB_MULTISAMPLE:
-        case WINED3D_LOCATION_RB_RESOLVED:
-            return WINED3D_RESOURCE_ACCESS_GPU;
-
-        default:
-            FIXME("Unhandled location %#x.\n", location);
-            return 0;
-    }
-}
-
-void wined3d_resource_get_memory(const struct wined3d_resource *resource,
-        DWORD location, struct wined3d_bo_address *data)
-{
-    if (location & WINED3D_LOCATION_BUFFER)
-    {
-        data->buffer_object = resource->buffer->name;
-        data->addr = NULL;
-        return;
-    }
-    if (location & WINED3D_LOCATION_USER_MEMORY)
-    {
-        data->buffer_object = 0;
-        data->addr = resource->user_memory;
-        return;
-    }
-    if (location & WINED3D_LOCATION_DIB)
-    {
-        data->buffer_object = 0;
-        data->addr = resource->bitmap_data;
-        return;
-    }
-    if (location & WINED3D_LOCATION_SYSMEM)
-    {
-        data->buffer_object = 0;
-        data->addr = resource->heap_memory;
-        return;
-    }
-    ERR("Unexpected location %s.\n", wined3d_debug_location(location));
-}
-
-/* Context activation is optionally by the caller. Context may be NULL. */
-static void wined3d_resource_copy_simple_location(struct wined3d_resource *resource,
-        struct wined3d_context *context, DWORD location)
-{
-    const struct wined3d_gl_info *gl_info;
-    struct wined3d_bo_address dst, src;
-    UINT size = resource->size;
-
-    wined3d_resource_get_memory(resource, location, &dst);
-    wined3d_resource_get_memory(resource, resource->locations, &src);
-
-    if (dst.buffer_object)
-    {
-        gl_info = context->gl_info;
-        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));
-        checkGLcall("Upload PBO");
-        return;
-    }
-    if (src.buffer_object)
-    {
-        gl_info = context->gl_info;
-        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));
-        checkGLcall("Download PBO");
-        return;
-    }
-    memcpy(dst.addr, src.addr, size);
-}
-
-/* Context activation is optionally by the caller. Context may be NULL. */
-void wined3d_resource_load_location(struct wined3d_resource *resource,
-        struct wined3d_context *context, DWORD location)
-{
-    DWORD required_access = wined3d_resource_access_from_location(location);
-    DWORD simple_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
-            | WINED3D_LOCATION_DIB | WINED3D_LOCATION_BUFFER;
-
-    if ((resource->locations & location) == location)
-    {
-        TRACE("Location(s) already up to date.\n");
-        return;
-    }
-
-    /* Keep this a WARN for now until surfaces are cleaned up. */
-    if ((resource->access_flags & required_access) != required_access)
-        WARN("Operation requires %#x access, but resource only has %#x.\n",
-                required_access, resource->access_flags);
-
-    if (location & simple_locations)
-    {
-        if (resource->locations & WINED3D_LOCATION_DISCARDED)
-        {
-            TRACE("Resource was discarded, nothing to do.\n");
-            resource->locations |= location;
-            return;
-        }
-        if (resource->locations & simple_locations)
-        {
-            wined3d_resource_copy_simple_location(resource, context, location);
-            resource->locations |= location;
-            return;
-        }
-    }
-
-    /* Context is NULL in ddraw-only operation without OpenGL. */
-    if (!context)
-        ERR("A context is required for non-sysmem operation.\n");
-
-    resource->resource_ops->resource_load_location(resource, context, location);
-}
-
-BYTE *wined3d_resource_get_map_ptr(const struct wined3d_resource *resource,
-        const struct wined3d_context *context, DWORD flags)
-{
-    const struct wined3d_gl_info *gl_info;
-    BYTE *ptr;
-
-    switch (resource->map_binding)
-    {
-        case WINED3D_LOCATION_BUFFER:
-            gl_info = context->gl_info;
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, resource->map_buffer->name));
-
-            if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
-            {
-                GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
-                mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
-                ptr = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
-                        0, resource->size, mapflags));
-            }
-            else
-            {
-                GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
-                ptr = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
-            }
-
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
-            checkGLcall("Map GL buffer");
-            return ptr;
-
-        case WINED3D_LOCATION_SYSMEM:
-            return resource->map_heap_memory;
-
-        case WINED3D_LOCATION_DIB:
-            return resource->bitmap_data;
-
-        case WINED3D_LOCATION_USER_MEMORY:
-            return resource->user_memory;
-
-        default:
-            ERR("Unexpected map binding %s.\n", wined3d_debug_location(resource->map_binding));
-            return NULL;
-    }
-}
-
-void wined3d_resource_release_map_ptr(const struct wined3d_resource *resource,
-        const struct wined3d_context *context)
-{
-    const struct wined3d_gl_info *gl_info;
-
-    switch (resource->map_binding)
-    {
-        case WINED3D_LOCATION_BUFFER:
-            gl_info = context->gl_info;
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, resource->map_buffer->name));
-            GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
-            checkGLcall("Unmap GL buffer");
-            return;
-
-        case WINED3D_LOCATION_SYSMEM:
-        case WINED3D_LOCATION_DIB:
-        case WINED3D_LOCATION_USER_MEMORY:
-            return;
-
-        default:
-            ERR("Unexpected map binding %s.\n", wined3d_debug_location(resource->map_binding));
-            return;
-    }
-}
-
-/* Context activation is done by the caller. */
-static void wined3d_resource_prepare_bo(struct wined3d_resource *resource, struct wined3d_context *context)
-{
-    if (resource->buffer)
-        return;
-
-    resource->buffer = wined3d_device_get_bo(resource->device, resource->size,
-            GL_STREAM_DRAW, GL_PIXEL_UNPACK_BUFFER, context);
-    resource->map_buffer = resource->buffer;
-    TRACE("Created GL buffer %u for resource %p.\n", resource->buffer->name, resource);
-    resource->map_heap_memory = NULL;
-}
-
-BOOL wined3d_resource_prepare_system_memory(struct wined3d_resource *resource)
-{
-    if (resource->heap_memory)
-        return TRUE;
-
-    if (!wined3d_resource_allocate_sysmem(resource))
-    {
-        ERR("Failed to allocate system memory.\n");
-        return FALSE;
-    }
-    resource->heap_memory = resource->map_heap_memory;
-    return TRUE;
-}
-
-/* Context activation is done by the caller. */
-BOOL wined3d_resource_prepare_map_memory(struct wined3d_resource *resource, struct wined3d_context *context)
-{
-    switch (resource->map_binding)
-    {
-        case WINED3D_LOCATION_BUFFER:
-            wined3d_resource_prepare_bo(resource, context);
-            return TRUE;
-
-        case WINED3D_LOCATION_SYSMEM:
-            return wined3d_resource_prepare_system_memory(resource);
-
-        case WINED3D_LOCATION_USER_MEMORY:
-            if (!resource->user_memory)
-                ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but resource->user_memory is NULL.\n");
-            return TRUE;
-
-        case WINED3D_LOCATION_DIB:
-            if (!resource->bitmap_data)
-                ERR("Map binding is set to WINED3D_LOCATION_DIB but resource->bitmap_data is NULL.\n");
-            return TRUE;
-
-        default:
-            ERR("Unexpected map binding %s.\n", wined3d_debug_location(resource->map_binding));
-            return FALSE;
-    }
-}
-
-BOOL wined3d_resource_check_block_align(const struct wined3d_resource *resource,
-        const struct wined3d_box *box)
-{
-    UINT width_mask, height_mask;
-    const struct wined3d_format *format = resource->format;
-
-    if (!box)
-        return TRUE;
-
-    /* This assumes power of two block sizes, but NPOT block sizes would be
-     * silly anyway.
-     *
-     * This also assumes that the format's block depth is 1. */
-    width_mask = format->block_width - 1;
-    height_mask = format->block_height - 1;
-
-    if (box->left & width_mask)
-        return FALSE;
-    if (box->top & height_mask)
-        return FALSE;
-    if (box->right & width_mask && box->right != resource->width)
-        return FALSE;
-    if (box->bottom & height_mask && box->bottom != resource->height)
-        return FALSE;
-
-    return TRUE;
-}
-
-void *wined3d_resource_map_internal(struct wined3d_resource *resource, DWORD flags)
-{
-    struct wined3d_device *device = resource->device;
-    struct wined3d_context *context = NULL;
-    void *mem;
-
-    if (device->d3d_initialized)
-        context = context_acquire(device, NULL);
-
-    if (!wined3d_resource_prepare_map_memory(resource, context))
-    {
-        WARN("Out of memory.\n");
-        context_release(context);
-        return NULL;
-    }
-
-    if (flags & WINED3D_MAP_DISCARD)
-    {
-        switch (resource->map_binding)
-        {
-            case WINED3D_LOCATION_BUFFER:
-                resource->map_buffer = wined3d_device_get_bo(device, resource->size,
-                        GL_STREAM_DRAW, GL_PIXEL_UNPACK_BUFFER, context);
-                break;
-
-            case WINED3D_LOCATION_SYSMEM:
-                wined3d_resource_allocate_sysmem(resource);
-                break;
-
-            default:
-                if (resource->access_fence)
-                    ERR("Location %s does not support DISCARD maps.\n",
-                            wined3d_debug_location(resource->map_binding));
-                if (resource->pool != WINED3D_POOL_DEFAULT)
-                    FIXME("Discard used on %s pool resource.\n", debug_d3dpool(resource->pool));
-        }
-        wined3d_resource_validate_location(resource, resource->map_binding);
-    }
-    else
-    {
-        wined3d_resource_load_location(resource, context, resource->map_binding);
-    }
-
-    mem = wined3d_resource_get_map_ptr(resource, context, flags);
-
-    if (context)
-        context_release(context);
-
-    return mem;
-}
-
-static void wined3d_resource_sync(struct wined3d_resource *resource)
-{
-    struct wined3d_resource *real_res = resource;
-    struct wined3d_surface *surface;
-    struct wined3d_volume *volume;
-
-    switch (resource->type)
-    {
-        case WINED3D_RTYPE_SURFACE:
-            surface = surface_from_resource(resource);
-            if (surface->container)
-                real_res = &surface->container->resource;
-            break;
-
-        case WINED3D_RTYPE_VOLUME:
-            volume = volume_from_resource(resource);
-            real_res = &volume->container->resource;
-            break;
-
-        default:
-            break;
-    }
-    wined3d_resource_wait_fence(real_res);
-}
-
-HRESULT wined3d_resource_map(struct wined3d_resource *resource,
-        struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
-{
-    struct wined3d_device *device = resource->device;
-    BYTE *base_memory;
-    const struct wined3d_format *format = resource->format;
-    const unsigned int fmt_flags = resource->format->flags[WINED3D_GL_RES_TYPE_TEX_2D];
-
-    TRACE("resource %p, map_desc %p, box %s, flags %#x.\n",
-            resource, map_desc, debug_box(box), flags);
-
-    if (resource->usage & WINED3DUSAGE_RENDERTARGET && wined3d_settings.ignore_rt_map)
-    {
-        WARN("Ignoring render target map, only finishing CS.\n");
-        wined3d_cs_emit_glfinish(device->cs);
-        map_desc->row_pitch = 0;
-        map_desc->slice_pitch = 0;
-        map_desc->data = NULL;
-        device->cs->ops->finish(device->cs);
-        return WINED3D_OK;
-    }
-
-    if (resource->map_count)
-    {
-        WARN("Volume is already mapped.\n");
-        return WINED3DERR_INVALIDCALL;
-    }
-
-    flags = wined3d_resource_sanitize_map_flags(resource, flags);
-
-    if (flags & WINED3D_MAP_NOOVERWRITE)
-        FIXME("WINED3D_MAP_NOOVERWRITE are not implemented yet.\n");
-
-    if (flags & WINED3D_MAP_DISCARD)
-    {
-        switch (resource->map_binding)
-        {
-            case WINED3D_LOCATION_BUFFER:
-            case WINED3D_LOCATION_SYSMEM:
-                break;
-
-            default:
-                FIXME("Implement discard maps with %s map binding.\n",
-                        wined3d_debug_location(resource->map_binding));
-                wined3d_resource_sync(resource);
-        }
-    }
-    else
-        wined3d_resource_sync(resource);
-
-    base_memory = wined3d_cs_emit_resource_map(device->cs, resource, flags);
-    if (!base_memory)
-    {
-        WARN("Map failed.\n");
-        return WINED3DERR_INVALIDCALL;
-    }
-
-    TRACE("Base memory pointer %p.\n", base_memory);
-
-    if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
-    {
-        map_desc->row_pitch = resource->width * format->byte_count;
-        map_desc->slice_pitch = map_desc->row_pitch * resource->height;
-    }
-    else
-    {
-        wined3d_resource_get_pitch(resource, &map_desc->row_pitch, &map_desc->slice_pitch);
-    }
-
-    if (!box)
-    {
-        map_desc->data = base_memory;
-    }
-    else
-    {
-        if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
-        {
-            /* Compressed textures are block based, so calculate the offset of
-             * the block that contains the top-left pixel of the locked rectangle. */
-            map_desc->data = base_memory
-                    + (box->front * map_desc->slice_pitch)
-                    + ((box->top / format->block_height) * map_desc->row_pitch)
-                    + ((box->left / format->block_width) * format->block_byte_count);
-        }
-        else
-        {
-            map_desc->data = base_memory
-                    + (map_desc->slice_pitch * box->front)
-                    + (map_desc->row_pitch * box->top)
-                    + (box->left * format->byte_count);
-        }
-    }
-
-    if (!(flags & WINED3D_MAP_READONLY))
-        resource->unmap_dirtify = TRUE;
-
-    resource->map_count++;
-
-    TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
-            map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
-
-    return WINED3D_OK;
-}
-
-void wined3d_resource_unmap_internal(struct wined3d_resource *resource)
-{
-    struct wined3d_device *device = resource->device;
-    struct wined3d_context *context = NULL;
-
-    if (device->d3d_initialized)
-        context = context_acquire(device, NULL);
-    wined3d_resource_release_map_ptr(resource, context);
-    if (context)
-        context_release(context);
-}
-
-HRESULT wined3d_resource_unmap(struct wined3d_resource *resource)
-{
-    struct wined3d_device *device = resource->device;
-    TRACE("resource %p.\n", resource);
-
-    if (resource->usage & WINED3DUSAGE_RENDERTARGET && wined3d_settings.ignore_rt_map)
-    {
-        WARN("Ignoring render target unmap.\n");
-        return WINED3D_OK;
-    }
-
-    if (!resource->map_count)
-    {
-        WARN("Trying to unlock an unlocked resource %p.\n", resource);
-        return WINEDDERR_NOTLOCKED;
-    }
-
-    wined3d_cs_emit_resource_unmap(device->cs, resource);
-
-    if (resource->unmap_dirtify)
-    {
-        wined3d_cs_emit_resource_changed(device->cs, resource,
-                resource->map_buffer, resource->map_heap_memory);
-    }
-    resource->unmap_dirtify = FALSE;
-
-    resource->map_count--;
-
-    return WINED3D_OK;
-}
-
-void wined3d_resource_changed(struct wined3d_resource *resource, struct wined3d_gl_bo *swap_buffer,
-        void *swap_heap_memory)
-{
-    struct wined3d_device *device = resource->device;
-
-    if (swap_buffer && swap_buffer != resource->buffer)
-    {
-        struct wined3d_context *context = context_acquire(device, NULL);
-        wined3d_device_release_bo(device, resource->buffer, context);
-        context_release(context);
-        resource->buffer = swap_buffer;
-    }
-    if (swap_heap_memory && swap_heap_memory != resource->heap_memory)
-    {
-        wined3d_resource_free_sysmem(resource);
-        resource->heap_memory = swap_heap_memory;
-    }
-
-    wined3d_resource_invalidate_location(resource, ~resource->map_binding);
-}
-#endif /* STAGING_CSMT */