Flutter Impeller
impeller::compiler::RuntimeStageData Class Reference

#include <runtime_stage_data.h>

Classes

struct  Shader
 

Public Member Functions

 RuntimeStageData ()
 
 ~RuntimeStageData ()
 
void AddShader (const std::shared_ptr< Shader > &data)
 
std::unique_ptr< fb::RuntimeStageT > CreateStageFlatbuffer (impeller::RuntimeStageBackend backend) const
 
std::unique_ptr< fb::RuntimeStagesT > CreateMultiStageFlatbuffer () const
 
std::shared_ptr< fml::Mapping > CreateJsonMapping () const
 
std::shared_ptr< fml::Mapping > CreateMapping () const
 

Detailed Description

Definition at line 20 of file runtime_stage_data.h.

Constructor & Destructor Documentation

◆ RuntimeStageData()

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

◆ ~RuntimeStageData()

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

Member Function Documentation

◆ AddShader()

void impeller::compiler::RuntimeStageData::AddShader ( const std::shared_ptr< Shader > &  data)

Definition at line 27 of file runtime_stage_data.cc.

27  {
28  FML_DCHECK(data);
29  FML_DCHECK(data_.find(data->backend) == data_.end());
30  data_[data->backend] = data;
31 }
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:69

References data.

Referenced by impeller::compiler::OutputIPLR().

◆ CreateJsonMapping()

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

Definition at line 214 of file runtime_stage_data.cc.

214  {
215  // Runtime Stage Data JSON format
216  // {
217  // "format_version": 1,
218  // "sksl": {
219  // "stage": 0,
220  // "entrypoint": "",
221  // "shader": "",
222  // "uniforms": [
223  // {
224  // "name": "..",
225  // "location": 0,
226  // "type": 0,
227  // "rows": 0,
228  // "columns": 0,
229  // "bit_width": 0,
230  // "array_elements": 0,
231  // }
232  // ]
233  // },
234  // "metal": ...
235  // },
236  nlohmann::json root;
237 
238  root[kFormatVersionKey] =
239  static_cast<uint32_t>(fb::RuntimeStagesFormatVersion::kVersion);
240  for (const auto& kvp : data_) {
241  nlohmann::json platform_object;
242 
243  const auto stage = ToJsonStage(kvp.second->stage);
244  if (!stage.has_value()) {
245  VALIDATION_LOG << "Invalid runtime stage.";
246  return nullptr;
247  }
248  platform_object[kStageKey] = static_cast<uint32_t>(stage.value());
249  platform_object[kEntrypointKey] = kvp.second->entrypoint;
250 
251  if (kvp.second->shader->GetSize() > 0u) {
252  std::string shader(
253  reinterpret_cast<const char*>(kvp.second->shader->GetMapping()),
254  kvp.second->shader->GetSize());
255  platform_object[kShaderKey] = shader.c_str();
256  }
257 
258  auto& uniforms = platform_object[kUniformsKey] = nlohmann::json::array_t{};
259  for (const auto& uniform : kvp.second->uniforms) {
260  nlohmann::json uniform_object;
261  uniform_object[kUniformNameKey] = uniform.name.c_str();
262  uniform_object[kUniformLocationKey] = uniform.location;
263  uniform_object[kUniformRowsKey] = uniform.rows;
264  uniform_object[kUniformColumnsKey] = uniform.columns;
265 
266  auto uniform_type = ToJsonType(uniform.type);
267  if (!uniform_type.has_value()) {
268  VALIDATION_LOG << "Invalid uniform type for runtime stage.";
269  return nullptr;
270  }
271 
272  uniform_object[kUniformTypeKey] = uniform_type.value();
273  uniform_object[kUniformBitWidthKey] = uniform.bit_width;
274  uniform_object[kUniformArrayElementsKey] =
275  uniform.array_elements.value_or(0);
276 
277  uniforms.push_back(uniform_object);
278  }
279 
280  root[RuntimeStageBackendToString(kvp.first)] = platform_object;
281  }
282 
283  auto json_string = std::make_shared<std::string>(root.dump(2u));
284 
285  return std::make_shared<fml::NonOwnedMapping>(
286  reinterpret_cast<const uint8_t*>(json_string->data()),
287  json_string->size(), [json_string](auto, auto) {});
288 }
static const char * kFormatVersionKey
static const char * kUniformLocationKey
static const char * kUniformNameKey
static std::optional< fb::Stage > ToJsonStage(spv::ExecutionModel stage)
static const char * kEntrypointKey
static const char * kUniformColumnsKey
static std::optional< uint32_t > ToJsonType(spirv_cross::SPIRType::BaseType type)
static std::string RuntimeStageBackendToString(RuntimeStageBackend backend)
static const char * kUniformTypeKey
static const char * kStageKey
static const char * kUniformsKey
static const char * kUniformRowsKey
static const char * kShaderKey
static const char * kUniformArrayElementsKey
static const char * kUniformBitWidthKey
#define VALIDATION_LOG
Definition: validation.h:91

