| 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::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 }
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:67

References data.

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

## ◆ CreateJsonMapping()

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

Definition at line 212 of file runtime_stage_data.cc.

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

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

397 {
398 auto runtime_stages = CreateMultiStageFlatbuffer();
399 if (!runtime_stages) {
400 return nullptr;
401 }
402
403 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
404 builder->Finish(fb::RuntimeStages::Pack(*builder.get(), runtime_stages.get()),
405 fb::RuntimeStagesIdentifier());
406 return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
407 builder->GetSize(),
408 [builder](auto, auto) {});
409 }
impeller::compiler::RuntimeStageData::CreateMultiStageFlatbuffer
std::unique_ptr< fb::RuntimeStagesT > CreateMultiStageFlatbuffer() const
Definition: runtime_stage_data.cc:369

References CreateMultiStageFlatbuffer().

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

## ◆ CreateMultiStageFlatbuffer()

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

Definition at line 369 of file runtime_stage_data.cc.

369 {
370 // The high level object API is used here for writing to the buffer. This is
371 // just a convenience.
372 auto runtime_stages = std::make_unique<fb::RuntimeStagesT>();
373
374 for (const auto& kvp : data_) {
375 auto runtime_stage = CreateStageFlatbuffer(kvp.first);
376 switch (kvp.first) {
377 case RuntimeStageBackend::kSkSL:
378 runtime_stages->sksl = std::move(runtime_stage);
379 break;
380 case RuntimeStageBackend::kMetal:
381 runtime_stages->metal = std::move(runtime_stage);
382 break;
383 case RuntimeStageBackend::kOpenGLES:
384 runtime_stages->opengles = std::move(runtime_stage);
385 break;
386 case RuntimeStageBackend::kVulkan:
387 runtime_stages->vulkan = std::move(runtime_stage);
388 break;
389 case RuntimeStageBackend::kOpenGLES3:
390 runtime_stages->opengles3 = std::move(runtime_stage);
391 break;
392 }
393 }
394 return runtime_stages;
395 }
impeller::compiler::RuntimeStageData::CreateStageFlatbuffer
std::unique_ptr< fb::RuntimeStageT > CreateStageFlatbuffer(impeller::RuntimeStageBackend backend) const
Definition: runtime_stage_data.cc:285
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::RuntimeStageBackend::kSkSL
@ kSkSL
impeller::RuntimeStageBackend::kOpenGLES3
@ kOpenGLES3

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

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

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:

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