15 #include "flutter/fml/paths.h"
30 constexpr
const char* kEGLImageExternalExtension =
"GL_OES_EGL_image_external";
31 constexpr
const char* kEGLImageExternalExtension300 =
32 "GL_OES_EGL_image_external_essl3";
41 std::stringstream sstream(msl_version);
42 std::string version_part;
46 if (std::getline(sstream, version_part,
'.')) {
47 major = std::stoi(version_part);
48 if (std::getline(sstream, version_part,
'.')) {
49 minor = std::stoi(version_part);
50 if (std::getline(sstream, version_part,
'.')) {
51 patch = std::stoi(version_part);
55 if (major < 1 || (major == 1 && minor < 2)) {
56 std::cerr <<
"--metal-version version must be at least 1.2. Have "
57 << msl_version << std::endl;
59 return spirv_cross::CompilerMSL::Options::make_msl_version(major, minor,
64 const spirv_cross::ParsedIR& ir,
66 std::optional<uint32_t> msl_version_override = {}) {
67 auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
68 spirv_cross::CompilerMSL::Options sl_options;
71 sl_options.msl_version = msl_version_override.value_or(
73 sl_options.ios_use_simdgroup_functions =
74 sl_options.is_ios() &&
75 sl_options.msl_version >=
76 spirv_cross::CompilerMSL::Options::make_msl_version(2, 4, 0);
77 sl_options.use_framebuffer_fetch_subpasses =
true;
78 sl_compiler->set_msl_options(sl_options);
89 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::Float);
91 SortUniforms(&ir, sl_compiler.get(), spirv_cross::SPIRType::SampledImage);
93 spv::ExecutionModel execution_model =
94 spv::ExecutionModel::ExecutionModelFragment;
96 execution_model = spv::ExecutionModel::ExecutionModelVertex;
99 uint32_t buffer_offset = 0;
100 uint32_t sampler_offset = 0;
101 for (
auto& float_id : floats) {
102 sl_compiler->add_msl_resource_binding(
103 {.stage = execution_model,
104 .basetype = spirv_cross::SPIRType::BaseType::Float,
105 .desc_set = sl_compiler->get_decoration(float_id,
106 spv::DecorationDescriptorSet),
108 sl_compiler->get_decoration(float_id, spv::DecorationBinding),
110 .msl_buffer = buffer_offset});
113 for (
auto& image_id : images) {
114 sl_compiler->add_msl_resource_binding({
115 .stage = execution_model,
116 .basetype = spirv_cross::SPIRType::BaseType::SampledImage,
118 sl_compiler->get_decoration(image_id, spv::DecorationDescriptorSet),
120 sl_compiler->get_decoration(image_id, spv::DecorationBinding),
125 .msl_texture = sampler_offset,
126 .msl_sampler = sampler_offset,
131 return CompilerBackend(sl_compiler);
135 const spirv_cross::ParsedIR& ir,
137 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
138 spirv_cross::CompilerGLSL::Options sl_options;
139 sl_options.force_zero_initialized_variables =
true;
140 sl_options.vertex.fixup_clipspace =
true;
141 sl_options.vulkan_semantics =
true;
142 gl_compiler->set_common_options(sl_options);
148 auto gl_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
159 for (
auto&
id : ir.ids_for_constant_or_variable) {
162 gl_compiler->require_extension(kEGLImageExternalExtension300);
164 gl_compiler->require_extension(kEGLImageExternalExtension);
170 spirv_cross::CompilerGLSL::Options sl_options;
171 sl_options.force_zero_initialized_variables =
true;
172 sl_options.vertex.fixup_clipspace =
true;
179 sl_options.es =
true;
181 sl_options.version = 300;
185 gl_compiler->remap_ext_framebuffer_fetch(0, 0,
true);
187 gl_compiler->set_variable_type_remap_callback(
188 [&](
const spirv_cross::SPIRType&
type,
const std::string& var_name,
189 std::string& name_of_type) {
191 name_of_type =
"samplerExternalOES";
198 sl_options.es =
false;
200 gl_compiler->set_common_options(sl_options);
206 auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
266 uint32_t CalculateUBOSize(
const spirv_cross::Compiler* compiler) {
267 spirv_cross::ShaderResources resources = compiler->get_shader_resources();
269 for (
const spirv_cross::Resource& ubo : resources.uniform_buffers) {
270 const spirv_cross::SPIRType& ubo_type =
271 compiler->get_type(ubo.base_type_id);
272 uint32_t size = compiler->get_declared_struct_size(ubo_type);
283 : options_(source_options) {
284 if (!source_mapping || source_mapping->GetMapping() ==
nullptr) {
286 <<
"Could not read shader source or shader source was empty.";
291 COMPILER_ERROR(error_stream_) <<
"Target platform not specified.";
306 shaderc_source_language::shaderc_source_language_glsl;
308 shaderc_profile::shaderc_profile_core,
314 shaderc_source_language::shaderc_source_language_hlsl;
327 target.
env = shaderc_target_env::shaderc_target_env_opengl;
328 target.
version = shaderc_env_version::shaderc_env_version_opengl_4_5;
329 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
331 target.
env = shaderc_target_env::shaderc_target_env_vulkan;
332 target.
version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
333 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
336 spirv_options.
target = target;
344 target.
env = shaderc_target_env::shaderc_target_env_vulkan;
345 target.
version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
346 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
353 spirv_options.
target = target;
360 target.
env = shaderc_target_env::shaderc_target_env_opengl;
361 target.
version = shaderc_env_version::shaderc_env_version_opengl_4_5;
362 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
364 spirv_options.
target = target;
375 target.
env = shaderc_target_env::shaderc_target_env_opengl;
376 target.
version = shaderc_env_version::shaderc_env_version_opengl_4_5;
377 target.
spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
384 shaderc_optimization_level::shaderc_optimization_level_zero;
385 spirv_options.
target = target;
396 for (
const auto& define : source_options.
defines) {
400 std::vector<std::string> included_file_names;
401 spirv_options.
includer = std::make_shared<Includer>(
403 [&included_file_names](
auto included_name) {
404 included_file_names.emplace_back(std::move(included_name));
413 if (!spirv_assembly_) {
416 included_file_names_ = std::move(included_file_names);
420 spirv_cross::Parser parser(
421 reinterpret_cast<const uint32_t*
>(spirv_assembly_->GetMapping()),
422 spirv_assembly_->GetSize() /
sizeof(uint32_t));
427 const auto parsed_ir =
428 std::make_shared<spirv_cross::ParsedIR>(parser.get_parsed_ir());
434 <<
"Could not create compiler for target platform.";
438 uint32_t ubo_size = CalculateUBOSize(sl_compiler.GetCompiler());
440 COMPILER_ERROR(error_stream_) <<
"Uniform buffer size exceeds max ("
448 auto sl_compilation_result =
456 auto stripped_spirv_options = spirv_options;
459 error_stream_, stripped_spirv_options.BuildShadercOptions());
461 sl_mapping_ = sl_compilation_result;
465 COMPILER_ERROR(error_stream_) <<
"Could not generate SL from SPIRV";
469 reflector_ = std::make_unique<Reflector>(std::move(reflector_options),
475 if (!reflector_->IsValid()) {
477 <<
"Could not complete reflection on generated shader.";
487 return spirv_assembly_;
498 std::string Compiler::GetSourcePrefix()
const {
499 std::stringstream stream;
505 return error_stream_.str();
509 return included_file_names_;
513 const std::string& separator) {
514 std::stringstream stream;
515 for (
size_t i = 0, count = items.size(); i < count; i++) {
516 const auto is_last = (i == count - 1);
526 std::string Compiler::GetDependencyNames(
const std::string& separator)
const {
527 std::vector<std::string> dependencies = included_file_names_;
528 dependencies.push_back(options_.
file_name);
533 std::initializer_list<std::string> targets_names)
const {
535 const auto targets =
JoinStrings(targets_names,
" ");
536 const auto dependencies = GetDependencyNames(
" ");
538 std::stringstream stream;
539 stream << targets <<
": " << dependencies <<
"\n";
541 auto contents = std::make_shared<std::string>(stream.str());
542 return std::make_unique<fml::NonOwnedMapping>(
543 reinterpret_cast<const uint8_t*
>(contents->data()), contents->size(),
544 [contents](
auto,
auto) {});
548 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 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)
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
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