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 213 of file runtime_stage_data.cc.

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

403  {
404  auto runtime_stages = CreateMultiStageFlatbuffer();
405  if (!runtime_stages) {
406  return nullptr;
407  }
408 
409  auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
410  builder->Finish(fb::RuntimeStages::Pack(*builder.get(), runtime_stages.get()),
411  fb::RuntimeStagesIdentifier());
412  return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
413  builder->GetSize(),
414  [builder](auto, auto) {});
415 }
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 373 of file runtime_stage_data.cc.

373  {
374  // The high level object API is used here for writing to the buffer. This is
375  // just a convenience.
376  auto runtime_stages = std::make_unique<fb::RuntimeStagesT>();
377  runtime_stages->format_version =
378  static_cast<uint32_t>(fb::RuntimeStagesFormatVersion::kVersion);
379 
380  for (const auto& kvp : data_) {
381  auto runtime_stage = CreateStageFlatbuffer(kvp.first);
382  switch (kvp.first) {
384  runtime_stages->sksl = std::move(runtime_stage);
385  break;
387  runtime_stages->metal = std::move(runtime_stage);
388  break;
390  runtime_stages->opengles = std::move(runtime_stage);
391  break;
393  runtime_stages->vulkan = std::move(runtime_stage);
394  break;
396  runtime_stages->opengles3 = std::move(runtime_stage);
397  break;
398  }
399  }
400  return runtime_stages;
401 }
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 289 of file runtime_stage_data.cc.

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

Referenced by CreateMultiStageFlatbuffer().


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