|
| 1 | +# Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style license that can be |
| 3 | +# found in the LICENSE file. |
| 4 | + |
| 5 | +import("//build/compiled_action.gni") |
| 6 | +import("//flutter/impeller/tools/args.gni") |
| 7 | + |
| 8 | +# Dispatches to the build or prebuilt impellerc depending on the value of |
| 9 | +# the `impeller_use_prebuilt_impellerc` argument. |
| 10 | +# * When the `single_invocation` argument is false, all variables are |
| 11 | +# forwarded to `compiled_action_foreach` or `action_foreach`, which will |
| 12 | +# invoke `impellerc` separately for each source. |
| 13 | +# * When the `single_invocation` argument is true, `impellerc` is only |
| 14 | +# invoked once via `compiled_action` or `action`. |
| 15 | +template("_impellerc") { |
| 16 | + if (invoker.single_invocation) { |
| 17 | + if (impeller_use_prebuilt_impellerc == "") { |
| 18 | + compiled_action(target_name) { |
| 19 | + forward_variables_from(invoker, "*") |
| 20 | + tool = "//flutter/impeller/compiler:impellerc" |
| 21 | + } |
| 22 | + } else { |
| 23 | + action(target_name) { |
| 24 | + forward_variables_from(invoker, "*", [ "args" ]) |
| 25 | + script = "//build/gn_run_binary.py" |
| 26 | + impellerc_path = |
| 27 | + rebase_path(impeller_use_prebuilt_impellerc, root_build_dir) |
| 28 | + args = [ impellerc_path ] + invoker.args |
| 29 | + } |
| 30 | + } |
| 31 | + } else { |
| 32 | + if (impeller_use_prebuilt_impellerc == "") { |
| 33 | + compiled_action_foreach(target_name) { |
| 34 | + forward_variables_from(invoker, "*") |
| 35 | + tool = "//flutter/impeller/compiler:impellerc" |
| 36 | + } |
| 37 | + } else { |
| 38 | + action_foreach(target_name) { |
| 39 | + forward_variables_from(invoker, "*", [ "args" ]) |
| 40 | + script = "//build/gn_run_binary.py" |
| 41 | + impellerc_path = |
| 42 | + rebase_path(impeller_use_prebuilt_impellerc, root_build_dir) |
| 43 | + args = [ impellerc_path ] + invoker.args |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +# Required: shaders The list of shaders inputs to compile. |
| 50 | +# Required: shader_target_flags The target flag(s) to append. Valid options: |
| 51 | +# --sksl |
| 52 | +# --metal-ios |
| 53 | +# --metal-desktop |
| 54 | +# --opengl-es |
| 55 | +# --opengl-desktop |
| 56 | +# --vulkan |
| 57 | +# --runtime-stage-metal |
| 58 | +# --runtime-stage-gles |
| 59 | +# --runtime-stage-vulkan |
| 60 | +# Not required for --shader_bundle mode. |
| 61 | +# Required: sl_file_extension The file extension to use for output files. |
| 62 | +# Not required for --shader_bundle mode. |
| 63 | +# Optional: iplr Causes --sl output to be in iplr/runtime |
| 64 | +# stage flatbuffer format. |
| 65 | +# Optional: shader_bundle Specifies a Flutter GPU shader bundle |
| 66 | +# configuration. |
| 67 | +# Required: shader_bundle_output Specifies the output filename of the shader |
| 68 | +# bundle. This is only required if |
| 69 | +# shader_bundle is supplied. |
| 70 | +# Optional: defines Specifies a list of valueless macro |
| 71 | +# definitions. |
| 72 | +# Optional: intermediates_subdir Specifies the subdirectory in which to put |
| 73 | +# intermediates. |
| 74 | +# Optional: json Causes output format to be JSON instead of |
| 75 | +# flatbuffer. |
| 76 | +template("impellerc") { |
| 77 | + assert(defined(invoker.shaders), "Impeller shaders must be specified.") |
| 78 | + assert(defined(invoker.shader_target_flags) || defined(invoker.shader_bundle), |
| 79 | + "The flag to impellerc for target selection must be specified.") |
| 80 | + assert(defined(invoker.sl_file_extension) || defined(invoker.shader_bundle), |
| 81 | + "The extension of the SL file must be specified (metal, glsl, etc..).") |
| 82 | + if (defined(invoker.shader_bundle)) { |
| 83 | + assert( |
| 84 | + defined(invoker.shader_bundle_output), |
| 85 | + "When shader_bundle is specified, shader_bundle_output must also be specified.") |
| 86 | + } |
| 87 | + |
| 88 | + if (defined(invoker.shader_target_flags)) { |
| 89 | + shader_target_flags = invoker.shader_target_flags |
| 90 | + } else { |
| 91 | + shader_target_flags = [] |
| 92 | + } |
| 93 | + |
| 94 | + sksl = false |
| 95 | + foreach(shader_target_flag, shader_target_flags) { |
| 96 | + sksl = shader_target_flag == "--sksl" |
| 97 | + } |
| 98 | + iplr = false |
| 99 | + if (defined(invoker.iplr) && invoker.iplr) { |
| 100 | + iplr = invoker.iplr |
| 101 | + } |
| 102 | + json = false |
| 103 | + if (defined(invoker.json) && invoker.json) { |
| 104 | + json = invoker.json |
| 105 | + } |
| 106 | + |
| 107 | + # Not needed on every path. |
| 108 | + not_needed([ |
| 109 | + "iplr", |
| 110 | + "sksl", |
| 111 | + "shader_bundle", |
| 112 | + "shader_bundle_output", |
| 113 | + ]) |
| 114 | + |
| 115 | + _impellerc(target_name) { |
| 116 | + pool = "//build/toolchain:toolchain_pool" |
| 117 | + shader_bundle = defined(invoker.shader_bundle) |
| 118 | + |
| 119 | + # When single_invocation is true, impellerc will be invoked exactly once. When it's |
| 120 | + # false, impellerc be invoked for each of the source file entries (invoker.shaders). |
| 121 | + single_invocation = shader_bundle |
| 122 | + |
| 123 | + if (defined(invoker.intermediates_subdir)) { |
| 124 | + subdir = invoker.intermediates_subdir |
| 125 | + generated_dir = "$target_gen_dir/$subdir" |
| 126 | + } else { |
| 127 | + generated_dir = "$target_gen_dir" |
| 128 | + } |
| 129 | + |
| 130 | + shader_lib_dir = rebase_path("//flutter/impeller/compiler/shader_lib") |
| 131 | + args = [ "--include=$shader_lib_dir" ] + shader_target_flags |
| 132 | + |
| 133 | + # When we're in single invocation mode, we can't use source enumeration. |
| 134 | + if (!single_invocation) { |
| 135 | + depfile_path = "$generated_dir/{{source_file_part}}.d" |
| 136 | + depfile_intermediate_path = rebase_path(depfile_path, root_build_dir) |
| 137 | + depfile = depfile_path |
| 138 | + args += [ |
| 139 | + "--input={{source}}", |
| 140 | + "--include={{source_dir}}", |
| 141 | + "--depfile=$depfile_intermediate_path", |
| 142 | + ] |
| 143 | + |
| 144 | + if (defined(invoker.shader_target_flag)) { |
| 145 | + args += [ "${invoker.shader_target_flag}" ] |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (defined(invoker.gles_language_version)) { |
| 150 | + gles_language_version = invoker.gles_language_version |
| 151 | + args += [ "--gles-language-version=$gles_language_version" ] |
| 152 | + } |
| 153 | + |
| 154 | + if (defined(invoker.metal_version)) { |
| 155 | + assert(is_mac || is_ios) |
| 156 | + metal_version = invoker.metal_version |
| 157 | + args += [ "--metal-version=$metal_version" ] |
| 158 | + } |
| 159 | + |
| 160 | + if (defined(invoker.use_half_textures) && invoker.use_half_textures) { |
| 161 | + args += [ "--use-half-textures" ] |
| 162 | + } |
| 163 | + if (defined(invoker.require_framebuffer_fetch) && |
| 164 | + invoker.require_framebuffer_fetch) { |
| 165 | + args += [ "--require-framebuffer-fetch" ] |
| 166 | + } |
| 167 | + |
| 168 | + if (json) { |
| 169 | + args += [ "--json" ] |
| 170 | + } |
| 171 | + |
| 172 | + if (iplr) { |
| 173 | + # When building in IPLR mode, the compiler may be executed twice |
| 174 | + args += [ "--iplr" ] |
| 175 | + } |
| 176 | + |
| 177 | + # The `sl_output` is the raw shader file output by the compiler. For --iplr |
| 178 | + # and --shader-bundle, this is used as the filename for the flatbuffer to |
| 179 | + # output. |
| 180 | + |
| 181 | + if (shader_bundle) { |
| 182 | + # When a shader bundle is specified, don't bother supplying flags for |
| 183 | + # the reflection state as these are ignored. In this mode, the compiler |
| 184 | + # is invoked multiple times and the reflection state for each shader is |
| 185 | + # written to the output flatbuffer. |
| 186 | + sl_output = "$generated_dir/${invoker.shader_bundle_output}" |
| 187 | + sl_output_path = rebase_path(sl_output, root_build_dir) |
| 188 | + |
| 189 | + args += [ |
| 190 | + "--sl=$sl_output_path", |
| 191 | + "--shader-bundle=${invoker.shader_bundle}", |
| 192 | + ] |
| 193 | + |
| 194 | + outputs = [ sl_output ] |
| 195 | + } else if (sksl) { |
| 196 | + # When SkSL is selected as a `shader_target_flags`, don't generate |
| 197 | + # C++ reflection state. Nothing needs to use it and it's likely invalid |
| 198 | + # given the special cases when generating SkSL. |
| 199 | + # Note that this configuration is orthogonal to the "--iplr" flag |
| 200 | + sl_output = |
| 201 | + "$generated_dir/{{source_file_part}}.${invoker.sl_file_extension}" |
| 202 | + sl_output_path = rebase_path(sl_output, root_build_dir) |
| 203 | + |
| 204 | + spirv_intermediate = "$generated_dir/{{source_file_part}}.spirv" |
| 205 | + spirv_intermediate_path = rebase_path(spirv_intermediate, root_build_dir) |
| 206 | + args += [ |
| 207 | + "--sl=$sl_output_path", |
| 208 | + "--spirv=$spirv_intermediate_path", |
| 209 | + ] |
| 210 | + |
| 211 | + outputs = [ sl_output ] |
| 212 | + } else { |
| 213 | + # The default branch. Here we just generate one shader along with all of |
| 214 | + # its C++ reflection state. |
| 215 | + |
| 216 | + sl_output = |
| 217 | + "$generated_dir/{{source_file_part}}.${invoker.sl_file_extension}" |
| 218 | + sl_output_path = rebase_path(sl_output, root_build_dir) |
| 219 | + |
| 220 | + reflection_json_intermediate = "$generated_dir/{{source_file_part}}.json" |
| 221 | + reflection_header_intermediate = "$generated_dir/{{source_file_part}}.h" |
| 222 | + reflection_cc_intermediate = "$generated_dir/{{source_file_part}}.cc" |
| 223 | + |
| 224 | + spirv_intermediate = "$generated_dir/{{source_file_part}}.spirv" |
| 225 | + spirv_intermediate_path = rebase_path(spirv_intermediate, root_build_dir) |
| 226 | + reflection_json_path = |
| 227 | + rebase_path(reflection_json_intermediate, root_build_dir) |
| 228 | + reflection_header_path = |
| 229 | + rebase_path(reflection_header_intermediate, root_build_dir) |
| 230 | + reflection_cc_path = |
| 231 | + rebase_path(reflection_cc_intermediate, root_build_dir) |
| 232 | + |
| 233 | + args += [ |
| 234 | + "--sl=$sl_output_path", |
| 235 | + "--spirv=$spirv_intermediate_path", |
| 236 | + "--reflection-json=$reflection_json_path", |
| 237 | + "--reflection-header=$reflection_header_path", |
| 238 | + "--reflection-cc=$reflection_cc_path", |
| 239 | + ] |
| 240 | + |
| 241 | + outputs = [ |
| 242 | + sl_output, |
| 243 | + reflection_header_intermediate, |
| 244 | + reflection_cc_intermediate, |
| 245 | + ] |
| 246 | + } |
| 247 | + |
| 248 | + if (defined(invoker.defines)) { |
| 249 | + foreach(def, invoker.defines) { |
| 250 | + args += [ "--define=$def" ] |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + if (single_invocation) { |
| 255 | + inputs = invoker.shaders |
| 256 | + } else { |
| 257 | + sources = invoker.shaders |
| 258 | + } |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +template("impellerc_reflect") { |
| 263 | + assert( |
| 264 | + defined(invoker.impellerc_invocation), |
| 265 | + "The target that specifies the ImpellerC invocation to reflect must be defined.") |
| 266 | + |
| 267 | + reflect_config = "reflect_$target_name" |
| 268 | + config(reflect_config) { |
| 269 | + include_dirs = [ get_path_info( |
| 270 | + get_label_info("//flutter/impeller:impeller", "target_gen_dir"), |
| 271 | + "dir") ] |
| 272 | + } |
| 273 | + |
| 274 | + impellerc_invocation = invoker.impellerc_invocation |
| 275 | + |
| 276 | + source_set(target_name) { |
| 277 | + public_configs = [ ":$reflect_config" ] |
| 278 | + public = filter_include(get_target_outputs(impellerc_invocation), [ "*.h" ]) |
| 279 | + sources = filter_include(get_target_outputs(impellerc_invocation), |
| 280 | + [ |
| 281 | + "*.h", |
| 282 | + "*.cc", |
| 283 | + "*.mm", |
| 284 | + ]) |
| 285 | + |
| 286 | + deps = [ |
| 287 | + "//flutter/impeller/core", |
| 288 | + impellerc_invocation, |
| 289 | + ] |
| 290 | + } |
| 291 | +} |
0 commit comments