14#include "inja/inja.hpp"
17#include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
18#include "runtime_stage_types_flatbuffers.h"
33static std::optional<fb::Stage>
ToStage(spv::ExecutionModel stage) {
35 case spv::ExecutionModel::ExecutionModelVertex:
36 return fb::Stage::kVertex;
37 case spv::ExecutionModel::ExecutionModelFragment:
38 return fb::Stage::kFragment;
39 case spv::ExecutionModel::ExecutionModelGLCompute:
40 return fb::Stage::kCompute;
47static std::optional<fb::Stage>
ToJsonStage(spv::ExecutionModel stage) {
49 case spv::ExecutionModel::ExecutionModelVertex:
50 return fb::Stage::kVertex;
51 case spv::ExecutionModel::ExecutionModelFragment:
52 return fb::Stage::kFragment;
53 case spv::ExecutionModel::ExecutionModelGLCompute:
54 return fb::Stage::kCompute;
62 spirv_cross::SPIRType::BaseType
type) {
64 case spirv_cross::SPIRType::Float:
66 case spirv_cross::SPIRType::SampledImage:
68 case spirv_cross::SPIRType::Struct:
70 case spirv_cross::SPIRType::Boolean:
71 case spirv_cross::SPIRType::SByte:
72 case spirv_cross::SPIRType::UByte:
73 case spirv_cross::SPIRType::Short:
74 case spirv_cross::SPIRType::UShort:
75 case spirv_cross::SPIRType::Int:
76 case spirv_cross::SPIRType::UInt:
77 case spirv_cross::SPIRType::Int64:
78 case spirv_cross::SPIRType::UInt64:
79 case spirv_cross::SPIRType::Half:
80 case spirv_cross::SPIRType::Double:
81 case spirv_cross::SPIRType::AccelerationStructure:
82 case spirv_cross::SPIRType::AtomicCounter:
83 case spirv_cross::SPIRType::Char:
84 case spirv_cross::SPIRType::ControlPointArray:
86 case spirv_cross::SPIRType::Interpolant:
87 case spirv_cross::SPIRType::RayQuery:
89 case spirv_cross::SPIRType::Unknown:
90 case spirv_cross::SPIRType::Void:
96 spirv_cross::SPIRType::BaseType
type) {
98 case spirv_cross::SPIRType::Boolean:
99 return fb::InputDataType::kBoolean;
100 case spirv_cross::SPIRType::SByte:
101 return fb::InputDataType::kSignedByte;
102 case spirv_cross::SPIRType::UByte:
104 case spirv_cross::SPIRType::Short:
105 return fb::InputDataType::kSignedShort;
106 case spirv_cross::SPIRType::UShort:
107 return fb::InputDataType::kUnsignedShort;
108 case spirv_cross::SPIRType::Int:
109 return fb::InputDataType::kSignedInt;
110 case spirv_cross::SPIRType::UInt:
111 return fb::InputDataType::kUnsignedInt;
112 case spirv_cross::SPIRType::Int64:
113 return fb::InputDataType::kSignedInt64;
114 case spirv_cross::SPIRType::UInt64:
115 return fb::InputDataType::kUnsignedInt64;
116 case spirv_cross::SPIRType::Float:
118 case spirv_cross::SPIRType::Double:
120 case spirv_cross::SPIRType::Unknown:
121 case spirv_cross::SPIRType::Void:
122 case spirv_cross::SPIRType::Half:
123 case spirv_cross::SPIRType::AtomicCounter:
124 case spirv_cross::SPIRType::Struct:
126 case spirv_cross::SPIRType::SampledImage:
128 case spirv_cross::SPIRType::AccelerationStructure:
129 case spirv_cross::SPIRType::RayQuery:
130 case spirv_cross::SPIRType::ControlPointArray:
131 case spirv_cross::SPIRType::Interpolant:
132 case spirv_cross::SPIRType::Char:
139 spirv_cross::SPIRType::BaseType
type) {
141 case spirv_cross::SPIRType::Boolean:
143 case spirv_cross::SPIRType::SByte:
145 case spirv_cross::SPIRType::UByte:
147 case spirv_cross::SPIRType::Short:
149 case spirv_cross::SPIRType::UShort:
151 case spirv_cross::SPIRType::Int:
153 case spirv_cross::SPIRType::UInt:
155 case spirv_cross::SPIRType::Int64:
157 case spirv_cross::SPIRType::UInt64:
159 case spirv_cross::SPIRType::Half:
161 case spirv_cross::SPIRType::Float:
163 case spirv_cross::SPIRType::Double:
165 case spirv_cross::SPIRType::SampledImage:
167 case spirv_cross::SPIRType::Struct:
169 case spirv_cross::SPIRType::AccelerationStructure:
170 case spirv_cross::SPIRType::AtomicCounter:
171 case spirv_cross::SPIRType::Char:
172 case spirv_cross::SPIRType::ControlPointArray:
174 case spirv_cross::SPIRType::Interpolant:
175 case spirv_cross::SPIRType::RayQuery:
177 case spirv_cross::SPIRType::Unknown:
178 case spirv_cross::SPIRType::Void:
233 for (
const auto& kvp : data_) {
234 nlohmann::json platform_object;
237 if (!stage.has_value()) {
241 platform_object[
kStageKey] =
static_cast<uint32_t
>(stage.value());
244 if (kvp.second->shader->GetSize() > 0u) {
246 reinterpret_cast<const char*
>(kvp.second->shader->GetMapping()),
247 kvp.second->shader->GetSize());
251 auto& uniforms = platform_object[
kUniformsKey] = nlohmann::json::array_t{};
252 for (
const auto& uniform : kvp.second->uniforms) {
253 nlohmann::json uniform_object;
260 if (!uniform_type.has_value()) {
268 uniform.array_elements.value_or(0);
270 uniforms.push_back(uniform_object);
276 auto json_string = std::make_shared<std::string>(
root.dump(2u));
278 return std::make_shared<fml::NonOwnedMapping>(
279 reinterpret_cast<const uint8_t*
>(json_string->data()),
280 json_string->size(), [json_string](
auto,
auto) {});
285 auto kvp = data_.find(
backend);
286 if (kvp == data_.end()) {
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()) {
297 runtime_stage->stage = stage.value();
298 if (!kvp->second->shader) {
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()};
307 for (
const auto& uniform : kvp->second->uniforms) {
308 auto desc = std::make_unique<fb::UniformDescriptionT>();
310 desc->name = uniform.name;
311 if (
desc->name.empty()) {
315 desc->location = uniform.location;
316 desc->rows = uniform.rows;
317 desc->columns = uniform.columns;
319 if (!uniform_type.has_value()) {
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();
329 for (
const auto& byte_type : uniform.struct_layout) {
330 desc->struct_layout.push_back(
static_cast<fb::StructByteType
>(byte_type));
332 desc->struct_float_count = uniform.struct_float_count;
334 runtime_stage->uniforms.emplace_back(std::move(
desc));
337 for (
const auto& input : kvp->second->inputs) {
338 auto desc = std::make_unique<fb::StageInputT>();
340 desc->name = input.name;
342 if (
desc->name.empty()) {
346 desc->location = input.location;
347 desc->set = input.set;
348 desc->binding = input.binding;
350 if (!input_type.has_value()) {
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;
360 runtime_stage->inputs.emplace_back(std::move(
desc));
363 return runtime_stage;
366std::unique_ptr<fb::RuntimeStagesT>
370 auto runtime_stages = std::make_unique<fb::RuntimeStagesT>();
372 for (
const auto& kvp : data_) {
376 runtime_stages->sksl = std::move(runtime_stage);
379 runtime_stages->metal = std::move(runtime_stage);
382 runtime_stages->opengles = std::move(runtime_stage);
385 runtime_stages->vulkan = std::move(runtime_stage);
389 return runtime_stages;
394 if (!runtime_stages) {
398 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
400 fb::RuntimeStagesIdentifier());
401 return std::make_shared<fml::NonOwnedMapping>(
builder->GetBufferPointer(),
std::unique_ptr< fb::RuntimeStagesT > CreateMultiStageFlatbuffer() const
std::shared_ptr< fml::Mapping > CreateMapping() const
std::unique_ptr< fb::RuntimeStageT > CreateStageFlatbuffer(impeller::RuntimeStageBackend backend) const
void AddShader(const std::shared_ptr< Shader > &data)
std::shared_ptr< fml::Mapping > CreateJsonMapping() const
#define FML_UNREACHABLE()
#define FML_DCHECK(condition)
static void * Pack(const T &ctx, SkArenaAlloc *alloc)
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
static std::optional< fb::InputDataType > ToInputType(spirv_cross::SPIRType::BaseType type)
static const char * kUniformLocationKey
static const char * kUniformNameKey
static std::optional< fb::Stage > ToJsonStage(spv::ExecutionModel stage)
static const char * kEntrypointKey
static std::optional< fb::UniformDataType > ToUniformType(spirv_cross::SPIRType::BaseType type)
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 std::optional< fb::Stage > ToStage(spv::ExecutionModel stage)
static const char * kUniformRowsKey
static const char * kShaderKey
static const char * kUniformArrayElementsKey
static const char * kTargetPlatformKey
static const char * kUniformBitWidthKey
std::shared_ptr< const fml::Mapping > data