25#include "third_party/skia/include/core/SkString.h"
26#include "third_party/skia/include/effects/SkRuntimeEffect.h"
32constexpr const char* kEGLImageExternalExtension =
"GL_OES_EGL_image_external";
33constexpr const char* kEGLImageExternalExtension300 =
34 "GL_OES_EGL_image_external_essl3";
43 std::stringstream sstream(msl_version);
44 std::string version_part;
48 if (std::getline(sstream, version_part,
'.')) {
49 major = std::stoi(version_part);
50 if (std::getline(sstream, version_part,
'.')) {
51 minor = std::stoi(version_part);
52 if (std::getline(sstream, version_part,
'.')) {
53 patch = std::stoi(version_part);
57 if (major < 1 || (major == 1 && minor < 2)) {
58 std::cerr <<
"--metal-version version must be at least 1.2. Have "
59 << msl_version << std::endl;
61 return spirv_cross::CompilerMSL::Options::make_msl_version(major, minor,
66 const spirv_cross::ParsedIR& ir,
68 std::optional<uint32_t> msl_version_override = {}) {
69 auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
70 spirv_cross::CompilerMSL::Options sl_options;
73 sl_options.msl_version = msl_version_override.value_or(
75 sl_options.ios_use_simdgroup_functions =
76 sl_options.is_ios() &&
77 sl_options.msl_version >=
78 spirv_cross::CompilerMSL::Options::make_msl_version(2, 4, 0);
79 sl_options.use_framebuffer_fetch_subpasses =
true;
80 sl_compiler->set_msl_options(sl_options);
91 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::Float);
93 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::SampledImage);
95 spv::ExecutionModel execution_model =
96 spv::ExecutionModel::ExecutionModelFragment;
98 execution_model = spv::ExecutionModel::ExecutionModelVertex;
101 uint32_t buffer_offset = 0;
102 uint32_t sampler_offset = 0;
103 for (
auto& float_id : floats) {
104 sl_compiler->add_msl_resource_binding(
105 {.stage = execution_model,
106 .basetype = spirv_cross::SPIRType::BaseType::Float,
107 .desc_set = sl_compiler->get_decoration(float_id,
108 spv::DecorationDescriptorSet),
110 sl_compiler->get_decoration(float_id, spv::DecorationBinding),
112 .msl_buffer = buffer_offset});
115 for (
auto& image_id :
images) {
116 sl_compiler->add_msl_resource_binding({
117 .stage = execution_model,
118 .basetype = spirv_cross::SPIRType::BaseType::SampledImage,
120 sl_compiler->get_decoration(image_id, spv::DecorationDescriptorSet),
122 sl_compiler->get_decoration(image_id, spv::DecorationBinding),
127 .msl_texture = sampler_offset,
128 .msl_sampler = sampler_offset,
133 return CompilerBackend(sl_compiler);
137 const spirv_cross::ParsedIR& ir,
139 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
140 spirv_cross::CompilerGLSL::Options sl_options;
141 sl_options.force_zero_initialized_variables =
true;
142 sl_options.vertex.fixup_clipspace =
true;
143 sl_options.vulkan_semantics =
true;
144 gl_compiler->set_common_options(sl_options);
150 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
161 for (
auto&
id : ir.ids_for_constant_or_variable) {
164 gl_compiler->require_extension(kEGLImageExternalExtension300);
166 gl_compiler->require_extension(kEGLImageExternalExtension);
172 spirv_cross::CompilerGLSL::Options sl_options;
173 sl_options.force_zero_initialized_variables =
true;
174 sl_options.vertex.fixup_clipspace =
true;
181 sl_options.es =
true;
183 sl_options.version = 300;
187 gl_compiler->remap_ext_framebuffer_fetch(0, 0,
true);
189 gl_compiler->set_variable_type_remap_callback(
190 [&](
const spirv_cross::SPIRType&
type,
const std::string& var_name,
191 std::string& name_of_type) {
193 name_of_type =
"samplerExternalOES";
200 sl_options.es =
false;
202 gl_compiler->set_common_options(sl_options);
208 auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
268uint32_t CalculateUBOSize(
const spirv_cross::Compiler* compiler) {
269 spirv_cross::ShaderResources resources = compiler->get_shader_resources();
271 for (
const spirv_cross::Resource& ubo : resources.uniform_buffers) {
272 const spirv_cross::SPIRType& ubo_type =
273 compiler->get_type(ubo.base_type_id);
274 uint32_t size = compiler->get_declared_struct_size(ubo_type);
285 : options_(source_options) {
286 if (!source_mapping || source_mapping->GetMapping() ==
nullptr) {
288 <<
"Could not read shader source or shader source was empty.";
293 COMPILER_ERROR(error_stream_) <<
"Target platform not specified.";
308 shaderc_source_language::shaderc_source_language_glsl;
310 shaderc_profile::shaderc_profile_core,
316 shaderc_source_language::shaderc_source_language_hlsl;
329 target.
env = shaderc_target_env::shaderc_target_env_opengl;
330 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
331 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
333 target.env = shaderc_target_env::shaderc_target_env_vulkan;
334 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
335 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
346 target.
env = shaderc_target_env::shaderc_target_env_vulkan;
347 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
348 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
362 target.
env = shaderc_target_env::shaderc_target_env_opengl;
363 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
364 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
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;
386 shaderc_optimization_level::shaderc_optimization_level_zero;
398 for (
const auto& define : source_options.
defines) {
402 std::vector<std::string> included_file_names;
403 spirv_options.
includer = std::make_shared<Includer>(
405 [&included_file_names](
auto included_name) {
406 included_file_names.emplace_back(std::move(included_name));
415 if (!spirv_assembly_) {
418 included_file_names_ = std::move(included_file_names);
422 spirv_cross::Parser parser(
423 reinterpret_cast<const uint32_t*
>(spirv_assembly_->GetMapping()),
424 spirv_assembly_->GetSize() /
sizeof(uint32_t));
429 const auto parsed_ir =
430 std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
436 <<
"Could not create compiler for target platform.";
440 uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler());
442 COMPILER_ERROR(error_stream_) <<
"Uniform buffer size exceeds max ("
450 auto sl_compilation_result_str = sl_compiler.GetCompiler()->compile();
451 auto sl_compilation_result =
459 auto stripped_spirv_options = spirv_options;
462 error_stream_, stripped_spirv_options.BuildShadercOptions());
464 sl_mapping_ = sl_compilation_result;
468 COMPILER_ERROR(error_stream_) <<
"Could not generate SL from SPIRV";
474 SkRuntimeEffect::Result result =
475 SkRuntimeEffect::MakeForShader(SkString(sl_compilation_result_str));
476 if (result.effect ==
nullptr) {
478 <<
"Compiled to invalid SkSL:\n"
479 << sl_compilation_result_str <<
"\nSkSL Error:\n"
480 << result.errorText.c_str();
485 reflector_ = std::make_unique<Reflector>(std::move(reflector_options),
491 if (!reflector_->IsValid()) {
493 <<
"Could not complete reflection on generated shader.";
503 return spirv_assembly_;
514std::string Compiler::GetSourcePrefix()
const {
515 std::stringstream stream;
521 return error_stream_.str();
525 return included_file_names_;
529 const std::string& separator) {
530 std::stringstream stream;
531 for (
size_t i = 0, count = items.size();
i < count;
i++) {
532 const auto is_last = (
i == count - 1);
542std::string Compiler::GetDependencyNames(
const std::string& separator)
const {
543 std::vector<std::string> dependencies = included_file_names_;
549 std::initializer_list<std::string> targets_names)
const {
551 const auto targets =
JoinStrings(targets_names,
" ");
552 const auto dependencies = GetDependencyNames(
" ");
554 std::stringstream stream;
555 stream << targets <<
": " << dependencies <<
"\n";
557 auto contents = std::make_shared<std::string>(stream.str());
558 return std::make_unique<fml::NonOwnedMapping>(
559 reinterpret_cast<const uint8_t*
>(contents->data()), contents->size(),
560 [contents](
auto,
auto) {});
564 return reflector_.get();
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.
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