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<DeviceHolderVK> 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,
70 for (
const auto& library_data : shader_libraries_data) {
73 if (!vulkan_library || !vulkan_library->IsValid()) {
74 VALIDATION_LOG <<
"Could not construct Vulkan shader library archive.";
77 vulkan_library->IterateAllShaders(iterator);
81 VALIDATION_LOG <<
"Could not create shader modules for all shader blobs.";
87 ShaderLibraryVK::~ShaderLibraryVK() =
default;
89 bool ShaderLibraryVK::IsValid()
const {
94 std::shared_ptr<const ShaderFunction> ShaderLibraryVK::GetFunction(
95 std::string_view name,
99 const auto key =
ShaderKey{{name.data(), name.size()}, stage};
100 auto found = functions_.find(key);
101 if (found != functions_.end()) {
102 return found->second;
108 void ShaderLibraryVK::RegisterFunction(std::string name,
110 std::shared_ptr<fml::Mapping> code,
111 RegistrationCallback callback) {
112 const auto result = RegisterFunction(name, stage, code);
120 const uint32_t kSPIRVMagic = 0x07230203;
121 if (mapping.GetSize() <
sizeof(kSPIRVMagic)) {
125 ::memcpy(&magic, mapping.GetMapping(),
sizeof(magic));
126 return magic == kSPIRVMagic;
129 bool ShaderLibraryVK::RegisterFunction(
130 const std::string& name,
132 const std::shared_ptr<fml::Mapping>& code) {
142 vk::ShaderModuleCreateInfo shader_module_info;
144 shader_module_info.setPCode(
145 reinterpret_cast<const uint32_t*
>(code->GetMapping()));
146 shader_module_info.setCodeSize(code->GetSize());
148 auto device_holder = device_holder_.lock();
149 if (!device_holder) {
152 FML_DCHECK(device_holder->GetDevice());
154 device_holder->GetDevice().createShaderModuleUnique(shader_module_info);
156 if (module.result != vk::Result::eSuccess) {
158 << 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{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);