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"
|
|
|
|
|
2019-05-09 16:56:35 +02:00
|
|
|
constexpr static uint32_t MinFlushIntervalUs = 750;
|
|
|
|
constexpr static uint32_t IncFlushIntervalUs = 250;
|
|
|
|
constexpr static uint32_t MaxPendingSubmits = 6;
|
2018-07-15 19:45:40 +02:00
|
|
|
|
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() {
|
2019-10-14 01:56:34 +02:00
|
|
|
ULONG refCount = m_refCount++;
|
|
|
|
if (!refCount)
|
|
|
|
m_parent->AddRef();
|
2019-10-25 21:36:08 +02:00
|
|
|
return refCount + 1;
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE D3D11ImmediateContext::Release() {
|
2019-10-14 01:56:34 +02:00
|
|
|
ULONG refCount = --m_refCount;
|
|
|
|
if (!refCount)
|
|
|
|
m_parent->Release();
|
|
|
|
return refCount;
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2019-06-16 19:24:44 +02:00
|
|
|
if (!pAsync || (DataSize && !pData))
|
2018-10-17 17:19:07 +02:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2019-06-16 19:24:44 +02:00
|
|
|
// Check whether the data size is actually correct
|
|
|
|
if (DataSize && DataSize != pAsync->GetDataSize())
|
2018-06-08 12:29:24 +02:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2019-06-16 19:24:44 +02:00
|
|
|
// Passing a non-null pData is actually allowed if
|
|
|
|
// DataSize is 0, but we should ignore that pointer
|
|
|
|
pData = DataSize ? pData : nullptr;
|
|
|
|
|
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,
|
2019-07-15 16:24:09 +02:00
|
|
|
// flush the context so that we're keeping the GPU busy.
|
2019-05-09 16:38:07 +02:00
|
|
|
if (hr == S_FALSE) {
|
2019-07-15 16:24:09 +02:00
|
|
|
// Don't mark the event query as stalling if the app does
|
|
|
|
// not intend to spin on it. This reduces flushes on End.
|
|
|
|
if (!(GetDataFlags & D3D11_ASYNC_GETDATA_DONOTFLUSH))
|
|
|
|
query->NotifyStall();
|
|
|
|
|
|
|
|
// Ignore the DONOTFLUSH flag here as some games will spin
|
|
|
|
// on queries without ever flushing the context otherwise.
|
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() {
|
2019-09-16 13:12:13 +02:00
|
|
|
Flush1(D3D11_CONTEXT_TYPE_ALL, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void STDMETHODCALLTYPE D3D11ImmediateContext::Flush1(
|
|
|
|
D3D11_CONTEXT_TYPE ContextType,
|
|
|
|
HANDLE hEvent) {
|
2018-03-11 20:25:09 +01:00
|
|
|
m_parent->FlushInitContext();
|
2019-09-16 13:12:13 +02:00
|
|
|
|
|
|
|
if (hEvent)
|
|
|
|
Logger::warn("D3D11: Flush1: Ignoring event");
|
2018-03-11 20:25:09 +01:00
|
|
|
|
2018-11-29 20:59:40 +01:00
|
|
|
D3D10DeviceLock lock = LockContext();
|
|
|
|
|
2019-07-17 12:52:25 +02:00
|
|
|
if (m_csIsBusy || !m_csChunk->empty()) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-16 13:17:00 +02:00
|
|
|
HRESULT STDMETHODCALLTYPE D3D11ImmediateContext::Signal(
|
|
|
|
ID3D11Fence* pFence,
|
|
|
|
UINT64 Value) {
|
|
|
|
Logger::err("D3D11ImmediateContext::Signal: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE D3D11ImmediateContext::Wait(
|
|
|
|
ID3D11Fence* pFence,
|
|
|
|
UINT64 Value) {
|
|
|
|
Logger::err("D3D11ImmediateContext::Wait: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-09-17 18:24:31 +02:00
|
|
|
if (unlikely(!pResource))
|
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-06-03 15:30:04 +02:00
|
|
|
if (unlikely(FAILED(hr)))
|
|
|
|
*pMappedResource = D3D11_MAPPED_SUBRESOURCE();
|
2018-10-16 12:29:04 +02:00
|
|
|
|
|
|
|
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-09-17 18:24:31 +02:00
|
|
|
if (unlikely(!pMappedResource))
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
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-09-19 20:28:22 +02:00
|
|
|
if (!WaitForResource(pResource->GetBuffer(), MapType, 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();
|
2019-05-20 15:56:31 +02:00
|
|
|
const Rc<DxvkBuffer> mappedBuffer = pResource->GetMappedBuffer(Subresource);
|
2018-03-14 14:40:09 +01:00
|
|
|
|
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
|
|
|
|
2019-05-20 19:26:15 +02:00
|
|
|
if (unlikely(Subresource >= pResource->CountSubresources()))
|
|
|
|
return E_INVALIDARG;
|
2019-09-17 18:24:31 +02:00
|
|
|
|
|
|
|
if (likely(pMappedResource != nullptr)) {
|
|
|
|
// Resources with an unknown memory layout cannot return a pointer
|
|
|
|
if (pResource->Desc()->Usage == D3D11_USAGE_DEFAULT
|
|
|
|
&& pResource->Desc()->TextureLayout == D3D11_TEXTURE_LAYOUT_UNDEFINED)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
} else {
|
|
|
|
if (pResource->Desc()->Usage != D3D11_USAGE_DEFAULT)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
2019-05-20 19:26:15 +02:00
|
|
|
|
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(
|
2019-05-20 15:56:31 +02:00
|
|
|
formatInfo->aspectMask, Subresource);
|
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
|
2019-09-19 20:28:22 +02:00
|
|
|
if (!WaitForResource(mappedImage, MapType, MapFlags))
|
2018-03-14 16:40:28 +01:00
|
|
|
return DXGI_ERROR_WAS_STILL_DRAWING;
|
|
|
|
|
2019-08-03 19:11:19 +02:00
|
|
|
// Mark the given subresource as mapped
|
|
|
|
pResource->SetMapType(Subresource, MapType);
|
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
// Query the subresource's memory layout and hope that
|
|
|
|
// the application respects the returned pitch values.
|
2019-09-17 18:24:31 +02:00
|
|
|
if (pMappedResource) {
|
|
|
|
VkSubresourceLayout layout = mappedImage->querySubresourceLayout(subresource);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
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-05-17 12:44:36 +02:00
|
|
|
if (pResource->Desc()->Usage == D3D11_USAGE_STAGING
|
|
|
|
&& !pResource->CanUpdateMappedBufferEarly()) {
|
|
|
|
UpdateMappedBuffer(pResource, subresource);
|
|
|
|
MapFlags &= ~D3D11_MAP_FLAG_DO_NOT_WAIT;
|
2018-03-14 16:40:28 +01:00
|
|
|
}
|
|
|
|
|
2019-05-17 12:44:36 +02:00
|
|
|
// Wait for mapped buffer to become available
|
2019-09-19 20:28:22 +02:00
|
|
|
if (!WaitForResource(mappedBuffer, MapType, MapFlags))
|
2019-05-17 12:44:36 +02:00
|
|
|
return DXGI_ERROR_WAS_STILL_DRAWING;
|
|
|
|
|
2019-01-09 17:56:53 +01:00
|
|
|
physSlice = mappedBuffer->getSliceHandle();
|
2018-01-20 13:22:44 +01:00
|
|
|
}
|
2018-03-14 00:45:07 +01:00
|
|
|
|
2019-08-03 19:11:19 +02:00
|
|
|
// Mark the given subresource as mapped
|
|
|
|
pResource->SetMapType(Subresource, MapType);
|
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
// Set up map pointer. Data is tightly packed within the mapped buffer.
|
2019-09-17 18:24:31 +02:00
|
|
|
if (pMappedResource) {
|
|
|
|
pMappedResource->pData = physSlice.mapPtr;
|
|
|
|
pMappedResource->RowPitch = formatInfo->elementSize * blockCount.width;
|
|
|
|
pMappedResource->DepthPitch = formatInfo->elementSize * blockCount.width * blockCount.height;
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:40:28 +01:00
|
|
|
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) {
|
2019-05-20 15:56:31 +02:00
|
|
|
D3D11_MAP mapType = pResource->GetMapType(Subresource);
|
|
|
|
pResource->SetMapType(Subresource, D3D11_MAP(~0u));
|
|
|
|
|
|
|
|
if (mapType == D3D11_MAP(~0u)
|
|
|
|
|| mapType == D3D11_MAP_READ)
|
2018-11-08 18:15:24 +01:00
|
|
|
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();
|
2019-05-20 15:56:31 +02:00
|
|
|
const Rc<DxvkBuffer> mappedBuffer = pResource->GetMappedBuffer(Subresource);
|
|
|
|
|
|
|
|
VkFormat packedFormat = m_parent->LookupPackedFormat(
|
|
|
|
pResource->Desc()->Format, pResource->GetFormatMode()).Format;
|
|
|
|
|
|
|
|
auto formatInfo = imageFormatInfo(packedFormat);
|
|
|
|
auto subresource = pResource->GetSubresourceFromIndex(
|
|
|
|
formatInfo->aspectMask, Subresource);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-07-04 21:37:17 +02:00
|
|
|
if (m_csThread.isBusy())
|
|
|
|
m_csThread.synchronize();
|
2018-01-21 18:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2019-09-19 20:28:22 +02:00
|
|
|
D3D11_MAP MapType,
|
2018-03-10 23:32:15 +01:00
|
|
|
UINT MapFlags) {
|
2019-09-19 20:28:22 +02:00
|
|
|
// Some games might 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;
|
2019-09-19 20:28:22 +02:00
|
|
|
|
|
|
|
// Determine access type to wait for based on map mode
|
|
|
|
DxvkAccess access = MapType == D3D11_MAP_READ
|
|
|
|
? DxvkAccess::Write
|
|
|
|
: DxvkAccess::Read;
|
2018-03-24 17:02:24 +01:00
|
|
|
|
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.
|
2019-10-14 00:14:00 +02:00
|
|
|
if (!Resource->isInUse(access))
|
|
|
|
SynchronizeCsThread();
|
2018-03-10 23:32:15 +01:00
|
|
|
|
2019-09-19 20:28:22 +02:00
|
|
|
if (Resource->isInUse(access)) {
|
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
|
|
|
|
2019-09-19 20:28:22 +02:00
|
|
|
while (Resource->isInUse(access))
|
2018-07-23 16:08:01 +02:00
|
|
|
dxvk::this_thread::yield();
|
|
|
|
}
|
2018-03-10 23:32:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-19 13:21:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
void D3D11ImmediateContext::EmitCsChunk(DxvkCsChunkRef&& chunk) {
|
|
|
|
m_csThread.dispatchChunk(std::move(chunk));
|
|
|
|
m_csIsBusy = true;
|
|
|
|
}
|
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.
|
2019-05-09 16:56:35 +02:00
|
|
|
uint32_t pending = m_device->pendingSubmissions();
|
|
|
|
|
|
|
|
if (StrongHint || pending <= MaxPendingSubmits) {
|
2018-06-04 23:31:49 +02:00
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2019-05-09 16:56:35 +02:00
|
|
|
uint32_t delay = MinFlushIntervalUs
|
|
|
|
+ IncFlushIntervalUs * pending;
|
|
|
|
|
2018-06-04 23:31:49 +02:00
|
|
|
// Prevent flushing too often in short intervals.
|
2019-05-09 16:56:35 +02:00
|
|
|
if (now - m_lastFlush >= std::chrono::microseconds(delay))
|
2018-06-04 23:31:49 +02:00
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 22:52:18 +01:00
|
|
|
|
2018-07-15 19:45:40 +02:00
|
|
|
}
|