Flutter Impeller
impeller::compiler::RuntimeStageData Class Reference

#include <runtime_stage_data.h>

Public Member Functions

 RuntimeStageData (std::string entrypoint, spv::ExecutionModel stage, TargetPlatform target_platform)
 
 ~RuntimeStageData ()
 
void AddUniformDescription (UniformDescription uniform)
 
void SetShaderData (std::shared_ptr< fml::Mapping > shader)
 
void SetSkSLData (std::shared_ptr< fml::Mapping > sksl)
 
std::shared_ptr< fml::Mapping > CreateMapping () const
 
std::shared_ptr< fml::Mapping > CreateJsonMapping () const
 

Detailed Description

Definition at line 28 of file runtime_stage_data.h.

Constructor & Destructor Documentation

◆ RuntimeStageData()

impeller::compiler::RuntimeStageData::RuntimeStageData ( std::string  entrypoint,
spv::ExecutionModel  stage,
TargetPlatform  target_platform 
)

Definition at line 18 of file runtime_stage_data.cc.

21  : entrypoint_(std::move(entrypoint)),
22  stage_(stage),
23  target_platform_(target_platform) {}

◆ ~RuntimeStageData()

impeller::compiler::RuntimeStageData::~RuntimeStageData ( )
default

Member Function Documentation

◆ AddUniformDescription()

void impeller::compiler::RuntimeStageData::AddUniformDescription ( UniformDescription  uniform)

Definition at line 27 of file runtime_stage_data.cc.

27  {
28  uniforms_.emplace_back(std::move(uniform));
29 }

◆ CreateJsonMapping()

std::shared_ptr< fml::Mapping > impeller::compiler::RuntimeStageData::CreateJsonMapping ( ) const

Definition at line 221 of file runtime_stage_data.cc.

221  {
222  // Runtime Stage Data JSON format
223  // {
224  // "stage": 0,
225  // "target_platform": "",
226  // "entrypoint": "",
227  // "shader": "",
228  // "sksl": "",
229  // "uniforms": [
230  // {
231  // "name": "..",
232  // "location": 0,
233  // "type": 0,
234  // "rows": 0,
235  // "columns": 0,
236  // "bit_width": 0,
237  // "array_elements": 0,
238  // }
239  // ]
240  // },
241  nlohmann::json root;
242 
243  const auto stage = ToJsonStage(stage_);
244  if (!stage.has_value()) {
245  VALIDATION_LOG << "Invalid runtime stage.";
246  return nullptr;
247  }
248  root[kStageKey] = stage.value();
249 
250  const auto target_platform = ToJsonTargetPlatform(target_platform_);
251  if (!target_platform.has_value()) {
252  VALIDATION_LOG << "Invalid target platform for runtime stage.";
253  return nullptr;
254  }
255  root[kTargetPlatformKey] = target_platform.value();
256 
257  if (shader_->GetSize() > 0u) {
258  std::string shader(reinterpret_cast<const char*>(shader_->GetMapping()),
259  shader_->GetSize());
260  root[kShaderKey] = shader.c_str();
261  }
262 
263  auto& uniforms = root[kUniformsKey] = nlohmann::json::array_t{};
264  for (const auto& uniform : uniforms_) {
265  nlohmann::json uniform_object;
266  uniform_object[kUniformNameKey] = uniform.name.c_str();
267  uniform_object[kUniformLocationKey] = uniform.location;
268  uniform_object[kUniformRowsKey] = uniform.rows;
269  uniform_object[kUniformColumnsKey] = uniform.columns;
270 
271  auto uniform_type = ToJsonType(uniform.type);
272  if (!uniform_type.has_value()) {
273  VALIDATION_LOG << "Invalid uniform type for runtime stage.";
274  return nullptr;
275  }
276 
277  uniform_object[kUniformTypeKey] = uniform_type.value();
278  uniform_object[kUniformBitWidthKey] = uniform.bit_width;
279 
280  if (uniform.array_elements.has_value()) {
281  uniform_object[kUniformArrayElementsKey] = uniform.array_elements.value();
282  } else {
283  uniform_object[kUniformArrayElementsKey] = 0;
284  }
285  uniforms.push_back(uniform_object);
286  }
287 
288  auto json_string = std::make_shared<std::string>(root.dump(2u));
289 
290  return std::make_shared<fml::NonOwnedMapping>(
291  reinterpret_cast<const uint8_t*>(json_string->data()),
292  json_string->size(), [json_string](auto, auto) {});
293 }

References impeller::compiler::kShaderKey, impeller::compiler::kStageKey, impeller::compiler::kTargetPlatformKey, impeller::compiler::kUniformArrayElementsKey, impeller::compiler::kUniformBitWidthKey, impeller::compiler::kUniformColumnsKey, impeller::compiler::kUniformLocationKey, impeller::compiler::kUniformNameKey, impeller::compiler::kUniformRowsKey, impeller::compiler::kUniformsKey, impeller::compiler::kUniformTypeKey, impeller::compiler::ToJsonStage(), impeller::compiler::ToJsonTargetPlatform(), impeller::compiler::ToJsonType(), and VALIDATION_LOG.

◆ CreateMapping()

std::shared_ptr< fml::Mapping > impeller::compiler::RuntimeStageData::CreateMapping ( ) const

Definition at line 295 of file runtime_stage_data.cc.