References impeller::compiler::kEntrypointKey, impeller::compiler::kFormatVersionKey, impeller::compiler::kShaderKey, impeller::compiler::kStageKey, 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::RuntimeStageBackendToString(), impeller::compiler::ToJsonStage(), impeller::compiler::ToJsonType(), and VALIDATION_LOG.

Referenced by impeller::compiler::OutputIPLR().

◆ CreateMapping()

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

Definition at line 411 of file runtime_stage_data.cc.

411  {
412  auto runtime_stages = CreateMultiStageFlatbuffer();
413  if (!runtime_stages) {
414  return nullptr;
415  }
416 
417  auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
418  builder->Finish(fb::RuntimeStages::Pack(*builder.get(), runtime_stages.get()),
419  fb::RuntimeStagesIdentifier());
420  return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
421  builder->GetSize(),
422  [builder](auto, auto) {});
423 }
std::unique_ptr< fb::RuntimeStagesT > CreateMultiStageFlatbuffer() const

References CreateMultiStageFlatbuffer().

Referenced by impeller::compiler::OutputIPLR().

◆ CreateMultiStageFlatbuffer()

std::unique_ptr< fb::RuntimeStagesT > impeller::compiler::RuntimeStageData::CreateMultiStageFlatbuffer ( ) const

Definition at line 381 of file runtime_stage_data.cc.

381  {
382  // The high level object API is used here for writing to the buffer. This is
383  // just a convenience.
384  auto runtime_stages = std::make_unique<fb::RuntimeStagesT>();
385  runtime_stages->format_version =
386  static_cast<uint32_t>(fb::RuntimeStagesFormatVersion::kVersion);
387 
388  for (const auto& kvp : data_) {
389  auto runtime_stage = CreateStageFlatbuffer(kvp.first);
390  switch (kvp.first) {
392  runtime_stages->sksl = std::move(runtime_stage);
393  break;
395  runtime_stages->metal = std::move(runtime_stage);
396  break;
398  runtime_stages->opengles = std::move(runtime_stage);
399  break;
401  runtime_stages->vulkan = std::move(runtime_stage);
402  break;
404  runtime_stages->opengles3 = std::move(runtime_stage);
405  break;
406  }
407  }
408  return runtime_stages;
409 }
std::unique_ptr< fb::RuntimeStageT > CreateStageFlatbuffer(impeller::RuntimeStageBackend backend) const

References CreateStageFlatbuffer(), impeller::kMetal, impeller::kOpenGLES, impeller::kOpenGLES3, impeller::kSkSL, and impeller::kVulkan.

Referenced by CreateMapping().

◆ CreateStageFlatbuffer()

std::unique_ptr< fb::RuntimeStageT > impeller::compiler::RuntimeStageData::CreateStageFlatbuffer ( impeller::RuntimeStageBackend  backend) const

Definition at line 290 of file runtime_stage_data.cc.

