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);
59 auto metadata = std::make_shared<ShaderMetadata>();
60 metadata->name = uniform.
name;
72 if (!RegisterShader(renderer)) {
77 renderer.
GetContext()->GetCapabilities()->GetDefaultColorFormat();
78 CreatePipeline(renderer, options,
true);
82 bool RuntimeEffectContents::RegisterShader(
84 const std::shared_ptr<Context>& context = renderer.
GetContext();
85 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
87 std::shared_ptr<const ShaderFunction>
function = library->GetFunction(
94 if (
function && runtime_stage_->IsDirty()) {
96 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(
function);
97 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
104 std::promise<bool> promise;
105 auto future = promise.get_future();
107 library->RegisterFunction(
108 runtime_stage_->GetEntrypoint(),
110 runtime_stage_->GetCodeMapping(),
111 fml::MakeCopyable([promise = std::move(promise)](
bool result)
mutable {
112 promise.set_value(result);
117 << runtime_stage_->GetEntrypoint() <<
")";
121 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
125 <<
"Failed to fetch runtime effect function immediately after "
126 "registering it (entry point: "
127 << runtime_stage_->GetEntrypoint() <<
")";
131 runtime_stage_->SetClean();
136 std::shared_ptr<Pipeline<PipelineDescriptor>>
137 RuntimeEffectContents::CreatePipeline(
const ContentContext& renderer,
138 ContentContextOptions options,
140 const std::shared_ptr<Context>& context = renderer.GetContext();
141 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
142 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
143 const auto color_attachment_format = caps->GetDefaultColorFormat();
144 const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
146 using VS = RuntimeEffectVertexShader;
148 PipelineDescriptor desc;
149 desc.SetLabel(
"Runtime Stage");
150 desc.AddStageEntrypoint(
152 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
155 std::shared_ptr<VertexDescriptor> vertex_descriptor =
156 std::make_shared<VertexDescriptor>();
157 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
158 VS::kInterleavedBufferLayout);
159 vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
160 vertex_descriptor->RegisterDescriptorSetLayouts(
161 runtime_stage_->GetDescriptorSetLayouts().data(),
162 runtime_stage_->GetDescriptorSetLayouts().size());
163 desc.SetVertexDescriptor(std::move(vertex_descriptor));
164 desc.SetColorAttachmentDescriptor(
165 0u, {.format = color_attachment_format, .blending_enabled =
true});
167 desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
168 desc.SetStencilPixelFormat(stencil_attachment_format);
170 desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
171 desc.SetDepthPixelFormat(stencil_attachment_format);
173 options.ApplyToPipelineDescriptor(desc);
175 context->GetPipelineLibrary()->GetPipeline(desc, async);
179 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
181 VALIDATION_LOG <<
"Failed to get or create runtime effect pipeline.";
191 const std::shared_ptr<Context>& context = renderer.
GetContext();
192 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
199 if (!RegisterShader(renderer)) {
208 size_t minimum_sampler_index = 100000000;
209 size_t buffer_index = 0;
210 size_t buffer_offset = 0;
212 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
215 switch (uniform.type) {
226 minimum_sampler_index =
227 std::min(minimum_sampler_index, uniform.location);
231 FML_DCHECK(renderer.
GetContext()->GetBackendType() !=
233 <<
"Uniform " << uniform.name
234 <<
" had unexpected type kFloat for Vulkan backend.";
239 uniform_data_->data() + buffer_offset, uniform.GetSize(),
243 uniform_slot.
name = uniform.name.c_str();
244 uniform_slot.
ext_res_0 = uniform.location;
249 buffer_offset += uniform.GetSize();
253 FML_DCHECK(renderer.
GetContext()->GetBackendType() ==
256 uniform_slot.
name = uniform.name.c_str();
257 uniform_slot.
binding = uniform.location;
261 std::vector<float> uniform_buffer;
262 uniform_buffer.reserve(uniform.struct_layout.size());
263 size_t uniform_byte_index = 0u;
264 for (
const auto& byte_type : uniform.struct_layout) {
265 if (byte_type == 0) {
266 uniform_buffer.push_back(0.f);
267 }
else if (byte_type == 1) {
268 uniform_buffer.push_back(
reinterpret_cast<float*
>(
269 uniform_data_->data())[uniform_byte_index++]);
274 size_t alignment = std::max(
sizeof(
float) * uniform_buffer.size(),
278 reinterpret_cast<const void*
>(uniform_buffer.data()),
279 sizeof(
float) * uniform_buffer.size(), alignment);
287 size_t sampler_index = 0;
288 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
291 switch (uniform.type) {
293 FML_DCHECK(sampler_index < texture_inputs_.size());
294 auto& input = texture_inputs_[sampler_index];
296 const std::unique_ptr<const Sampler>& sampler =
297 context->GetSamplerLibrary()->GetSampler(
298 input.sampler_descriptor);
301 image_slot.
name = uniform.name.c_str();
302 image_slot.
binding = uniform.binding;
303 image_slot.
texture_index = uniform.location - minimum_sampler_index;
306 *metadata, input.texture, sampler);
319 using VS = RuntimeEffectVertexShader;
325 runtime_stage_->GetEntrypoint(), options, [&]() {
326 return CreatePipeline(renderer, options,
false);
330 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
332 VS::FrameInfo{}, bind_callback);