Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1d506df

Browse files
[Impeller] Allow metal shaders to compile through SPIR-V with openGL semantics. (#40616)
[Impeller] Allow metal shaders to compile through SPIR-V with openGL semantics.
1 parent 78f9c68 commit 1d506df

File tree

6 files changed

+68
-20
lines changed

6 files changed

+68
-20
lines changed

impeller/compiler/compiler.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
366366
switch (source_options.target_platform) {
367367
case TargetPlatform::kMetalDesktop:
368368
case TargetPlatform::kMetalIOS:
369+
spirv_options.SetOptimizationLevel(
370+
shaderc_optimization_level::shaderc_optimization_level_performance);
371+
if (source_options.use_half_textures) {
372+
spirv_options.SetTargetEnvironment(
373+
shaderc_target_env::shaderc_target_env_opengl,
374+
shaderc_env_version::shaderc_env_version_opengl_4_5);
375+
spirv_options.SetTargetSpirv(
376+
shaderc_spirv_version::shaderc_spirv_version_1_0);
377+
} else {
378+
spirv_options.SetTargetEnvironment(
379+
shaderc_target_env::shaderc_target_env_vulkan,
380+
shaderc_env_version::shaderc_env_version_vulkan_1_1);
381+
spirv_options.SetTargetSpirv(
382+
shaderc_spirv_version::shaderc_spirv_version_1_3);
383+
}
384+
break;
369385
case TargetPlatform::kOpenGLES:
370386
case TargetPlatform::kOpenGLDesktop:
371387
spirv_options.SetOptimizationLevel(

impeller/compiler/impellerc_main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ bool Main(const fml::CommandLine& command_line) {
7575
options.json_format = switches.json_format;
7676
options.gles_language_version = switches.gles_language_version;
7777
options.metal_version = switches.metal_version;
78+
options.use_half_textures = switches.use_half_textures;
7879

7980
Reflector::Options reflector_options;
8081
reflector_options.target_platform = switches.target_platform;

impeller/compiler/source_options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ struct SourceOptions {
2929
bool json_format = false;
3030
std::string metal_version;
3131

32+
/// @brief Whether half-precision textures should be supported, requiring
33+
/// opengl semantics. Only used on metal targets.
34+
bool use_half_textures = false;
35+
3236
SourceOptions();
3337

3438
~SourceOptions();

impeller/compiler/switches.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void Switches::PrintHelp(std::ostream& stream) {
7272
stream << "[optional] --depfile=<depfile_path>" << std::endl;
7373
stream << "[optional] --gles-language-verision=<number>" << std::endl;
7474
stream << "[optional] --json" << std::endl;
75-
stream << "[optional] --remap-samplers (force metal sampler index to match "
76-
"declared order)"
75+
stream << "[optional] --use-half-textures (force openGL semantics when "
76+
"targeting metal)"
7777
<< std::endl;
7878
}
7979

@@ -135,7 +135,8 @@ Switches::Switches(const fml::CommandLine& command_line)
135135
metal_version(
136136
command_line.GetOptionValueWithDefault("metal-version", "1.2")),
137137
entry_point(
138-
command_line.GetOptionValueWithDefault("entry-point", "main")) {
138+
command_line.GetOptionValueWithDefault("entry-point", "main")),
139+
use_half_textures(command_line.HasOption("use-half-textures")) {
139140
auto language =
140141
command_line.GetOptionValueWithDefault("source-language", "glsl");
141142
std::transform(language.begin(), language.end(), language.begin(),

impeller/compiler/switches.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ namespace compiler {
1919

2020
struct Switches {
2121
TargetPlatform target_platform = TargetPlatform::kUnknown;
22-
std::shared_ptr<fml::UniqueFD> working_directory;
23-
std::vector<IncludeDir> include_directories;
24-
std::string source_file_name;
25-
SourceType input_type;
26-
std::string sl_file_name;
27-
bool iplr;
28-
std::string spirv_file_name;
29-
std::string reflection_json_name;
30-
std::string reflection_header_name;
31-
std::string reflection_cc_name;
32-
std::string depfile_path;
33-
std::vector<std::string> defines;
34-
bool json_format;
22+
std::shared_ptr<fml::UniqueFD> working_directory = nullptr;
23+
std::vector<IncludeDir> include_directories = {};
24+
std::string source_file_name = "";
25+
SourceType input_type = SourceType::kUnknown;
26+
std::string sl_file_name = "";
27+
bool iplr = false;
28+
std::string spirv_file_name = "";
29+
std::string reflection_json_name = "";
30+
std::string reflection_header_name = "";
31+
std::string reflection_cc_name = "";
32+
std::string depfile_path = "";
33+
std::vector<std::string> defines = {};
34+
bool json_format = false;
3535
SourceLanguage source_language = SourceLanguage::kUnknown;
36-
uint32_t gles_language_version;
37-
std::string metal_version;
38-
std::string entry_point;
36+
uint32_t gles_language_version = 0;
37+
std::string metal_version = "";
38+
std::string entry_point = "";
39+
bool use_half_textures = false;
3940

4041
Switches();
4142

impeller/tools/impeller.gni

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ template("impellerc") {
305305
args += [ "--metal-version=$metal_version" ]
306306
}
307307

308+
if (defined(invoker.use_half_textures) && invoker.use_half_textures) {
309+
args += [ "--use-half-textures" ]
310+
}
311+
308312
if (json) {
309313
args += [ "--json" ]
310314
}
@@ -403,6 +407,11 @@ template("_impeller_shaders_metal") {
403407
metal_version = invoker.metal_version
404408
}
405409

410+
use_half_textures = false
411+
if (defined(invoker.use_half_textures) && invoker.use_half_textures) {
412+
use_half_textures = invoker.use_half_textures
413+
}
414+
406415
shaders_base_name = string_join("",
407416
[
408417
invoker.name,
@@ -413,6 +422,7 @@ template("_impeller_shaders_metal") {
413422
shaders = invoker.shaders
414423
metal_version = metal_version
415424
sl_file_extension = "metal"
425+
use_half_textures = use_half_textures
416426
shader_target_flag = ""
417427
defines = [ "IMPELLER_TARGET_METAL" ]
418428
if (is_ios) {
@@ -666,12 +676,26 @@ template("_impeller_shaders_vk") {
666676
#
667677
# The SPIR-V version required by the shaders.
668678
#
679+
# @param[options] use_half_textures
680+
#
681+
# Whether the metal shader is using half-precision textures and requires
682+
# openGL semantics when compilig SPIR-V.
683+
#
669684
template("impeller_shaders") {
670685
metal_version = "1.2"
671686
if (defined(invoker.metal_version)) {
672687
metal_version = invoker.metal_version
673688
}
674-
not_needed([ "metal_version" ])
689+
690+
use_half_textures = false
691+
if (defined(invoker.use_half_textures) && invoker.use_half_textures) {
692+
use_half_textures = true
693+
}
694+
695+
not_needed([
696+
"metal_version",
697+
"use_half_textures",
698+
])
675699

676700
enable_opengles = impeller_enable_opengles
677701
if (defined(invoker.enable_opengles)) {
@@ -685,6 +709,7 @@ template("impeller_shaders") {
685709
name = invoker.name
686710
shaders = invoker.shaders
687711
metal_version = metal_version
712+
use_half_textures = use_half_textures
688713
}
689714
}
690715

0 commit comments

Comments
 (0)