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 return !!CreatePipeline(renderer,
options);
81bool RuntimeEffectContents::RegisterShader(
83 const std::shared_ptr<Context>& context = renderer.GetContext();
84 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
86 std::shared_ptr<const ShaderFunction>
function = library->GetFunction(
93 if (
function && runtime_stage_->IsDirty()) {
94 renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
95 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(
function);
96 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
103 std::promise<bool> promise;
104 auto future = promise.get_future();
106 library->RegisterFunction(
107 runtime_stage_->GetEntrypoint(),
109 runtime_stage_->GetCodeMapping(),
111 promise.set_value(result);
116 << runtime_stage_->GetEntrypoint() <<
")";
120 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
124 <<
"Failed to fetch runtime effect function immediately after "
125 "registering it (entry point: "
126 << runtime_stage_->GetEntrypoint() <<
")";
130 runtime_stage_->SetClean();
135std::shared_ptr<Pipeline<PipelineDescriptor>>
136RuntimeEffectContents::CreatePipeline(
const ContentContext& renderer,
137 ContentContextOptions
options)
const {
138 const std::shared_ptr<Context>& context =
renderer.GetContext();
139 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
140 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
141 const auto color_attachment_format = caps->GetDefaultColorFormat();
142 const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
144 using VS = RuntimeEffectVertexShader;
146 PipelineDescriptor
desc;
147 desc.SetLabel(
"Runtime Stage");
148 desc.AddStageEntrypoint(
150 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
153 std::shared_ptr<VertexDescriptor> vertex_descriptor =
154 std::make_shared<VertexDescriptor>();
155 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
156 VS::kInterleavedBufferLayout);
157 vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
158 vertex_descriptor->RegisterDescriptorSetLayouts(
159 runtime_stage_->GetDescriptorSetLayouts().data(),
160 runtime_stage_->GetDescriptorSetLayouts().size());
161 desc.SetVertexDescriptor(std::move(vertex_descriptor));
162 desc.SetColorAttachmentDescriptor(
163 0u, {.format = color_attachment_format, .blending_enabled =
true});
165 desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
166 desc.SetStencilPixelFormat(stencil_attachment_format);
168 desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
169 desc.SetDepthPixelFormat(stencil_attachment_format);
171 options.ApplyToPipelineDescriptor(desc);
172 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
174 VALIDATION_LOG <<
"Failed to get or create runtime effect pipeline.";
184 const std::shared_ptr<Context>& context = renderer.GetContext();
185 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
192 if (!RegisterShader(renderer)) {
201 size_t minimum_sampler_index = 100000000;
202 size_t buffer_index = 0;
203 size_t buffer_offset = 0;
205 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
208 switch (uniform.type) {
219 minimum_sampler_index =
220 std::min(minimum_sampler_index, uniform.location);
224 FML_DCHECK(renderer.GetContext()->GetBackendType() !=
226 <<
"Uniform " << uniform.name
227 <<
" had unexpected type kFloat for Vulkan backend.";
231 BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
232 uniform_data_->data() + buffer_offset, uniform.GetSize(),
236 uniform_slot.
name = uniform.name.c_str();
237 uniform_slot.
ext_res_0 = uniform.location;
240 metadata, std::move(buffer_view));
242 buffer_offset += uniform.GetSize();
246 FML_DCHECK(renderer.GetContext()->GetBackendType() ==
249 uniform_slot.
name = uniform.name.c_str();
250 uniform_slot.
binding = uniform.location;
254 std::vector<float> uniform_buffer;
255 uniform_buffer.reserve(uniform.struct_layout.size());
256 size_t uniform_byte_index = 0u;
257 for (
const auto& byte_type : uniform.struct_layout) {
258 if (byte_type == 0) {
259 uniform_buffer.push_back(0.f);
260 }
else if (byte_type == 1) {
261 uniform_buffer.push_back(
reinterpret_cast<float*
>(
262 uniform_data_->data())[uniform_byte_index++]);
267 size_t alignment = std::max(
sizeof(
float) * uniform_buffer.size(),
270 BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
271 reinterpret_cast<const void*
>(uniform_buffer.data()),
272 sizeof(
float) * uniform_buffer.size(), alignment);
280 size_t sampler_index = 0;
281 for (
const auto& uniform : runtime_stage_->GetUniforms()) {
284 switch (uniform.type) {
286 FML_DCHECK(sampler_index < texture_inputs_.size());
287 auto& input = texture_inputs_[sampler_index];
289 const std::unique_ptr<const Sampler>& sampler =
290 context->GetSamplerLibrary()->GetSampler(
291 input.sampler_descriptor);
294 image_slot.
name = uniform.name.c_str();
295 image_slot.
binding = uniform.binding;
296 image_slot.
texture_index = uniform.location - minimum_sampler_index;
299 *metadata, input.texture, sampler);
312 using VS = RuntimeEffectVertexShader;
317 return renderer.GetCachedRuntimeEffectPipeline(
318 runtime_stage_->GetEntrypoint(),
options,
319 [&]() {
return CreatePipeline(renderer,
options); });
322 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
324 VS::FrameInfo{}, bind_callback);
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Render passes encode render commands directed as one specific render target into an underlying comman...
virtual bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
bool BootstrapShader(const ContentContext &renderer) const
Load the runtime effect and ensure a default PSO is initialized.
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
void SetTextureInputs(std::vector< TextureInput > texture_inputs)
void SetUniformData(std::shared_ptr< std::vector< uint8_t > > uniform_data)
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
#define FML_UNREACHABLE()
#define FML_DCHECK(condition)
Dart_NativeFunction function
internal::CopyableLambda< T > MakeCopyable(T lambda)
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
static ShaderType GetShaderType(RuntimeUniformType type)
constexpr size_t DefaultUniformAlignment()
PixelFormat color_attachment_pixel_format
Metadata required to bind a combined texture and sampler.
size_t texture_index
ext_res_0 is the Metal binding value.
const char * name
The name of the uniform slot.
size_t binding
The Vulkan binding value.