PipelineCache.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/Vulkan/PipelineCache.h>
  6. #include <AnKi/Core/ConfigSet.h>
  7. #include <AnKi/Util/Filesystem.h>
  8. #include <AnKi/Util/File.h>
  9. namespace anki
  10. {
  11. Error PipelineCache::init(VkDevice dev, VkPhysicalDevice pdev, CString cacheDir, const ConfigSet& cfg,
  12. GrAllocator<U8> alloc)
  13. {
  14. ANKI_ASSERT(cacheDir && dev && pdev);
  15. m_dumpSize = cfg.getNumberU32("gr_diskShaderCacheMaxSize");
  16. m_dumpFilename.sprintf(alloc, "%s/vk_pipeline_cache", &cacheDir[0]);
  17. // Try read the pipeline cache file.
  18. DynamicArrayAuto<U8, PtrSize> diskDump(alloc);
  19. if(fileExists(m_dumpFilename.toCString()))
  20. {
  21. File file;
  22. ANKI_CHECK(file.open(m_dumpFilename.toCString(), FileOpenFlag::BINARY | FileOpenFlag::READ));
  23. PtrSize diskDumpSize = file.getSize();
  24. if(diskDumpSize <= sizeof(U8) * VK_UUID_SIZE)
  25. {
  26. ANKI_VK_LOGI("Pipeline cache dump appears to be empty: %s", &m_dumpFilename[0]);
  27. }
  28. else
  29. {
  30. // Get current pipeline UUID and compare it with the cache's
  31. VkPhysicalDeviceProperties props;
  32. vkGetPhysicalDeviceProperties(pdev, &props);
  33. Array<U8, VK_UUID_SIZE> cacheUuid;
  34. ANKI_CHECK(file.read(&cacheUuid[0], VK_UUID_SIZE));
  35. if(memcmp(&cacheUuid[0], &props.pipelineCacheUUID[0], VK_UUID_SIZE) != 0)
  36. {
  37. ANKI_VK_LOGI("Pipeline cache dump is not compatible with the current device: %s", &m_dumpFilename[0]);
  38. }
  39. else
  40. {
  41. diskDump.create(diskDumpSize - VK_UUID_SIZE);
  42. ANKI_CHECK(file.read(&diskDump[0], diskDumpSize - VK_UUID_SIZE));
  43. }
  44. }
  45. }
  46. else
  47. {
  48. ANKI_VK_LOGI("Pipeline cache dump not found: %s", &m_dumpFilename[0]);
  49. }
  50. // Create the cache
  51. VkPipelineCacheCreateInfo ci = {};
  52. ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
  53. if(diskDump.getSize())
  54. {
  55. ANKI_VK_LOGI("Will load %zu bytes of pipeline cache", diskDump.getSize());
  56. ci.initialDataSize = diskDump.getSize();
  57. ci.pInitialData = &diskDump[0];
  58. }
  59. ANKI_VK_CHECK(vkCreatePipelineCache(dev, &ci, nullptr, &m_cacheHandle));
  60. return Error::NONE;
  61. }
  62. void PipelineCache::destroy(VkDevice dev, VkPhysicalDevice pdev, GrAllocator<U8> alloc)
  63. {
  64. const Error err = destroyInternal(dev, pdev, alloc);
  65. if(err)
  66. {
  67. ANKI_VK_LOGE("An error occurred while storing the pipeline cache to disk. Will ignore");
  68. }
  69. m_dumpFilename.destroy(alloc);
  70. }
  71. Error PipelineCache::destroyInternal(VkDevice dev, VkPhysicalDevice pdev, GrAllocator<U8> alloc)
  72. {
  73. if(m_cacheHandle)
  74. {
  75. ANKI_ASSERT(dev && pdev);
  76. // Get size of cache
  77. size_t size = 0;
  78. ANKI_VK_CHECK(vkGetPipelineCacheData(dev, m_cacheHandle, &size, nullptr));
  79. size = min(size, m_dumpSize);
  80. if(size > 0)
  81. {
  82. // Read cache
  83. DynamicArrayAuto<U8, PtrSize> cacheData(alloc);
  84. cacheData.create(size);
  85. ANKI_VK_CHECK(vkGetPipelineCacheData(dev, m_cacheHandle, &size, &cacheData[0]));
  86. // Write file
  87. File file;
  88. ANKI_CHECK(file.open(&m_dumpFilename[0], FileOpenFlag::BINARY | FileOpenFlag::WRITE));
  89. VkPhysicalDeviceProperties props;
  90. vkGetPhysicalDeviceProperties(pdev, &props);
  91. ANKI_CHECK(file.write(&props.pipelineCacheUUID[0], VK_UUID_SIZE));
  92. ANKI_CHECK(file.write(&cacheData[0], size));
  93. ANKI_VK_LOGI("Dumped %zu bytes of the pipeline cache", size);
  94. }
  95. // Destroy cache
  96. vkDestroyPipelineCache(dev, m_cacheHandle, nullptr);
  97. m_cacheHandle = VK_NULL_HANDLE;
  98. }
  99. return Error::NONE;
  100. }
  101. } // end namespace anki