From 0034f7b2093529090452af0073f26d66aed5138c Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Mon, 14 Dec 2020 11:59:53 -0800 Subject: [PATCH] d3d12: Don't allocate mappable textures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There's not really a reason to directly map textures. Doing so requires the texture to be allocated in system RAM instead of video RAM, which means all GPU access to it would be needlessly slow. Notably, the one texture type that was allocated this way is the display target texture for the software driver path. Instead, use pipe_transfer_map to be able to copy the texture to system RAM. Reviewed-by: Louis-Francis Ratté-Boulianne Part-of: --- src/gallium/drivers/d3d12/d3d12_resource.cpp | 20 ++++-------------- src/gallium/drivers/d3d12/d3d12_screen.cpp | 22 +++++++++++++------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/gallium/drivers/d3d12/d3d12_resource.cpp b/src/gallium/drivers/d3d12/d3d12_resource.cpp index 8fc90c75ad8..76e50b046b0 100644 --- a/src/gallium/drivers/d3d12/d3d12_resource.cpp +++ b/src/gallium/drivers/d3d12/d3d12_resource.cpp @@ -45,10 +45,9 @@ static bool can_map_directly(struct pipe_resource *pres) { - return pres->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_SHARED | PIPE_BIND_LINEAR) || - (pres->target == PIPE_BUFFER && + return pres->target == PIPE_BUFFER && pres->usage != PIPE_USAGE_DEFAULT && - pres->usage != PIPE_USAGE_IMMUTABLE); + pres->usage != PIPE_USAGE_IMMUTABLE; } static void @@ -217,16 +216,7 @@ init_texture(struct d3d12_screen *screen, PIPE_BIND_SHARED | PIPE_BIND_LINEAR)) desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; - D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_DEFAULT; - - if (templ->bind & (PIPE_BIND_DISPLAY_TARGET | - PIPE_BIND_SCANOUT | - PIPE_BIND_SHARED)) - heap_type = D3D12_HEAP_TYPE_READBACK; - else if (templ->usage == PIPE_USAGE_STAGING) - heap_type = D3D12_HEAP_TYPE_UPLOAD; - - D3D12_HEAP_PROPERTIES heap_pris = screen->dev->GetCustomHeapProperties(0, heap_type); + D3D12_HEAP_PROPERTIES heap_pris = screen->dev->GetCustomHeapProperties(0, D3D12_HEAP_TYPE_DEFAULT); HRESULT hres = screen->dev->CreateCommittedResource(&heap_pris, D3D12_HEAP_FLAG_NONE, @@ -237,9 +227,7 @@ init_texture(struct d3d12_screen *screen, if (FAILED(hres)) return false; - if (screen->winsys && (templ->bind & (PIPE_BIND_DISPLAY_TARGET | - PIPE_BIND_SCANOUT | - PIPE_BIND_SHARED))) { + if (screen->winsys && (templ->bind & PIPE_BIND_DISPLAY_TARGET)) { struct sw_winsys *winsys = screen->winsys; res->dt = winsys->displaytarget_create(screen->winsys, res->base.bind, diff --git a/src/gallium/drivers/d3d12/d3d12_screen.cpp b/src/gallium/drivers/d3d12/d3d12_screen.cpp index 44e44729440..ab091af6f54 100644 --- a/src/gallium/drivers/d3d12/d3d12_screen.cpp +++ b/src/gallium/drivers/d3d12/d3d12_screen.cpp @@ -621,27 +621,35 @@ d3d12_flush_frontbuffer(struct pipe_screen * pscreen, struct d3d12_screen *screen = d3d12_screen(pscreen); struct sw_winsys *winsys = screen->winsys; struct d3d12_resource *res = d3d12_resource(pres); - ID3D12Resource *d3d12_res = d3d12_resource_resource(res); - if (!winsys) + if (!winsys || !pctx) return; - if (pctx) - d3d12_flush_cmdlist_and_wait(d3d12_context(pctx)); - assert(res->dt); void *map = winsys->displaytarget_map(winsys, res->dt, 0); if (map) { - d3d12_res->ReadFromSubresource(map, res->dt_stride, 0, 0, NULL); + pipe_transfer *transfer = nullptr; + void *res_map = pipe_transfer_map(pctx, pres, level, layer, PIPE_MAP_READ, 0, 0, + u_minify(pres->width0, level), + u_minify(pres->height0, level), + &transfer); + if (res_map) { + util_copy_rect((ubyte*)map, pres->format, res->dt_stride, 0, 0, + transfer->box.width, transfer->box.height, + (const ubyte*)res_map, transfer->stride, 0, 0); + pipe_transfer_unmap(pctx, transfer); + } winsys->displaytarget_unmap(winsys, res->dt); } #ifdef _WIN32 // WindowFromDC is Windows-only, and this method requires an HWND, so only use it on Windows ID3D12SharingContract *sharing_contract; - if (SUCCEEDED(screen->cmdqueue->QueryInterface(IID_PPV_ARGS(&sharing_contract)))) + if (SUCCEEDED(screen->cmdqueue->QueryInterface(IID_PPV_ARGS(&sharing_contract)))) { + ID3D12Resource *d3d12_res = d3d12_resource_resource(res); sharing_contract->Present(d3d12_res, 0, WindowFromDC((HDC)winsys_drawable_handle)); + } #endif winsys->displaytarget_display(winsys, res->dt, winsys_drawable_handle, sub_box);