2018-03-03 20:59:17 +01:00
|
|
|
#include "d3d11_cmdlist.h"
|
2018-01-20 13:22:44 +01:00
|
|
|
#include "d3d11_context_imm.h"
|
|
|
|
#include "d3d11_device.h"
|
|
|
|
#include "d3d11_texture.h"
|
|
|
|
|
2018-07-15 19:45:40 +02:00
|
|
|
constexpr static uint32_t MinFlushIntervalUs = 1250;
|
|
|
|
constexpr static uint32_t MaxPendingSubmits = 3;
|
|
|
|
|
2018-01-20 13:22:44 +01:00
|
|
|
namespace dxvk {
|
|
|
|
|
|
|
|
D3D11ImmediateContext::D3D11ImmediateContext(
|
2018-04-28 14:17:52 +02:00
|
|
|
D3D11Device* pParent,
|
|
|
|
const Rc<DxvkDevice>& Device)
|
2018-11-20 10:44:04 +01:00
|
|
|
: D3D11DeviceContext(pParent, Device, DxvkCsChunkFlag::SingleUse),
|
2018-01-23 09:23:31 +01:00
|
|
|
m_csThread(Device->createContext()) {
|
2019-02-07 00:57:21 +01:00
|
|
|
EmitCs([
|
|
|
|
cDevice = m_device,
|
|
|
|
cRelaxedBarriers = pParent->GetOptions()->relaxedBarriers
|
|
|
|
] (DxvkContext* ctx) {
|
2018-03-22 13:40:45 +01:00
|
|
|
ctx->beginRecording(cDevice->createCommandList());
|
2019-02-07 00:57:21 +01:00
|
|
|
|
|
|
|
if (cRelaxedBarriers)
|
|
|
|
ctx->setBarrierControl(DxvkBarrierControl::IgnoreWriteAfterWrite);
|
2018-03-22 13:40:45 +01:00
|
|
|
});
|
2018-01-20 13:22:44 +01:00
|
|
|
|
2018-03-22 13:40:45 +01:00
|
|
|
ClearState();
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
D3D11ImmediateContext::~D3D11ImmediateContext() {
|
2018-01-20 23:12:03 +01:00
|
|
|
Flush();
|
2018-01-21 18:04:22 +01:00
|
|
|
SynchronizeCsThread();
|
|
|
|
SynchronizeDevice();
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE D3D11ImmediateContext::AddRef() {
|
|
|
|
return m_parent->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE D3D11ImmediateContext::Release() {
|
|
|
|
return m_parent->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
D3D11_DEVICE_CONTEXT_TYPE STDMETHODCALLTYPE D3D11ImmediateContext::GetType() {
|
|
|
|
return D3D11_DEVICE_CONTEXT_IMMEDIATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UINT STDMETHODCALLTYPE D3D11ImmediateContext::GetContextFlags() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-08 12:29:24 +02:00
|
|
|
HRESULT STDMETHODCALLTYPE D3D11ImmediateContext::GetData(
|
|
|
|
ID3D11Asynchronous* pAsync,
|
|
|
|
void* pData,
|
|
|
|
UINT DataSize,
|
|
|
|
UINT GetDataFlags) {
|
2018-10-17 17:19:07 +02:00
|
|
|
if (!pAsync)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2018-06-08 12:29:24 +02:00
|
|
|
// Make sure that we can safely write to the memory
|
|
|
|
// location pointed to by pData if it is specified.
|
|
|
|
if (DataSize == 0)
|
|
|
|
pData = nullptr;
|
|
|
|
|
2018-10-17 17:19:07 +02:00
|
|
|
if (pData && pAsync->GetDataSize() != DataSize) {
|
2018-06-08 12:29:24 +02:00
|
|
|
Logger::err(str::format(
|
|
|
|
"D3D11: GetData: Data size mismatch",
|
|
|
|
"\n Expected: ", pAsync->GetDataSize(),
|
|
|
|
"\n Got: ", DataSize));
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:54:44 +01:00
|
|
|
// Ensure that all query commands actually get
|
|
|
|
// executed before trying to access the query
|
|
|
|
SynchronizeCsThread();
|
|
|
|
|
2018-10-17 17:19:07 +02:00
|
|
|
// Get query status directly from the query object
|
2019-05-09 16:38:07 +02:00
|
|
|
auto query = static_cast<D3D11Query*>(pAsync);
|
|
|
|
HRESULT hr = query->GetData(pData, GetDataFlags);
|
2018-06-08 13:11:24 +02:00
|
|
|
|
|
|
|
// If we're likely going to spin on the asynchronous object,
|
|
|
|
// flush the context so that we're keeping the GPU busy
|
2019-05-09 16:38:07 +02:00
|
|
|
if (hr == S_FALSE) {
|
|
|
|
query->NotifyStall();
|
2018-10-17 17:22:52 +02:00
|
|
|
FlushImplicit(FALSE);
|
2019-05-09 16:38:07 +02:00
|
|
|
}
|
2018-06-08 12:29:24 +02:00
|
|
|
|
2018-06-08 13:11:24 +02:00
|
|
|
return hr;
|
2018-06-08 12:29:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-09 16:38:07 +02:00
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::End(ID3D11Asynchronous* pAsync) {
|
|
|
|
D3D11DeviceContext::End(pAsync);
|
|
|
|
|
|
|
|
auto query = static_cast<D3D11Query*>(pAsync);
|
|
|
|
if (unlikely(query && query->IsEvent())) {
|
|
|
|
query->NotifyEnd();
|
|
|
|
query->IsStalling()
|
|
|
|
? Flush()
|
|
|
|
: FlushImplicit(TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-20 13:22:44 +01:00
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::Flush() {
|
2018-03-11 20:25:09 +01:00
|
|
|
m_parent->FlushInitContext();
|
|
|
|
|
2018-11-29 20:59:40 +01:00
|
|
|
D3D10DeviceLock lock = LockContext();
|
|
|
|
|
2018-03-22 12:58:26 +01:00
|
|
|
if (m_csIsBusy || m_csChunk->commandCount() != 0) {
|
2018-02-19 11:27:14 +01:00
|
|
|
// Add commands to flush the threaded
|
|
|
|
// context, then flush the command list
|
2018-11-01 13:24:42 +01:00
|
|
|
EmitCs([] (DxvkContext* ctx) {
|
|
|
|
ctx->flushCommandList();
|
2018-02-19 11:27:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
FlushCsChunk();
|
2018-03-23 11:54:19 +01:00
|
|
|
|
2018-07-09 20:31:54 +02:00
|
|
|
// Reset flush timer used for implicit flushes
|
|
|
|
m_lastFlush = std::chrono::high_resolution_clock::now();
|
|
|
|
m_csIsBusy = false;
|
2018-02-19 11:27:14 +01:00
|
|
|
}
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::ExecuteCommandList(
|
|
|
|
ID3D11CommandList* pCommandList,
|
2018-03-06 18:34:34 +01:00
|
|
|
BOOL RestoreContextState) {
|
2018-11-29 20:59:40 +01:00
|
|
|
D3D10DeviceLock lock = LockContext();
|
|
|
|
|
2018-04-03 19:52:14 +02:00
|
|
|
auto commandList = static_cast<D3D11CommandList*>(pCommandList);
|
|
|
|
|
|
|
|
// Flush any outstanding commands so that
|
|
|
|
// we don't mess up the execution order
|
2018-03-12 23:36:55 +01:00
|
|
|
FlushCsChunk();
|
|
|
|
|
2018-04-03 19:52:14 +02:00
|
|
|
// As an optimization, flush everything if the
|
|
|
|
// number of pending draw calls is high enough.
|
2018-10-17 17:22:52 +02:00
|
|
|
FlushImplicit(FALSE);
|
2018-04-03 19:52:14 +02:00
|
|
|
|
|
|
|
// Dispatch command list to the CS thread and
|
|
|
|
// restore the immediate context's state
|
|
|
|
commandList->EmitToCsThread(&m_csThread);
|
2018-03-03 20:59:17 +01:00
|
|
|
|
|
|
|
if (RestoreContextState)
|
|
|
|
RestoreState();
|
|
|
|
else
|
|
|
|
ClearState();
|
2018-03-22 12:58:26 +01:00
|
|
|
|
2018-04-03 19:52:14 +02:00
|
|
|
// Mark CS thread as busy so that subsequent
|
|
|
|
// flush operations get executed correctly.
|
2018-03-22 12:58:26 +01:00
|
|
|
m_csIsBusy = true;
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE D3D11ImmediateContext::FinishCommandList(
|
2018-03-06 18:34:34 +01:00
|
|
|
BOOL RestoreDeferredContextState,
|
2018-01-20 13:22:44 +01:00
|
|
|
ID3D11CommandList **ppCommandList) {
|
2018-04-02 12:04:20 +02:00
|
|
|
InitReturnPtr(ppCommandList);
|
|
|
|
|
2018-01-20 13:22:44 +01:00
|
|
|
Logger::err("D3D11: FinishCommandList called on immediate context");
|
|
|
|
return DXGI_ERROR_INVALID_CALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE D3D11ImmediateContext::Map(
|
|
|
|
ID3D11Resource* pResource,
|
|
|
|
UINT Subresource,
|
|
|
|
D3D11_MAP MapType,
|
|
|
|
UINT MapFlags,
|
|
|
|
D3D11_MAPPED_SUBRESOURCE* pMappedResource) {
|
2018-11-29 20:59:40 +01:00
|
|
|
D3D10DeviceLock lock = LockContext();
|
|
|
|
|
2019-05-01 03:01:36 +02:00
|
|
|
if (unlikely(!pResource || !pMappedResource))
|
2018-10-16 12:29:04 +02:00
|
|
|
return E_INVALIDARG;
|
2018-03-22 12:58:26 +01:00
|
|
|
|
2018-01-20 13:22:44 +01:00
|
|
|
D3D11_RESOURCE_DIMENSION resourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
|
|
|
|
pResource->GetType(&resourceDim);
|
2018-10-16 12:29:04 +02:00
|
|
|
|
|
|
|
HRESULT hr;
|
2018-01-20 13:22:44 +01:00
|
|
|
|
2019-05-01 03:01:36 +02:00
|
|
|
if (likely(resourceDim == D3D11_RESOURCE_DIMENSION_BUFFER)) {
|
2018-10-16 12:29:04 +02:00
|
|
|
hr = MapBuffer(
|
2018-03-14 14:40:09 +01:00
|
|
|
static_cast<D3D11Buffer*>(pResource),
|
|
|
|
MapType, MapFlags, pMappedResource);
|
2018-01-20 13:22:44 +01:00
|
|
|
} else {
|
2018-10-16 12:29:04 +02:00
|
|
|
hr = MapImage(
|
2018-03-14 14:40:09 +01:00
|
|
|
GetCommonTexture(pResource),
|
|
|
|
Subresource, MapType, MapFlags,
|
|
|
|
pMappedResource);
|
|
|
|
}
|
2018-10-16 12:29:04 +02:00
|
|
|
|
2019-02-04 10:24:04 +01:00
|
|
|
if (unlikely(FAILED(hr))) {
|
2018-10-16 12:29:04 +02:00
|
|
|
pMappedResource->pData = nullptr;
|
|
|
|
pMappedResource->RowPitch = 0;
|
|
|
|
pMappedResource->DepthPitch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
2018-03-14 14:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::Unmap(
|
|
|
|
ID3D11Resource* pResource,
|
|
|
|
UINT Subresource) {
|
|
|
|
D3D11_RESOURCE_DIMENSION resourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
|
|
|
|
pResource->GetType(&resourceDim);
|
|
|
|
|
2019-05-01 03:01:36 +02:00
|
|
|
if (unlikely(resourceDim != D3D11_RESOURCE_DIMENSION_BUFFER)) {
|
|
|
|
D3D10DeviceLock lock = LockContext();
|
2018-03-14 14:40:09 +01:00
|
|
|
UnmapImage(GetCommonTexture(pResource), Subresource);
|
2019-05-01 03:01:36 +02:00
|
|
|
}
|
2018-03-14 14:40:09 +01:00
|
|
|
}
|
2018-10-25 20:54:10 +02:00
|
|
|
|
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::UpdateSubresource(
|
|
|
|
ID3D11Resource* pDstResource,
|
|
|
|
UINT DstSubresource,
|
|
|
|
const D3D11_BOX* pDstBox,
|
|
|
|
const void* pSrcData,
|
|
|
|
UINT SrcRowPitch,
|
|
|
|
UINT SrcDepthPitch) {
|
|
|
|
FlushImplicit(FALSE);
|
|
|
|
|
|
|
|
D3D11DeviceContext::UpdateSubresource(
|
|
|
|
pDstResource, DstSubresource, pDstBox,
|
|
|
|
pSrcData, SrcRowPitch, SrcDepthPitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::UpdateSubresource1(
|
|
|
|
ID3D11Resource* pDstResource,
|
|
|
|
UINT DstSubresource,
|
|
|
|
const D3D11_BOX* pDstBox,
|
|
|
|
const void* pSrcData,
|
|
|
|
UINT SrcRowPitch,
|
|
|
|
UINT SrcDepthPitch,
|
|
|
|
UINT CopyFlags) {
|
|
|
|
FlushImplicit(FALSE);
|
|
|
|
|
|
|
|
D3D11DeviceContext::UpdateSubresource1(
|
|
|
|
pDstResource, DstSubresource, pDstBox,
|
|
|
|
pSrcData, SrcRowPitch, SrcDepthPitch,
|
|
|
|
CopyFlags);
|
|
|
|
}
|
|
|
|
|
2019-05-09 13:04:07 +02:00
|
|
|
|
2018-06-27 18:34:17 +02:00
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::OMSetRenderTargets(
|
|
|
|
UINT NumViews,
|
|
|
|
ID3D11RenderTargetView* const* ppRenderTargetViews,
|
|
|
|
ID3D11DepthStencilView* pDepthStencilView) {
|
2019-05-09 13:04:07 +02:00
|
|
|
FlushImplicit(TRUE);
|
2018-06-27 18:34:17 +02:00
|
|
|
|
|
|
|
D3D11DeviceContext::OMSetRenderTargets(
|
|
|
|
NumViews, ppRenderTargetViews, pDepthStencilView);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::OMSetRenderTargetsAndUnorderedAccessViews(
|
|
|
|
UINT NumRTVs,
|
|
|
|
ID3D11RenderTargetView* const* ppRenderTargetViews,
|
|
|
|
ID3D11DepthStencilView* pDepthStencilView,
|
|
|
|
UINT UAVStartSlot,
|
|
|
|
UINT NumUAVs,
|
|
|
|
ID3D11UnorderedAccessView* const* ppUnorderedAccessViews,
|
|
|
|
const UINT* pUAVInitialCounts) {
|
2019-05-09 13:04:07 +02:00
|
|
|
FlushImplicit(TRUE);
|
2018-06-27 18:34:17 +02:00
|
|
|
|
|
|
|
D3D11DeviceContext::OMSetRenderTargetsAndUnorderedAccessViews(
|
|
|
|
NumRTVs, ppRenderTargetViews, pDepthStencilView,
|
|
|
|
UAVStartSlot, NumUAVs, ppUnorderedAccessViews,
|
|
|
|
pUAVInitialCounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 14:40:09 +01:00
|
|
|
HRESULT D3D11ImmediateContext::MapBuffer(
|
|
|
|
D3D11Buffer* pResource,
|
|
|
|
D3D11_MAP MapType,
|
|
|
|
UINT MapFlags,
|
|
|
|
D3D11_MAPPED_SUBRESOURCE* pMappedResource) {
|
2019-02-04 10:24:04 +01:00
|
|
|
if (unlikely(pResource->GetMapMode() == D3D11_COMMON_BUFFER_MAP_MODE_NONE)) {
|
2018-03-14 14:40:09 +01:00
|
|
|
Logger::err("D3D11: Cannot map a device-local buffer");
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MapType == D3D11_MAP_WRITE_DISCARD) {
|
|
|
|
// Allocate a new backing slice for the buffer and set
|
|
|
|
// it as the 'new' mapped slice. This assumes that the
|
|
|
|
// only way to invalidate a buffer is by mapping it.
|
2019-01-09 17:56:53 +01:00
|
|
|
auto physSlice = pResource->DiscardSlice();
|
|
|
|
pMappedResource->pData = physSlice.mapPtr;
|
2018-11-02 15:43:46 +01:00
|
|
|
pMappedResource->RowPitch = pResource->Desc()->ByteWidth;
|
|
|
|
pMappedResource->DepthPitch = pResource->Desc()->ByteWidth;
|
2018-03-14 00:45:07 +01:00
|
|
|
|
2018-03-14 14:40:09 +01:00
|
|
|
EmitCs([
|
2019-02-04 10:24:04 +01:00
|
|
|
cBuffer = pResource->GetBuffer(),
|
|
|
|
cBufferSlice = physSlice
|
2018-03-14 14:40:09 +01:00
|
|
|
] (DxvkContext* ctx) {
|
2019-01-09 17:56:53 +01:00
|
|
|
ctx->invalidateBuffer(cBuffer, cBufferSlice);
|
2018-03-14 14:40:09 +01:00
|
|
|
});
|
2018-10-16 12:29:04 +02:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
} else {
|
|
|
|
// Wait until the resource is no longer in use
|
|
|
|
if (MapType != D3D11_MAP_WRITE_NO_OVERWRITE) {
|
2019-02-04 10:24:04 +01:00
|
|
|
if (!WaitForResource(pResource->GetBuffer(), MapFlags))
|
2018-10-16 12:29:04 +02:00
|
|
|
return DXGI_ERROR_WAS_STILL_DRAWING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use map pointer from previous map operation. This
|
|
|
|
// way we don't have to synchronize with the CS thread
|
|
|
|
// if the map mode is D3D11_MAP_WRITE_NO_OVERWRITE.
|
2019-01-09 17:56:53 +01:00
|
|
|
DxvkBufferSliceHandle physSlice = pResource->GetMappedSlice();
|
2018-10-16 12:29:04 +02:00
|
|
|
|
2019-01-09 17:56:53 +01:00
|
|
|
pMappedResource->pData = physSlice.mapPtr;
|
2018-11-02 15:43:46 +01:00
|
|
|
pMappedResource->RowPitch = pResource->Desc()->ByteWidth;
|
|
|
|
pMappedResource->DepthPitch = pResource->Desc()->ByteWidth;
|
2018-10-16 12:29:04 +02:00
|
|
|
return S_OK;
|
2018-03-14 14:40:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HRESULT D3D11ImmediateContext::MapImage(
|
|
|
|
D3D11CommonTexture* pResource,
|
|
|
|
UINT Subresource,
|
|
|
|
D3D11_MAP MapType,
|
|
|
|
UINT MapFlags,
|
|
|
|
D3D11_MAPPED_SUBRESOURCE* pMappedResource) {
|
|
|
|
const Rc<DxvkImage> mappedImage = pResource->GetImage();
|
|
|
|
const Rc<DxvkBuffer> mappedBuffer = pResource->GetMappedBuffer();
|
|
|
|
|
2019-02-04 10:24:04 +01:00
|
|
|
if (unlikely(pResource->GetMapMode() == D3D11_COMMON_TEXTURE_MAP_MODE_NONE)) {
|
2018-03-14 14:40:09 +01:00
|
|
|
Logger::err("D3D11: Cannot map a device-local image");
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
2019-03-26 15:14:56 +01:00
|
|
|
|
|
|
|
VkFormat packedFormat = m_parent->LookupPackedFormat(
|
|
|
|
pResource->Desc()->Format, pResource->GetFormatMode()).Format;
|
2018-03-14 14:40:09 +01:00
|
|
|
|
2019-03-26 15:14:56 +01:00
|
|
|
auto formatInfo = imageFormatInfo(packedFormat);
|
2018-11-08 18:09:05 +01:00
|
|
|
auto subresource = pResource->GetSubresourceFromIndex(
|
2018-04-21 20:34:41 +02:00
|
|
|
formatInfo->aspectMask, Subresource);
|
2018-03-14 14:40:09 +01:00
|
|
|
|
2018-11-08 18:15:24 +01:00
|
|
|
pResource->SetMappedSubresource(subresource, MapType);
|
2018-03-14 14:40:09 +01:00
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
if (pResource->GetMapMode() == D3D11_COMMON_TEXTURE_MAP_MODE_DIRECT) {
|
|
|
|
const VkImageType imageType = mappedImage->info().type;
|
2018-03-14 00:45:07 +01:00
|
|
|
|
2018-03-24 17:02:24 +01:00
|
|
|
// Wait for the resource to become available
|
2018-03-14 16:40:28 +01:00
|
|
|
if (!WaitForResource(mappedImage, MapFlags))
|
|
|
|
return DXGI_ERROR_WAS_STILL_DRAWING;
|
|
|
|
|
|
|
|
// Query the subresource's memory layout and hope that
|
|
|
|
// the application respects the returned pitch values.
|
2018-03-22 12:58:26 +01:00
|
|
|
VkSubresourceLayout layout = mappedImage->querySubresourceLayout(subresource);
|
2018-03-14 16:40:28 +01:00
|
|
|
pMappedResource->pData = mappedImage->mapPtr(layout.offset);
|
|
|
|
pMappedResource->RowPitch = imageType >= VK_IMAGE_TYPE_2D ? layout.rowPitch : layout.size;
|
|
|
|
pMappedResource->DepthPitch = imageType >= VK_IMAGE_TYPE_3D ? layout.depthPitch : layout.size;
|
|
|
|
return S_OK;
|
2018-03-14 14:40:09 +01:00
|
|
|
} else {
|
2018-11-08 18:09:05 +01:00
|
|
|
VkExtent3D levelExtent = mappedImage->mipLevelExtent(subresource.mipLevel);
|
|
|
|
VkExtent3D blockCount = util::computeBlockCount(levelExtent, formatInfo->blockSize);
|
2018-03-14 16:40:28 +01:00
|
|
|
|
2019-01-09 17:56:53 +01:00
|
|
|
DxvkBufferSliceHandle physSlice;
|
2018-03-14 00:45:07 +01:00
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
if (MapType == D3D11_MAP_WRITE_DISCARD) {
|
|
|
|
// We do not have to preserve the contents of the
|
|
|
|
// buffer if the entire image gets discarded.
|
2019-01-09 17:56:53 +01:00
|
|
|
physSlice = mappedBuffer->allocSlice();
|
2018-03-10 23:32:15 +01:00
|
|
|
|
2018-03-14 00:45:07 +01:00
|
|
|
EmitCs([
|
2019-01-09 17:56:53 +01:00
|
|
|
cImageBuffer = mappedBuffer,
|
|
|
|
cBufferSlice = physSlice
|
2018-03-14 00:45:07 +01:00
|
|
|
] (DxvkContext* ctx) {
|
2019-01-09 17:56:53 +01:00
|
|
|
ctx->invalidateBuffer(cImageBuffer, cBufferSlice);
|
2018-03-14 00:45:07 +01:00
|
|
|
});
|
2018-03-14 16:40:28 +01:00
|
|
|
} else {
|
|
|
|
// When using any map mode which requires the image contents
|
|
|
|
// to be preserved, and if the GPU has write access to the
|
|
|
|
// image, copy the current image contents into the buffer.
|
2019-03-26 15:14:56 +01:00
|
|
|
if (pResource->Desc()->Usage == D3D11_USAGE_STAGING) {
|
2018-11-08 18:09:05 +01:00
|
|
|
auto subresourceLayers = vk::makeSubresourceLayers(subresource);
|
2018-03-14 16:40:28 +01:00
|
|
|
|
|
|
|
EmitCs([
|
|
|
|
cImageBuffer = mappedBuffer,
|
|
|
|
cImage = mappedImage,
|
|
|
|
cSubresources = subresourceLayers,
|
2019-03-26 15:14:56 +01:00
|
|
|
cLevelExtent = levelExtent,
|
|
|
|
cPackedFormat = packedFormat
|
2018-03-14 16:40:28 +01:00
|
|
|
] (DxvkContext* ctx) {
|
2019-03-26 15:14:56 +01:00
|
|
|
if (cSubresources.aspectMask != (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
|
|
|
|
ctx->copyImageToBuffer(
|
|
|
|
cImageBuffer, 0, VkExtent2D { 0u, 0u },
|
|
|
|
cImage, cSubresources, VkOffset3D { 0, 0, 0 },
|
|
|
|
cLevelExtent);
|
|
|
|
} else {
|
|
|
|
ctx->copyDepthStencilImageToPackedBuffer(
|
|
|
|
cImageBuffer, 0, cImage, cSubresources,
|
|
|
|
VkOffset2D { 0, 0 },
|
|
|
|
VkExtent2D { cLevelExtent.width, cLevelExtent.height },
|
|
|
|
cPackedFormat);
|
|
|
|
}
|
2018-03-14 16:40:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-09 17:56:53 +01:00
|
|
|
WaitForResource(mappedBuffer, 0);
|
|
|
|
physSlice = mappedBuffer->getSliceHandle();
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
2018-03-14 00:45:07 +01:00
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
// Set up map pointer. Data is tightly packed within the mapped buffer.
|
2019-01-09 17:56:53 +01:00
|
|
|
pMappedResource->pData = physSlice.mapPtr;
|
2018-03-14 16:40:28 +01:00
|
|
|
pMappedResource->RowPitch = formatInfo->elementSize * blockCount.width;
|
|
|
|
pMappedResource->DepthPitch = formatInfo->elementSize * blockCount.width * blockCount.height;
|
|
|
|
return S_OK;
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 14:40:09 +01:00
|
|
|
void D3D11ImmediateContext::UnmapImage(
|
|
|
|
D3D11CommonTexture* pResource,
|
2018-01-20 13:22:44 +01:00
|
|
|
UINT Subresource) {
|
2018-11-08 18:15:24 +01:00
|
|
|
if (pResource->GetMapType() == D3D11_MAP_READ)
|
|
|
|
return;
|
|
|
|
|
2018-03-14 14:40:09 +01:00
|
|
|
if (pResource->GetMapMode() == D3D11_COMMON_TEXTURE_MAP_MODE_BUFFER) {
|
2018-01-20 13:22:44 +01:00
|
|
|
// Now that data has been written into the buffer,
|
|
|
|
// we need to copy its contents into the image
|
2018-03-14 14:40:09 +01:00
|
|
|
const Rc<DxvkImage> mappedImage = pResource->GetImage();
|
|
|
|
const Rc<DxvkBuffer> mappedBuffer = pResource->GetMappedBuffer();
|
2018-03-14 00:45:07 +01:00
|
|
|
|
2018-03-14 14:40:09 +01:00
|
|
|
VkImageSubresource subresource = pResource->GetMappedSubresource();
|
2018-03-14 00:45:07 +01:00
|
|
|
|
|
|
|
VkExtent3D levelExtent = mappedImage
|
|
|
|
->mipLevelExtent(subresource.mipLevel);
|
|
|
|
|
|
|
|
VkImageSubresourceLayers subresourceLayers = {
|
|
|
|
subresource.aspectMask,
|
|
|
|
subresource.mipLevel,
|
|
|
|
subresource.arrayLayer, 1 };
|
|
|
|
|
|
|
|
EmitCs([
|
|
|
|
cSrcBuffer = mappedBuffer,
|
|
|
|
cDstImage = mappedImage,
|
|
|
|
cDstLayers = subresourceLayers,
|
2019-03-26 15:14:56 +01:00
|
|
|
cDstLevelExtent = levelExtent,
|
|
|
|
cPackedFormat = GetPackedDepthStencilFormat(pResource->Desc()->Format)
|
2018-03-14 00:45:07 +01:00
|
|
|
] (DxvkContext* ctx) {
|
2019-03-26 15:14:56 +01:00
|
|
|
if (cPackedFormat == VK_FORMAT_UNDEFINED) {
|
|
|
|
ctx->copyBufferToImage(cDstImage, cDstLayers,
|
|
|
|
VkOffset3D { 0, 0, 0 }, cDstLevelExtent,
|
|
|
|
cSrcBuffer, 0, { 0u, 0u });
|
|
|
|
} else {
|
|
|
|
ctx->copyPackedBufferToDepthStencilImage(
|
|
|
|
cDstImage, cDstLayers,
|
|
|
|
VkOffset2D { 0, 0 },
|
|
|
|
VkExtent2D { cDstLevelExtent.width, cDstLevelExtent.height },
|
|
|
|
cSrcBuffer, 0, cPackedFormat);
|
|
|
|
}
|
2018-03-14 00:45:07 +01:00
|
|
|
});
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
2018-03-14 16:40:28 +01:00
|
|
|
|
|
|
|
pResource->ClearMappedSubresource();
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-03 16:41:44 +02:00
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::SwapDeviceContextState(
|
|
|
|
ID3DDeviceContextState* pState,
|
|
|
|
ID3DDeviceContextState** ppPreviousState) {
|
|
|
|
InitReturnPtr(ppPreviousState);
|
|
|
|
|
|
|
|
if (!pState)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Com<D3D11DeviceContextState> oldState = std::move(m_stateObject);
|
|
|
|
Com<D3D11DeviceContextState> newState = static_cast<D3D11DeviceContextState*>(pState);
|
|
|
|
|
|
|
|
if (oldState == nullptr)
|
|
|
|
oldState = new D3D11DeviceContextState(m_parent);
|
|
|
|
|
|
|
|
if (ppPreviousState)
|
|
|
|
*ppPreviousState = oldState.ref();
|
|
|
|
|
|
|
|
m_stateObject = newState;
|
|
|
|
|
|
|
|
oldState->SetState(m_state);
|
|
|
|
newState->GetState(m_state);
|
|
|
|
|
|
|
|
RestoreState();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-21 18:04:22 +01:00
|
|
|
void D3D11ImmediateContext::SynchronizeCsThread() {
|
2018-12-30 20:47:04 +01:00
|
|
|
D3D10DeviceLock lock = LockContext();
|
|
|
|
|
2018-01-21 18:04:22 +01:00
|
|
|
// Dispatch current chunk so that all commands
|
|
|
|
// recorded prior to this function will be run
|
2018-01-23 12:03:26 +01:00
|
|
|
FlushCsChunk();
|
2018-01-21 18:04:22 +01:00
|
|
|
|
|
|
|
m_csThread.synchronize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void D3D11ImmediateContext::SynchronizeDevice() {
|
2018-01-20 13:22:44 +01:00
|
|
|
m_device->waitForIdle();
|
|
|
|
}
|
|
|
|
|
2018-01-20 22:52:18 +01:00
|
|
|
|
2018-03-10 23:32:15 +01:00
|
|
|
bool D3D11ImmediateContext::WaitForResource(
|
|
|
|
const Rc<DxvkResource>& Resource,
|
|
|
|
UINT MapFlags) {
|
2018-03-24 17:02:24 +01:00
|
|
|
// Some games (e.g. The Witcher 3) do not work correctly
|
|
|
|
// when a map fails with D3D11_MAP_FLAG_DO_NOT_WAIT set
|
2018-08-07 14:58:08 +02:00
|
|
|
if (!m_parent->GetOptions()->allowMapFlagNoWait)
|
2018-03-24 17:02:24 +01:00
|
|
|
MapFlags &= ~D3D11_MAP_FLAG_DO_NOT_WAIT;
|
|
|
|
|
2018-03-10 23:32:15 +01:00
|
|
|
// Wait for the any pending D3D11 command to be executed
|
|
|
|
// on the CS thread so that we can determine whether the
|
|
|
|
// resource is currently in use or not.
|
|
|
|
SynchronizeCsThread();
|
|
|
|
|
|
|
|
if (Resource->isInUse()) {
|
2018-07-23 16:08:01 +02:00
|
|
|
if (MapFlags & D3D11_MAP_FLAG_DO_NOT_WAIT) {
|
|
|
|
// We don't have to wait, but misbehaving games may
|
|
|
|
// still try to spin on `Map` until the resource is
|
|
|
|
// idle, so we should flush pending commands
|
2018-10-17 17:22:52 +02:00
|
|
|
FlushImplicit(FALSE);
|
2018-03-22 11:02:14 +01:00
|
|
|
return false;
|
2018-07-23 16:08:01 +02:00
|
|
|
} else {
|
|
|
|
// Make sure pending commands using the resource get
|
|
|
|
// executed on the the GPU if we have to wait for it
|
|
|
|
Flush();
|
2018-09-01 21:25:39 +02:00
|
|
|
SynchronizeCsThread();
|
2018-07-23 16:08:01 +02:00
|
|
|
|
|
|
|
while (Resource->isInUse())
|
|
|
|
dxvk::this_thread::yield();
|
|
|
|
}
|
2018-03-10 23:32:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-27 16:07:38 +02:00
|
|
|
void D3D11ImmediateContext::EmitCsChunk(DxvkCsChunkRef&& chunk) {
|
2018-01-23 12:03:26 +01:00
|
|
|
m_csThread.dispatchChunk(std::move(chunk));
|
2018-03-22 12:58:26 +01:00
|
|
|
m_csIsBusy = true;
|
2018-01-20 22:52:18 +01:00
|
|
|
}
|
2018-06-04 23:31:49 +02:00
|
|
|
|
|
|
|
|
2018-10-17 17:22:52 +02:00
|
|
|
void D3D11ImmediateContext::FlushImplicit(BOOL StrongHint) {
|
2018-06-04 23:31:49 +02:00
|
|
|
// Flush only if the GPU is about to go idle, in
|
|
|
|
// order to keep the number of submissions low.
|
2018-10-17 17:22:52 +02:00
|
|
|
if (StrongHint || m_device->pendingSubmissions() <= MaxPendingSubmits) {
|
2018-06-04 23:31:49 +02:00
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
// Prevent flushing too often in short intervals.
|
|
|
|
if (now - m_lastFlush >= std::chrono::microseconds(MinFlushIntervalUs))
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 22:52:18 +01:00
|
|
|
|
2018-07-15 19:45:40 +02:00
|
|
|
}
|