| Flutter Impeller |

Classes | Public Member Functions | List of all members
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::RuntimeStagesT > | CreateFlatbuffer () const
std::shared_ptr< fml::Mapping > | CreateJsonMapping () const
std::shared_ptr< fml::Mapping > | CreateMapping () const

## Detailed Description

Definition at line 44 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 }

Referenced by impeller::compiler::GenerateShaderFB(), and impeller::compiler::OutputIPLR().

## ◆ CreateFlatbuffer()

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

Definition at line 296 of file runtime_stage_data.cc.

296 {
297 // The high level object API is used here for writing to the buffer. This is
298 // just a convenience.
299 auto runtime_stages = std::make_unique<fb::RuntimeStagesT>();
300
301 for (const auto& kvp : data_) {
302 auto runtime_stage = std::make_unique<fb::RuntimeStageT>();
303 runtime_stage->entrypoint = kvp.second->entrypoint;
304 const auto stage = ToStage(kvp.second->stage);
305 if (!stage.has_value()) {
306 VALIDATION_LOG << "Invalid runtime stage.";
307 return nullptr;
308 }
309 runtime_stage->stage = stage.value();
310 if (!kvp.second->shader) {
311 VALIDATION_LOG << "No shader specified for runtime stage.";
312 return nullptr;
313 }
314 if (kvp.second->shader->GetSize() > 0u) {
315 runtime_stage->shader = {
316 kvp.second->shader->GetMapping(),
317 kvp.second->shader->GetMapping() + kvp.second->shader->GetSize()};
318 }
319 for (const auto& uniform : kvp.second->uniforms) {
320 auto desc = std::make_unique<fb::UniformDescriptionT>();
321
322 desc->name = uniform.name;
323 if (desc->name.empty()) {
324 VALIDATION_LOG << "Uniform name cannot be empty.";
325 return nullptr;
326 }
327 desc->location = uniform.location;
328 desc->rows = uniform.rows;
329 desc->columns = uniform.columns;
330 auto uniform_type = ToUniformType(uniform.type);
331 if (!uniform_type.has_value()) {
332 VALIDATION_LOG << "Invalid uniform type for runtime stage.";
333 return nullptr;
334 }
335 desc->type = uniform_type.value();
336 desc->bit_width = uniform.bit_width;
337 if (uniform.array_elements.has_value()) {
338 desc->array_elements = uniform.array_elements.value();
339 }
340
341 runtime_stage->uniforms.emplace_back(std::move(desc));
342 }
343
344 for (const auto& input : kvp.second->inputs) {
345 auto desc = std::make_unique<fb::StageInputT>();
346
347 desc->name = input.name;
348
349 if (desc->name.empty()) {
350 VALIDATION_LOG << "Stage input name cannot be empty.";
351 return nullptr;
352 }
353 desc->location = input.location;
354 desc->set = input.set;
355 desc->binding = input.binding;
356 auto input_type = ToInputType(input.type);
357 if (!input_type.has_value()) {
358 VALIDATION_LOG << "Invalid uniform type for runtime stage.";
359 return nullptr;
360 }
361 desc->type = input_type.value();
362 desc->bit_width = input.bit_width;
363 desc->vec_size = input.vec_size;
364 desc->columns = input.columns;
365 desc->offset = input.offset;
366
367 runtime_stage->inputs.emplace_back(std::move(desc));
368 }
369 switch (kvp.first) {
370 case RuntimeStageBackend::kSkSL:
371 runtime_stages->sksl = std::move(runtime_stage);
372 break;
373 case RuntimeStageBackend::kMetal:
374 runtime_stages->metal = std::move(runtime_stage);
375 break;
376 case RuntimeStageBackend::kOpenGLES:
377 runtime_stages->opengles = std::move(runtime_stage);
378 break;
379 case RuntimeStageBackend::kVulkan:
380 runtime_stages->vulkan = std::move(runtime_stage);
381 break;
382 }
383 }
384 return runtime_stages;
385 }

References impeller::kMetal, impeller::kOpenGLES, impeller::kSkSL, impeller::kVulkan, impeller::compiler::ToInputType(), impeller::compiler::ToStage(), impeller::compiler::ToUniformType(), and VALIDATION_LOG.

Referenced by CreateMapping(), and impeller::compiler::GenerateShaderFB().

## ◆ CreateJsonMapping()

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

Definition at line 219 of file runtime_stage_data.cc.

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

References impeller::compiler::kEntrypointKey, 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 387 of file runtime_stage_data.cc.

387 {
388 auto runtime_stages = CreateFlatbuffer();
389 if (!runtime_stages) {
390 return nullptr;
391 }
392
393 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
394 builder->Finish(fb::RuntimeStages::Pack(*builder.get(), runtime_stages.get()),
395 fb::RuntimeStagesIdentifier());
396 return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
397 builder->GetSize(),
398 [builder](auto, auto) {});
399 }

References CreateFlatbuffer().

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

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

- impeller/compiler/runtime_stage_data.h
- impeller/compiler/runtime_stage_data.cc

impeller::compiler::ToInputType
static std::optional< fb::InputDataType > ToInputType(spirv_cross::SPIRType::BaseType type)
Definition: runtime_stage_data.cc:105
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::compiler::kUniformBitWidthKey
static const char * kUniformBitWidthKey
Definition: runtime_stage_data.cc:203
impeller::compiler::kEntrypointKey
static const char * kEntrypointKey
Definition: runtime_stage_data.cc:195
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::compiler::kUniformNameKey
static const char * kUniformNameKey
Definition: runtime_stage_data.cc:198
impeller::compiler::kUniformColumnsKey
static const char * kUniformColumnsKey
Definition: runtime_stage_data.cc:202
impeller::compiler::kShaderKey
static const char * kShaderKey
Definition: runtime_stage_data.cc:197
impeller::compiler::kUniformTypeKey
static const char * kUniformTypeKey
Definition: runtime_stage_data.cc:200
impeller::compiler::kStageKey
static const char * kStageKey
Definition: runtime_stage_data.cc:193
impeller::compiler::ToStage
static std::optional< fb::Stage > ToStage(spv::ExecutionModel stage)
Definition: runtime_stage_data.cc:33
impeller::compiler::kUniformArrayElementsKey
static const char * kUniformArrayElementsKey
Definition: runtime_stage_data.cc:204
impeller::compiler::ToJsonType
static std::optional< uint32_t > ToJsonType(spirv_cross::SPIRType::BaseType type)
Definition: runtime_stage_data.cc:148
impeller::compiler::RuntimeStageBackendToString
static std::string RuntimeStageBackendToString(RuntimeStageBackend backend)
Definition: runtime_stage_data.cc:206
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::compiler::kUniformLocationKey
static const char * kUniformLocationKey
Definition: runtime_stage_data.cc:199
impeller::RuntimeStageBackend::kSkSL
@ kSkSL
impeller::compiler::kUniformsKey
static const char * kUniformsKey
Definition: runtime_stage_data.cc:196
impeller::compiler::kUniformRowsKey
static const char * kUniformRowsKey
Definition: runtime_stage_data.cc:201
impeller::compiler::ToUniformType
static std::optional< fb::UniformDataType > ToUniformType(spirv_cross::SPIRType::BaseType type)
Definition: runtime_stage_data.cc:61
impeller::compiler::RuntimeStageData::CreateFlatbuffer
std::unique_ptr< fb::RuntimeStagesT > CreateFlatbuffer() const
Definition: runtime_stage_data.cc:296
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller::compiler::ToJsonStage
static std::optional< fb::Stage > ToJsonStage(spv::ExecutionModel stage)
Definition: runtime_stage_data.cc:47