PipelineCache.cpp 3.4 KB

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