vkd3d: Reference count CPU descriptors.

We have to keep all image views referenced by all used VkFramebuffers.

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 2018-08-21 18:08:01 +02:00 committed by Alexandre Julliard
parent 3bf9d1bc67
commit d074fab10b
3 changed files with 93 additions and 25 deletions

View File

@ -750,6 +750,19 @@ static bool d3d12_command_allocator_add_descriptor_pool(struct d3d12_command_all
return true; return true;
} }
static bool d3d12_command_allocator_add_view(struct d3d12_command_allocator *allocator,
struct vkd3d_view *view)
{
if (!vkd3d_array_reserve((void **)&allocator->views, &allocator->views_size,
allocator->view_count + 1, sizeof(*allocator->views)))
return false;
vkd3d_view_incref(view);
allocator->views[allocator->view_count++] = view;
return true;
}
static bool d3d12_command_allocator_add_buffer_view(struct d3d12_command_allocator *allocator, static bool d3d12_command_allocator_add_buffer_view(struct d3d12_command_allocator *allocator,
VkBufferView view) VkBufferView view)
{ {
@ -808,6 +821,12 @@ static void d3d12_command_allocator_free_resources(struct d3d12_command_allocato
} }
allocator->buffer_view_count = 0; allocator->buffer_view_count = 0;
for (i = 0; i < allocator->view_count; ++i)
{
vkd3d_view_decref(allocator->views[i], device);
}
allocator->view_count = 0;
for (i = 0; i < allocator->descriptor_pool_count; ++i) for (i = 0; i < allocator->descriptor_pool_count; ++i)
{ {
VK_CALL(vkDestroyDescriptorPool(device->vk_device, allocator->descriptor_pools[i], NULL)); VK_CALL(vkDestroyDescriptorPool(device->vk_device, allocator->descriptor_pools[i], NULL));
@ -889,6 +908,7 @@ static ULONG STDMETHODCALLTYPE d3d12_command_allocator_Release(ID3D12CommandAllo
d3d12_command_allocator_free_resources(allocator); d3d12_command_allocator_free_resources(allocator);
vkd3d_free(allocator->transfer_buffers); vkd3d_free(allocator->transfer_buffers);
vkd3d_free(allocator->buffer_views); vkd3d_free(allocator->buffer_views);
vkd3d_free(allocator->views);
vkd3d_free(allocator->descriptor_pools); vkd3d_free(allocator->descriptor_pools);
vkd3d_free(allocator->pipelines); vkd3d_free(allocator->pipelines);
vkd3d_free(allocator->framebuffers); vkd3d_free(allocator->framebuffers);
@ -1077,6 +1097,10 @@ static HRESULT d3d12_command_allocator_init(struct d3d12_command_allocator *allo
allocator->descriptor_pools_size = 0; allocator->descriptor_pools_size = 0;
allocator->descriptor_pool_count = 0; allocator->descriptor_pool_count = 0;
allocator->views = NULL;
allocator->views_size = 0;
allocator->view_count = 0;
allocator->buffer_views = NULL; allocator->buffer_views = NULL;
allocator->buffer_views_size = 0; allocator->buffer_views_size = 0;
allocator->buffer_view_count = 0; allocator->buffer_view_count = 0;
@ -3497,6 +3521,7 @@ static void STDMETHODCALLTYPE d3d12_command_list_OMSetRenderTargets(ID3D12Graphi
struct d3d12_command_list *list = impl_from_ID3D12GraphicsCommandList(iface); struct d3d12_command_list *list = impl_from_ID3D12GraphicsCommandList(iface);
const struct d3d12_rtv_desc *rtv_desc; const struct d3d12_rtv_desc *rtv_desc;
const struct d3d12_dsv_desc *dsv_desc; const struct d3d12_dsv_desc *dsv_desc;
struct vkd3d_view *view;
unsigned int i; unsigned int i;
TRACE("iface %p, render_target_descriptor_count %u, render_target_descriptors %p, " TRACE("iface %p, render_target_descriptor_count %u, render_target_descriptors %p, "
@ -3523,7 +3548,14 @@ static void STDMETHODCALLTYPE d3d12_command_list_OMSetRenderTargets(ID3D12Graphi
d3d12_command_list_track_resource_usage(list, rtv_desc->resource); d3d12_command_list_track_resource_usage(list, rtv_desc->resource);
list->views[i + 1] = rtv_desc->vk_view; /* In D3D12 CPU descriptors are consumed when a command is recorded. */
view = rtv_desc->view;
if (!d3d12_command_allocator_add_view(list->allocator, view))
{
WARN("Failed to add view.\n");
}
list->views[i + 1] = view->u.vk_image_view;
list->fb_width = max(list->fb_width, rtv_desc->width); list->fb_width = max(list->fb_width, rtv_desc->width);
list->fb_height = max(list->fb_height, rtv_desc->height); list->fb_height = max(list->fb_height, rtv_desc->height);
list->fb_layer_count = max(list->fb_layer_count, rtv_desc->layer_count); list->fb_layer_count = max(list->fb_layer_count, rtv_desc->layer_count);
@ -3535,11 +3567,17 @@ static void STDMETHODCALLTYPE d3d12_command_list_OMSetRenderTargets(ID3D12Graphi
d3d12_command_list_track_resource_usage(list, dsv_desc->resource); d3d12_command_list_track_resource_usage(list, dsv_desc->resource);
/* In D3D12 CPU descriptors are consumed when a command is recorded. */
view = dsv_desc->view;
if (!d3d12_command_allocator_add_view(list->allocator, view))
{
WARN("Failed to add view.\n");
}
list->views[0] = view->u.vk_image_view;
list->fb_width = max(list->fb_width, dsv_desc->width); list->fb_width = max(list->fb_width, dsv_desc->width);
list->fb_height = max(list->fb_height, dsv_desc->height); list->fb_height = max(list->fb_height, dsv_desc->height);
list->fb_layer_count = max(list->fb_layer_count, 1); list->fb_layer_count = max(list->fb_layer_count, 1);
list->views[0] = dsv_desc->vk_view;
} }
d3d12_command_list_invalidate_current_framebuffer(list); d3d12_command_list_invalidate_current_framebuffer(list);
@ -3549,7 +3587,7 @@ static void STDMETHODCALLTYPE d3d12_command_list_OMSetRenderTargets(ID3D12Graphi
static void d3d12_command_list_clear(struct d3d12_command_list *list, static void d3d12_command_list_clear(struct d3d12_command_list *list,
const struct vkd3d_vk_device_procs *vk_procs, const struct VkAttachmentDescription *attachment_desc, const struct vkd3d_vk_device_procs *vk_procs, const struct VkAttachmentDescription *attachment_desc,
const struct VkAttachmentReference *color_reference, const struct VkAttachmentReference *ds_reference, const struct VkAttachmentReference *color_reference, const struct VkAttachmentReference *ds_reference,
VkImageView vk_view, size_t width, size_t height, unsigned int layer_count, struct vkd3d_view *view, size_t width, size_t height, unsigned int layer_count,
const union VkClearValue *clear_value, unsigned int rect_count, const D3D12_RECT *rects) const union VkClearValue *clear_value, unsigned int rect_count, const D3D12_RECT *rects)
{ {
struct VkSubpassDescription sub_pass_desc; struct VkSubpassDescription sub_pass_desc;
@ -3608,12 +3646,17 @@ static void d3d12_command_list_clear(struct d3d12_command_list *list,
return; return;
} }
if (!d3d12_command_allocator_add_view(list->allocator, view))
{
WARN("Failed to add view.\n");
}
fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
fb_desc.pNext = NULL; fb_desc.pNext = NULL;
fb_desc.flags = 0; fb_desc.flags = 0;
fb_desc.renderPass = vk_render_pass; fb_desc.renderPass = vk_render_pass;
fb_desc.attachmentCount = 1; fb_desc.attachmentCount = 1;
fb_desc.pAttachments = &vk_view; fb_desc.pAttachments = &view->u.vk_image_view;
fb_desc.width = width; fb_desc.width = width;
fb_desc.height = height; fb_desc.height = height;
fb_desc.layers = layer_count; fb_desc.layers = layer_count;
@ -3693,7 +3736,7 @@ static void STDMETHODCALLTYPE d3d12_command_list_ClearDepthStencilView(ID3D12Gra
ds_reference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; ds_reference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
d3d12_command_list_clear(list, &list->device->vk_procs, &attachment_desc, NULL, &ds_reference, d3d12_command_list_clear(list, &list->device->vk_procs, &attachment_desc, NULL, &ds_reference,
dsv_desc->vk_view, dsv_desc->width, dsv_desc->height, 1, &clear_value, rect_count, rects); dsv_desc->view, dsv_desc->width, dsv_desc->height, 1, &clear_value, rect_count, rects);
} }
static void STDMETHODCALLTYPE d3d12_command_list_ClearRenderTargetView(ID3D12GraphicsCommandList *iface, static void STDMETHODCALLTYPE d3d12_command_list_ClearRenderTargetView(ID3D12GraphicsCommandList *iface,
@ -3724,7 +3767,7 @@ static void STDMETHODCALLTYPE d3d12_command_list_ClearRenderTargetView(ID3D12Gra
color_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; color_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
d3d12_command_list_clear(list, &list->device->vk_procs, &attachment_desc, &color_reference, NULL, d3d12_command_list_clear(list, &list->device->vk_procs, &attachment_desc, &color_reference, NULL,
rtv_desc->vk_view, rtv_desc->width, rtv_desc->height, rtv_desc->layer_count, rtv_desc->view, rtv_desc->width, rtv_desc->height, rtv_desc->layer_count,
&clear_value, rect_count, rects); &clear_value, rect_count, rects);
} }

View File

@ -1099,12 +1099,12 @@ static struct vkd3d_view *vkd3d_view_create(void)
return view; return view;
} }
static void vkd3d_view_incref(struct vkd3d_view *view) void vkd3d_view_incref(struct vkd3d_view *view)
{ {
InterlockedIncrement(&view->refcount); InterlockedIncrement(&view->refcount);
} }
static void vkd3d_view_decref(struct vkd3d_view *view, static void vkd3d_view_decref_descriptor(struct vkd3d_view *view,
const struct d3d12_desc *descriptor, struct d3d12_device *device) const struct d3d12_desc *descriptor, struct d3d12_device *device)
{ {
const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs; const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs;
@ -1115,7 +1115,11 @@ static void vkd3d_view_decref(struct vkd3d_view *view,
TRACE("Destroying view %p.\n", view); TRACE("Destroying view %p.\n", view);
if (descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_SRV || descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_UAV) if (!descriptor)
{
VK_CALL(vkDestroyImageView(device->vk_device, view->u.vk_image_view, NULL));
}
else if (descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_SRV || descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_UAV)
{ {
if (descriptor->vk_descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER if (descriptor->vk_descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
|| descriptor->vk_descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) || descriptor->vk_descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)
@ -1134,6 +1138,11 @@ static void vkd3d_view_decref(struct vkd3d_view *view,
vkd3d_free(view); vkd3d_free(view);
} }
void vkd3d_view_decref(struct vkd3d_view *view, struct d3d12_device *device)
{
vkd3d_view_decref_descriptor(view, NULL, device);
}
static void d3d12_desc_destroy(struct d3d12_desc *descriptor, static void d3d12_desc_destroy(struct d3d12_desc *descriptor,
struct d3d12_device *device) struct d3d12_device *device)
{ {
@ -1142,7 +1151,7 @@ static void d3d12_desc_destroy(struct d3d12_desc *descriptor,
|| descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_UAV || descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_UAV
|| descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_SAMPLER) || descriptor->magic == VKD3D_DESCRIPTOR_MAGIC_SAMPLER)
{ {
vkd3d_view_decref(descriptor->u.view, descriptor, device); vkd3d_view_decref_descriptor(descriptor->u.view, descriptor, device);
} }
memset(descriptor, 0, sizeof(*descriptor)); memset(descriptor, 0, sizeof(*descriptor));
@ -1789,12 +1798,10 @@ HRESULT vkd3d_create_static_sampler(struct d3d12_device *device,
/* RTVs */ /* RTVs */
static void d3d12_rtv_desc_destroy(struct d3d12_rtv_desc *rtv, struct d3d12_device *device) static void d3d12_rtv_desc_destroy(struct d3d12_rtv_desc *rtv, struct d3d12_device *device)
{ {
const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs;
if (rtv->magic != VKD3D_DESCRIPTOR_MAGIC_RTV) if (rtv->magic != VKD3D_DESCRIPTOR_MAGIC_RTV)
return; return;
VK_CALL(vkDestroyImageView(device->vk_device, rtv->vk_view, NULL)); vkd3d_view_decref(rtv->view, device);
memset(rtv, 0, sizeof(*rtv)); memset(rtv, 0, sizeof(*rtv));
} }
@ -1803,6 +1810,7 @@ void d3d12_rtv_desc_create_rtv(struct d3d12_rtv_desc *rtv_desc, struct d3d12_dev
{ {
const struct vkd3d_format *format; const struct vkd3d_format *format;
VkImageViewType vk_view_type; VkImageViewType vk_view_type;
struct vkd3d_view *view;
uint32_t miplevel_idx; uint32_t miplevel_idx;
d3d12_rtv_desc_destroy(rtv_desc, device); d3d12_rtv_desc_destroy(rtv_desc, device);
@ -1834,30 +1842,35 @@ void d3d12_rtv_desc_create_rtv(struct d3d12_rtv_desc *rtv_desc, struct d3d12_dev
if (desc && desc->u.Texture2D.PlaneSlice) if (desc && desc->u.Texture2D.PlaneSlice)
FIXME("Ignoring plane slice %u.\n", desc->u.Texture2D.PlaneSlice); FIXME("Ignoring plane slice %u.\n", desc->u.Texture2D.PlaneSlice);
if (!(view = vkd3d_view_create()))
return;
miplevel_idx = desc ? desc->u.Texture2D.MipSlice : 0; miplevel_idx = desc ? desc->u.Texture2D.MipSlice : 0;
vk_view_type = resource->desc.DepthOrArraySize > 1 vk_view_type = resource->desc.DepthOrArraySize > 1
? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D;
if (vkd3d_create_texture_view(device, resource, format, vk_view_type, if (vkd3d_create_texture_view(device, resource, format, vk_view_type,
miplevel_idx, 1, 0, VK_REMAINING_ARRAY_LAYERS, false, &rtv_desc->vk_view) < 0) miplevel_idx, 1, 0, VK_REMAINING_ARRAY_LAYERS, false, &view->u.vk_image_view) < 0)
{
vkd3d_free(view);
return; return;
}
rtv_desc->magic = VKD3D_DESCRIPTOR_MAGIC_RTV;
rtv_desc->format = format->vk_format; rtv_desc->format = format->vk_format;
rtv_desc->width = d3d12_resource_desc_get_width(&resource->desc, miplevel_idx); rtv_desc->width = d3d12_resource_desc_get_width(&resource->desc, miplevel_idx);
rtv_desc->height = d3d12_resource_desc_get_height(&resource->desc, miplevel_idx); rtv_desc->height = d3d12_resource_desc_get_height(&resource->desc, miplevel_idx);
rtv_desc->layer_count = resource->desc.DepthOrArraySize; rtv_desc->layer_count = resource->desc.DepthOrArraySize;
rtv_desc->magic = VKD3D_DESCRIPTOR_MAGIC_RTV; rtv_desc->view = view;
rtv_desc->resource = resource; rtv_desc->resource = resource;
} }
/* DSVs */ /* DSVs */
static void d3d12_dsv_desc_destroy(struct d3d12_dsv_desc *dsv, struct d3d12_device *device) static void d3d12_dsv_desc_destroy(struct d3d12_dsv_desc *dsv, struct d3d12_device *device)
{ {
const struct vkd3d_vk_device_procs *vk_procs = &device->vk_procs;
if (dsv->magic != VKD3D_DESCRIPTOR_MAGIC_DSV) if (dsv->magic != VKD3D_DESCRIPTOR_MAGIC_DSV)
return; return;
VK_CALL(vkDestroyImageView(device->vk_device, dsv->vk_view, NULL)); vkd3d_view_decref(dsv->view, device);
memset(dsv, 0, sizeof(*dsv)); memset(dsv, 0, sizeof(*dsv));
} }
@ -1865,6 +1878,7 @@ void d3d12_dsv_desc_create_dsv(struct d3d12_dsv_desc *dsv_desc, struct d3d12_dev
struct d3d12_resource *resource, const D3D12_DEPTH_STENCIL_VIEW_DESC *desc) struct d3d12_resource *resource, const D3D12_DEPTH_STENCIL_VIEW_DESC *desc)
{ {
const struct vkd3d_format *format; const struct vkd3d_format *format;
struct vkd3d_view *view;
uint32_t miplevel_idx; uint32_t miplevel_idx;
d3d12_dsv_desc_destroy(dsv_desc, device); d3d12_dsv_desc_destroy(dsv_desc, device);
@ -1896,15 +1910,19 @@ void d3d12_dsv_desc_create_dsv(struct d3d12_dsv_desc *dsv_desc, struct d3d12_dev
if (desc && desc->Flags) if (desc && desc->Flags)
FIXME("Ignoring flags %#x.\n", desc->Flags); FIXME("Ignoring flags %#x.\n", desc->Flags);
miplevel_idx = desc ? desc->u.Texture2D.MipSlice : 0; if (!(view = vkd3d_view_create()))
if (vkd3d_create_texture_view(device, resource, format, VK_IMAGE_VIEW_TYPE_2D,
miplevel_idx, 1, 0, 1, false, &dsv_desc->vk_view) < 0)
return; return;
miplevel_idx = desc ? desc->u.Texture2D.MipSlice : 0;
if (vkd3d_create_texture_view(device, resource, format, VK_IMAGE_VIEW_TYPE_2D,
miplevel_idx, 1, 0, 1, false, &view->u.vk_image_view) < 0)
return;
dsv_desc->magic = VKD3D_DESCRIPTOR_MAGIC_DSV;
dsv_desc->format = format->vk_format; dsv_desc->format = format->vk_format;
dsv_desc->width = d3d12_resource_desc_get_width(&resource->desc, miplevel_idx); dsv_desc->width = d3d12_resource_desc_get_width(&resource->desc, miplevel_idx);
dsv_desc->height = d3d12_resource_desc_get_height(&resource->desc, miplevel_idx); dsv_desc->height = d3d12_resource_desc_get_height(&resource->desc, miplevel_idx);
dsv_desc->magic = VKD3D_DESCRIPTOR_MAGIC_DSV; dsv_desc->view = view;
dsv_desc->resource = resource; dsv_desc->resource = resource;
} }

View File

@ -257,6 +257,9 @@ struct vkd3d_view
VkBufferView vk_counter_view; VkBufferView vk_counter_view;
}; };
void vkd3d_view_decref(struct vkd3d_view *view, struct d3d12_device *device) DECLSPEC_HIDDEN;
void vkd3d_view_incref(struct vkd3d_view *view) DECLSPEC_HIDDEN;
struct d3d12_desc struct d3d12_desc
{ {
uint32_t magic; uint32_t magic;
@ -306,7 +309,7 @@ struct d3d12_rtv_desc
uint64_t width; uint64_t width;
unsigned int height; unsigned int height;
unsigned int layer_count; unsigned int layer_count;
VkImageView vk_view; struct vkd3d_view *view;
struct d3d12_resource *resource; struct d3d12_resource *resource;
}; };
@ -324,7 +327,7 @@ struct d3d12_dsv_desc
VkFormat format; VkFormat format;
uint64_t width; uint64_t width;
unsigned int height; unsigned int height;
VkImageView vk_view; struct vkd3d_view *view;
struct d3d12_resource *resource; struct d3d12_resource *resource;
}; };
@ -561,6 +564,10 @@ struct d3d12_command_allocator
size_t descriptor_pools_size; size_t descriptor_pools_size;
size_t descriptor_pool_count; size_t descriptor_pool_count;
struct vkd3d_view **views;
size_t views_size;
size_t view_count;
VkBufferView *buffer_views; VkBufferView *buffer_views;
size_t buffer_views_size; size_t buffer_views_size;
size_t buffer_view_count; size_t buffer_view_count;