|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "impeller/entity/contents/runtime_effect_contents.h" |
| 6 | + |
| 7 | +#include <future> |
| 8 | +#include <memory> |
| 9 | + |
| 10 | +#include "flutter/fml/logging.h" |
| 11 | +#include "flutter/fml/make_copyable.h" |
| 12 | +#include "impeller/base/validation.h" |
| 13 | +#include "impeller/entity/contents/clip_contents.h" |
| 14 | +#include "impeller/entity/contents/content_context.h" |
| 15 | +#include "impeller/entity/position_no_color.vert.h" |
| 16 | +#include "impeller/renderer/formats.h" |
| 17 | +#include "impeller/renderer/pipeline_library.h" |
| 18 | +#include "impeller/renderer/render_pass.h" |
| 19 | +#include "impeller/renderer/shader_function.h" |
| 20 | +#include "impeller/renderer/shader_types.h" |
| 21 | + |
| 22 | +namespace impeller { |
| 23 | + |
| 24 | +void RuntimeEffectContents::SetRuntimeStage( |
| 25 | + std::shared_ptr<RuntimeStage> runtime_stage) { |
| 26 | + runtime_stage_ = std::move(runtime_stage); |
| 27 | +} |
| 28 | + |
| 29 | +void RuntimeEffectContents::SetUniformData(std::vector<uint8_t> uniform_data) { |
| 30 | + uniform_data_ = std::move(uniform_data); |
| 31 | +} |
| 32 | + |
| 33 | +bool RuntimeEffectContents::Render(const ContentContext& renderer, |
| 34 | + const Entity& entity, |
| 35 | + RenderPass& pass) const { |
| 36 | + auto context = renderer.GetContext(); |
| 37 | + auto library = context->GetShaderLibrary(); |
| 38 | + |
| 39 | + //-------------------------------------------------------------------------- |
| 40 | + /// Get or register shader. |
| 41 | + /// |
| 42 | + |
| 43 | + // TODO(113719): Register the shader function earlier. |
| 44 | + |
| 45 | + std::shared_ptr<const ShaderFunction> function = library->GetFunction( |
| 46 | + runtime_stage_->GetEntrypoint(), ShaderStage::kFragment); |
| 47 | + |
| 48 | + if (!function) { |
| 49 | + std::promise<bool> promise; |
| 50 | + auto future = promise.get_future(); |
| 51 | + |
| 52 | + library->RegisterFunction( |
| 53 | + runtime_stage_->GetEntrypoint(), |
| 54 | + ToShaderStage(runtime_stage_->GetShaderStage()), |
| 55 | + runtime_stage_->GetCodeMapping(), |
| 56 | + fml::MakeCopyable([promise = std::move(promise)](bool result) mutable { |
| 57 | + promise.set_value(result); |
| 58 | + })); |
| 59 | + |
| 60 | + if (!future.get()) { |
| 61 | + VALIDATION_LOG << "Failed to build runtime effect (entry point: " |
| 62 | + << runtime_stage_->GetEntrypoint() << ")"; |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + function = library->GetFunction(runtime_stage_->GetEntrypoint(), |
| 67 | + ShaderStage::kFragment); |
| 68 | + if (!function) { |
| 69 | + VALIDATION_LOG |
| 70 | + << "Failed to fetch runtime effect function immediately after " |
| 71 | + "registering it (entry point: " |
| 72 | + << runtime_stage_->GetEntrypoint() << ")"; |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + //-------------------------------------------------------------------------- |
| 78 | + /// Resolve geometry. |
| 79 | + /// |
| 80 | + |
| 81 | + auto geometry_result = GetGeometry()->GetPositionBuffer( |
| 82 | + context->GetResourceAllocator(), pass.GetTransientsBuffer(), |
| 83 | + renderer.GetTessellator(), pass.GetRenderTargetSize()); |
| 84 | + |
| 85 | + //-------------------------------------------------------------------------- |
| 86 | + /// Get or create runtime stage pipeline. |
| 87 | + /// |
| 88 | + |
| 89 | + using VS = PositionNoColorVertexShader; |
| 90 | + PipelineDescriptor desc; |
| 91 | + desc.SetLabel("Runtime Stage"); |
| 92 | + desc.AddStageEntrypoint( |
| 93 | + library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex)); |
| 94 | + desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(), |
| 95 | + ShaderStage::kFragment)); |
| 96 | + auto vertex_descriptor = std::make_shared<VertexDescriptor>(); |
| 97 | + if (!vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs)) { |
| 98 | + VALIDATION_LOG << "Failed to set stage inputs for runtime effect pipeline."; |
| 99 | + } |
| 100 | + desc.SetVertexDescriptor(std::move(vertex_descriptor)); |
| 101 | + desc.SetColorAttachmentDescriptor(0u, {.format = PixelFormat::kDefaultColor}); |
| 102 | + desc.SetStencilAttachmentDescriptors({}); |
| 103 | + desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil); |
| 104 | + |
| 105 | + auto options = OptionsFromPassAndEntity(pass, entity); |
| 106 | + if (geometry_result.prevent_overdraw) { |
| 107 | + options.stencil_compare = CompareFunction::kEqual; |
| 108 | + options.stencil_operation = StencilOperation::kIncrementClamp; |
| 109 | + } |
| 110 | + options.ApplyToPipelineDescriptor(desc); |
| 111 | + |
| 112 | + auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).get(); |
| 113 | + if (!pipeline) { |
| 114 | + VALIDATION_LOG << "Failed to get or create runtime effect pipeline."; |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + Command cmd; |
| 119 | + cmd.label = "RuntimeEffectContents"; |
| 120 | + cmd.pipeline = pipeline; |
| 121 | + cmd.stencil_reference = entity.GetStencilDepth(); |
| 122 | + cmd.BindVertices(geometry_result.vertex_buffer); |
| 123 | + cmd.primitive_type = geometry_result.type; |
| 124 | + |
| 125 | + //-------------------------------------------------------------------------- |
| 126 | + /// Vertex stage uniforms. |
| 127 | + /// |
| 128 | + |
| 129 | + VS::VertInfo frame_info; |
| 130 | + frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * |
| 131 | + entity.GetTransformation(); |
| 132 | + VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); |
| 133 | + |
| 134 | + //-------------------------------------------------------------------------- |
| 135 | + /// Fragment stage uniforms. |
| 136 | + /// |
| 137 | + |
| 138 | + size_t buffer_index = 0; |
| 139 | + for (auto uniform : runtime_stage_->GetUniforms()) { |
| 140 | + // TODO(113715): Populate this metadata once GLES is able to handle |
| 141 | + // non-struct uniform names. |
| 142 | + ShaderMetadata metadata; |
| 143 | + |
| 144 | + size_t alignment = |
| 145 | + std::max(uniform.bit_width / 8, DefaultUniformAlignment()); |
| 146 | + auto buffer_view = pass.GetTransientsBuffer().Emplace( |
| 147 | + &uniform_data_[uniform.location * sizeof(float)], uniform.GetSize(), |
| 148 | + alignment); |
| 149 | + |
| 150 | + ShaderUniformSlot slot; |
| 151 | + slot.name = uniform.name.c_str(); |
| 152 | + slot.ext_res_0 = buffer_index; |
| 153 | + cmd.BindResource(ShaderStage::kFragment, slot, metadata, buffer_view); |
| 154 | + |
| 155 | + buffer_index++; |
| 156 | + } |
| 157 | + |
| 158 | + pass.AddCommand(std::move(cmd)); |
| 159 | + |
| 160 | + if (geometry_result.prevent_overdraw) { |
| 161 | + return ClipRestoreContents().Render(renderer, entity, pass); |
| 162 | + } |
| 163 | + return true; |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace impeller |
0 commit comments