From 62ca4f38269139ef2a8b9842ec538d9e7f7a0e76 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Fri, 21 Apr 2017 00:57:10 +0200 Subject: [PATCH] wined3d: Introduce context functions to map/unmap a wined3d_bo_address. Signed-off-by: Henri Verbeet Signed-off-by: Alexandre Julliard --- dlls/wined3d/context.c | 42 +++++++++++++++++++++ dlls/wined3d/surface.c | 54 ++++++++++++--------------- dlls/wined3d/texture.c | 68 +++------------------------------- dlls/wined3d/wined3d_private.h | 8 ++-- 4 files changed, 74 insertions(+), 98 deletions(-) diff --git a/dlls/wined3d/context.c b/dlls/wined3d/context.c index 3e5e1d03f0b..5a899e56d1f 100644 --- a/dlls/wined3d/context.c +++ b/dlls/wined3d/context.c @@ -2581,6 +2581,48 @@ void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint } } +void *context_map_bo_address(struct wined3d_context *context, + const struct wined3d_bo_address *data, size_t size, GLenum binding, DWORD flags) +{ + const struct wined3d_gl_info *gl_info = context->gl_info; + BYTE *memory; + + if (!data->buffer_object) + return data->addr; + + context_bind_bo(context, binding, data->buffer_object); + + if (gl_info->supported[ARB_MAP_BUFFER_RANGE]) + { + GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT; + memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags)); + } + else + { + memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags))); + memory += (INT_PTR)data->addr; + } + + context_bind_bo(context, binding, 0); + checkGLcall("Map buffer object"); + + return memory; +} + +void context_unmap_bo_address(struct wined3d_context *context, + const struct wined3d_bo_address *data, GLenum binding) +{ + const struct wined3d_gl_info *gl_info = context->gl_info; + + if (!data->buffer_object) + return; + + context_bind_bo(context, binding, data->buffer_object); + GL_EXTCALL(glUnmapBuffer(binding)); + context_bind_bo(context, binding, 0); + checkGLcall("Unmap buffer object"); +} + static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen) { if (context->render_offscreen == offscreen) return; diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c index f2fbf21aa93..9760f7242c6 100644 --- a/dlls/wined3d/surface.c +++ b/dlls/wined3d/surface.c @@ -1314,16 +1314,16 @@ static struct wined3d_texture *surface_convert_format(struct wined3d_texture *sr wined3d_texture_get_pitch(dst_texture, 0, &dst_row_pitch, &dst_slice_pitch); wined3d_texture_get_memory(dst_texture, 0, &dst_data, map_binding); - src = wined3d_texture_map_bo_address(&src_data, src_texture->sub_resources[sub_resource_idx].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); - dst = wined3d_texture_map_bo_address(&dst_data, dst_texture->sub_resources[0].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); + src = context_map_bo_address(context, &src_data, + src_texture->sub_resources[sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0); + dst = context_map_bo_address(context, + &dst_data, dst_texture->sub_resources[0].size, GL_PIXEL_UNPACK_BUFFER, 0); conv->convert(src, dst, src_row_pitch, dst_row_pitch, desc.width, desc.height); wined3d_texture_invalidate_location(dst_texture, 0, ~map_binding); - wined3d_texture_unmap_bo_address(&dst_data, gl_info, GL_PIXEL_UNPACK_BUFFER); - wined3d_texture_unmap_bo_address(&src_data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &dst_data, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &src_data, GL_PIXEL_UNPACK_BUFFER); } else { @@ -2278,8 +2278,8 @@ static BOOL surface_load_texture(struct wined3d_surface *surface, format.byte_count = format.conv_byte_count; wined3d_format_calculate_pitch(&format, 1, width, height, &dst_row_pitch, &dst_slice_pitch); - src_mem = wined3d_texture_map_bo_address(&data, src_slice_pitch, - gl_info, GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READONLY); + src_mem = context_map_bo_address(context, &data, src_slice_pitch, + GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READONLY); if (!(dst_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch))) { ERR("Out of memory (%u).\n", dst_slice_pitch); @@ -2289,7 +2289,7 @@ static BOOL surface_load_texture(struct wined3d_surface *surface, format.convert(src_mem, dst_mem, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, width, height, 1); src_row_pitch = dst_row_pitch; - wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER); data.buffer_object = 0; data.addr = dst_mem; @@ -2302,8 +2302,8 @@ static BOOL surface_load_texture(struct wined3d_surface *surface, wined3d_format_calculate_pitch(&format, device->surface_alignment, width, height, &dst_row_pitch, &dst_slice_pitch); - src_mem = wined3d_texture_map_bo_address(&data, src_slice_pitch, - gl_info, GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READONLY); + src_mem = context_map_bo_address(context, &data, src_slice_pitch, + GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READONLY); if (!(dst_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch))) { ERR("Out of memory (%u).\n", dst_slice_pitch); @@ -2315,7 +2315,7 @@ static BOOL surface_load_texture(struct wined3d_surface *surface, conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch, width, height, palette, &texture->async.gl_color_key); src_row_pitch = dst_row_pitch; - wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER); data.buffer_object = 0; data.addr = dst_mem; @@ -2875,7 +2875,6 @@ static HRESULT surface_cpu_blt(struct wined3d_texture *dst_texture, unsigned int struct wined3d_device *device = dst_texture->resource.device; const struct wined3d_format *src_format, *dst_format; struct wined3d_texture *converted_texture = NULL; - const struct wined3d_gl_info *gl_info = NULL; struct wined3d_bo_address src_data, dst_data; unsigned int src_fmt_flags, dst_fmt_flags; struct wined3d_map_desc dst_map, src_map; @@ -2895,10 +2894,7 @@ static HRESULT surface_cpu_blt(struct wined3d_texture *dst_texture, unsigned int src_sub_resource_idx, debug_box(src_box), flags, fx, debug_d3dtexturefiltertype(filter)); if (device->d3d_initialized) - { context = context_acquire(device, NULL, 0); - gl_info = context->gl_info; - } if (src_texture == dst_texture && src_sub_resource_idx == dst_sub_resource_idx) { @@ -2911,8 +2907,8 @@ static HRESULT surface_cpu_blt(struct wined3d_texture *dst_texture, unsigned int wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~map_binding); wined3d_texture_get_pitch(dst_texture, texture_level, &dst_map.row_pitch, &dst_map.slice_pitch); wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &dst_data, map_binding); - dst_map.data = wined3d_texture_map_bo_address(&dst_data, dst_texture->sub_resources[dst_sub_resource_idx].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); + dst_map.data = context_map_bo_address(context, &dst_data, + dst_texture->sub_resources[dst_sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0); src_map = dst_map; src_format = dst_texture->resource.format; @@ -2945,8 +2941,8 @@ static HRESULT surface_cpu_blt(struct wined3d_texture *dst_texture, unsigned int ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(map_binding)); wined3d_texture_get_pitch(src_texture, texture_level, &src_map.row_pitch, &src_map.slice_pitch); wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &src_data, map_binding); - src_map.data = wined3d_texture_map_bo_address(&src_data, src_texture->sub_resources[src_sub_resource_idx].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); + src_map.data = context_map_bo_address(context, &src_data, + src_texture->sub_resources[src_sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0); map_binding = dst_texture->resource.map_binding; texture_level = dst_sub_resource_idx % dst_texture->level_count; @@ -2955,8 +2951,8 @@ static HRESULT surface_cpu_blt(struct wined3d_texture *dst_texture, unsigned int wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~map_binding); wined3d_texture_get_pitch(dst_texture, texture_level, &dst_map.row_pitch, &dst_map.slice_pitch); wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &dst_data, map_binding); - dst_map.data = wined3d_texture_map_bo_address(&dst_data, dst_texture->sub_resources[dst_sub_resource_idx].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); + dst_map.data = context_map_bo_address(context, &dst_data, + dst_texture->sub_resources[dst_sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0); } bpp = dst_format->byte_count; @@ -3331,9 +3327,9 @@ error: FIXME(" Unsupported flags %#x.\n", flags); release: - wined3d_texture_unmap_bo_address(&dst_data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &dst_data, GL_PIXEL_UNPACK_BUFFER); if (!same_sub_resource) - wined3d_texture_unmap_bo_address(&src_data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &src_data, GL_PIXEL_UNPACK_BUFFER); if (converted_texture) wined3d_texture_decref(converted_texture); if (context) @@ -3346,7 +3342,6 @@ static void surface_cpu_blt_colour_fill(struct wined3d_rendertarget_view *view, const struct wined3d_box *box, const struct wined3d_color *colour) { struct wined3d_device *device = view->resource->device; - const struct wined3d_gl_info *gl_info = NULL; struct wined3d_context *context = NULL; struct wined3d_texture *texture; struct wined3d_bo_address data; @@ -3371,10 +3366,7 @@ static void surface_cpu_blt_colour_fill(struct wined3d_rendertarget_view *view, } if (device->d3d_initialized) - { context = context_acquire(device, NULL, 0); - gl_info = context->gl_info; - } c = wined3d_format_convert_from_float(view->format, colour); bpp = view->format->byte_count; @@ -3389,8 +3381,8 @@ static void surface_cpu_blt_colour_fill(struct wined3d_rendertarget_view *view, wined3d_texture_get_pitch(texture, view->sub_resource_idx % texture->level_count, &map.row_pitch, &map.slice_pitch); wined3d_texture_get_memory(texture, view->sub_resource_idx, &data, map_binding); - map.data = wined3d_texture_map_bo_address(&data, texture->sub_resources[view->sub_resource_idx].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); + map.data = context_map_bo_address(context, &data, + texture->sub_resources[view->sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0); map.data = (BYTE *)map.data + (box->front * map.slice_pitch) + ((box->top / view->format->block_height) * map.row_pitch) @@ -3443,7 +3435,7 @@ static void surface_cpu_blt_colour_fill(struct wined3d_rendertarget_view *view, memcpy(row, map.data, w * bpp); } - wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER); if (context) context_release(context); } diff --git a/dlls/wined3d/texture.c b/dlls/wined3d/texture.c index 2419616731c..dbfdc3fba56 100644 --- a/dlls/wined3d/texture.c +++ b/dlls/wined3d/texture.c @@ -275,47 +275,6 @@ BOOL wined3d_texture_load_location(struct wined3d_texture *texture, return ret; } -/* Context activation is done by the caller. */ -void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size, - const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags) -{ - BYTE *memory; - - if (!data->buffer_object) - return data->addr; - - GL_EXTCALL(glBindBuffer(binding, data->buffer_object)); - - if (gl_info->supported[ARB_MAP_BUFFER_RANGE]) - { - GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT; - memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags)); - } - else - { - memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags))); - memory += (INT_PTR)data->addr; - } - - GL_EXTCALL(glBindBuffer(binding, 0)); - checkGLcall("Map buffer object"); - - return memory; -} - -/* Context activation is done by the caller. */ -void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data, - const struct wined3d_gl_info *gl_info, GLenum binding) -{ - if (!data->buffer_object) - return; - - GL_EXTCALL(glBindBuffer(binding, data->buffer_object)); - GL_EXTCALL(glUnmapBuffer(binding)); - GL_EXTCALL(glBindBuffer(binding, 0)); - checkGLcall("Unmap buffer object"); -} - void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx, struct wined3d_bo_address *data, DWORD locations) { @@ -1210,7 +1169,6 @@ HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture, static void texture2d_create_dc(void *object) { - const struct wined3d_gl_info *gl_info = NULL; struct wined3d_surface *surface = object; struct wined3d_context *context = NULL; const struct wined3d_format *format; @@ -1236,17 +1194,14 @@ static void texture2d_create_dc(void *object) } if (device->d3d_initialized) - { context = context_acquire(device, NULL, 0); - gl_info = context->gl_info; - } wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding); wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding); wined3d_texture_get_pitch(texture, surface->texture_level, &row_pitch, &slice_pitch); wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding); - desc.pMemory = wined3d_texture_map_bo_address(&data, texture->sub_resources[sub_resource_idx].size, - gl_info, GL_PIXEL_UNPACK_BUFFER, 0); + desc.pMemory = context_map_bo_address(context, &data, + texture->sub_resources[sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0); if (context) context_release(context); @@ -1274,7 +1229,6 @@ static void texture2d_create_dc(void *object) static void texture2d_destroy_dc(void *object) { - const struct wined3d_gl_info *gl_info = NULL; struct wined3d_surface *surface = object; D3DKMT_DESTROYDCFROMMEMORY destroy_desc; struct wined3d_context *context = NULL; @@ -1304,13 +1258,10 @@ static void texture2d_destroy_dc(void *object) surface->bitmap = NULL; if (device->d3d_initialized) - { context = context_acquire(device, NULL, 0); - gl_info = context->gl_info; - } wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding); - wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER); if (context) context_release(context); @@ -1891,7 +1842,6 @@ static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resour struct wined3d_texture_sub_resource *sub_resource; struct wined3d_device *device = resource->device; unsigned int fmt_flags = resource->format_flags; - const struct wined3d_gl_info *gl_info = NULL; struct wined3d_context *context = NULL; struct wined3d_texture *texture; struct wined3d_bo_address data; @@ -1935,10 +1885,7 @@ static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resour } if (device->d3d_initialized) - { context = context_acquire(device, NULL, 0); - gl_info = context->gl_info; - } if (flags & WINED3D_MAP_DISCARD) { @@ -1966,8 +1913,7 @@ static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resour wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding); wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding); - base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size, - gl_info, GL_PIXEL_UNPACK_BUFFER, flags); + base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags); TRACE("Base memory pointer %p.\n", base_memory); if (context) @@ -2031,7 +1977,6 @@ static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *reso { struct wined3d_texture_sub_resource *sub_resource; struct wined3d_device *device = resource->device; - const struct wined3d_gl_info *gl_info = NULL; struct wined3d_context *context = NULL; struct wined3d_texture *texture; struct wined3d_bo_address data; @@ -2051,13 +1996,10 @@ static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *reso } if (device->d3d_initialized) - { context = context_acquire(device, NULL, 0); - gl_info = context->gl_info; - } wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding); - wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER); + context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER); if (context) context_release(context); diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h index b5d29074cdb..b8ee4139905 100644 --- a/dlls/wined3d/wined3d_private.h +++ b/dlls/wined3d/wined3d_private.h @@ -1966,6 +1966,8 @@ void context_gl_resource_released(struct wined3d_device *device, GLuint name, BOOL rb_namespace) DECLSPEC_HIDDEN; void context_invalidate_compute_state(struct wined3d_context *context, DWORD state_id) DECLSPEC_HIDDEN; void context_invalidate_state(struct wined3d_context *context, DWORD state_id) DECLSPEC_HIDDEN; +void *context_map_bo_address(struct wined3d_context *context, const struct wined3d_bo_address *data, + size_t size, GLenum binding, DWORD flags) DECLSPEC_HIDDEN; struct wined3d_context *context_reacquire(const struct wined3d_device *device, struct wined3d_context *context) DECLSPEC_HIDDEN; void context_release(struct wined3d_context *context) DECLSPEC_HIDDEN; @@ -1980,6 +1982,8 @@ void context_state_drawbuf(struct wined3d_context *context, void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id) DECLSPEC_HIDDEN; void context_surface_update(struct wined3d_context *context, const struct wined3d_surface *surface) DECLSPEC_HIDDEN; +void context_unmap_bo_address(struct wined3d_context *context, + const struct wined3d_bo_address *data, GLenum binding) DECLSPEC_HIDDEN; /***************************************************************************** * Internal representation of a light @@ -2993,8 +2997,6 @@ void wined3d_texture_load(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb) DECLSPEC_HIDDEN; BOOL wined3d_texture_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location) DECLSPEC_HIDDEN; -void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size, - const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags) DECLSPEC_HIDDEN; BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location) DECLSPEC_HIDDEN; void wined3d_texture_prepare_texture(struct wined3d_texture *texture, @@ -3002,8 +3004,6 @@ void wined3d_texture_prepare_texture(struct wined3d_texture *texture, void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding) DECLSPEC_HIDDEN; void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain) DECLSPEC_HIDDEN; -void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data, - const struct wined3d_gl_info *gl_info, GLenum binding) DECLSPEC_HIDDEN; void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx, const struct wined3d_context *context, const struct wined3d_box *box, const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch) DECLSPEC_HIDDEN;