管線一直是圖形學中的實現圖形學功能的邏輯概念。在以往OpenGL語言中,管線被隱藏,在內部隱式執行。Vulkan吸收Mantle API特點,暴露更多底層細節,將驅動控制交由用戶控制。這種機制CPU多線程更加優秀,減少CPU在驅動上的時間。因此,vulkan可以顯示創建管線。因爲管線需要組裝固定函數以及可編程部分,創建管線需要不少時間。對於程序啓動或者在程序中更改狀態(需要重新創建管線)的響應都會變快。
vulkan使用管線緩存(PipelineCahe)技術加速管線創建,加速效果非常可觀。在我使用一個簡單vulkan入門程序(畫三角形)管線實驗對比結果如下:
- 有管線緩存: 6.4ms(實際管線創建3.9ms )
- 無管線緩存: 8.6ms
有管線緩存計算時間考慮了讀取磁盤和將緩存存入磁盤時間,創建管線只消耗3.9ms。實現的主體代碼如下:
//不使用緩存測試主體代碼,每次測試前將管線緩存刪除
auto StartPipelineTime = std::chrono::high_resolution_clock::now();
if(vkCreateGraphicsPipelines(m_Device, PipelineCache, 1, &PipelineInfo, nullptr, &vPass.m_GraphicsPipeline) != VK_SUCCESS)
{
throw std::runtime_error("failed to create graphics pipeline!");
}
auto EndPipelineTime = std::chrono::high_resolution_clock::now();
double m_DeltaTime = std::chrono::duration<float, std::chrono::seconds::period>(EndPipelineTime - StartPipelineTime).count();
std::cout << "創建pielineCache時間" <<m_DeltaTime<< std::endl;
//-------------------------------------------------------------------------
//使用緩存測試主題代碼
auto StartPipelineTime = std::chrono::high_resolution_clock::now();
__getPipelineCache(PipelineCache, PipelineCacheFileName);
if(vkCreateGraphicsPipelines(m_Device, PipelineCache, 1, &PipelineInfo, nullptr, &vPass.m_GraphicsPipeline) != VK_SUCCESS)
{
throw std::runtime_error("failed to create graphics pipeline!");
}
__savePipelineCache(PipelineCache,PipelineCacheFileName);
auto EndPipelineTime = std::chrono::high_resolution_clock::now();
double m_DeltaTime = std::chrono::duration<float, std::chrono::seconds::period>(EndPipelineTime - StartPipelineTime).count();
std::cout << "Creating pielineCache time " << m_DeltaTime << std::endl;
管線緩存可以大幅加速管線創建,對於在運行中需要改變管線狀態(需要重新創建)以及二次啓動程序的加速非常明顯。在vulkand的Specification的PipelineCache章節,對相關的API具有非常詳細的講解。主要涉及的API是:
- vkCreateGraphicsPipelines
- vkCreatePipelineCache
- vkGetPipelineCacheData
- vkDestroyPipelinecache
上訴實現管線緩存主體分爲三個部分,創建管線緩存(__getPipelineCache函數)、創建管線、保存管線緩存(__savePipelineCache函數)。
__getPipelineCache函數代碼如下:
void CVulKan::__getPipelineCache(VkPipelineCache& vPipelineCache, const std::string& vFileName)
{
int CacheSize = 0;
void* CacheData;
//if file does not exist ,create the file
FILE* FileExist = fopen(vFileName.c_str(), "ab");
fclose(FileExist);
FILE* ReadFile = fopen(vFileName.c_str(), "rb");
//get the cache data from file,if possible
if (ReadFile)
{
fseek(ReadFile, 0, SEEK_END);
CacheSize = ftell(ReadFile);
rewind(ReadFile);
CacheData = (char*)malloc(sizeof(char)*CacheSize);
if (CacheData == nullptr)
{
throw std::exception(" Allocating memory error in get pipelinecache");
}
int Result = fread(CacheData, 1, CacheSize, ReadFile);
fclose(ReadFile);
if (Result != CacheSize)
throw std::exception("Reading fail error in get pipelinecache");
//
// here, need do something to check data validity and compatibility
//
VkPipelineCacheCreateInfo PipelineCacheCreateInfo = {};
PipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
PipelineCacheCreateInfo.pNext = NULL;
PipelineCacheCreateInfo.initialDataSize = CacheSize;
PipelineCacheCreateInfo.pInitialData = CacheData;
if (vkCreatePipelineCache(m_Device, &PipelineCacheCreateInfo, nullptr, &vPipelineCache)!=VK_SUCCESS)
throw std::exception("Creating pipelinecache error");
}
else
{
throw std::exception("Read file error in get pipelincache");
}
}
__getPipelineCache函數代碼實現了創建管線緩存的功能,實現簡單,就是從保留數據的文件讀取緩存數據,如果無緩存數據(文件爲空),該管線緩存還是會被創建成功(被初始化爲空)。在這個函數中需要對緩存數據進行驗證以及檢測兼容性,這裏未實現,該數據驗證可以參考github代碼。
創建管線很簡單,調用vkCreateGrapgics函數如下:
vkCreateGraphicsPipelines(m_Device, PipelineCache, 1, &PipelineInfo, nullptr, &vPass.m_GraphicsPipeline)
在vulkan的API文檔(Specification)指出,該函數的第二個參數指向管線緩存,若該對象爲空(NULL),將不會啓動管線緩存,如不爲空,將會使用該緩存加速創建,並將新的管線數據返回(可以用作管線緩存初始化)。
__savePipelineCache函數代碼如下:
void CVulKan::__savePipelineCache(VkPipelineCache& vPipelineCache, const std::string& vFileName)
{
size_t CacheSize = 0;
void* PCacheData = nullptr;
//first call, use the nullptr to get cache size
if (vkGetPipelineCacheData(m_Device, vPipelineCache, &CacheSize, nullptr)!=VK_SUCCESS)
throw std::runtime_error::exception("Getting cache size fail from pipelinecache");
PCacheData = (char*)malloc(sizeof(char)*CacheSize);
if (!PCacheData)
throw std::runtime_error::exception("Allocating memory error in saving pipelinecache");
//second call, repeat the call to get real cacheData
if (vkGetPipelineCacheData(m_Device, vPipelineCache, &CacheSize, PCacheData) != VK_SUCCESS)
{
free(PCacheData);
throw std::runtime_error::exception("Getting cache fail from pipelinecache");
}
//write the data to the disk
FILE* PWritedFile=nullptr;
PWritedFile = fopen(vFileName.c_str(), "wb");
if (PWritedFile)
{
fwrite(PCacheData, sizeof(char), CacheSize, PWritedFile);
fclose(PWritedFile);
free(PCacheData);
std::cout << "Cacahe writed to the disk" << vFileName << std::endl;
}
else
{
fclose(PWritedFile);
free(PCacheData);
throw std::runtime_error::exception("Unable to write pipelinecacahe to disk");
}
}
__savePipelineCache函數主要是將更新的pipelinecache寫入磁盤。
管線緩存的另外一個延伸是mergePipeline,用於合併多個線程中的管線緩存(涉及多線程優化),有興趣的可以自己研究。因爲管線緩存時immutable變量,所以使用時不擔心併發問題。