Flutter Engine
The Flutter Engine
shader.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/lib/gpu/shader.h"
6
7#include <utility>
8
9#include "flutter/lib/gpu/formats.h"
10#include "fml/make_copyable.h"
15
16namespace flutter {
17namespace gpu {
18
21 auto result =
22 std::find_if(metadata.members.begin(), metadata.members.end(),
24 return member.name == name;
25 });
26 if (result == metadata.members.end()) {
27 return nullptr;
28 }
29 return &(*result);
30}
31
33
34Shader::Shader() = default;
35
36Shader::~Shader() = default;
37
39 std::string entrypoint,
41 std::shared_ptr<fml::Mapping> code_mapping,
42 std::vector<impeller::ShaderStageIOSlot> inputs,
43 std::vector<impeller::ShaderStageBufferLayout> layouts,
44 std::unordered_map<std::string, UniformBinding> uniform_structs,
45 std::unordered_map<std::string, impeller::SampledImageSlot>
46 uniform_textures,
47 std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts) {
48 auto shader = fml::MakeRefCounted<Shader>();
49 shader->entrypoint_ = std::move(entrypoint);
50 shader->stage_ = stage;
51 shader->code_mapping_ = std::move(code_mapping);
52 shader->inputs_ = std::move(inputs);
53 shader->layouts_ = std::move(layouts);
54 shader->uniform_structs_ = std::move(uniform_structs);
55 shader->uniform_textures_ = std::move(uniform_textures);
56 shader->descriptor_set_layouts_ = std::move(descriptor_set_layouts);
57 return shader;
58}
59
60std::shared_ptr<const impeller::ShaderFunction> Shader::GetFunctionFromLibrary(
61 impeller::ShaderLibrary& library) {
62 return library.GetFunction(entrypoint_, stage_);
63}
64
66 auto& lib = *context.GetContext()->GetShaderLibrary();
67 return GetFunctionFromLibrary(lib) != nullptr;
68}
69
71 if (IsRegistered(context)) {
72 return true; // Already registered.
73 }
74
75 auto& lib = *context.GetContext()->GetShaderLibrary();
76
77 std::promise<bool> promise;
78 auto future = promise.get_future();
79 lib.RegisterFunction(
80 entrypoint_, stage_, code_mapping_,
81 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
82 promise.set_value(result);
83 }));
84 if (!future.get()) {
85 return false; // Registration failed.
86 }
87 return true;
88}
89
90std::shared_ptr<impeller::VertexDescriptor> Shader::CreateVertexDescriptor()
91 const {
92 auto vertex_descriptor = std::make_shared<impeller::VertexDescriptor>();
93 vertex_descriptor->SetStageInputs(inputs_, layouts_);
94 return vertex_descriptor;
95}
96
98 return stage_;
99}
100
101const std::vector<impeller::DescriptorSetLayout>&
103 return descriptor_set_layouts_;
104}
105
107 const std::string& name) const {
108 auto uniform = uniform_structs_.find(name);
109 if (uniform == uniform_structs_.end()) {
110 return nullptr;
111 }
112 return &uniform->second;
113}
114
116 const std::string& name) const {
117 auto uniform = uniform_textures_.find(name);
118 if (uniform == uniform_textures_.end()) {
119 return nullptr;
120 }
121 return &uniform->second;
122}
123
124} // namespace gpu
125} // namespace flutter
126
127//----------------------------------------------------------------------------
128/// Exports
129///
130
132 flutter::gpu::Shader* wrapper,
133 Dart_Handle struct_name_handle) {
134 auto name = tonic::StdStringFromDart(struct_name_handle);
135 const auto* uniform = wrapper->GetUniformStruct(name);
136 if (uniform == nullptr) {
137 return -1;
138 }
139
140 return uniform->size_in_bytes;
141}
142
144 flutter::gpu::Shader* wrapper,
145 Dart_Handle struct_name_handle,
146 Dart_Handle member_name_handle) {
147 auto struct_name = tonic::StdStringFromDart(struct_name_handle);
148 const auto* uniform = wrapper->GetUniformStruct(struct_name);
149 if (uniform == nullptr) {
150 return -1;
151 }
152
153 auto member_name = tonic::StdStringFromDart(member_name_handle);
154 const auto* member = uniform->GetMemberMetadata(member_name);
155 if (member == nullptr) {
156 return -1;
157 }
158
159 return member->offset;
160}
std::shared_ptr< impeller::Context > GetContext()
Definition: context.cc:65
An immutable collection of shaders loaded from a shader bundle asset.
Definition: shader.h:23
const Shader::UniformBinding * GetUniformStruct(const std::string &name) const
Definition: shader.cc:106
impeller::ShaderStage GetShaderStage() const
Definition: shader.cc:97
bool IsRegistered(Context &context)
Definition: shader.cc:65
bool RegisterSync(Context &context)
Definition: shader.cc:70
static fml::RefPtr< Shader > Make(std::string entrypoint, impeller::ShaderStage stage, std::shared_ptr< fml::Mapping > code_mapping, std::vector< impeller::ShaderStageIOSlot > inputs, std::vector< impeller::ShaderStageBufferLayout > layouts, std::unordered_map< std::string, UniformBinding > uniform_structs, std::unordered_map< std::string, impeller::SampledImageSlot > uniform_textures, std::vector< impeller::DescriptorSetLayout > descriptor_set_layouts)
Definition: shader.cc:38
std::shared_ptr< impeller::VertexDescriptor > CreateVertexDescriptor() const
Definition: shader.cc:90
const std::vector< impeller::DescriptorSetLayout > & GetDescriptorSetLayouts() const
Definition: shader.cc:102
const impeller::SampledImageSlot * GetUniformTexture(const std::string &name) const
Definition: shader.cc:115
std::shared_ptr< const impeller::ShaderFunction > GetFunctionFromLibrary(impeller::ShaderLibrary &library)
Definition: shader.cc:60
virtual std::shared_ptr< const ShaderFunction > GetFunction(std::string_view name, ShaderStage stage)=0
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
GAsyncResult * result
int InternalFlutterGpu_Shader_GetUniformStructSize(flutter::gpu::Shader *wrapper, Dart_Handle struct_name_handle)
Definition: shader.cc:131
int InternalFlutterGpu_Shader_GetUniformMemberOffset(flutter::gpu::Shader *wrapper, Dart_Handle struct_name_handle, Dart_Handle member_name_handle)
Definition: shader.cc:143
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, CommandBuffer)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
std::string StdStringFromDart(Dart_Handle handle)
impeller::ShaderMetadata metadata
Definition: shader.h:30
const impeller::ShaderStructMemberMetadata * GetMemberMetadata(const std::string &name) const
Definition: shader.cc:20
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
std::vector< ShaderStructMemberMetadata > members
Definition: shader_types.h:75