15 #include "flutter/fml/paths.h"
25 #include "third_party/skia/include/core/SkString.h"
26 #include "third_party/skia/include/effects/SkRuntimeEffect.h"
32 constexpr
const char* kEGLImageExternalExtension =
"GL_OES_EGL_image_external";
33 constexpr
const char* kEGLImageExternalExtension300 =
34 "GL_OES_EGL_image_external_essl3";
35 constexpr
int kVerboseErrorLineThreshold = 6;
44 std::stringstream sstream(msl_version);
45 std::string version_part;
49 if (std::getline(sstream, version_part,
'.')) {
50 major = std::stoi(version_part);
51 if (std::getline(sstream, version_part,
'.')) {
52 minor = std::stoi(version_part);
53 if (std::getline(sstream, version_part,
'.')) {
54 patch = std::stoi(version_part);
58 if (major < 1 || (major == 1 && minor < 2)) {
59 std::cerr <<
"--metal-version version must be at least 1.2. Have "
60 << msl_version << std::endl;
62 return spirv_cross::CompilerMSL::Options::make_msl_version(major, minor,
67 const spirv_cross::ParsedIR& ir,
69 std::optional<uint32_t> msl_version_override = {}) {
70 auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
71 spirv_cross::CompilerMSL::Options sl_options;
74 sl_options.msl_version = msl_version_override.value_or(
76 sl_options.ios_use_simdgroup_functions =
77 sl_options.is_ios() &&
78 sl_options.msl_version >=
79 spirv_cross::CompilerMSL::Options::make_msl_version(2, 4, 0);
80 sl_options.use_framebuffer_fetch_subpasses =
true;
81 sl_compiler->set_msl_options(sl_options);
92 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::Float);
94 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::SampledImage);
96 spv::ExecutionModel execution_model =
97 spv::ExecutionModel::ExecutionModelFragment;
99 execution_model = spv::ExecutionModel::ExecutionModelVertex;
102 uint32_t buffer_offset = 0;
103 uint32_t sampler_offset = 0;
104 for (
auto& float_id : floats) {
105 sl_compiler->add_msl_resource_binding(
106 {.stage = execution_model,
107 .basetype = spirv_cross::SPIRType::BaseType::Float,
108 .desc_set = sl_compiler->get_decoration(float_id,
109 spv::DecorationDescriptorSet),
111 sl_compiler->get_decoration(float_id, spv::DecorationBinding),
113 .msl_buffer = buffer_offset});
116 for (
auto& image_id : images) {
117 sl_compiler->add_msl_resource_binding({
118 .stage = execution_model,
119 .basetype = spirv_cross::SPIRType::BaseType::SampledImage,
121 sl_compiler->get_decoration(image_id, spv::DecorationDescriptorSet),
123 sl_compiler->get_decoration(image_id, spv::DecorationBinding),
128 .msl_texture = sampler_offset,
129 .msl_sampler = sampler_offset,
134 return CompilerBackend(sl_compiler);
138 const spirv_cross::ParsedIR& ir,
140 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
141 spirv_cross::CompilerGLSL::Options sl_options;
142 sl_options.force_zero_initialized_variables =
true;
143 sl_options.vertex.fixup_clipspace =
true;
144 sl_options.vulkan_semantics =
true;
145 gl_compiler->set_common_options(sl_options);
151 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
162 for (
auto&
id : ir.ids_for_constant_or_variable) {
165 gl_compiler->require_extension(kEGLImageExternalExtension300);
167 gl_compiler->require_extension(kEGLImageExternalExtension);
173 spirv_cross::CompilerGLSL::Options sl_options;
174 sl_options.force_zero_initialized_variables =
true;
175 sl_options.vertex.fixup_clipspace =
true;
182 sl_options.es =
true;
184 sl_options.version = 300;
188 gl_compiler->remap_ext_framebuffer_fetch(0, 0,
true);
190 gl_compiler->set_variable_type_remap_callback(
191 [&](
const spirv_cross::SPIRType& type,
const std::string& var_name,
192 std::string& name_of_type) {
194 name_of_type =
"samplerExternalOES";
201 sl_options.es =
false;
203 gl_compiler->set_common_options(sl_options);
209 auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
271 uint32_t CalculateUBOSize(
const spirv_cross::Compiler* compiler) {
272 spirv_cross::ShaderResources resources = compiler->get_shader_resources();
274 for (
const spirv_cross::Resource& ubo : resources.uniform_buffers) {
275 const spirv_cross::SPIRType& ubo_type =
276 compiler->get_type(ubo.base_type_id);
277 uint32_t size = compiler->get_declared_struct_size(ubo_type);
288 : options_(source_options) {
289 if (!source_mapping || source_mapping->GetMapping() ==
nullptr) {
291 <<
"Could not read shader source or shader source was empty.";
296 COMPILER_ERROR(error_stream_) <<
"Target platform not specified.";
311 shaderc_source_language::shaderc_source_language_glsl;
313 shaderc_profile::shaderc_profile_core,
319 shaderc_source_language::shaderc_source_language_hlsl;
332 target.
env = shaderc_target_env::shaderc_target_env_opengl;
333 target.
version = shaderc_env_version::shaderc_env_version_opengl_4_5;
334 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
336 target.
env = shaderc_target_env::shaderc_target_env_vulkan;
337 target.
version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
338 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
341 spirv_options.
target = target;
349 target.
env = shaderc_target_env::shaderc_target_env_vulkan;
350 target.
version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
351 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
358 spirv_options.
target = target;
365 target.
env = shaderc_target_env::shaderc_target_env_opengl;
366 target.
version = shaderc_env_version::shaderc_env_version_opengl_4_5;
367 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
369 spirv_options.
target = target;
380 target.
env = shaderc_target_env::shaderc_target_env_opengl;
381 target.
version = shaderc_env_version::shaderc_env_version_opengl_4_5;
382 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
389 shaderc_optimization_level::shaderc_optimization_level_zero;
390 spirv_options.
target = target;
401 for (
const auto& define : source_options.
defines) {
405 std::vector<std::string> included_file_names;
406 spirv_options.
includer = std::make_shared<Includer>(
408 [&included_file_names](
auto included_name) {
409 included_file_names.emplace_back(std::move(included_name));
418 if (!spirv_assembly_) {
421 included_file_names_ = std::move(included_file_names);
425 spirv_cross::Parser parser(
426 reinterpret_cast<const uint32_t*
>(spirv_assembly_->GetMapping()),
427 spirv_assembly_->GetSize() /
sizeof(uint32_t));
432 const auto parsed_ir =
433 std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
439 <<
"Could not create compiler for target platform.";
443 uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler());
445 COMPILER_ERROR(error_stream_) <<
"Uniform buffer size exceeds max ("
453 auto sl_compilation_result_str = sl_compiler.GetCompiler()->compile();
454 auto sl_compilation_result =
462 auto stripped_spirv_options = spirv_options;
465 error_stream_, stripped_spirv_options.BuildShadercOptions());
467 sl_mapping_ = sl_compilation_result;
471 COMPILER_ERROR(error_stream_) <<
"Could not generate SL from SPIRV";
476 !ValidateSkSLResult(sl_compilation_result_str).ok()) {
480 reflector_ = std::make_unique<Reflector>(std::move(reflector_options),
486 if (!reflector_->IsValid()) {
488 <<
"Could not complete reflection on generated shader.";
495 absl::Status Compiler::ValidateSkSLResult(
const std::string& sksl) {
497 SkRuntimeEffect::Result result =
498 SkRuntimeEffect::MakeForShader(SkString(sksl));
500 if (result.effect !=
nullptr) {
501 return absl::OkStatus();
506 std::stringstream output;
507 bool is_truncated =
false;
510 auto append_and_truncate = [&](
const std::string& text) {
511 std::stringstream text_stream(text);
513 int lines_outputted = 0;
514 while (std::getline(text_stream, line)) {
515 output <<
"\n " << line;
516 if (++lines_outputted == kVerboseErrorLineThreshold) {
520 if (lines_outputted == kVerboseErrorLineThreshold) {
521 auto full_line_count = std::count(text.begin(), text.end(),
'\n') + 1;
522 output <<
"\n... (truncated " << full_line_count - lines_outputted
528 output <<
"\nCompiled to invalid SkSL:";
529 append_and_truncate(sksl);
530 output <<
"\nSkSL Error:";
531 std::string error_text(result.errorText.c_str());
532 append_and_truncate(error_text);
540 COMPILER_ERROR(verbose_error_stream_) <<
"\nCompiled to invalid SkSL:\n"
541 << sksl <<
"\nSkSL Error:\n"
545 return absl::InternalError(
"SkSL validation failed.");
551 return spirv_assembly_;
562 std::string Compiler::GetSourcePrefix()
const {
563 std::stringstream stream;
569 return error_stream_.str();
573 return verbose_error_stream_.str();
577 return included_file_names_;
581 const std::string& separator) {
582 std::stringstream stream;
583 for (
size_t i = 0, count = items.size(); i < count; i++) {
584 const auto is_last = (i == count - 1);
594 std::string Compiler::GetDependencyNames(
const std::string& separator)
const {
595 std::vector<std::string> dependencies = included_file_names_;
601 std::initializer_list<std::string> targets_names)
const {
603 const auto targets =
JoinStrings(targets_names,
" ");
604 const auto dependencies = GetDependencyNames(
" ");
606 std::stringstream stream;
607 stream << targets <<
": " << dependencies <<
"\n";
609 auto contents = std::make_shared<std::string>(stream.str());
610 return std::make_unique<fml::NonOwnedMapping>(
611 reinterpret_cast<const uint8_t*
>(contents->data()), contents->size(),
612 [contents](
auto,
auto) {});
616 return reflector_.get();
std::string GetVerboseErrorMessages() const
std::shared_ptr< fml::Mapping > GetSPIRVAssembly() const
const Reflector * GetReflector() const
Compiler(const std::shared_ptr< const fml::Mapping > &source_mapping, const SourceOptions &options, Reflector::Options reflector_options)
const std::vector< std::string > & GetIncludedFileNames() const
std::unique_ptr< fml::Mapping > CreateDepfileContents(std::initializer_list< std::string > targets) const
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
std::string GetErrorMessages() const
std::shared_ptr< fml::Mapping > CompileToSPV(std::stringstream &error_stream, const shaderc::CompileOptions &spirv_options) const
#define COMPILER_ERROR(stream)
static CompilerBackend CreateSkSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
static const uint32_t kMaxUniformBufferSize
static CompilerBackend CreateVulkanCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
constexpr char kExternalTexturePrefix[]
static bool EntryPointMustBeNamedMain(TargetPlatform platform)
static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options, std::optional< uint32_t > msl_version_override={})
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
static std::string JoinStrings(std::vector< std::string > items, const std::string &separator)
spirv_cross::CompilerMSL::Options::Platform TargetPlatformToMSLPlatform(TargetPlatform platform)
static uint32_t ParseMSLVersion(const std::string &msl_version)
bool StringStartsWith(const std::string &target, const std::string &prefix)
static CompilerBackend CreateGLSLCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
spv::ExecutionModel ToExecutionModel(SourceType type)
std::vector< spirv_cross::ID > SortUniforms(const spirv_cross::ParsedIR *ir, const spirv_cross::Compiler *compiler, std::optional< spirv_cross::SPIRType::BaseType > type_filter, bool include)
Sorts uniform declarations in an IR according to decoration order.
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Creates a mapping with string data.
spirv_cross::Compiler * GetCompiler()
std::optional< shaderc_source_language > source_langauge
std::vector< std::string > macro_definitions
bool relaxed_vulkan_rules
shaderc_optimization_level optimization_level
std::optional< SPIRVCompilerSourceProfile > source_profile
std::shared_ptr< Includer > includer
shaderc::CompileOptions BuildShadercOptions() const
std::optional< SPIRVCompilerTargetEnv > target
shaderc_spirv_version spirv_version
shaderc_env_version version
uint32_t gles_language_version
bool use_half_textures
Whether half-precision textures should be supported, requiring opengl semantics. Only used on metal t...
SourceLanguage source_language
std::vector< IncludeDir > include_dirs
std::filesystem::path file_name
bool require_framebuffer_fetch
Whether the GLSL framebuffer fetch extension will be required.
std::string entry_point_name
TargetPlatform target_platform
std::shared_ptr< fml::UniqueFD > working_directory
std::string metal_version
std::vector< std::string > defines