291  {
292  auto kvp = data_.find(backend);
293  if (kvp == data_.end()) {
294  return nullptr;
295  }
296 
297  auto runtime_stage = std::make_unique<fb::RuntimeStageT>();
298  runtime_stage->entrypoint = kvp->second->entrypoint;
299  const auto stage = ToStage(kvp->second->stage);
300  if (!stage.has_value()) {
301  VALIDATION_LOG << "Invalid runtime stage.";
302  return nullptr;
303  }
304  runtime_stage->stage = stage.value();
305  if (!kvp->second->shader) {
306  VALIDATION_LOG << "No shader specified for runtime stage.";
307  return nullptr;
308  }
309  if (kvp->second->shader->GetSize() > 0u) {
310  runtime_stage->shader = {
311  kvp->second->shader->GetMapping(),
312  kvp->second->shader->GetMapping() + kvp->second->shader->GetSize()};
313  }
314  for (const auto& uniform : kvp->second->uniforms) {
315  auto desc = std::make_unique<fb::UniformDescriptionT>();
316 
317  desc->name = uniform.name;
318  if (desc->name.empty()) {
319  VALIDATION_LOG << "Uniform name cannot be empty.";
320  return nullptr;
321  }
322  desc->location = uniform.location;
323  desc->rows = uniform.rows;
324  desc->columns = uniform.columns;
325  auto uniform_type = ToUniformType(uniform.type);
326  if (!uniform_type.has_value()) {
327  VALIDATION_LOG << "Invalid uniform type for runtime stage.";
328  return nullptr;
329  }
330  desc->type = uniform_type.value();
331  desc->bit_width = uniform.bit_width;
332  if (uniform.array_elements.has_value()) {
333  desc->array_elements = uniform.array_elements.value();
334  }
335 
336  for (const auto& byte_type : uniform.padding_layout) {
337  desc->padding_layout.push_back(static_cast<fb::PaddingType>(byte_type));
338  }
339  desc->struct_float_count = uniform.struct_float_count;
340 
341  for (const StructField& field : uniform.struct_fields) {
342  auto field_desc = std::make_unique<fb::StructFieldT>(fb::StructFieldT{});
343  field_desc->name = field.name;
344  field_desc->byte_size = field.byte_size;
345  desc->struct_fields.emplace_back(std::move(field_desc));
346  }
347 
348  runtime_stage->uniforms.emplace_back(std::move(desc));
349  }
350 
351  for (const auto& input : kvp->second->inputs) {
352  auto desc = std::make_unique<fb::StageInputT>();
353 
354  desc->name = input.name;
355 
356  if (desc->name.empty()) {
357  VALIDATION_LOG << "Stage input name cannot be empty.";
358  return nullptr;
359  }
360  desc->location = input.location;
361  desc->set = input.set;
362  desc->binding = input.binding;
363  auto input_type = ToInputType(input.type);
364  if (!input_type.has_value()) {
365  VALIDATION_LOG << "Invalid uniform type for runtime stage.";
366  return nullptr;
367  }
368  desc->type = input_type.value();
369  desc->bit_width = input.bit_width;
370  desc->vec_size = input.vec_size;
371  desc->columns = input.columns;
372  desc->offset = input.offset;
373 
374  runtime_stage->inputs.emplace_back(std::move(desc));
375  }
376 
377  return runtime_stage;
378 }
static std::optional< fb::Stage > ToStage(spv::ExecutionModel stage)
static std::optional< fb::UniformDataType > ToUniformType(spirv_cross::SPIRType::BaseType type)
static std::optional< fb::InputDataType > ToInputType(spirv_cross::SPIRType::BaseType type)

References impeller::compiler::StructField::byte_size, impeller::compiler::StructField::name, impeller::compiler::ToInputType(), impeller::compiler::ToStage(), impeller::compiler::ToUniformType(), and VALIDATION_LOG.

Referenced by CreateMultiStageFlatbuffer().


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