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;
76 options.color_attachment_pixel_format =
77 renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
82bool 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()) {
95 renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
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(),
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();
136std::shared_ptr<Pipeline<PipelineDescriptor>>
137RuntimeEffectContents::CreatePipeline(
const ContentContext&
renderer,
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);
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();
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);
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();
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;
324 return renderer.GetCachedRuntimeEffectPipeline(
325 runtime_stage_->GetEntrypoint(),
options, [&]() {
330 return ColorSourceContents::DrawGeometry<VS>(
renderer, entity, pass,
332 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
static float max(float r, float g, float b)
static float min(float r, float g, float b)
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()
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.