9 #include "flutter/fml/logging.h"
10 #include "flutter/fml/trace_event.h"
32 std::stringstream stream;
36 stream <<
"_unknown_";
42 stream <<
"_fragment_";
45 stream <<
"_compute_";
52 ShaderLibraryVK::ShaderLibraryVK(
53 std::weak_ptr<DeviceHolder> device_holder,
54 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data)
55 : device_holder_(
std::move(device_holder)) {
56 TRACE_EVENT0(
"impeller",
"CreateShaderLibrary");
58 auto iterator = [&](
auto type,
68 for (
const auto& library_data : shader_libraries_data) {
71 if (!vulkan_library || !vulkan_library->IsValid()) {
72 VALIDATION_LOG <<
"Could not construct Vulkan shader library archive.";
75 vulkan_library->IterateAllShaders(iterator);
79 VALIDATION_LOG <<
"Could not create shader modules for all shader blobs.";
85 ShaderLibraryVK::~ShaderLibraryVK() =
default;
87 bool ShaderLibraryVK::IsValid()
const {
92 std::shared_ptr<const ShaderFunction> ShaderLibraryVK::GetFunction(
93 std::string_view name,
97 const auto key =
ShaderKey{{name.data(), name.size()}, stage};
98 auto found = functions_.find(key);
99 if (found != functions_.end()) {
100 return found->second;
106 void ShaderLibraryVK::RegisterFunction(std::string name,
108 std::shared_ptr<fml::Mapping> code,
109 RegistrationCallback callback) {
110 const auto result = RegisterFunction(name, stage, code);
118 const uint32_t kSPIRVMagic = 0x07230203;
119 if (mapping.GetSize() <
sizeof(kSPIRVMagic)) {
123 ::memcpy(&magic, mapping.GetMapping(),
sizeof(magic));
124 return magic == kSPIRVMagic;
127 bool ShaderLibraryVK::RegisterFunction(
128 const std::string& name,
130 const std::shared_ptr<fml::Mapping>& code) {
140 vk::ShaderModuleCreateInfo shader_module_info;
142 shader_module_info.setPCode(
143 reinterpret_cast<const uint32_t*
>(code->GetMapping()));
144 shader_module_info.setCodeSize(code->GetSize());
146 auto device_holder = device_holder_.lock();
147 if (!device_holder) {
150 FML_DCHECK(device_holder->GetDevice());
152 device_holder->GetDevice().createShaderModuleUnique(shader_module_info);
154 if (module.result != vk::Result::eSuccess) {
156 << vk::to_string(module.result);
162 vk::UniqueShaderModule shader_module = std::move(module.value);
163 ContextVK::SetDebugName(device_holder->GetDevice(), *shader_module,
166 WriterLock lock(functions_mutex_);
167 functions_[ShaderKey{key_name, stage}] = std::shared_ptr<ShaderFunctionVK>(
168 new ShaderFunctionVK(device_holder_,
172 std::move(shader_module)
179 void ShaderLibraryVK::UnregisterFunction(std::string name,
ShaderStage stage) {
180 WriterLock lock(functions_mutex_);
182 const auto key = ShaderKey{name, stage};
184 auto found = functions_.find(key);
185 if (found != functions_.end()) {
187 <<
" was not found, so it couldn't be unregistered.";
191 functions_.erase(found);