Flutter Impeller
impeller::RuntimeStage Class Reference

#include <runtime_stage.h>

Public Types

using Map = std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > >
 

Public Member Functions

 ~RuntimeStage ()
 
 RuntimeStage (RuntimeStage &&)
 
RuntimeStage & operator= (RuntimeStage &&)
 
RuntimeShaderStage GetShaderStage () const
 
const std::vector< RuntimeUniformDescription > & GetUniforms () const
 
const std::vector< DescriptorSetLayout > & GetDescriptorSetLayouts () const
 
const std::string & GetEntrypoint () const
 
const RuntimeUniformDescription * GetUniform (const std::string &name) const
 
const std::shared_ptr< fml::Mapping > & GetCodeMapping () const
 
bool IsDirty () const
 
void SetClean ()
 
void SetLibraryId (std::string library_id)
 
const std::string & GetLibraryId () const
 

Static Public Member Functions

static absl::StatusOr< Map > DecodeRuntimeStages (const std::shared_ptr< fml::Mapping > &payload)
 
static absl::StatusOr< RuntimeStage > Create (const fb::RuntimeStage *runtime_stage, const std::shared_ptr< fml::Mapping > &payload)
 

Static Public Attributes

static const char * kVulkanUBOName
 

Detailed Description

Definition at line 21 of file runtime_stage.h.

Member Typedef Documentation

◆ Map

using impeller::RuntimeStage::Map = std::map<RuntimeStageBackend, std::shared_ptr<RuntimeStage> >

Definition at line 25 of file runtime_stage.h.

Constructor & Destructor Documentation

◆ ~RuntimeStage()

impeller::RuntimeStage::~RuntimeStage ( )
default

◆ RuntimeStage()

impeller::RuntimeStage::RuntimeStage ( RuntimeStage &&  )
default

Member Function Documentation

◆ Create()

absl::StatusOr< RuntimeStage > impeller::RuntimeStage::Create ( const fb::RuntimeStage *  runtime_stage,
const std::shared_ptr< fml::Mapping > &  payload 
)
static

Definition at line 67 of file runtime_stage.cc.

