Flutter Engine
The Flutter Engine
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::MappingCreateJsonMapping () const
 
std::shared_ptr< fml::MappingCreateMapping () 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 {
29 FML_DCHECK(data_.find(data->backend) == data_.end());
30 data_[data->backend] = data;
31}
#define FML_DCHECK(condition)
Definition: logging.h:103
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ CreateJsonMapping()

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

Definition at line 210 of file runtime_stage_data.cc.

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

◆ CreateMapping()

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

Definition at line 392 of file runtime_stage_data.cc.

392 {
393 auto runtime_stages = CreateMultiStageFlatbuffer();
394 if (!runtime_stages) {
395 return nullptr;
396 }
397
398 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
399 builder->Finish(fb::RuntimeStages::Pack(*builder.get(), runtime_stages.get()),
400 fb::RuntimeStagesIdentifier());
401 return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
402 builder->GetSize(),
403 [builder](auto, auto) {});
404}
std::unique_ptr< fb::RuntimeStagesT > CreateMultiStageFlatbuffer() const
static void * Pack(const T &ctx, SkArenaAlloc *alloc)

◆ CreateMultiStageFlatbuffer()

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

Definition at line 367 of file runtime_stage_data.cc.

367 {
368 // The high level object API is used here for writing to the buffer. This is
369 // just a convenience.
370 auto runtime_stages = std::make_unique<fb::RuntimeStagesT>();
371
372 for (const auto& kvp : data_) {
373 auto runtime_stage = CreateStageFlatbuffer(kvp.first);
374 switch (kvp.first) {
376 runtime_stages->sksl = std::move(runtime_stage);
377 break;
379 runtime_stages->metal = std::move(runtime_stage);
380 break;
382 runtime_stages->opengles = std::move(runtime_stage);
383 break;
385 runtime_stages->vulkan = std::move(runtime_stage);
386 break;
387 }
388 }
389 return runtime_stages;
390}
std::unique_ptr< fb::RuntimeStageT > CreateStageFlatbuffer(impeller::RuntimeStageBackend backend) const

◆ CreateStageFlatbuffer()

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

Definition at line 283 of file runtime_stage_data.cc.

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

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