vulkan-pipelinecache(管线缓存)

  管线一直是图形学中的实现图形学功能的逻辑概念。在以往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变量,所以使用时不担心并发问题。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章