9 #include "flutter/fml/logging.h"
10 #include "flutter/fml/trace_event.h"
31 std::stringstream stream;
35 stream <<
"_unknown_";
41 stream <<
"_fragment_";
44 stream <<
"_compute_";
51 ShaderLibraryVK::ShaderLibraryVK(
52 std::weak_ptr<DeviceHolderVK> device_holder,
53 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data)
54 : device_holder_(
std::move(device_holder)) {
55 TRACE_EVENT0(
"impeller",
"CreateShaderLibrary");
57 auto iterator = [&](
auto type,
69 for (
const auto& library_data : shader_libraries_data) {
70 auto blob_library = ShaderArchive{library_data};
71 if (!blob_library.IsValid()) {
75 blob_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);
160 vk::UniqueShaderModule shader_module = std::move(module.value);
161 ContextVK::SetDebugName(device_holder->GetDevice(), *shader_module,
164 WriterLock lock(functions_mutex_);
165 functions_[ShaderKey{name, stage}] = std::shared_ptr<ShaderFunctionVK>(
166 new ShaderFunctionVK(device_holder_,
170 std::move(shader_module)
177 void ShaderLibraryVK::UnregisterFunction(std::string name,
ShaderStage stage) {
178 WriterLock lock(functions_mutex_);
180 const auto key = ShaderKey{name, stage};
182 auto found = functions_.find(key);
183 if (found == functions_.end()) {
185 <<
" was not found, so it couldn't be unregistered.";
189 functions_.erase(found);
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
static bool IsMappingSPIRV(const fml::Mapping &mapping)
static std::string VKShaderNameToShaderKeyName(const std::string &name, ShaderStage stage)