69  {
70  if (!runtime_stage) {
71  return absl::InvalidArgumentError("Runtime stage is null.");
72  }
73 
74  RuntimeStage stage(payload);
75  stage.stage_ = ToShaderStage(runtime_stage->stage());
76  stage.entrypoint_ = runtime_stage->entrypoint()->str();
77  stage.library_id_ = MakeFallbackLibraryId();
78 
79  auto* uniforms = runtime_stage->uniforms();
80 
81  // Note: image bindings are screwy and will always have the same offset.
82  // track the binding of the UBO to determine where the image bindings go.
83  // This is only guaranteed to give us the correct bindings if there is a
84  // single sampler2D.
85  std::optional<size_t> ubo_id;
86  if (uniforms) {
87  for (auto i = uniforms->begin(), end = uniforms->end(); i != end; i++) {
88  RuntimeUniformDescription desc;
89  desc.name = i->name()->str();
90  desc.location = i->location();
91  desc.binding = i->binding();
92  desc.type = ToType(i->type());
93  if (desc.type == kStruct) {
94  ubo_id = desc.location;
95  desc.binding = desc.location;
96  }
97  desc.dimensions = RuntimeUniformDimensions{
98  static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
99  desc.bit_width = i->bit_width();
100  desc.array_elements = i->array_elements();
101  if (i->padding_layout()) {
102  for (const auto& byte_type : *i->padding_layout()) {
104  switch (byte_type) {
107  break;
110  break;
111  }
112  desc.padding_layout.push_back(type);
113  }
114  }
115  if (i->struct_fields()) {
116  for (const auto& elem : *i->struct_fields()) {
117  desc.struct_fields.emplace_back(
118  StructField{.name = elem->name()->str(),
119  .byte_size = static_cast<size_t>(elem->byte_size())});
120  }
121  }
122  desc.struct_float_count = i->struct_float_count();
123  stage.uniforms_.push_back(std::move(desc));
124  }
125  }
126 
127  stage.code_mapping_ = std::make_shared<fml::NonOwnedMapping>(
128  runtime_stage->shader()->data(), //
129  runtime_stage->shader()->size(), //
130  [payload = stage.payload_](auto, auto) {} //
131  );
132 
133  size_t binding = 64;
134  if (ubo_id.has_value() && ubo_id.value() == binding) {
135  binding++;
136  }
137  for (auto& uniform : stage.uniforms_) {
138  if (uniform.type == kSampledImage) {
139  uniform.binding = binding;
140  binding++;
141  if (ubo_id.has_value() && ubo_id.value() == binding) {
142  binding++;
143  }
144  }
145  }
146 
147  for (const auto& uniform : stage.GetUniforms()) {
148  if (uniform.type == kStruct) {
149  stage.descriptor_set_layouts_.push_back(DescriptorSetLayout{
150  static_cast<uint32_t>(uniform.location),
153  });
154  } else if (uniform.type == kSampledImage) {
155  stage.descriptor_set_layouts_.push_back(DescriptorSetLayout{
156  static_cast<uint32_t>(uniform.binding),
159  });
160  }
161  }
162 
163  return stage;
164 }
RuntimeStage(RuntimeStage &&)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
static RuntimeUniformType ToType(fb::UniformDataType type)
constexpr auto kPadding
const size_t end

References impeller::RuntimeUniformDescription::array_elements, impeller::RuntimeUniformDescription::binding, impeller::RuntimeUniformDescription::bit_width, impeller::RuntimeUniformDescription::dimensions, end, GetUniforms(), impeller::kFloat, impeller::kFragment, impeller::kPadding, impeller::kSampledImage, impeller::kStruct, impeller::kUniformBuffer, impeller::RuntimeUniformDescription::location, impeller::StructField::name, impeller::RuntimeUniformDescription::name, impeller::RuntimeUniformDescription::padding_layout, impeller::RuntimeUniformDescription::struct_fields, impeller::RuntimeUniformDescription::struct_float_count, impeller::ToShaderStage(), impeller::ToType(), and impeller::RuntimeUniformDescription::type.

◆ DecodeRuntimeStages()

absl::StatusOr< RuntimeStage::Map > impeller::RuntimeStage::DecodeRuntimeStages ( const std::shared_ptr< fml::Mapping > &  payload)
static

Definition at line 176 of file runtime_stage.cc.

177  {
178  if (payload == nullptr || !payload->GetMapping()) {
179  return absl::InvalidArgumentError("Payload is null or empty.");
180  }
181  if (!fb::RuntimeStagesBufferHasIdentifier(payload->GetMapping())) {
182  return absl::InvalidArgumentError(
183  "Payload does not have valid identifier.");
184  }
185 
186  flatbuffers::Verifier verifier(payload->GetMapping(), payload->GetSize());
187  if (!fb::VerifyRuntimeStagesBuffer(verifier)) {
188  return absl::InvalidArgumentError(
189  "Runtime stages buffer failed verification.");
190  }
191 
192  auto raw_stages = fb::GetRuntimeStages(payload->GetMapping());
193  if (!raw_stages) {
194  return absl::InvalidArgumentError("Failed to get runtime stages.");
195  }
196 
197  const uint32_t version = raw_stages->format_version();
198  const auto expected =
199  static_cast<uint32_t>(fb::RuntimeStagesFormatVersion::kVersion);
200  if (version != expected) {
201  std::stringstream stream;
202  stream << "Unsupported runtime stages format version. Expected " << expected
203  << ", got " << version << ".";
204  return absl::InvalidArgumentError(stream.str());
205  }
206 
207  return Map{
209  RuntimeStageIfPresent(raw_stages->sksl(), payload)},
211  RuntimeStageIfPresent(raw_stages->metal(), payload)},
213  RuntimeStageIfPresent(raw_stages->opengles(), payload)},
215  RuntimeStageIfPresent(raw_stages->opengles3(), payload)},
217  RuntimeStageIfPresent(raw_stages->vulkan(), payload)},
218  };
219 }
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
Definition: runtime_stage.h:25

