vkd3d: Make resource mapping thread-safe.

VkDeviceMemory must be externally synchronized.

Signed-off-by: Józef Kucia <jkucia@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Józef Kucia 2019-06-20 21:08:09 +02:00 committed by Alexandre Julliard
parent fb8f0dc5b3
commit dc074af2a0
2 changed files with 57 additions and 59 deletions

View File

@ -411,7 +411,8 @@ struct d3d12_heap *unsafe_impl_from_ID3D12Heap(ID3D12Heap *iface)
return impl_from_ID3D12Heap(iface); return impl_from_ID3D12Heap(iface);
} }
static HRESULT d3d12_heap_map(struct d3d12_heap *heap, uint64_t offset, void **data) static HRESULT d3d12_heap_map(struct d3d12_heap *heap, uint64_t offset,
struct d3d12_resource *resource, void **data)
{ {
struct d3d12_device *device = heap->device; struct d3d12_device *device = heap->device;
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -425,29 +426,41 @@ static HRESULT d3d12_heap_map(struct d3d12_heap *heap, uint64_t offset, void **d
return hresult_from_errno(rc); return hresult_from_errno(rc);
} }
if (!heap->map_ptr) assert(!resource->map_count || heap->map_ptr);
if (!resource->map_count)
{ {
const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs; if (!heap->map_ptr)
TRACE("Mapping heap %p.\n", heap);
if ((vr = VK_CALL(vkMapMemory(device->vk_device, heap->vk_memory,
0, VK_WHOLE_SIZE, 0, &heap->map_ptr))) < 0)
{ {
WARN("Failed to map device memory, vr %d.\n", vr); const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs;
heap->map_ptr = NULL;
TRACE("Mapping heap %p.\n", heap);
assert(!heap->map_count);
if ((vr = VK_CALL(vkMapMemory(device->vk_device, heap->vk_memory,
0, VK_WHOLE_SIZE, 0, &heap->map_ptr))) < 0)
{
WARN("Failed to map device memory, vr %d.\n", vr);
heap->map_ptr = NULL;
}
hr = hresult_from_vk_result(vr);
} }
hr = hresult_from_vk_result(vr); if (heap->map_ptr)
++heap->map_count;
} }
if (heap->map_ptr) if (hr == S_OK)
{ {
assert(heap->map_ptr);
*data = (BYTE *)heap->map_ptr + offset; *data = (BYTE *)heap->map_ptr + offset;
++heap->map_count; ++resource->map_count;
} }
else else
{ {
assert(!heap->map_ptr);
*data = NULL; *data = NULL;
} }
@ -456,7 +469,7 @@ static HRESULT d3d12_heap_map(struct d3d12_heap *heap, uint64_t offset, void **d
return hr; return hr;
} }
static void d3d12_heap_unmap(struct d3d12_heap *heap) static void d3d12_heap_unmap(struct d3d12_heap *heap, struct d3d12_resource *resource)
{ {
struct d3d12_device *device = heap->device; struct d3d12_device *device = heap->device;
int rc; int rc;
@ -467,24 +480,34 @@ static void d3d12_heap_unmap(struct d3d12_heap *heap)
return; return;
} }
if (heap->map_count) if (!resource->map_count)
{ {
--heap->map_count; WARN("Resource %p is not mapped.\n", resource);
if (!heap->map_count) goto done;
{
const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs;
TRACE("Unmapping heap %p.\n", heap);
VK_CALL(vkUnmapMemory(device->vk_device, heap->vk_memory));
heap->map_ptr = NULL;
}
}
else
{
WARN("Heap %p is not mapped.\n", heap);
} }
--resource->map_count;
if (resource->map_count)
goto done;
if (!heap->map_count)
{
ERR("Heap %p is not mapped.\n", heap);
goto done;
}
--heap->map_count;
if (!heap->map_count)
{
const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs;
TRACE("Unmapping heap %p, ptr %p.\n", heap, heap->map_ptr);
VK_CALL(vkUnmapMemory(device->vk_device, heap->vk_memory));
heap->map_ptr = NULL;
}
done:
pthread_mutex_unlock(&heap->mutex); pthread_mutex_unlock(&heap->mutex);
} }
@ -1087,7 +1110,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_resource_Map(ID3D12Resource *iface, UINT
{ {
struct d3d12_resource *resource = impl_from_ID3D12Resource(iface); struct d3d12_resource *resource = impl_from_ID3D12Resource(iface);
unsigned int sub_resource_count; unsigned int sub_resource_count;
HRESULT hr = S_OK; HRESULT hr;
TRACE("iface %p, sub_resource %u, read_range %p, data %p.\n", TRACE("iface %p, sub_resource %u, read_range %p, data %p.\n",
iface, sub_resource, read_range, data); iface, sub_resource, read_range, data);
@ -1120,20 +1143,10 @@ static HRESULT STDMETHODCALLTYPE d3d12_resource_Map(ID3D12Resource *iface, UINT
WARN("Ignoring read range %p.\n", read_range); WARN("Ignoring read range %p.\n", read_range);
if (!resource->map_count) if (FAILED(hr = d3d12_heap_map(resource->heap, resource->heap_offset, resource, data)))
{ WARN("Failed to map resource %p, hr %#x.\n", resource, hr);
if FAILED(hr = d3d12_heap_map(resource->heap, resource->heap_offset, &resource->map_ptr))
{
WARN("Failed to map resource, hr %#x.\n", hr);
resource->map_ptr = NULL;
}
}
if (resource->map_ptr) TRACE("Returning pointer %p.\n", *data);
{
*data = resource->map_ptr;
++resource->map_count;
}
return hr; return hr;
} }
@ -1154,22 +1167,9 @@ static void STDMETHODCALLTYPE d3d12_resource_Unmap(ID3D12Resource *iface, UINT s
return; return;
} }
if (!resource->map_count)
{
WARN("Resource %p is not mapped.\n", resource);
return;
}
WARN("Ignoring written range %p.\n", written_range); WARN("Ignoring written range %p.\n", written_range);
--resource->map_count; d3d12_heap_unmap(resource->heap, resource);
if (!resource->map_count)
{
resource->map_ptr = NULL;
assert(resource->heap);
d3d12_heap_unmap(resource->heap);
}
} }
static D3D12_RESOURCE_DESC * STDMETHODCALLTYPE d3d12_resource_GetDesc(ID3D12Resource *iface, static D3D12_RESOURCE_DESC * STDMETHODCALLTYPE d3d12_resource_GetDesc(ID3D12Resource *iface,
@ -1445,7 +1445,6 @@ static HRESULT d3d12_resource_init(struct d3d12_resource *resource, struct d3d12
} }
resource->map_count = 0; resource->map_count = 0;
resource->map_ptr = NULL;
resource->initial_state = initial_state; resource->initial_state = initial_state;

View File

@ -391,7 +391,6 @@ struct d3d12_resource
unsigned int flags; unsigned int flags;
unsigned int map_count; unsigned int map_count;
void *map_ptr;
struct d3d12_heap *heap; struct d3d12_heap *heap;
uint64_t heap_offset; uint64_t heap_offset;