26#include "third_party/skia/include/core/SkString.h"
27#include "third_party/skia/include/effects/SkRuntimeEffect.h"
33constexpr const char* kEGLImageExternalExtension =
"GL_OES_EGL_image_external";
34constexpr const char* kEGLImageExternalExtension300 =
35 "GL_OES_EGL_image_external_essl3";
36constexpr int kVerboseErrorLineThreshold = 6;
45 std::stringstream sstream(msl_version);
46 std::string version_part;
50 if (std::getline(sstream, version_part,
'.')) {
51 major = std::stoi(version_part);
52 if (std::getline(sstream, version_part,
'.')) {
53 minor = std::stoi(version_part);
54 if (std::getline(sstream, version_part,
'.')) {
55 patch = std::stoi(version_part);
59 if (major < 1 || (major == 1 && minor < 2)) {
60 std::cerr <<
"--metal-version version must be at least 1.2. Have "
61 << msl_version << std::endl;
63 return spirv_cross::CompilerMSL::Options::make_msl_version(major, minor,
68 const spirv_cross::ParsedIR& ir,
70 std::optional<uint32_t> msl_version_override = {}) {
71 auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
72 spirv_cross::CompilerMSL::Options sl_options;
75 sl_options.msl_version = msl_version_override.value_or(
77 sl_options.ios_use_simdgroup_functions =
78 sl_options.is_ios() &&
79 sl_options.msl_version >=
80 spirv_cross::CompilerMSL::Options::make_msl_version(2, 4, 0);
81 sl_options.use_framebuffer_fetch_subpasses =
true;
82 sl_compiler->set_msl_options(sl_options);
93 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::Float);
95 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::SampledImage);
97 spv::ExecutionModel execution_model =
98 spv::ExecutionModel::ExecutionModelFragment;
100 execution_model = spv::ExecutionModel::ExecutionModelVertex;
103 uint32_t buffer_offset = 0;
104 uint32_t sampler_offset = 0;
105 for (
auto& float_id : floats) {
106 sl_compiler->add_msl_resource_binding(
107 {.stage = execution_model,
108 .basetype = spirv_cross::SPIRType::BaseType::Float,
109 .desc_set = sl_compiler->get_decoration(float_id,
110 spv::DecorationDescriptorSet),
112 sl_compiler->get_decoration(float_id, spv::DecorationBinding),
114 .msl_buffer = buffer_offset});
117 for (
auto& image_id :
images) {
118 sl_compiler->add_msl_resource_binding({
119 .stage = execution_model,
120 .basetype = spirv_cross::SPIRType::BaseType::SampledImage,
122 sl_compiler->get_decoration(image_id, spv::DecorationDescriptorSet),
124 sl_compiler->get_decoration(image_id, spv::DecorationBinding),
129 .msl_texture = sampler_offset,
130 .msl_sampler = sampler_offset,
135 return CompilerBackend(sl_compiler);
139 const spirv_cross::ParsedIR& ir,
141 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
142 spirv_cross::CompilerGLSL::Options sl_options;
143 sl_options.force_zero_initialized_variables =
true;
144 sl_options.vertex.fixup_clipspace =
true;
145 sl_options.vulkan_semantics =
true;
146 gl_compiler->set_common_options(sl_options);
152 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
163 for (
auto&
id : ir.ids_for_constant_or_variable) {
166 gl_compiler->require_extension(kEGLImageExternalExtension300);
168 gl_compiler->require_extension(kEGLImageExternalExtension);
174 spirv_cross::CompilerGLSL::Options sl_options;
175 sl_options.force_zero_initialized_variables =
true;
176 sl_options.vertex.fixup_clipspace =
true;
183 sl_options.es =
true;
185 sl_options.version = 300;
189 gl_compiler->remap_ext_framebuffer_fetch(0, 0,
true);
191 gl_compiler->set_variable_type_remap_callback(
192 [&](
const spirv_cross::SPIRType&
type,
const std::string& var_name,
193 std::string& name_of_type) {
195 name_of_type =
"samplerExternalOES";
202 sl_options.es =
false;
204 gl_compiler->set_common_options(sl_options);
210 auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
272uint32_t CalculateUBOSize(
const spirv_cross::Compiler* compiler) {
273 spirv_cross::ShaderResources resources = compiler->get_shader_resources();
275 for (
const spirv_cross::Resource& ubo : resources.uniform_buffers) {
276 const spirv_cross::SPIRType& ubo_type =
277 compiler->get_type(ubo.base_type_id);
278 uint32_t size = compiler->get_declared_struct_size(ubo_type);
286std::string StripUnderscoresAndUpper(std::string_view
name) {
288 result.reserve(
name.size());
289 for (
char ch :
name) {
293 if (ch >=
'a' && ch <=
'z') {
294 result.push_back(
static_cast<char>(ch -
'a' +
'A'));
296 result.push_back(ch);
309void CanonicalizeUniformBlockInstanceNamesForGL(
311 spirv_cross::Compiler* compiler) {
316 for (
const spirv_cross::Resource& ubo :
317 compiler->get_shader_resources().uniform_buffers) {
318 const std::string block_name = compiler->get_name(ubo.base_type_id);
319 const std::string instance_name = compiler->get_name(ubo.id);
320 if (StripUnderscoresAndUpper(instance_name) ==
321 StripUnderscoresAndUpper(block_name)) {
324 compiler->set_name(ubo.id,
"_" + block_name);
333 : options_(source_options) {
334 if (!source_mapping || source_mapping->GetMapping() ==
nullptr) {
336 <<
"Could not read shader source or shader source was empty.";
341 COMPILER_ERROR(error_stream_) <<
"Target platform not specified.";
356 shaderc_source_language::shaderc_source_language_glsl;
358 shaderc_profile::shaderc_profile_core,
364 shaderc_source_language::shaderc_source_language_hlsl;
377 target.
env = shaderc_target_env::shaderc_target_env_opengl;
378 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
379 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
381 target.env = shaderc_target_env::shaderc_target_env_vulkan;
382 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
383 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
394 target.
env = shaderc_target_env::shaderc_target_env_vulkan;
395 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
396 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
410 target.
env = shaderc_target_env::shaderc_target_env_opengl;
411 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
412 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
425 target.
env = shaderc_target_env::shaderc_target_env_opengl;
426 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
427 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
434 shaderc_optimization_level::shaderc_optimization_level_zero;
446 for (
const auto& define : source_options.
defines) {
450 std::vector<std::string> included_file_names;
451 spirv_options.
includer = std::make_shared<Includer>(
453 [&included_file_names](
auto included_name) {
454 included_file_names.emplace_back(std::move(included_name));
463 if (!spirv_assembly_) {
466 included_file_names_ = std::move(included_file_names);
470 spirv_cross::Parser parser(
471 reinterpret_cast<const uint32_t*
>(spirv_assembly_->GetMapping()),
472 spirv_assembly_->GetSize() /
sizeof(uint32_t));
477 const auto parsed_ir =
478 std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
484 <<
"Could not create compiler for target platform.";
488 CanonicalizeUniformBlockInstanceNamesForGL(source_options.
target_platform,
489 sl_compiler.GetCompiler());
491 uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler());
493 COMPILER_ERROR(error_stream_) <<
"Uniform buffer size exceeds max ("
501 auto sl_compilation_result_str = sl_compiler.GetCompiler()->compile();
502 auto sl_compilation_result =
510 auto stripped_spirv_options = spirv_options;
513 error_stream_, stripped_spirv_options.BuildShadercOptions());
515 sl_mapping_ = sl_compilation_result;
519 COMPILER_ERROR(error_stream_) <<
"Could not generate SL from SPIRV";
524 !ValidateSkSLResult(sl_compilation_result_str).ok()) {
528 reflector_ = std::make_unique<Reflector>(std::move(reflector_options),
534 if (!reflector_->IsValid()) {
536 <<
"Could not complete reflection on generated shader.";
543absl::Status Compiler::ValidateSkSLResult(
const std::string& sksl) {
545 SkRuntimeEffect::Result result =
546 SkRuntimeEffect::MakeForShader(SkString(sksl));
548 if (result.effect !=
nullptr) {
549 return absl::OkStatus();
554 std::stringstream output;
555 bool is_truncated =
false;
558 auto append_and_truncate = [&](
const std::string&
text) {
559 std::stringstream text_stream(
text);
561 int lines_outputted = 0;
562 while (std::getline(text_stream, line)) {
563 output <<
"\n " << line;
564 if (++lines_outputted == kVerboseErrorLineThreshold) {
568 if (lines_outputted == kVerboseErrorLineThreshold) {
569 auto full_line_count = std::count(
text.begin(),
text.end(),
'\n') + 1;
570 output <<
"\n... (truncated " << full_line_count - lines_outputted
576 output <<
"\nCompiled to invalid SkSL:";
577 append_and_truncate(sksl);
578 output <<
"\nSkSL Error:";
579 std::string error_text(result.errorText.c_str());
580 append_and_truncate(error_text);
588 COMPILER_ERROR(verbose_error_stream_) <<
"\nCompiled to invalid SkSL:\n"
589 << sksl <<
"\nSkSL Error:\n"
593 return absl::InternalError(
"SkSL validation failed.");
599 return spirv_assembly_;
610std::string Compiler::GetSourcePrefix()
const {
611 std::stringstream stream;
617 return error_stream_.str();
621 return verbose_error_stream_.str();
625 return included_file_names_;
629 const std::string& separator) {
630 std::stringstream stream;
631 for (
size_t i = 0, count = items.size();
i < count;
i++) {
632 const auto is_last = (
i == count - 1);
642std::string Compiler::GetDependencyNames(
const std::string& separator)
const {
643 std::vector<std::string> dependencies = included_file_names_;
649 std::initializer_list<std::string> targets_names)
const {
651 const auto targets =
JoinStrings(targets_names,
" ");
652 const auto dependencies = GetDependencyNames(
" ");
654 std::stringstream stream;
655 stream << targets <<
": " << dependencies <<
"\n";
657 auto contents = std::make_shared<std::string>(stream.str());
658 return std::make_unique<fml::NonOwnedMapping>(
659 reinterpret_cast<const uint8_t*
>(contents->data()), contents->size(),
660 [contents](
auto,
auto) {});
664 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 FML_UNREACHABLE()
#define COMPILER_ERROR(stream)
std::array< MockImage, 3 > images
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.
impeller::ShaderType type
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
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