10 #include "flutter/fml/logging.h"
11 #include "flutter/fml/make_copyable.h"
17 #include "impeller/entity/runtime_effect.vert.h"
26 std::shared_ptr<RuntimeStage> runtime_stage) {
27 runtime_stage_ = std::move(runtime_stage);
31 std::shared_ptr<std::vector<uint8_t>> uniform_data) {
32 uniform_data_ = std::move(uniform_data);
36 std::vector<TextureInput> texture_inputs) {
37 texture_inputs_ = std::move(texture_inputs);
57 auto metadata = std::make_shared<ShaderMetadata>();
58 metadata->name = uniform.
name;
71 const std::shared_ptr<Context>& context = renderer.
GetContext();
72 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
80 std::shared_ptr<const ShaderFunction>
function = library->GetFunction(
87 if (
function && runtime_stage_->IsDirty()) {
89 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(
function);
90 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
97 std::promise<bool> promise;
98 auto future = promise.get_future();
100 library->RegisterFunction(
101 runtime_stage_->GetEntrypoint(),
103 runtime_stage_->GetCodeMapping(),
104 fml::MakeCopyable([promise = std::move(promise)](
bool result)
mutable {
105 promise.set_value(result);
110 << runtime_stage_->GetEntrypoint() <<
")";
114 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
118 <<
"Failed to fetch runtime effect function immediately after "
119 "registering it (entry point: "
120 << runtime_stage_->GetEntrypoint() <<
")";
124 runtime_stage_->SetClean();
132 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
133 const auto color_attachment_format = caps->GetDefaultColorFormat();
134 const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
136 using VS = RuntimeEffectVertexShader;
142 std::vector<DescriptorSetLayout> descriptor_set_layouts;
145 &descriptor_set_layouts](
147 descriptor_set_layouts.clear();
149 size_t minimum_sampler_index = 100000000;
150 size_t buffer_index = 0;
151 size_t buffer_offset = 0;
153 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
156 switch (uniform.type) {
167 minimum_sampler_index =
168 std::min(minimum_sampler_index, uniform.location);
172 FML_DCHECK(renderer.
GetContext()->GetBackendType() !=
174 <<
"Uniform " << uniform.name
175 <<
" had unexpected type kFloat for Vulkan backend.";
179 uniform_data_->data() + buffer_offset, uniform.GetSize(),
183 uniform_slot.
name = uniform.name.c_str();
184 uniform_slot.
ext_res_0 = uniform.location;
187 metadata, buffer_view);
189 buffer_offset += uniform.GetSize();
193 FML_DCHECK(renderer.
GetContext()->GetBackendType() ==
196 static_cast<uint32_t
>(uniform.location),
201 uniform_slot.
name = uniform.name.c_str();
202 uniform_slot.
binding = uniform.location;
204 std::vector<float> uniform_buffer;
205 uniform_buffer.reserve(uniform.struct_layout.size());
206 size_t uniform_byte_index = 0u;
207 for (
const auto& byte_type : uniform.struct_layout) {
208 if (byte_type == 0) {
209 uniform_buffer.push_back(0.f);
210 }
else if (byte_type == 1) {
211 uniform_buffer.push_back(
reinterpret_cast<float*
>(
212 uniform_data_->data())[uniform_byte_index++]);
218 size_t alignment = std::max(
sizeof(
float) * uniform_buffer.size(),
222 reinterpret_cast<const void*
>(uniform_buffer.data()),
223 sizeof(
float) * uniform_buffer.size(), alignment);
231 size_t sampler_index = 0;
232 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
235 switch (uniform.type) {
237 FML_DCHECK(sampler_index < texture_inputs_.size());
238 auto& input = texture_inputs_[sampler_index];
240 const std::unique_ptr<const Sampler>& sampler =
241 context->GetSamplerLibrary()->GetSampler(
242 input.sampler_descriptor);
245 image_slot.
name = uniform.name.c_str();
247 uint32_t sampler_binding_location = 0u;
248 if (!descriptor_set_layouts.empty()) {
249 sampler_binding_location =
250 descriptor_set_layouts.back().binding + 1;
254 sampler_binding_location,
259 image_slot.
binding = sampler_binding_location;
260 image_slot.
texture_index = uniform.location - minimum_sampler_index;
263 *metadata, input.texture, sampler);
280 auto create_callback =
288 auto vertex_descriptor = std::make_shared<VertexDescriptor>();
289 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
290 VS::kInterleavedBufferLayout);
291 vertex_descriptor->RegisterDescriptorSetLayouts(
292 VS::kDescriptorSetLayouts);
293 vertex_descriptor->RegisterDescriptorSetLayouts(
294 descriptor_set_layouts.data(), descriptor_set_layouts.size());
297 0u, {.format = color_attachment_format, .blending_enabled =
true});
305 options.ApplyToPipelineDescriptor(desc);
306 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
308 VALIDATION_LOG <<
"Failed to get or create runtime effect pipeline.";
315 runtime_stage_->GetEntrypoint(), options, create_callback);
318 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
320 VS::FrameInfo{}, bind_callback);