11 #include "flutter/fml/mapping.h"
17 "flutter.impeller.vkcache";
25 std::shared_ptr<fml::Mapping> data) {
30 std::unique_ptr<fml::Mapping> data) {
35 const fml::UniqueFD& base_directory,
36 const std::string& cache_file_name,
38 if (!base_directory.is_valid()) {
41 std::unique_ptr<fml::Mapping> mapping =
42 fml::FileMapping::CreateReadOnly(base_directory, cache_file_name);
57 std::shared_ptr<DeviceHolderVK> device_holder,
58 fml::UniqueFD cache_directory)
59 : caps_(
std::move(caps)),
60 device_holder_(device_holder),
61 cache_directory_(
std::move(cache_directory)) {
62 if (!caps_ || !device_holder->GetDevice()) {
68 auto existing_cache_data =
71 vk::PipelineCacheCreateInfo cache_info;
72 if (existing_cache_data) {
73 cache_info.initialDataSize = existing_cache_data->GetSize();
74 cache_info.pInitialData = existing_cache_data->GetMapping();
77 auto [result, existing_cache] =
78 device_holder->GetDevice().createPipelineCacheUnique(cache_info);
80 if (result == vk::Result::eSuccess) {
81 cache_ = std::move(existing_cache);
86 FML_LOG(INFO) <<
"Existing pipeline cache was invalid: "
87 << vk::to_string(result) <<
". Starting with a fresh cache.";
88 cache_info.pInitialData =
nullptr;
89 cache_info.initialDataSize = 0u;
90 auto [result2, new_cache] =
91 device_holder->GetDevice().createPipelineCacheUnique(cache_info);
92 if (result2 == vk::Result::eSuccess) {
93 cache_ = std::move(new_cache);
96 << vk::to_string(result2);
100 is_valid_ = !!cache_;
104 std::shared_ptr<DeviceHolderVK> device_holder = device_holder_.lock();
117 const vk::GraphicsPipelineCreateInfo& info) {
118 std::shared_ptr<DeviceHolderVK> strong_device = device_holder_.lock();
119 if (!strong_device) {
123 auto [result, pipeline] =
124 strong_device->GetDevice().createGraphicsPipelineUnique(*cache_, info);
125 if (result != vk::Result::eSuccess) {
127 << vk::to_string(result);
129 return std::move(pipeline);
133 const vk::ComputePipelineCreateInfo& info) {
134 std::shared_ptr<DeviceHolderVK> strong_device = device_holder_.lock();
135 if (!strong_device) {
139 auto [result, pipeline] =
140 strong_device->GetDevice().createComputePipelineUnique(*cache_, info);
141 if (result != vk::Result::eSuccess) {
143 << vk::to_string(result);
145 return std::move(pipeline);
148 std::shared_ptr<fml::Mapping> PipelineCacheVK::CopyPipelineCacheData()
const {
149 std::shared_ptr<DeviceHolderVK> strong_device = device_holder_.lock();
150 if (!strong_device) {
157 auto [result, data] =
158 strong_device->GetDevice().getPipelineCacheData(*cache_);
159 if (result != vk::Result::eSuccess) {
160 VALIDATION_LOG <<
"Could not get pipeline cache data to persist.";
163 auto shared_data = std::make_shared<std::vector<uint8_t>>();
164 std::swap(*shared_data, data);
165 return std::make_shared<fml::NonOwnedMapping>(
166 shared_data->data(), shared_data->size(), [shared_data](
auto,
auto) {});
170 if (!cache_directory_.is_valid()) {
173 auto data = CopyPipelineCacheData();
181 <<
"Could not decorate pipeline cache with additional metadata.";