Path: blob/21.2-virgl/src/gallium/drivers/d3d12/d3d12_bufmgr.cpp
4570 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "d3d12_bufmgr.h"24#include "d3d12_format.h"25#include "d3d12_screen.h"2627#include "D3D12ResourceState.h"2829#include "pipebuffer/pb_buffer.h"30#include "pipebuffer/pb_bufmgr.h"3132#include "util/format/u_format.h"33#include "util/u_memory.h"3435#include <directx/d3d12.h>36#include <dxguids/dxguids.h>3738struct d3d12_bufmgr {39struct pb_manager base;4041ID3D12Device *dev;42};4344extern const struct pb_vtbl d3d12_buffer_vtbl;4546static inline struct d3d12_bufmgr *47d3d12_bufmgr(struct pb_manager *mgr)48{49assert(mgr);5051return (struct d3d12_bufmgr *)mgr;52}5354static struct TransitionableResourceState *55create_trans_state(ID3D12Resource *res, enum pipe_format format)56{57D3D12_RESOURCE_DESC desc = res->GetDesc();5859// Calculate the total number of subresources60unsigned arraySize = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ?611 : desc.DepthOrArraySize;62unsigned total_subresources = desc.MipLevels *63arraySize *64d3d12_non_opaque_plane_count(desc.Format);65total_subresources *= util_format_has_stencil(util_format_description(format)) ?662 : 1;6768return new TransitionableResourceState(res,69total_subresources,70SupportsSimultaneousAccess(desc));71}7273struct d3d12_bo *74d3d12_bo_wrap_res(ID3D12Resource *res, enum pipe_format format)75{76struct d3d12_bo *bo;7778bo = CALLOC_STRUCT(d3d12_bo);79if (!bo)80return NULL;8182bo->refcount = 1;83bo->res = res;84bo->trans_state = create_trans_state(res, format);8586return bo;87}8889struct d3d12_bo *90d3d12_bo_new(ID3D12Device *dev, uint64_t size, const pb_desc *pb_desc)91{92ID3D12Resource *res;9394D3D12_RESOURCE_DESC res_desc;95res_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;96res_desc.Format = DXGI_FORMAT_UNKNOWN;97res_desc.Alignment = pb_desc->alignment;98res_desc.Width = size;99res_desc.Height = 1;100res_desc.DepthOrArraySize = 1;101res_desc.MipLevels = 1;102res_desc.SampleDesc.Count = 1;103res_desc.SampleDesc.Quality = 0;104res_desc.Flags = D3D12_RESOURCE_FLAG_NONE;105res_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;106107D3D12_HEAP_TYPE heap_type = D3D12_HEAP_TYPE_DEFAULT;108if (pb_desc->usage & PB_USAGE_CPU_READ)109heap_type = D3D12_HEAP_TYPE_READBACK;110else if (pb_desc->usage & PB_USAGE_CPU_WRITE)111heap_type = D3D12_HEAP_TYPE_UPLOAD;112113D3D12_HEAP_PROPERTIES heap_pris = dev->GetCustomHeapProperties(0, heap_type);114HRESULT hres = dev->CreateCommittedResource(&heap_pris,115D3D12_HEAP_FLAG_NONE,116&res_desc,117D3D12_RESOURCE_STATE_COMMON,118NULL,119IID_PPV_ARGS(&res));120121if (FAILED(hres))122return NULL;123124return d3d12_bo_wrap_res(res, PIPE_FORMAT_NONE);125}126127struct d3d12_bo *128d3d12_bo_wrap_buffer(struct pb_buffer *buf)129{130struct d3d12_bo *bo;131132bo = CALLOC_STRUCT(d3d12_bo);133if (!bo)134return NULL;135136bo->refcount = 1;137bo->buffer = buf;138bo->trans_state = NULL; /* State from base BO will be used */139140return bo;141}142143void144d3d12_bo_unreference(struct d3d12_bo *bo)145{146if (bo == NULL)147return;148149assert(p_atomic_read(&bo->refcount) > 0);150151if (p_atomic_dec_zero(&bo->refcount)) {152if (bo->buffer) {153pb_reference(&bo->buffer, NULL);154} else {155delete bo->trans_state;156bo->res->Release();157}158FREE(bo);159}160}161162void *163d3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range)164{165struct d3d12_bo *base_bo;166D3D12_RANGE offset_range = {0, 0};167uint64_t offset;168void *ptr;169170base_bo = d3d12_bo_get_base(bo, &offset);171172if (!range || offset == 0) {173/* Nothing to do */174} else if (range->Begin >= range->End) {175offset_range.Begin = offset;176offset_range.End = offset + d3d12_bo_get_size(bo);177range = &offset_range;178} else {179offset_range.Begin = range->Begin + offset;180offset_range.End = range->End + offset;181range = &offset_range;182}183184if (FAILED(base_bo->res->Map(0, range, &ptr)))185return NULL;186187return (uint8_t *)ptr + (range ? range->Begin : 0);188}189190void191d3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range)192{193struct d3d12_bo *base_bo;194D3D12_RANGE offset_range = {0, 0};195uint64_t offset;196197base_bo = d3d12_bo_get_base(bo, &offset);198199if (!range || bo == base_bo)200{201/* Nothing to do */202} else if (range->Begin >= range->End) {203offset_range.Begin = offset;204offset_range.End = offset + base_bo->res->GetDesc().Width;205range = &offset_range;206} else {207offset_range.Begin = range->Begin + offset;208offset_range.End = range->End + offset;209range = &offset_range;210}211212base_bo->res->Unmap(0, range);213}214215static void216d3d12_buffer_destroy(void *winsys, struct pb_buffer *pbuf)217{218struct d3d12_buffer *buf = d3d12_buffer(pbuf);219220d3d12_bo_unmap(buf->bo, &buf->range);221d3d12_bo_unreference(buf->bo);222FREE(buf);223}224225static void *226d3d12_buffer_map(struct pb_buffer *pbuf,227enum pb_usage_flags flags,228void *flush_ctx)229{230return d3d12_buffer(pbuf)->map;231}232233static void234d3d12_buffer_unmap(struct pb_buffer *pbuf)235{236}237238static void239d3d12_buffer_get_base_buffer(struct pb_buffer *buf,240struct pb_buffer **base_buf,241pb_size *offset)242{243*base_buf = buf;244*offset = 0;245}246247static enum pipe_error248d3d12_buffer_validate(struct pb_buffer *pbuf,249struct pb_validate *vl,250enum pb_usage_flags flags )251{252/* Always pinned */253return PIPE_OK;254}255256static void257d3d12_buffer_fence(struct pb_buffer *pbuf,258struct pipe_fence_handle *fence )259{260}261262const struct pb_vtbl d3d12_buffer_vtbl = {263d3d12_buffer_destroy,264d3d12_buffer_map,265d3d12_buffer_unmap,266d3d12_buffer_validate,267d3d12_buffer_fence,268d3d12_buffer_get_base_buffer269};270271static struct pb_buffer *272d3d12_bufmgr_create_buffer(struct pb_manager *pmgr,273pb_size size,274const struct pb_desc *pb_desc)275{276struct d3d12_bufmgr *mgr = d3d12_bufmgr(pmgr);277struct d3d12_buffer *buf;278279buf = CALLOC_STRUCT(d3d12_buffer);280if (!buf)281return NULL;282283// Align the buffer to D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT284// in case it is to be used as a CBV.285size = align64(size, D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);286287pipe_reference_init(&buf->base.reference, 1);288buf->base.alignment_log2 = util_logbase2(pb_desc->alignment);289buf->base.usage = pb_desc->usage;290buf->base.vtbl = &d3d12_buffer_vtbl;291buf->base.size = size;292buf->range.Begin = 0;293buf->range.End = size;294295buf->bo = d3d12_bo_new(mgr->dev, size, pb_desc);296if (!buf->bo) {297FREE(buf);298return NULL;299}300301if (pb_desc->usage & PB_USAGE_CPU_READ_WRITE) {302buf->map = d3d12_bo_map(buf->bo, &buf->range);303if (!buf->map) {304d3d12_bo_unreference(buf->bo);305FREE(buf);306return NULL;307}308}309310return &buf->base;311}312313static void314d3d12_bufmgr_flush(struct pb_manager *mgr)315{316/* No-op */317}318319static void320d3d12_bufmgr_destroy(struct pb_manager *_mgr)321{322struct d3d12_bufmgr *mgr = d3d12_bufmgr(_mgr);323FREE(mgr);324}325326struct pb_manager *327d3d12_bufmgr_create(struct d3d12_screen *screen)328{329struct d3d12_bufmgr *mgr;330331mgr = CALLOC_STRUCT(d3d12_bufmgr);332if (!mgr)333return NULL;334335mgr->base.destroy = d3d12_bufmgr_destroy;336mgr->base.create_buffer = d3d12_bufmgr_create_buffer;337mgr->base.flush = d3d12_bufmgr_flush;338339mgr->dev = screen->dev;340341return &mgr->base;342}343344345