295  {
296  // The high level object API is used here for writing to the buffer. This is
297  // just a convenience.
298  fb::RuntimeStageT runtime_stage;
299  runtime_stage.entrypoint = entrypoint_;
300  const auto stage = ToStage(stage_);
301  if (!stage.has_value()) {
302  VALIDATION_LOG << "Invalid runtime stage.";
303  return nullptr;
304  }
305  runtime_stage.stage = stage.value();
306  const auto target_platform = ToTargetPlatform(target_platform_);
307  if (!target_platform.has_value()) {
308  VALIDATION_LOG << "Invalid target platform for runtime stage.";
309  return nullptr;
310  }
311  runtime_stage.target_platform = target_platform.value();
312  if (!shader_) {
313  VALIDATION_LOG << "No shader specified for runtime stage.";
314  return nullptr;
315  }
316  if (shader_->GetSize() > 0u) {
317  runtime_stage.shader = {shader_->GetMapping(),
318  shader_->GetMapping() + shader_->GetSize()};
319  }
320  // It is not an error for the SkSL to be ommitted.
321  if (sksl_->GetSize() > 0u) {
322  runtime_stage.sksl = {sksl_->GetMapping(),
323  sksl_->GetMapping() + sksl_->GetSize()};
324  }
325  for (const auto& uniform : uniforms_) {
326  auto desc = std::make_unique<fb::UniformDescriptionT>();
327 
328  desc->name = uniform.name;
329  if (desc->name.empty()) {
330  VALIDATION_LOG << "Uniform name cannot be empty.";
331  return nullptr;
332  }
333  desc->location = uniform.location;
334  desc->rows = uniform.rows;
335  desc->columns = uniform.columns;
336  auto uniform_type = ToType(uniform.type);
337  if (!uniform_type.has_value()) {
338  VALIDATION_LOG << "Invalid uniform type for runtime stage.";
339  return nullptr;
340  }
341  desc->type = uniform_type.value();
342  desc->bit_width = uniform.bit_width;
343  if (uniform.array_elements.has_value()) {
344  desc->array_elements = uniform.array_elements.value();
345  }
346 
347  runtime_stage.uniforms.emplace_back(std::move(desc));
348  }
349  auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
350  builder->Finish(fb::RuntimeStage::Pack(*builder.get(), &runtime_stage),
351  fb::RuntimeStageIdentifier());
352  return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
353  builder->GetSize(),
354  [builder](auto, auto) {});
355 }

References impeller::compiler::ToStage(), impeller::compiler::ToTargetPlatform(), impeller::compiler::ToType(), and VALIDATION_LOG.

◆ SetShaderData()

void impeller::compiler::RuntimeStageData::SetShaderData ( std::shared_ptr< fml::Mapping >  shader)

Definition at line 31 of file runtime_stage_data.cc.

31  {
32  shader_ = std::move(shader);
33 }

◆ SetSkSLData()

void impeller::compiler::RuntimeStageData::SetSkSLData ( std::shared_ptr< fml::Mapping >  sksl)

Definition at line 35 of file runtime_stage_data.cc.

35  {
36  sksl_ = std::move(sksl);
37 }

The documentation for this class was generated from the following files:
impeller::compiler::kUniformBitWidthKey
static const char * kUniformBitWidthKey
Definition: runtime_stage_data.cc:218
impeller::compiler::kTargetPlatformKey
static const char * kTargetPlatformKey
Definition: runtime_stage_data.cc:209
impeller::compiler::ToTargetPlatform
static std::optional< fb::TargetPlatform > ToTargetPlatform(TargetPlatform platform)
Definition: runtime_stage_data.cc:75
impeller::compiler::kUniformNameKey
static const char * kUniformNameKey
Definition: runtime_stage_data.cc:213
impeller::compiler::kUniformColumnsKey
static const char * kUniformColumnsKey
Definition: runtime_stage_data.cc:217
impeller::compiler::kShaderKey
static const char * kShaderKey
Definition: runtime_stage_data.cc:212
impeller::compiler::kUniformTypeKey
static const char * kUniformTypeKey
Definition: runtime_stage_data.cc:215
impeller::compiler::kStageKey
static const char * kStageKey
Definition: runtime_stage_data.cc:208
impeller::compiler::ToStage
static std::optional< fb::Stage > ToStage(spv::ExecutionModel stage)
Definition: runtime_stage_data.cc:39
impeller::compiler::kUniformArrayElementsKey
static const char * kUniformArrayElementsKey
Definition: runtime_stage_data.cc:219
impeller::compiler::ToType
static std::optional< fb::UniformDataType > ToType(spirv_cross::SPIRType::BaseType type)
Definition: runtime_stage_data.cc:118
impeller::compiler::ToJsonType
static std::optional< uint32_t > ToJsonType(spirv_cross::SPIRType::BaseType type)
Definition: runtime_stage_data.cc:163
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::compiler::kUniformLocationKey
static const char * kUniformLocationKey
Definition: runtime_stage_data.cc:214
impeller::compiler::ToJsonStage
static std::optional< uint32_t > ToJsonStage(spv::ExecutionModel stage)
Definition: runtime_stage_data.cc:57
impeller::compiler::kUniformsKey
static const char * kUniformsKey
Definition: runtime_stage_data.cc:211
impeller::compiler::kUniformRowsKey
static const char * kUniformRowsKey
Definition: runtime_stage_data.cc:216
impeller::compiler::ToJsonTargetPlatform
static std::optional< uint32_t > ToJsonTargetPlatform(TargetPlatform platform)
Definition: runtime_stage_data.cc:97