[tint] Allow setting the bgra swizzle values from the command line

Add a flag for `bgra-swizzle` to set the bgra swizzle values in a
transform.

Bug: 389559096
Change-Id: Ic4e3aea8d8a06f9b1773a54bd75dbb8864885fa0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/231434
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/cmd/tint/main.cc b/src/tint/cmd/tint/main.cc
index 8a56551..684e197 100644
--- a/src/tint/cmd/tint/main.cc
+++ b/src/tint/cmd/tint/main.cc
@@ -183,6 +183,7 @@
 
 #if TINT_BUILD_GLSL_WRITER
     bool glsl_desktop = false;
+    std::vector<uint32_t> bgra_swizzle;
 #endif  // TINT_BUILD_GLSL_WRITER
 };
 
@@ -402,6 +403,9 @@
     auto& glsl_desktop = options.Add<BoolOption>(
         "glsl-desktop", "Set the version to the desktop GL instead of ES", Default{false});
     TINT_DEFER(opts->glsl_desktop = *glsl_desktop.value);
+
+    auto& bgra_swizzle =
+        options.Add<StringOption>("bgra-swizzle", "BGRA swizzle indices", Default{""});
 #endif  // TINT_BUILD_GLSL_WRITER
 
 #if TINT_BUILD_MSL_WRITER
@@ -582,6 +586,19 @@
     }
 #endif  // TINT_BUILD_HLSL_WRITER || TINT_BUILD_MSL_WRITER
 
+#if TINT_BUILD_GLSL_WRITER
+    if (bgra_swizzle.value.has_value() && !bgra_swizzle.value.value().empty()) {
+        for (auto val : tint::Split(*bgra_swizzle.value, ",")) {
+            auto bgra_val = tint::strconv::ParseUint32(val);
+            if (bgra_val != tint::Success) {
+                std::cerr << "invalid bgra_swizzle value: " << val;
+                return false;
+            }
+            opts->bgra_swizzle.push_back(bgra_val.Get());
+        }
+    }
+#endif  // TINT_BUILD_GLSL_WRITER
+
     auto files = result.Get();
     if (files.Length() > 1) {
         std::cerr << "More than one input file specified: "
@@ -1285,6 +1302,10 @@
         offset += 8;
     }
 
+    for (auto idx : options.bgra_swizzle) {
+        gen_options.bgra_swizzle_locations.insert({idx});
+    }
+
     // Convert the AST program to an IR module.
     auto ir = tint::wgsl::reader::ProgramToLoweredIR(res.Get());
     if (ir != tint::Success) {