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_";
52ShaderLibraryVK::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)) {
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.";
87ShaderLibraryVK::~ShaderLibraryVK() =
default;
89bool ShaderLibraryVK::IsValid()
const {
94std::shared_ptr<const ShaderFunction> ShaderLibraryVK::GetFunction(
95 std::string_view
name,
100 auto found = functions_.find(
key);
101 if (found != functions_.end()) {
102 return found->second;
108void ShaderLibraryVK::RegisterFunction(std::string
name,
110 std::shared_ptr<fml::Mapping> code,
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;
129bool 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) {
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)
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);
virtual const uint8_t * GetMapping() const =0
virtual size_t GetSize() const =0
static std::shared_ptr< ShaderArchive > CreateArchiveFromMapping(const std::shared_ptr< const fml::Mapping > &mapping, ArchiveRenderingBackend backend)
std::function< void(bool)> RegistrationCallback
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_UNREACHABLE()
#define FML_DCHECK(condition)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
static bool IsMappingSPIRV(const fml::Mapping &mapping)
static std::string VKShaderNameToShaderKeyName(const std::string &name, ShaderStage stage)
#define TRACE_EVENT0(category_group, name)