Allow to create WddmAllocation with multiple handles

Change-Id: Iac9df91b08a6ce610b985586dfb6b5f63dc668cb
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski
2019-03-05 09:25:18 +01:00
committed by sys_ocldev
parent 7df0be9a13
commit 5b22a50b28
13 changed files with 120 additions and 119 deletions

View File

@@ -290,7 +290,7 @@ bool Wddm::makeResident(const D3DKMT_HANDLE *handles, uint32_t count, bool cantT
bool Wddm::mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr) {
void *mapPtr = allocation->getReservedAddress() != nullptr ? allocation->getReservedAddress() : cpuPtr;
return mapGpuVirtualAddressImpl(allocation->gmm, allocation->handle, mapPtr, allocation->getGpuAddressToModify(),
return mapGpuVirtualAddressImpl(allocation->gmm, allocation->getDefaultHandle(), mapPtr, allocation->getGpuAddressToModify(),
MemoryManager::selectHeap(allocation, mapPtr, *hardwareInfoTable[gfxPlatform->eProductFamily]));
}
@@ -466,8 +466,8 @@ bool Wddm::createAllocation64k(const Gmm *gmm, D3DKMT_HANDLE &outHandle) {
}
outHandle = AllocationInfo.hAllocation;
kmDafListener->notifyWriteTarget(featureTable->ftrKmdDaf, adapter, device, outHandle, gdi->escape);
return true;
}
@@ -590,7 +590,7 @@ bool Wddm::openSharedHandle(D3DKMT_HANDLE handle, WddmAllocation *alloc) {
status = gdi->openResource(&OpenResource);
DEBUG_BREAK_IF(status != STATUS_SUCCESS);
alloc->handle = allocationInfo[0].hAllocation;
alloc->setDefaultHandle(allocationInfo[0].hAllocation);
alloc->resourceHandle = OpenResource.hResource;
auto resourceInfo = const_cast<void *>(allocationInfo[0].pPrivateDriverData);
@@ -627,7 +627,7 @@ bool Wddm::openNTHandle(HANDLE handle, WddmAllocation *alloc) {
status = gdi->openResourceFromNtHandle(&openResourceFromNtHandle);
DEBUG_BREAK_IF(status != STATUS_SUCCESS);
alloc->handle = allocationInfo2[0].hAllocation;
alloc->setDefaultHandle(allocationInfo2[0].hAllocation);
alloc->resourceHandle = openResourceFromNtHandle.hResource;
auto resourceInfo = const_cast<void *>(allocationInfo2[0].pPrivateDriverData);
@@ -652,7 +652,6 @@ void *Wddm::lockResource(const D3DKMT_HANDLE &handle, bool applyMakeResidentPrio
DEBUG_BREAK_IF(status != STATUS_SUCCESS);
kmDafLock(handle);
return lock2.pData;
}

View File

@@ -9,8 +9,9 @@
namespace OCLRT {
std::string WddmAllocation::getAllocationInfoString() const {
std::stringstream ss;
ss << " Handle: " << handle;
return ss.str();
return getHandleInfoString();
}
uint32_t WddmAllocation::getNumHandles() const {
return 1u;
}
} // namespace OCLRT

View File

@@ -25,10 +25,6 @@ constexpr size_t trimListUnusedPosition = std::numeric_limits<size_t>::max();
class WddmAllocation : public GraphicsAllocation {
public:
// OS assigned fields
D3DKMT_HANDLE handle = 0u; // set by createAllocation
D3DKMT_HANDLE resourceHandle = 0u; // used by shared resources
WddmAllocation(AllocationType allocationType, void *cpuPtrIn, size_t sizeIn, void *reservedAddr, MemoryPool::Type pool, bool multiOsContextCapable)
: GraphicsAllocation(allocationType, cpuPtrIn, castToUint64(cpuPtrIn), 0llu, sizeIn, pool, multiOsContextCapable), reservedAddressSpace(reservedAddr) {
trimCandidateListPositions.fill(trimListUnusedPosition);
@@ -50,6 +46,13 @@ class WddmAllocation : public GraphicsAllocation {
ResidencyData &getResidencyData() {
return residency;
}
const std::array<D3DKMT_HANDLE, maxHandleCount> &getHandles() const { return handles; }
D3DKMT_HANDLE &getHandleToModify(uint32_t handleIndex) { return handles[handleIndex]; }
D3DKMT_HANDLE getDefaultHandle() const { return handles[0]; }
void setDefaultHandle(D3DKMT_HANDLE handle) {
handles[0] = handle;
}
uint32_t getNumHandles() const;
void setTrimCandidateListPosition(uint32_t osContextId, size_t position) {
trimCandidateListPositions[osContextId] = position;
@@ -71,12 +74,23 @@ class WddmAllocation : public GraphicsAllocation {
}
void setGpuAddress(uint64_t graphicsAddress) { this->gpuAddress = graphicsAddress; }
void setCpuAddress(void *cpuPtr) { this->cpuPtr = cpuPtr; }
bool needsMakeResidentBeforeLock = false;
std::string getAllocationInfoString() const override;
uint64_t &getGpuAddressToModify() { return gpuAddress; }
// OS assigned fields
D3DKMT_HANDLE resourceHandle = 0u; // used by shared resources
bool needsMakeResidentBeforeLock = false;
protected:
std::string getHandleInfoString() const {
std::stringstream ss;
for (auto &handle : handles) {
ss << " Handle: " << handle;
}
return ss.str();
}
std::array<D3DKMT_HANDLE, maxHandleCount> handles{};
ResidencyData residency;
std::array<size_t, maxOsContextCount> trimCandidateListPositions;
void *reservedAddressSpace = nullptr;

View File

@@ -107,13 +107,13 @@ FlushStamp WddmCommandStreamReceiver<GfxFamily>::flush(BatchBuffer &batchBuffer,
template <typename GfxFamily>
void WddmCommandStreamReceiver<GfxFamily>::makeResident(GraphicsAllocation &gfxAllocation) {
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", reinterpret_cast<WddmAllocation *>(&gfxAllocation));
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", static_cast<WddmAllocation *>(&gfxAllocation));
if (gfxAllocation.fragmentsStorage.fragmentCount == 0) {
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation handle =", reinterpret_cast<WddmAllocation *>(&gfxAllocation)->handle);
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation default handle =", static_cast<WddmAllocation *>(&gfxAllocation)->getDefaultHandle());
} else {
for (uint32_t allocationId = 0; allocationId < reinterpret_cast<WddmAllocation *>(&gfxAllocation)->fragmentsStorage.fragmentCount; allocationId++) {
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "fragment handle =", reinterpret_cast<WddmAllocation *>(&gfxAllocation)->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle);
for (uint32_t allocationId = 0; allocationId < static_cast<WddmAllocation *>(&gfxAllocation)->fragmentsStorage.fragmentCount; allocationId++) {
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "fragment handle =", static_cast<WddmAllocation *>(&gfxAllocation)->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle);
}
}
@@ -197,7 +197,7 @@ void WddmCommandStreamReceiver<GfxFamily>::kmDafLockAllocations(ResidencyContain
if ((GraphicsAllocation::AllocationType::LINEAR_STREAM == graphicsAllocation->getAllocationType()) ||
(GraphicsAllocation::AllocationType::FILL_PATTERN == graphicsAllocation->getAllocationType()) ||
(GraphicsAllocation::AllocationType::COMMAND_BUFFER == graphicsAllocation->getAllocationType())) {
wddm->kmDafLock(static_cast<WddmAllocation *>(graphicsAllocation)->handle);
wddm->kmDafLock(static_cast<WddmAllocation *>(graphicsAllocation)->getDefaultHandle());
}
}
}

View File

@@ -67,7 +67,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(const Allocati
auto gmm = new Gmm(nullptr, sizeAligned, false, allocationData.flags.preferRenderCompressed, true, {});
wddmAllocation->gmm = gmm;
if (!wddm->createAllocation64k(gmm, wddmAllocation->handle)) {
if (!wddm->createAllocation64k(gmm, wddmAllocation->getHandleToModify(0u))) {
delete gmm;
return nullptr;
}
@@ -257,7 +257,7 @@ void WddmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAll
fragment.osInternalStorage = new OsHandle();
fragment.osInternalStorage->gpuPtr = gfxAllocation->getGpuAddress();
fragment.osInternalStorage->handle = wddmMemory->handle;
fragment.osInternalStorage->handle = wddmMemory->getDefaultHandle();
fragment.osInternalStorage->gmm = gfxAllocation->gmm;
fragment.residency = &wddmMemory->getResidencyData();
hostPtrManager->storeFragment(fragment);
@@ -276,13 +276,13 @@ void WddmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *g
void *WddmMemoryManager::lockResourceImpl(GraphicsAllocation &graphicsAllocation) {
auto &wddmAllocation = static_cast<WddmAllocation &>(graphicsAllocation);
return wddm->lockResource(wddmAllocation.handle, wddmAllocation.needsMakeResidentBeforeLock);
return wddm->lockResource(wddmAllocation.getDefaultHandle(), wddmAllocation.needsMakeResidentBeforeLock);
}
void WddmMemoryManager::unlockResourceImpl(GraphicsAllocation &graphicsAllocation) {
auto &wddmAllocation = static_cast<WddmAllocation &>(graphicsAllocation);
wddm->unlockResource(wddmAllocation.handle);
wddm->unlockResource(wddmAllocation.getDefaultHandle());
if (wddmAllocation.needsMakeResidentBeforeLock) {
auto evictionStatus = wddm->evictTemporaryResource(wddmAllocation.handle);
auto evictionStatus = wddm->evictTemporaryResource(wddmAllocation.getDefaultHandle());
DEBUG_BREAK_IF(evictionStatus == EvictionStatus::FAILED);
}
}
@@ -315,14 +315,14 @@ void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation
input->fragmentsStorage.fragmentCount > 0) {
cleanGraphicsMemoryCreatedFromHostPtr(gfxAllocation);
} else {
D3DKMT_HANDLE *allocationHandles = nullptr;
const D3DKMT_HANDLE *allocationHandles = nullptr;
uint32_t allocationCount = 0;
D3DKMT_HANDLE resourceHandle = 0;
if (input->peekSharedHandle()) {
resourceHandle = input->resourceHandle;
} else {
allocationHandles = &input->handle;
allocationCount = 1;
allocationHandles = input->getHandles().data();
allocationCount = input->getNumHandles();
}
auto status = tryDeferDeletions(allocationHandles, allocationCount, resourceHandle);
DEBUG_BREAK_IF(!status);
@@ -355,7 +355,7 @@ bool WddmMemoryManager::validateAllocation(WddmAllocation *alloc) {
if (alloc == nullptr)
return false;
auto size = alloc->getUnderlyingBufferSize();
if (alloc->getGpuAddress() == 0u || size == 0 || (alloc->handle == 0 && alloc->fragmentsStorage.fragmentCount == 0))
if (alloc->getGpuAddress() == 0u || size == 0 || (alloc->getDefaultHandle() == 0 && alloc->fragmentsStorage.fragmentCount == 0))
return false;
return true;
}
@@ -459,10 +459,10 @@ AlignedMallocRestrictions *WddmMemoryManager::getAlignedMallocRestrictions() {
}
bool WddmMemoryManager::createWddmAllocation(WddmAllocation *allocation) {
auto wddmSuccess = wddm->createAllocation(allocation->getAlignedCpuPtr(), allocation->gmm, allocation->handle);
auto wddmSuccess = wddm->createAllocation(allocation->getAlignedCpuPtr(), allocation->gmm, allocation->getHandleToModify(0u));
if (wddmSuccess == STATUS_GRAPHICS_NO_VIDEO_MEMORY && deferredDeleter) {
deferredDeleter->drain(true);
wddmSuccess = wddm->createAllocation(allocation->getAlignedCpuPtr(), allocation->gmm, allocation->handle);
wddmSuccess = wddm->createAllocation(allocation->getAlignedCpuPtr(), allocation->gmm, allocation->getHandleToModify(0u));
}
if (wddmSuccess == STATUS_SUCCESS) {
@@ -472,7 +472,7 @@ bool WddmMemoryManager::createWddmAllocation(WddmAllocation *allocation) {
mapSuccess = wddm->mapGpuVirtualAddress(allocation, allocation->getAlignedCpuPtr());
}
if (!mapSuccess) {
wddm->destroyAllocations(&allocation->handle, 1, allocation->resourceHandle);
wddm->destroyAllocations(allocation->getHandles().data(), allocation->getNumHandles(), allocation->resourceHandle);
wddmSuccess = STATUS_UNSUCCESSFUL;
}
}

View File

@@ -57,7 +57,7 @@ WddmAllocation *WddmResidencyController::getTrimCandidateHead() {
while ((trimCandidateList[i] == nullptr) && (i < size))
i++;
return reinterpret_cast<WddmAllocation *>(trimCandidateList[i]);
return static_cast<WddmAllocation *>(trimCandidateList[i]);
}
void WddmResidencyController::addToTrimCandidateList(GraphicsAllocation *allocation) {
@@ -195,14 +195,13 @@ void WddmResidencyController::trimResidency(D3DDDI_TRIMRESIDENCYSET_FLAGS flags,
if (wasAllocationUsedSinceLastTrim(wddmAllocation->getResidencyData().getFenceValueForContextId(osContextId))) {
break;
}
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation: handle =", wddmAllocation->handle, "lastFence =", (wddmAllocation)->getResidencyData().getFenceValueForContextId(osContextId));
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation: default handle =", wddmAllocation->getDefaultHandle(), "lastFence =", (wddmAllocation)->getResidencyData().getFenceValueForContextId(osContextId));
uint32_t fragmentsToEvict = 0;
if (wddmAllocation->fragmentsStorage.fragmentCount == 0) {
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "Evict allocation: handle =", wddmAllocation->handle, "lastFence =", (wddmAllocation)->getResidencyData().getFenceValueForContextId(osContextId));
this->wddm.evict(&wddmAllocation->handle, 1, sizeToTrim);
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "Evict allocation: default handle =", wddmAllocation->getDefaultHandle(), "lastFence =", (wddmAllocation)->getResidencyData().getFenceValueForContextId(osContextId));
this->wddm.evict(wddmAllocation->getHandles().data(), wddmAllocation->getNumHandles(), sizeToTrim);
}
for (uint32_t allocationId = 0; allocationId < wddmAllocation->fragmentsStorage.fragmentCount; allocationId++) {
@@ -213,11 +212,9 @@ void WddmResidencyController::trimResidency(D3DDDI_TRIMRESIDENCYSET_FLAGS flags,
fragmentStorageData.residency->resident[osContextId] = false;
}
}
if (fragmentsToEvict != 0) {
this->wddm.evict((D3DKMT_HANDLE *)fragmentEvictHandles, fragmentsToEvict, sizeToTrim);
}
wddmAllocation->getResidencyData().resident[osContextId] = false;
this->removeFromTrimCandidateList(wddmAllocation, false);
@@ -261,7 +258,7 @@ bool WddmResidencyController::trimResidencyToBudget(uint64_t bytes) {
}
if (wddmAllocation->fragmentsStorage.fragmentCount == 0) {
this->wddm.evict(&wddmAllocation->handle, 1, sizeToTrim);
this->wddm.evict(wddmAllocation->getHandles().data(), wddmAllocation->getNumHandles(), sizeToTrim);
sizeEvicted = wddmAllocation->getAlignedSize();
} else {
auto &fragmentStorageData = wddmAllocation->fragmentsStorage.fragmentStorageData;
@@ -310,7 +307,7 @@ bool WddmResidencyController::makeResidentResidencyAllocations(ResidencyContaine
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", this->getMonitoredFence().currentFenceValue);
for (uint32_t i = 0; i < residencyCount; i++) {
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
WddmAllocation *allocation = static_cast<WddmAllocation *>(allocationsForResidency[i]);
ResidencyData &residencyData = allocation->getResidencyData();
bool fragmentResidency[3] = {false, false, false};
@@ -332,7 +329,8 @@ bool WddmResidencyController::makeResidentResidencyAllocations(ResidencyContaine
handlesForResidency[totalHandlesCount++] = allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle;
}
} else if (!residencyData.resident[osContextId]) {
handlesForResidency[totalHandlesCount++] = allocation->handle;
memcpy_s(&handlesForResidency[totalHandlesCount], allocation->getNumHandles() * sizeof(D3DKMT_HANDLE), allocation->getHandles().data(), allocation->getNumHandles() * sizeof(D3DKMT_HANDLE));
totalHandlesCount += allocation->getNumHandles();
}
}
@@ -356,7 +354,7 @@ bool WddmResidencyController::makeResidentResidencyAllocations(ResidencyContaine
if (result == true) {
for (uint32_t i = 0; i < residencyCount; i++) {
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
WddmAllocation *allocation = static_cast<WddmAllocation *>(allocationsForResidency[i]);
// Update fence value not to early destroy / evict allocation
const auto currentFence = this->getMonitoredFence().currentFenceValue;
allocation->getResidencyData().updateCompletionData(currentFence, this->osContextId);
@@ -379,7 +377,7 @@ void WddmResidencyController::makeNonResidentEvictionAllocations(ResidencyContai
const size_t residencyCount = evictionAllocations.size();
for (uint32_t i = 0; i < residencyCount; i++) {
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(evictionAllocations[i]);
WddmAllocation *allocation = static_cast<WddmAllocation *>(evictionAllocations[i]);
this->addToTrimCandidateList(allocation);
}
}