Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
runtime_stage.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <array>
8#include <memory>
9
10#include "fml/mapping.h"
14#include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
15#include "runtime_stage_types_flatbuffers.h"
16
17namespace impeller {
18
19static RuntimeUniformType ToType(fb::UniformDataType type) {
20 switch (type) {
21 case fb::UniformDataType::kFloat:
23 case fb::UniformDataType::kSampledImage:
25 case fb::UniformDataType::kStruct:
27 }
29}
30
31static RuntimeShaderStage ToShaderStage(fb::Stage stage) {
32 switch (stage) {
33 case fb::Stage::kVertex:
35 case fb::Stage::kFragment:
37 case fb::Stage::kCompute:
39 }
41}
42
43/// The generated name from GLSLang/shaderc for the UBO containing non-opaque
44/// uniforms specified in the user-written runtime effect shader.
45///
46/// Vulkan does not allow non-opaque uniforms outside of a UBO.
48 "_RESERVED_IDENTIFIER_FIXUP_gl_DefaultUniformBlock";
49
50std::unique_ptr<RuntimeStage> RuntimeStage::RuntimeStageIfPresent(
51 const fb::RuntimeStage* runtime_stage,
52 const std::shared_ptr<fml::Mapping>& payload) {
53 if (!runtime_stage) {
54 return nullptr;
55 }
56
57 return std::unique_ptr<RuntimeStage>(
58 new RuntimeStage(runtime_stage, payload));
59}
60
62 const std::shared_ptr<fml::Mapping>& payload) {
63 if (payload == nullptr || !payload->GetMapping()) {
64 return {};
65 }
66 if (!fb::RuntimeStagesBufferHasIdentifier(payload->GetMapping())) {
67 return {};
68 }
69
70 auto raw_stages = fb::GetRuntimeStages(payload->GetMapping());
71 return {
73 RuntimeStageIfPresent(raw_stages->sksl(), payload)},
75 RuntimeStageIfPresent(raw_stages->metal(), payload)},
77 RuntimeStageIfPresent(raw_stages->opengles(), payload)},
79 RuntimeStageIfPresent(raw_stages->vulkan(), payload)},
80 };
81}
82
83RuntimeStage::RuntimeStage(const fb::RuntimeStage* runtime_stage,
84 const std::shared_ptr<fml::Mapping>& payload)
85 : payload_(payload) {
86 FML_DCHECK(runtime_stage);
87
88 stage_ = ToShaderStage(runtime_stage->stage());
89 entrypoint_ = runtime_stage->entrypoint()->str();
90
91 auto* uniforms = runtime_stage->uniforms();
92 if (uniforms) {
93 for (auto i = uniforms->begin(), end = uniforms->end(); i != end; i++) {
95 desc.name = i->name()->str();
96 desc.location = i->location();
97 desc.binding = i->binding();
98 desc.type = ToType(i->type());
99 desc.dimensions = RuntimeUniformDimensions{
100 static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
101 desc.bit_width = i->bit_width();
102 desc.array_elements = i->array_elements();
103 if (i->struct_layout()) {
104 for (const auto& byte_type : *i->struct_layout()) {
105 desc.struct_layout.push_back(static_cast<uint8_t>(byte_type));
106 }
107 }
108 desc.binding = i->binding();
109 desc.struct_float_count = i->struct_float_count();
110 uniforms_.push_back(std::move(desc));
111 }
112 }
113
114 code_mapping_ = std::make_shared<fml::NonOwnedMapping>(
115 runtime_stage->shader()->data(), //
116 runtime_stage->shader()->size(), //
117 [payload = payload_](auto, auto) {} //
118 );
119
120 for (const auto& uniform : GetUniforms()) {
121 if (uniform.type == kStruct) {
122 descriptor_set_layouts_.push_back(DescriptorSetLayout{
123 static_cast<uint32_t>(uniform.location),
126 });
127 } else if (uniform.type == kSampledImage) {
128 descriptor_set_layouts_.push_back(DescriptorSetLayout{
129 static_cast<uint32_t>(uniform.binding),
132 });
133 }
134 }
135
136 is_valid_ = true;
137}
138
142
144 return is_valid_;
145}
146
147const std::shared_ptr<fml::Mapping>& RuntimeStage::GetCodeMapping() const {
148 return code_mapping_;
149}
150
151const std::vector<RuntimeUniformDescription>& RuntimeStage::GetUniforms()
152 const {
153 return uniforms_;
154}
155
157 const std::string& name) const {
158 for (const auto& uniform : uniforms_) {
159 if (uniform.name == name) {
160 return &uniform;
161 }
162 }
163 return nullptr;
164}
165
166const std::string& RuntimeStage::GetEntrypoint() const {
167 return entrypoint_;
168}
169
171 return stage_;
172}
173
175 return is_dirty_;
176}
177
179 is_dirty_ = false;
180}
181
182const std::vector<DescriptorSetLayout>& RuntimeStage::GetDescriptorSetLayouts()
183 const {
184 return descriptor_set_layouts_;
185}
186
187} // namespace impeller
const std::string & GetEntrypoint() const
RuntimeStage & operator=(RuntimeStage &&)
const std::vector< RuntimeUniformDescription > & GetUniforms() const
const RuntimeUniformDescription * GetUniform(const std::string &name) const
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
static const char * kVulkanUBOName
static Map DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
const std::shared_ptr< fml::Mapping > & GetCodeMapping() const
const std::vector< DescriptorSetLayout > & GetDescriptorSetLayouts() const
RuntimeShaderStage GetShaderStage() const
RuntimeStage(const fb::RuntimeStage *runtime_stage, const std::shared_ptr< fml::Mapping > &payload)
glong glong end
#define FML_UNREACHABLE()
Definition logging.h:109
#define FML_DCHECK(condition)
Definition logging.h:103
const char * name
Definition fuchsia.cc:50
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
static RuntimeUniformType ToType(fb::UniformDataType type)