Skip to content

Commit

Permalink
Don't use VkInstance and VkPhysicalDevice in PipelineBinaryCache
Browse files Browse the repository at this point in the history
This refactoring in preparation to be able to use PipelineBinaryCache in
an offline tool, without having to run with a Vulkan device installed.

See #64 for more details on
the high-level direction.
  • Loading branch information
kuhar authored and JaxLinAMD committed Nov 19, 2020
1 parent fc11e79 commit cb0215b
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 168 deletions.
26 changes: 13 additions & 13 deletions icd/api/cache_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 }
{
}

Expand All @@ -83,8 +83,8 @@ Result CacheAdapter::GetEntry(
{
Result result = Result::Success;
bool mustPopulate = false;
Util::QueryResult* pQuery = static_cast<Util::QueryResult*>(m_pInstance->AllocMem(sizeof(Util::QueryResult),
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT));
Util::QueryResult* pQuery =
static_cast<Util::QueryResult*>(m_pPipelineBinaryCache->AllocMem(sizeof(Util::QueryResult)));

if (pQuery != nullptr)
{
Expand All @@ -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)
Expand Down Expand Up @@ -157,7 +157,7 @@ void CacheAdapter::ReleaseEntry(
{
Util::QueryResult* pQuery = static_cast<Util::QueryResult*>(rawHandle);
m_pPipelineBinaryCache->ReleaseCacheRef(pQuery);
m_pInstance->FreeMem(rawHandle);
m_pPipelineBinaryCache->FreeMem(rawHandle);
}
}

Expand Down
13 changes: 4 additions & 9 deletions icd/api/include/cache_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};

Expand Down
80 changes: 54 additions & 26 deletions icd/api/include/pipeline_binary_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
namespace Util
{
class IPlatformKey;

#if ICD_GPUOPEN_DEVMODE_BUILD
class DevModeMgr;
#endif
} // namespace Util

namespace vk
Expand All @@ -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,
Expand Down Expand Up @@ -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; }
Expand All @@ -164,7 +189,7 @@ class PipelineBinaryCache
PAL_DISALLOW_COPY_AND_ASSIGN(PipelineBinaryCache);

explicit PipelineBinaryCache(
Instance* pInstance,
VkAllocationCallbacks* pAllocationCallbacks,
const Vkgc::GfxIpVersion& gfxIp,
bool internal);

Expand All @@ -180,7 +205,7 @@ class PipelineBinaryCache
Util::ICacheLayer** pBottomLayer);

VkResult InitLayers(
const PhysicalDevice* pPhysicalDevice,
const char* pDefaultCacheFilePath,
bool internal,
const RuntimeSettings& settings);

Expand All @@ -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; }
Expand All @@ -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<Util::IArchiveFile*, 8, PalAllocator>;
Expand Down
Loading

0 comments on commit cb0215b

Please sign in to comment.