11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/make_copyable.h"
18 #include "impeller/entity/runtime_effect.vert.h"
28 std::shared_ptr<RuntimeStage> runtime_stage) {
29 runtime_stage_ = std::move(runtime_stage);
33 std::shared_ptr<std::vector<uint8_t>> uniform_data) {
34 uniform_data_ = std::move(uniform_data);
38 std::vector<TextureInput> texture_inputs) {
39 texture_inputs_ = std::move(texture_inputs);
55 auto metadata = std::make_shared<ShaderMetadata>();
56 metadata->name = uniform.
name;
68 if (!RegisterShader(renderer)) {
73 renderer.
GetContext()->GetCapabilities()->GetDefaultColorFormat();
74 CreatePipeline(renderer, options,
true);
78 bool RuntimeEffectContents::RegisterShader(
80 const std::shared_ptr<Context>& context = renderer.
GetContext();
81 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
83 std::shared_ptr<const ShaderFunction>
function = library->GetFunction(
90 if (
function && runtime_stage_->IsDirty()) {
92 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(
function);
93 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
100 std::promise<bool> promise;
101 auto future = promise.get_future();
103 library->RegisterFunction(
104 runtime_stage_->GetEntrypoint(),
106 runtime_stage_->GetCodeMapping(),
107 fml::MakeCopyable([promise = std::move(promise)](
bool result)
mutable {
108 promise.set_value(result);
113 << runtime_stage_->GetEntrypoint() <<
")";
117 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
121 <<
"Failed to fetch runtime effect function immediately after "
122 "registering it (entry point: "
123 << runtime_stage_->GetEntrypoint() <<
")";
127 runtime_stage_->SetClean();
132 std::shared_ptr<Pipeline<PipelineDescriptor>>
133 RuntimeEffectContents::CreatePipeline(
const ContentContext& renderer,
134 ContentContextOptions options,
136 const std::shared_ptr<Context>& context = renderer.GetContext();
137 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
138 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
139 const auto color_attachment_format = caps->GetDefaultColorFormat();
140 const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
142 using VS = RuntimeEffectVertexShader;
144 PipelineDescriptor desc;
145 desc.SetLabel(
"Runtime Stage");
146 desc.AddStageEntrypoint(
148 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
151 std::shared_ptr<VertexDescriptor> vertex_descriptor =
152 std::make_shared<VertexDescriptor>();
153 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
154 VS::kInterleavedBufferLayout);
155 vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
156 vertex_descriptor->RegisterDescriptorSetLayouts(
157 runtime_stage_->GetDescriptorSetLayouts().data(),
158 runtime_stage_->GetDescriptorSetLayouts().size());
159 desc.SetVertexDescriptor(std::move(vertex_descriptor));
160 desc.SetColorAttachmentDescriptor(
161 0u, {.format = color_attachment_format, .blending_enabled =
true});
163 desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
164 desc.SetStencilPixelFormat(stencil_attachment_format);
166 desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
167 desc.SetDepthPixelFormat(stencil_attachment_format);
169 options.ApplyToPipelineDescriptor(desc);
171 context->GetPipelineLibrary()->GetPipeline(desc, async);
175 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
177 VALIDATION_LOG <<
"Failed to get or create runtime effect pipeline.";
187 const std::shared_ptr<Context>& context = renderer.
GetContext();
188 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
195 if (!RegisterShader(renderer)) {
204 size_t minimum_sampler_index = 100000000;
205 size_t buffer_index = 0;
206 size_t buffer_offset = 0;
208 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
211 switch (uniform.type) {
222 minimum_sampler_index =
223 std::min(minimum_sampler_index, uniform.location);
227 FML_DCHECK(renderer.
GetContext()->GetBackendType() !=
229 <<
"Uniform " << uniform.name
230 <<
" had unexpected type kFloat for Vulkan backend.";
235 uniform_data_->data() + buffer_offset, uniform.GetSize(),
239 uniform_slot.
name = uniform.name.c_str();
240 uniform_slot.
ext_res_0 = uniform.location;
245 buffer_offset += uniform.GetSize();
249 FML_DCHECK(renderer.
GetContext()->GetBackendType() ==
252 uniform_slot.
name = uniform.name.c_str();
253 uniform_slot.
binding = uniform.location;
257 std::vector<float> uniform_buffer;
258 uniform_buffer.reserve(uniform.struct_layout.size());
259 size_t uniform_byte_index = 0u;
260 for (
const auto& byte_type : uniform.struct_layout) {
261 if (byte_type == 0) {
262 uniform_buffer.push_back(0.f);
263 }
else if (byte_type == 1) {
264 uniform_buffer.push_back(
reinterpret_cast<float*
>(
265 uniform_data_->data())[uniform_byte_index++]);
270 size_t alignment = std::max(
sizeof(
float) * uniform_buffer.size(),
274 reinterpret_cast<const void*
>(uniform_buffer.data()),
275 sizeof(
float) * uniform_buffer.size(), alignment);
283 size_t sampler_index = 0;
284 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
287 switch (uniform.type) {
289 FML_DCHECK(sampler_index < texture_inputs_.size());
290 auto& input = texture_inputs_[sampler_index];
292 const std::unique_ptr<const Sampler>& sampler =
293 context->GetSamplerLibrary()->GetSampler(
294 input.sampler_descriptor);
297 image_slot.
name = uniform.name.c_str();
298 image_slot.
binding = uniform.binding;
299 image_slot.
texture_index = uniform.location - minimum_sampler_index;
302 *metadata, input.texture, sampler);
315 using VS = RuntimeEffectVertexShader;
321 runtime_stage_->GetEntrypoint(), options, [&]() {
322 return CreatePipeline(renderer, options,
false);
326 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
328 VS::FrameInfo{}, bind_callback);