diff --git a/icd/api/cache_adapter.cpp b/icd/api/cache_adapter.cpp index 2fa30df9..9bedd9bb 100644 --- a/icd/api/cache_adapter.cpp +++ b/icd/api/cache_adapter.cpp @@ -33,21 +33,21 @@ #include "include/vk_utils.h" #include "palMetroHash.h" +#include "pipeline_binary_cache.h" namespace vk { // ===================================================================================================================== CacheAdapter* CacheAdapter::Create( - Instance* pInstance, - PipelineBinaryCache* pPipelineBinaryCache) + PipelineBinaryCache* pPipelineBinaryCache) { CacheAdapter* pObj = nullptr; - void* pMem = pInstance->AllocMem(sizeof(CacheAdapter), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = pPipelineBinaryCache->AllocMem(sizeof(CacheAdapter)); if (pMem != nullptr) { - pObj = VK_PLACEMENT_NEW(pMem) CacheAdapter(pInstance, pPipelineBinaryCache); + pObj = VK_PLACEMENT_NEW(pMem) CacheAdapter(pPipelineBinaryCache); } return pObj; @@ -56,17 +56,17 @@ CacheAdapter* CacheAdapter::Create( // ===================================================================================================================== void CacheAdapter::Destroy() { + PipelineBinaryCache* pCache = m_pPipelineBinaryCache; + void* pMem = this; Util::Destructor(this); - m_pInstance->FreeMem(this); + pCache->FreeMem(pMem); } // ===================================================================================================================== CacheAdapter::CacheAdapter( - Instance* pInstance, - PipelineBinaryCache* pPipelineBinaryCache) + PipelineBinaryCache* pPipelineBinaryCache) : - m_pInstance { pInstance }, - m_pPipelineBinaryCache { pPipelineBinaryCache } + m_pPipelineBinaryCache { pPipelineBinaryCache } { } @@ -83,8 +83,8 @@ Result CacheAdapter::GetEntry( { Result result = Result::Success; bool mustPopulate = false; - Util::QueryResult* pQuery = static_cast(m_pInstance->AllocMem(sizeof(Util::QueryResult), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); + Util::QueryResult* pQuery = + static_cast(m_pPipelineBinaryCache->AllocMem(sizeof(Util::QueryResult))); if (pQuery != nullptr) { @@ -110,7 +110,7 @@ Result CacheAdapter::GetEntry( } else if (palResult != Util::Result::Success) { - m_pInstance->FreeMem(pQuery); + m_pPipelineBinaryCache->FreeMem(pQuery); pQuery = nullptr; result = Result::ErrorUnknown; if (palResult == Util::Result::NotFound) @@ -157,7 +157,7 @@ void CacheAdapter::ReleaseEntry( { Util::QueryResult* pQuery = static_cast(rawHandle); m_pPipelineBinaryCache->ReleaseCacheRef(pQuery); - m_pInstance->FreeMem(rawHandle); + m_pPipelineBinaryCache->FreeMem(rawHandle); } } diff --git a/icd/api/include/cache_adapter.h b/icd/api/include/cache_adapter.h index 699246fc..860cefb5 100644 --- a/icd/api/include/cache_adapter.h +++ b/icd/api/include/cache_adapter.h @@ -30,21 +30,19 @@ */ #pragma once -#include "include/vk_instance.h" -#include "pipeline_binary_cache.h" #include "vkgcDefs.h" namespace vk { using namespace Vkgc; +class PipelineBinaryCache; + // An adapter class that implements ICache in terms of a simple Get/Set interface. class CacheAdapter : public ICache { public: - static CacheAdapter* Create( - Instance* pInstance, - PipelineBinaryCache* pPipelineBinaryCache); + static CacheAdapter* Create(PipelineBinaryCache* pPipelineBinaryCache); ~CacheAdapter(); void Destroy(); @@ -71,11 +69,8 @@ class CacheAdapter : public ICache size_t* pDataLen) override; private: - explicit CacheAdapter( - Instance* pInstance, - PipelineBinaryCache* pPipelineBinaryCache); + explicit CacheAdapter(PipelineBinaryCache* pPipelineBinaryCache); - Instance* m_pInstance; PipelineBinaryCache* m_pPipelineBinaryCache; }; diff --git a/icd/api/include/pipeline_binary_cache.h b/icd/api/include/pipeline_binary_cache.h index dfcc3b77..f00c9600 100644 --- a/icd/api/include/pipeline_binary_cache.h +++ b/icd/api/include/pipeline_binary_cache.h @@ -40,6 +40,10 @@ namespace Util { class IPlatformKey; + +#if ICD_GPUOPEN_DEVMODE_BUILD +class DevModeMgr; +#endif } // namespace Util namespace vk @@ -65,22 +69,37 @@ class PipelineBinaryCache using CacheId = Util::MetroHash::Hash; static PipelineBinaryCache* Create( - Instance* pInstance, - size_t initDataSize, - const void* pInitData, - bool internal, - const Vkgc::GfxIpVersion& gfxIp, - const PhysicalDevice* pPhysicalDevice); + VkAllocationCallbacks* pAllocationCallbacks, + Util::IPlatformKey* pKey, + const Vkgc::GfxIpVersion& gfxIp, + const vk::RuntimeSettings& settings, + const char* pDefaultCacheFilePath, +#if ICD_GPUOPEN_DEVMODE_BUILD + vk::DevModeMgr* pDevModeMgr, +#endif + size_t initDataSize, + const void* pInitData, + bool internal); static bool IsValidBlob( - const PhysicalDevice* pPhysicalDevice, - size_t dataSize, - const void* pData); + VkAllocationCallbacks* pAllocationCallbacks, + Util::IPlatformKey* pKey, + size_t dataSize, + const void* pData); + + static Util::Result CalculateHashId( + VkAllocationCallbacks* pAllocationCallbacks, + const Util::IPlatformKey* pPlatformKey, + const void* pData, + size_t dataSize, + uint8_t* pHashId); ~PipelineBinaryCache(); VkResult Initialize( - const PhysicalDevice* pPhysicalDevice); + const RuntimeSettings& settings, + const char* pDefaultCacheFilePath, + const Util::IPlatformKey* pKey); Util::Result QueryPipelineBinary( const CacheId* pCacheId, @@ -154,6 +173,12 @@ class PipelineBinaryCache void FreePipelineBinary(const void* pPipelineBinary); + void* AllocMem( + size_t memSize) const; + + void FreeMem( + void* pMem) const; + void Destroy(); CacheAdapter* GetCacheAdapter() { return m_pCacheAdapter; } @@ -164,7 +189,7 @@ class PipelineBinaryCache PAL_DISALLOW_COPY_AND_ASSIGN(PipelineBinaryCache); explicit PipelineBinaryCache( - Instance* pInstance, + VkAllocationCallbacks* pAllocationCallbacks, const Vkgc::GfxIpVersion& gfxIp, bool internal); @@ -180,7 +205,7 @@ class PipelineBinaryCache Util::ICacheLayer** pBottomLayer); VkResult InitLayers( - const PhysicalDevice* pPhysicalDevice, + const char* pDefaultCacheFilePath, bool internal, const RuntimeSettings& settings); @@ -196,7 +221,7 @@ class PipelineBinaryCache const RuntimeSettings& settings); VkResult InitArchiveLayers( - const PhysicalDevice* pPhysicalDevice, + const char* pDefaultCacheFilePath, const RuntimeSettings& settings); Util::ICacheLayer* GetMemoryLayer() const { return m_pMemoryLayer; } @@ -205,33 +230,36 @@ class PipelineBinaryCache Util::ICacheLayer* CreateFileLayer(Util::IArchiveFile* pFile); // Override the driver's default location - static constexpr char EnvVarPath[] = "AMD_VK_PIPELINE_CACHE_PATH"; + static constexpr char EnvVarPath[] = "AMD_VK_PIPELINE_CACHE_PATH"; // Override the driver's default name (Hash of application name) - static constexpr char EnvVarFileName[] = "AMD_VK_PIPELINE_CACHE_FILENAME"; + static constexpr char EnvVarFileName[] = "AMD_VK_PIPELINE_CACHE_FILENAME"; // Filename of an additional, read-only archive - static constexpr char EnvVarReadOnlyFileName[] = "AMD_VK_PIPELINE_CACHE_READ_ONLY_FILENAME"; + static constexpr char EnvVarReadOnlyFileName[] = "AMD_VK_PIPELINE_CACHE_READ_ONLY_FILENAME"; + + static const uint32_t ArchiveType; // TypeId created by hashed string VK_SHADER_PIPELINE_CACHE + static const uint32_t ElfType; // TypeId created by hashed string VK_PIPELINE_ELF - static const uint32_t ArchiveType; // TypeId created by hashed string VK_SHADER_PIPELINE_CACHE - static const uint32_t ElfType; // TypeId created by hashed string VK_PIPELINE_ELF + Vkgc::GfxIpVersion m_gfxIp; // Compared against e_flags of reinjected elf files - Vkgc::GfxIpVersion m_gfxIp; // Compared against e_flags of reinjected elf files + VkAllocationCallbacks* m_pAllocationCallbacks; // Allocator for use when interacting with the cache - Instance* const m_pInstance; // Allocator for use when interacting with the cache + vk::PalAllocator m_palAllocator; // PalAllocator for helper objects, e.g., FileVector - const Util::IPlatformKey* m_pPlatformKey; // Platform identifying key + const Util::IPlatformKey* m_pPlatformKey; // Platform identifying key - Util::ICacheLayer* m_pTopLayer; // Top layer of the cache chain where queries are submitted + Util::ICacheLayer* m_pTopLayer; // Top layer of the cache chain where queries are submitted #if ICD_GPUOPEN_DEVMODE_BUILD - Util::ICacheLayer* m_pReinjectionLayer; // Reinjection interface layer + vk::DevModeMgr* m_pDevModeMgr; + Util::ICacheLayer* m_pReinjectionLayer; // Reinjection interface layer - HashMapping m_hashMapping; // Maps the internalPipelineHash to the appropriate CacheId - Util::RWLock m_hashMappingLock; // Prevents collisions during writes to the map + HashMapping m_hashMapping; // Maps the internalPipelineHash to the appropriate CacheId + Util::RWLock m_hashMappingLock; // Prevents collisions during writes to the map #endif - Util::ICacheLayer* m_pMemoryLayer; + Util::ICacheLayer* m_pMemoryLayer; // Archive based cache layers using FileVector = Util::Vector; diff --git a/icd/api/pipeline_binary_cache.cpp b/icd/api/pipeline_binary_cache.cpp index df408244..0a628ab5 100644 --- a/icd/api/pipeline_binary_cache.cpp +++ b/icd/api/pipeline_binary_cache.cpp @@ -29,7 +29,6 @@ *********************************************************************************************************************** */ #include "include/pipeline_binary_cache.h" -#include "include/vk_physical_device.h" #include "palArchiveFile.h" #include "palAutoBuffer.h" @@ -68,20 +67,20 @@ const uint32_t PipelineBinaryCache::ElfType = Util::HashString(ElfTypeString static Util::Hash128 ParseHash128(const char* str); #endif -static Util::Result CalculateHashId( - Instance* pInstance, - const Util::IPlatformKey* pPlatformKey, - const void* pData, - size_t dataSize, - uint8_t* pHashId) +Util::Result PipelineBinaryCache::CalculateHashId( + VkAllocationCallbacks* pAllocationCallbacks, + const Util::IPlatformKey* pPlatformKey, + const void* pData, + size_t dataSize, + uint8_t* pHashId) { - Util::Result result = Util::Result::Success; - Util::IHashContext* pContext = nullptr; - size_t contextSize = pPlatformKey->GetKeyContext()->GetDuplicateObjectSize(); - void* pContextMem = pInstance->AllocMem( - contextSize, - VK_DEFAULT_MEM_ALIGN, - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + Util::Result result = Util::Result::Success; + Util::IHashContext* pContext = nullptr; + size_t contextSize = pPlatformKey->GetKeyContext()->GetDuplicateObjectSize(); + void* pContextMem = pAllocationCallbacks->pfnAllocation(pAllocationCallbacks->pUserData, + contextSize, + VK_DEFAULT_MEM_ALIGN, + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); if (pContextMem != nullptr) { @@ -101,16 +100,17 @@ static Util::Result CalculateHashId( } if (pContextMem != nullptr) { - pInstance->FreeMem(pContextMem); + pAllocationCallbacks->pfnFree(pAllocationCallbacks->pUserData, pContextMem); } return result; } bool PipelineBinaryCache::IsValidBlob( - const PhysicalDevice* pPhysicalDevice, - size_t dataSize, - const void* pData) + VkAllocationCallbacks* pAllocationCallbacks, + Util::IPlatformKey* pKey, + size_t dataSize, + const void* pData) { bool isValid = false; size_t blobSize = dataSize; @@ -120,14 +120,13 @@ bool PipelineBinaryCache::IsValidBlob( pData = Util::VoidPtrInc(pData, sizeof(PipelineBinaryCachePrivateHeader)); blobSize -= sizeof(PipelineBinaryCachePrivateHeader); - if (pPhysicalDevice->GetPlatformKey() != nullptr) + if (pKey != nullptr) { - Util::Result result = CalculateHashId( - pPhysicalDevice->Manager()->VkInstance(), - pPhysicalDevice->GetPlatformKey(), - pData, - blobSize, - hashId); + Util::Result result = CalculateHashId(pAllocationCallbacks, + pKey, + pData, + blobSize, + hashId); if (result == Util::Result::Success) { @@ -141,23 +140,37 @@ bool PipelineBinaryCache::IsValidBlob( // ===================================================================================================================== // Allocate and initialize a PipelineBinaryCache object PipelineBinaryCache* PipelineBinaryCache::Create( - Instance* pInstance, + VkAllocationCallbacks* pAllocationCallbacks, + Util::IPlatformKey* pKey, + const Vkgc::GfxIpVersion& gfxIp, + const RuntimeSettings& settings, + const char* pDefaultCacheFilePath, +#if ICD_GPUOPEN_DEVMODE_BUILD + vk::DevModeMgr* pDevModeMgr, +#endif size_t initDataSize, const void* pInitData, - bool internal, - const Vkgc::GfxIpVersion& gfxIp, - const PhysicalDevice* pPhysicalDevice) + bool internal + ) { + VK_ASSERT(pAllocationCallbacks != nullptr); + PipelineBinaryCache* pObj = nullptr; - void* pMem = pInstance->AllocMem(sizeof(PipelineBinaryCache), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = pAllocationCallbacks->pfnAllocation(pAllocationCallbacks->pUserData, + sizeof(PipelineBinaryCache), + VK_DEFAULT_MEM_ALIGN, + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); if (pMem != nullptr) { - pObj = VK_PLACEMENT_NEW(pMem) PipelineBinaryCache(pInstance, gfxIp, internal); + pObj = VK_PLACEMENT_NEW(pMem) PipelineBinaryCache(pAllocationCallbacks, gfxIp, internal); - if (pObj->Initialize(pPhysicalDevice) != VK_SUCCESS) +#if ICD_GPUOPEN_DEVMODE_BUILD + pObj->m_pDevModeMgr = pDevModeMgr; +#endif + + if (pObj->Initialize(settings, pDefaultCacheFilePath, pKey) != VK_SUCCESS) { pObj->Destroy(); - pInstance->FreeMem(pMem); pObj = nullptr; } else if ((pInitData != nullptr) && @@ -198,23 +211,25 @@ PipelineBinaryCache* PipelineBinaryCache::Create( // ===================================================================================================================== PipelineBinaryCache::PipelineBinaryCache( - Instance* pInstance, + VkAllocationCallbacks* pAllocationCallbacks, const Vkgc::GfxIpVersion& gfxIp, bool internal) : - m_pInstance { pInstance }, - m_pPlatformKey { nullptr }, - m_pTopLayer { nullptr }, + m_pAllocationCallbacks { pAllocationCallbacks }, + m_palAllocator { pAllocationCallbacks }, + m_pPlatformKey { nullptr }, + m_pTopLayer { nullptr }, #if ICD_GPUOPEN_DEVMODE_BUILD - m_pReinjectionLayer{ nullptr }, - m_hashMapping { 32, pInstance->Allocator() }, + m_pDevModeMgr { nullptr }, + m_pReinjectionLayer { nullptr }, + m_hashMapping { 32, &m_palAllocator }, #endif - m_pMemoryLayer { nullptr }, - m_pArchiveLayer { nullptr }, - m_openFiles { pInstance->Allocator() }, - m_archiveLayers { pInstance->Allocator() }, - m_isInternalCache { internal }, - m_pCacheAdapter { nullptr } + m_pMemoryLayer { nullptr }, + m_pArchiveLayer { nullptr }, + m_openFiles { &m_palAllocator }, + m_archiveLayers { &m_palAllocator }, + m_isInternalCache { internal }, + m_pCacheAdapter { nullptr } { // Without copy constructor, a class type variable can't be initialized in initialization list with gcc 4.8.5. // Initialize m_gfxIp here instead to make gcc 4.8.5 work. @@ -233,7 +248,7 @@ PipelineBinaryCache::~PipelineBinaryCache() for (FileVector::Iter i = m_openFiles.Begin(); i.IsValid(); i.Next()) { i.Get()->Destroy(); - m_pInstance->FreeMem(i.Get()); + FreeMem(i.Get()); } m_openFiles.Clear(); @@ -241,7 +256,7 @@ PipelineBinaryCache::~PipelineBinaryCache() for (LayerVector::Iter i = m_archiveLayers.Begin(); i.IsValid(); i.Next()) { i.Get()->Destroy(); - m_pInstance->FreeMem(i.Get()); + FreeMem(i.Get()); } m_archiveLayers.Clear(); @@ -249,7 +264,7 @@ PipelineBinaryCache::~PipelineBinaryCache() if (m_pMemoryLayer != nullptr) { m_pMemoryLayer->Destroy(); - m_pInstance->FreeMem(m_pMemoryLayer); + FreeMem(m_pMemoryLayer); } #if ICD_GPUOPEN_DEVMODE_BUILD @@ -260,6 +275,31 @@ PipelineBinaryCache::~PipelineBinaryCache() #endif } +// ===================================================================================================================== +// Allocates memory using the internal VkAllocationCallbacks. Uses the default memory alignment and the object system +// allocation scope. +// Any memory returned by this function must be freed with PipelineBinaryCache::FreeMem. +void* PipelineBinaryCache::AllocMem( + size_t memSize) const +{ + VK_ASSERT(memSize > 0); + return m_pAllocationCallbacks->pfnAllocation(m_pAllocationCallbacks->pUserData, + memSize, + VK_DEFAULT_MEM_ALIGN, + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); +} + +// ===================================================================================================================== +// Frees memory allocated by PipelineBinaryCache::AllocMem. No-op when passed a null pointer. +void PipelineBinaryCache::FreeMem( + void* pMem) const +{ + if (pMem != nullptr) + { + m_pAllocationCallbacks->pfnFree(m_pAllocationCallbacks->pUserData, pMem); + } +} + // ===================================================================================================================== // Query if a pipeline binary exists in cache // Must call ReleaseCacheRef() when the flags contains AcquireEntryRef @@ -304,11 +344,7 @@ Util::Result PipelineBinaryCache::LoadPipelineBinary( if (result == Util::Result::Success) { - void* pOutputMem = m_pInstance->AllocMem( - query.dataSize, - VK_DEFAULT_MEM_ALIGN, - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - + void* pOutputMem = AllocMem(query.dataSize); if (pOutputMem != nullptr) { result = m_pTopLayer->Load(&query, pOutputMem); @@ -320,7 +356,7 @@ Util::Result PipelineBinaryCache::LoadPipelineBinary( } else { - m_pInstance->FreeMem(pOutputMem); + FreeMem(pOutputMem); } } } @@ -438,10 +474,7 @@ Util::Result PipelineBinaryCache::LoadReinjectionBinary( if (result == Util::Result::Success) { - void* pOutputMem = m_pInstance->AllocMem( - query.dataSize, - VK_DEFAULT_MEM_ALIGN, - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pOutputMem = AllocMem(query.dataSize); if (pOutputMem != nullptr) { @@ -454,7 +487,7 @@ Util::Result PipelineBinaryCache::LoadReinjectionBinary( } else { - m_pInstance->FreeMem(pOutputMem); + FreeMem(pOutputMem); } } } @@ -478,7 +511,7 @@ Util::Result PipelineBinaryCache::StoreReinjectionBinary( uint32_t gfxIpMinor = 0u; uint32_t gfxIpStepping = 0u; - Util::Abi::PipelineAbiReader reader(m_pInstance->Allocator(), pPipelineBinary); + Util::Abi::PipelineAbiReader reader(&m_palAllocator, pPipelineBinary); reader.GetGfxIpVersion(&gfxIpMajor, &gfxIpMinor, &gfxIpStepping); if (gfxIpMajor == m_gfxIp.major && @@ -502,10 +535,7 @@ Util::Result PipelineBinaryCache::StoreReinjectionBinary( void PipelineBinaryCache::FreePipelineBinary( const void* pPipelineBinary) { - if (pPipelineBinary != nullptr) - { - m_pInstance->FreeMem(const_cast(pPipelineBinary)); - } + FreeMem(const_cast(pPipelineBinary)); } // ===================================================================================================================== @@ -513,28 +543,32 @@ void PipelineBinaryCache::FreePipelineBinary( void PipelineBinaryCache::Destroy() { #if ICD_GPUOPEN_DEVMODE_BUILD - if (m_pInstance->GetDevModeMgr() != nullptr) + if (m_pDevModeMgr != nullptr) { - m_pInstance->GetDevModeMgr()->DeregisterPipelineCache(this); + m_pDevModeMgr->DeregisterPipelineCache(this); } #endif + + VkAllocationCallbacks* pAllocationCallbacks = m_pAllocationCallbacks; + void* pMem = this; Util::Destructor(this); + pAllocationCallbacks->pfnFree(pAllocationCallbacks->pUserData, pMem); } // ===================================================================================================================== // Build the cache layer chain VkResult PipelineBinaryCache::Initialize( - const PhysicalDevice* pPhysicalDevice) + const RuntimeSettings& settings, + const char* pDefaultCacheFilePath, + const Util::IPlatformKey* pKey) { VkResult result = VK_SUCCESS; - const RuntimeSettings& settings = pPhysicalDevice->GetRuntimeSettings(); - m_entriesMutex.Init(); if (result == VK_SUCCESS) { - m_pPlatformKey = pPhysicalDevice->GetPlatformKey(); + m_pPlatformKey = pKey; } if (m_pPlatformKey == nullptr) @@ -544,7 +578,7 @@ VkResult PipelineBinaryCache::Initialize( if (result == VK_SUCCESS) { - result = InitLayers(pPhysicalDevice, m_isInternalCache, settings); + result = InitLayers(pDefaultCacheFilePath, m_isInternalCache, settings); } if (result == VK_SUCCESS) @@ -556,7 +590,7 @@ VkResult PipelineBinaryCache::Initialize( if ((result == VK_SUCCESS) && (m_pReinjectionLayer != nullptr)) { - Util::Result palResult = m_pInstance->GetDevModeMgr()->RegisterPipelineCache( + Util::Result palResult = m_pDevModeMgr->RegisterPipelineCache( this, settings.devModePipelineUriServicePostSizeLimit); @@ -583,7 +617,7 @@ VkResult PipelineBinaryCache::Initialize( if (result == VK_SUCCESS) { - m_pCacheAdapter = CacheAdapter::Create(m_pInstance, this); + m_pCacheAdapter = CacheAdapter::Create(this); VK_ALERT(m_pCacheAdapter == nullptr); } @@ -598,11 +632,11 @@ VkResult PipelineBinaryCache::InitReinjectionLayer( { VkResult result = VK_ERROR_FEATURE_NOT_PRESENT; - if (m_pInstance->GetDevModeMgr() != nullptr) + if (m_pDevModeMgr != nullptr) { Util::MemoryCacheCreateInfo info = {}; Util::AllocCallbacks allocCbs = { - m_pInstance->GetAllocCallbacks(), + m_pAllocationCallbacks, allocator::PalAllocFuncDelegator, allocator::PalFreeFuncDelegator }; @@ -614,7 +648,7 @@ VkResult PipelineBinaryCache::InitReinjectionLayer( info.evictDuplicates = true; size_t memSize = Util::GetMemoryCacheLayerSize(&info); - void* pMem = m_pInstance->AllocMem(memSize, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = AllocMem(memSize); if (pMem == nullptr) { @@ -630,7 +664,7 @@ VkResult PipelineBinaryCache::InitReinjectionLayer( if (result != VK_SUCCESS) { - m_pInstance->FreeMem(pMem); + FreeMem(pMem); } } @@ -702,12 +736,8 @@ Util::Result PipelineBinaryCache::InjectBinariesFromDirectory( if (result == Util::Result::Success) { // Allocate space for ppFileNames and pFileNameBuffer - ppFileNames = (const char**)m_pInstance->AllocMem( - (sizeof(const char*) * fileCount), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - pFileNameBuffer = m_pInstance->AllocMem( - fileNameBufferSize, - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + ppFileNames = (const char**)AllocMem(sizeof(const char*) * fileCount); + pFileNameBuffer = AllocMem(fileNameBufferSize); // Populate ppFileNames and pFileNameBuffer result = Util::ListDir( @@ -719,8 +749,8 @@ Util::Result PipelineBinaryCache::InjectBinariesFromDirectory( if (result != Util::Result::Success) { - m_pInstance->FreeMem(pFileNameBuffer); - m_pInstance->FreeMem(ppFileNames); + FreeMem(pFileNameBuffer); + FreeMem(ppFileNames); } } @@ -745,7 +775,7 @@ Util::Result PipelineBinaryCache::InjectBinariesFromDirectory( if (Util::File::Exists(filePath)) { pipelineBinarySize = Util::File::GetFileSize(filePath); - pPipelineBinary = m_pInstance->AllocMem(pipelineBinarySize, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + pPipelineBinary = AllocMem(pipelineBinarySize); if (pPipelineBinary != nullptr) { @@ -770,7 +800,7 @@ Util::Result PipelineBinaryCache::InjectBinariesFromDirectory( } } - m_pInstance->FreeMem(pPipelineBinary); + FreeMem(pPipelineBinary); } else { @@ -779,8 +809,8 @@ Util::Result PipelineBinaryCache::InjectBinariesFromDirectory( } } - m_pInstance->FreeMem(pFileNameBuffer); - m_pInstance->FreeMem(ppFileNames); + FreeMem(pFileNameBuffer); + FreeMem(ppFileNames); } } @@ -796,7 +826,7 @@ VkResult PipelineBinaryCache::InitMemoryCacheLayer( VK_ASSERT(m_pMemoryLayer == nullptr); Util::AllocCallbacks allocCallbacks = {}; - allocCallbacks.pClientData = m_pInstance->GetAllocCallbacks(); + allocCallbacks.pClientData = m_pAllocationCallbacks; allocCallbacks.pfnAlloc = allocator::PalAllocFuncDelegator; allocCallbacks.pfnFree = allocator::PalFreeFuncDelegator; @@ -808,7 +838,7 @@ VkResult PipelineBinaryCache::InitMemoryCacheLayer( createInfo.evictDuplicates = true; size_t layerSize = Util::GetMemoryCacheLayerSize(&createInfo); - void* pMem = m_pInstance->AllocMem(layerSize, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = AllocMem(layerSize); VkResult result = VK_SUCCESS; @@ -823,7 +853,7 @@ VkResult PipelineBinaryCache::InitMemoryCacheLayer( if (result != VK_SUCCESS) { - m_pInstance->FreeMem(pMem); + FreeMem(pMem); } } @@ -844,7 +874,7 @@ Util::IArchiveFile* PipelineBinaryCache::OpenReadOnlyArchive( Util::IArchiveFile* pFile = nullptr; Util::AllocCallbacks allocCbs = { - m_pInstance->GetAllocCallbacks(), + m_pAllocationCallbacks, allocator::PalAllocFuncDelegator, allocator::PalFreeFuncDelegator }; @@ -863,7 +893,7 @@ Util::IArchiveFile* PipelineBinaryCache::OpenReadOnlyArchive( info.maxReadBufferMem = bufferSize; size_t memSize = Util::GetArchiveFileObjectSize(&info); - void* pMem = m_pInstance->AllocMem(memSize, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = AllocMem(memSize); if (pMem != nullptr) { @@ -878,7 +908,7 @@ Util::IArchiveFile* PipelineBinaryCache::OpenReadOnlyArchive( } else { - m_pInstance->FreeMem(pMem); + FreeMem(pMem); pFile = nullptr; } } @@ -900,7 +930,7 @@ Util::IArchiveFile* PipelineBinaryCache::OpenWritableArchive( Util::IArchiveFile* pFile = nullptr; Util::AllocCallbacks allocCbs = { - m_pInstance->GetAllocCallbacks(), + m_pAllocationCallbacks, allocator::PalAllocFuncDelegator, allocator::PalFreeFuncDelegator }; @@ -919,7 +949,7 @@ Util::IArchiveFile* PipelineBinaryCache::OpenWritableArchive( info.maxReadBufferMem = bufferSize; size_t memSize = Util::GetArchiveFileObjectSize(&info); - void* pMem = m_pInstance->AllocMem(memSize, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = AllocMem(memSize); if (pMem != nullptr) { @@ -942,7 +972,7 @@ Util::IArchiveFile* PipelineBinaryCache::OpenWritableArchive( } else { - m_pInstance->FreeMem(pMem); + FreeMem(pMem); pFile = nullptr; } } @@ -960,7 +990,7 @@ Util::ICacheLayer* PipelineBinaryCache::CreateFileLayer( Util::ICacheLayer* pLayer = nullptr; Util::AllocCallbacks allocCbs = { - m_pInstance->GetAllocCallbacks(), + m_pAllocationCallbacks, allocator::PalAllocFuncDelegator, allocator::PalFreeFuncDelegator }; @@ -971,13 +1001,13 @@ Util::ICacheLayer* PipelineBinaryCache::CreateFileLayer( info.dataTypeId = ElfType; size_t memSize = Util::GetArchiveFileCacheLayerSize(&info); - void* pMem = m_pInstance->AllocMem(memSize, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + void* pMem = AllocMem(memSize); if (pMem != nullptr) { if (Util::CreateArchiveFileCacheLayer(&info, pMem, &pLayer) != Util::Result::Success) { - m_pInstance->FreeMem(pMem); + FreeMem(pMem); pLayer = nullptr; } } @@ -988,7 +1018,7 @@ Util::ICacheLayer* PipelineBinaryCache::CreateFileLayer( // ===================================================================================================================== // Open the archive file and initialize its cache layer VkResult PipelineBinaryCache::InitArchiveLayers( - const PhysicalDevice* pPhysicalDevice, + const char* pDefaultCacheFilePath, const RuntimeSettings& settings) { VkResult result = VK_SUCCESS; @@ -1007,7 +1037,7 @@ VkResult PipelineBinaryCache::InitArchiveLayers( if (settings.usePipelineCachingDefaultLocation) { const char* pCacheSubPath = settings.pipelineCachingDefaultLocation; - const char* pUserDataPath = pPhysicalDevice->PalDevice()->GetCacheFilePath(); + const char* pUserDataPath = pDefaultCacheFilePath; if ((pCacheSubPath != nullptr) && (pUserDataPath != nullptr)) @@ -1067,7 +1097,7 @@ VkResult PipelineBinaryCache::InitArchiveLayers( else { pFile->Destroy(); - m_pInstance->FreeMem(pFile); + FreeMem(pFile); } } } @@ -1168,7 +1198,7 @@ VkResult PipelineBinaryCache::InitArchiveLayers( else { pFile->Destroy(); - m_pInstance->FreeMem(pFile); + FreeMem(pFile); } } } @@ -1187,14 +1217,14 @@ VkResult PipelineBinaryCache::InitArchiveLayers( // ===================================================================================================================== // Initialize layers (a single layer that supports storage for binaries needs to succeed) VkResult PipelineBinaryCache::InitLayers( - const PhysicalDevice* pPhysicalDevice, + const char* pDefaultCacheFilePath, bool internal, const RuntimeSettings& settings) { VkResult result = VK_ERROR_INITIALIZATION_FAILED; #if ICD_GPUOPEN_DEVMODE_BUILD - if ((InitReinjectionLayer(settings) == VK_SUCCESS)) + if (InitReinjectionLayer(settings) == VK_SUCCESS) { result = VK_SUCCESS; } @@ -1208,7 +1238,7 @@ VkResult PipelineBinaryCache::InitLayers( // If cache handle is vkPipelineCache, we shouldn't store it to disk. if (internal) { - if (InitArchiveLayers(pPhysicalDevice, settings) == VK_SUCCESS) + if (InitArchiveLayers(pDefaultCacheFilePath, settings) == VK_SUCCESS) { result = VK_SUCCESS; } @@ -1308,7 +1338,7 @@ VkResult PipelineBinaryCache::Serialize( { if (*pSize > (sizeof(BinaryCacheEntry) + sizeof(PipelineBinaryCachePrivateHeader))) { - Util::AutoBuffer cacheIds(curCount, m_pInstance->Allocator()); + Util::AutoBuffer cacheIds(curCount, &m_palAllocator); size_t remainingSpace = *pSize - sizeof(PipelineBinaryCachePrivateHeader); result = PalToVkResult(Util::GetMemoryCacheLayerHashIds(m_pMemoryLayer, curCount, &cacheIds[0])); @@ -1339,7 +1369,7 @@ VkResult PipelineBinaryCache::Serialize( pDataDst = Util::VoidPtrInc(pDataDst, dataSize); remainingSpace -= (sizeof(BinaryCacheEntry) + dataSize); } - m_pInstance->FreeMem(const_cast(pBinaryCacheData)); + FreeMem(const_cast(pBinaryCacheData)); } } } @@ -1353,7 +1383,7 @@ VkResult PipelineBinaryCache::Serialize( void* pData = Util::VoidPtrInc(pBlob, sizeof(PipelineBinaryCachePrivateHeader)); result = PalToVkResult(CalculateHashId( - m_pInstance, + m_pAllocationCallbacks, m_pPlatformKey, pData, *pSize - sizeof(PipelineBinaryCachePrivateHeader), @@ -1390,7 +1420,7 @@ VkResult PipelineBinaryCache::Merge( result = PalToVkResult(Util::GetMemoryCacheLayerCurSize(pMemoryLayer, &curCount, &curDataSize)); if ((result == VK_SUCCESS) && (curCount > 0)) { - Util::AutoBuffer cacheIds(curCount, m_pInstance->Allocator()); + Util::AutoBuffer cacheIds(curCount, &m_palAllocator); result = PalToVkResult(Util::GetMemoryCacheLayerHashIds(pMemoryLayer, curCount, &cacheIds[0])); if (result == VK_SUCCESS) @@ -1404,7 +1434,7 @@ VkResult PipelineBinaryCache::Merge( if (result == VK_SUCCESS) { result = PalToVkResult(StorePipelineBinary(&cacheIds[j], dataSize, pBinaryCacheData)); - m_pInstance->FreeMem(const_cast(pBinaryCacheData)); + FreeMem(const_cast(pBinaryCacheData)); if (result != VK_SUCCESS) { break; diff --git a/icd/api/pipeline_compiler.cpp b/icd/api/pipeline_compiler.cpp index e1d70c08..cfbf1b19 100644 --- a/icd/api/pipeline_compiler.cpp +++ b/icd/api/pipeline_compiler.cpp @@ -104,7 +104,6 @@ void PipelineCompiler::DestroyPipelineBinaryCache() if (m_pBinaryCache != nullptr) { m_pBinaryCache->Destroy(); - m_pPhysicalDevice->VkInstance()->FreeMem(m_pBinaryCache); m_pBinaryCache = nullptr; } } @@ -173,7 +172,17 @@ VkResult PipelineCompiler::Initialize() (m_pPhysicalDevice->VkInstance()->GetDevModeMgr() != nullptr))) { m_pBinaryCache = PipelineBinaryCache::Create( - m_pPhysicalDevice->VkInstance(), 0, nullptr, true, m_gfxIp, m_pPhysicalDevice); + m_pPhysicalDevice->VkInstance()->GetAllocCallbacks(), + m_pPhysicalDevice->GetPlatformKey(), + m_gfxIp, + m_pPhysicalDevice->GetRuntimeSettings(), + m_pPhysicalDevice->PalDevice()->GetCacheFilePath(), +#if ICD_GPUOPEN_DEVMODE_BUILD + m_pPhysicalDevice->VkInstance()->GetDevModeMgr(), +#endif + 0, + nullptr, + true); // This isn't a terminal failure, the device can continue without the pipeline cache if need be. VK_ALERT(m_pBinaryCache == nullptr); diff --git a/icd/api/vk_pipeline_cache.cpp b/icd/api/vk_pipeline_cache.cpp index 08d78807..884f12f9 100644 --- a/icd/api/vk_pipeline_cache.cpp +++ b/icd/api/vk_pipeline_cache.cpp @@ -103,8 +103,12 @@ VkResult PipelineCache::Create( { const void* pData = Util::VoidPtrInc(pCreateInfo->pInitialData, sizeof(PipelineCacheHeaderData)); size_t dataSize = pCreateInfo->initialDataSize - sizeof(PipelineCacheHeaderData); + vk::PhysicalDevice* pPhysicalDevice = pDevice->VkPhysicalDevice(DefaultDeviceIndex); - if (PipelineBinaryCache::IsValidBlob(pDevice->VkPhysicalDevice(DefaultDeviceIndex), dataSize, pData)) + if (PipelineBinaryCache::IsValidBlob(pPhysicalDevice->VkInstance()->GetAllocCallbacks(), + pPhysicalDevice->GetPlatformKey(), + dataSize, + pData)) { usePipelineCacheInitialData = true; } @@ -200,9 +204,19 @@ VkResult PipelineCache::Create( initialDataSize = pCreateInfo->initialDataSize - sizeof(PipelineCacheHeaderData); } - pBinaryCache = PipelineBinaryCache::Create(pDevice->VkPhysicalDevice(DefaultDeviceIndex)->VkInstance(), - initialDataSize, pInitialData, false, - pDevice->GetCompiler(DefaultDeviceIndex)->GetGfxIp(), pDevice->VkPhysicalDevice(DefaultDeviceIndex)); + vk::PhysicalDevice* pDefaultPhysicalDevice = pDevice->VkPhysicalDevice(DefaultDeviceIndex); + pBinaryCache = PipelineBinaryCache::Create( + pDefaultPhysicalDevice->VkInstance()->GetAllocCallbacks(), + pDefaultPhysicalDevice->GetPlatformKey(), + pDevice->GetCompiler(DefaultDeviceIndex)->GetGfxIp(), + pDefaultPhysicalDevice->GetRuntimeSettings(), + pDefaultPhysicalDevice->PalDevice()->GetCacheFilePath(), +#if ICD_GPUOPEN_DEVMODE_BUILD + pDefaultPhysicalDevice->VkInstance()->GetDevModeMgr(), +#endif + initialDataSize, + pInitialData, + false); // This isn't a terminal failure, the device can continue without the pipeline cache if need be. VK_ALERT(pBinaryCache == nullptr); @@ -227,7 +241,6 @@ VkResult PipelineCache::Destroy( if (m_pBinaryCache != nullptr) { m_pBinaryCache->Destroy(); - pDevice->VkPhysicalDevice(DefaultDeviceIndex)->VkInstance()->FreeMem(m_pBinaryCache); m_pBinaryCache = nullptr; }