References impeller::kMetal, impeller::kOpenGLES, impeller::kOpenGLES3, impeller::kSkSL, and impeller::kVulkan.

Referenced by impeller::interop::FragmentProgram::FragmentProgram(), impeller::GoldenPlaygroundTest::OpenAssetAsRuntimeStage(), impeller::PlaygroundTest::OpenAssetAsRuntimeStage(), and impeller::testing::TEST_P().

◆ GetCodeMapping()

const std::shared_ptr< fml::Mapping > & impeller::RuntimeStage::GetCodeMapping ( ) const

Definition at line 228 of file runtime_stage.cc.

228  {
229  return code_mapping_;
230 }

Referenced by impeller::RuntimeStagePlayground::RegisterStage().

◆ GetDescriptorSetLayouts()

const std::vector< DescriptorSetLayout > & impeller::RuntimeStage::GetDescriptorSetLayouts ( ) const

Definition at line 271 of file runtime_stage.cc.

272  {
273  return descriptor_set_layouts_;
274 }

◆ GetEntrypoint()

const std::string & impeller::RuntimeStage::GetEntrypoint ( ) const

Definition at line 247 of file runtime_stage.cc.

247  {
248  return entrypoint_;
249 }

Referenced by impeller::RuntimeStagePlayground::RegisterStage().

◆ GetLibraryId()

const std::string & impeller::RuntimeStage::GetLibraryId ( ) const

Definition at line 267 of file runtime_stage.cc.

267  {
268  return library_id_;
269 }

◆ GetShaderStage()

RuntimeShaderStage impeller::RuntimeStage::GetShaderStage ( ) const

Definition at line 251 of file runtime_stage.cc.

251  {
252  return stage_;
253 }

Referenced by impeller::RuntimeStagePlayground::RegisterStage().

◆ GetUniform()

const RuntimeUniformDescription * impeller::RuntimeStage::GetUniform ( const std::string &  name) const

Definition at line 237 of file runtime_stage.cc.

238  {
239  for (const auto& uniform : uniforms_) {
240  if (uniform.name == name) {
241  return &uniform;
242  }
243  }
244  return nullptr;
245 }

◆ GetUniforms()

const std::vector< RuntimeUniformDescription > & impeller::RuntimeStage::GetUniforms ( ) const

Definition at line 232 of file runtime_stage.cc.

233  {
234  return uniforms_;
235 }

Referenced by Create().

◆ IsDirty()

bool impeller::RuntimeStage::IsDirty ( ) const

Definition at line 255 of file runtime_stage.cc.

255  {
256  return is_dirty_;
257 }

◆ operator=()

RuntimeStage & impeller::RuntimeStage::operator= ( RuntimeStage &&  )
default

◆ SetClean()

void impeller::RuntimeStage::SetClean ( )

Definition at line 259 of file runtime_stage.cc.

259  {
260  is_dirty_ = false;
261 }

◆ SetLibraryId()

void impeller::RuntimeStage::SetLibraryId ( std::string  library_id)

Stable per-source identifier used to namespace this stage's entrypoint in the shared shader registry, so that user-supplied runtime shaders cannot collide with engine-internal shaders or with shaders from other asset paths. Defaults to a process-unique fallback assigned at decode time. Should be overwritten with a stable id (typically the asset path the stage was loaded from) so that hot reload of the same asset continues to evict and replace the same registry slot.

Definition at line 263 of file runtime_stage.cc.

263  {
264  library_id_ = std::move(library_id);
265 }

Member Data Documentation

◆ kVulkanUBOName

const char * impeller::RuntimeStage::kVulkanUBOName
static
Initial value:
=
"_RESERVED_IDENTIFIER_FIXUP_gl_DefaultUniformBlock"

The generated name from GLSLang/shaderc for the UBO containing non-opaque uniforms specified in the user-written runtime effect shader.

Vulkan does not allow non-opaque uniforms outside of a UBO.

Definition at line 23 of file runtime_stage.h.

Referenced by impeller::testing::TEST_P().


The documentation for this class was generated from the following files: