Debian Codegraph @

src/sksl/codegen/SkSLWGSLValidator.cpp

raw
1 /*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/sksl/codegen/SkSLWGSLValidator.h"
9
10 #include "src/sksl/SkSLErrorReporter.h"
11 #include "src/sksl/SkSLPosition.h"
12
13 #include "src/tint/lang/wgsl/enums.h"
14 #include "src/tint/lang/wgsl/reader/options.h"
15 #include "tint/tint.h"
16
17 namespace SkSL {
18
19 static bool validate_wgsl(ErrorReporter& reporter,
20 std::string_view wgsl,
21 bool appendError,
22 std::string* warnings) {
23 // Enable the WGSL optional features that Skia might rely on.
24 tint::wgsl::reader::Options options;
25 for (auto extension : {tint::wgsl::Extension::kChromiumExperimentalPixelLocal,
26 tint::wgsl::Extension::kChromiumExperimentalFramebufferFetch,
27 tint::wgsl::Extension::kDualSourceBlending,
28 tint::wgsl::Extension::kF16}) {
29 options.allowed_features.extensions.insert(extension);
30 }
31 options.allowed_features.features.insert(
32 tint::wgsl::LanguageFeature::kUnrestrictedPointerParameters);
33
34 // Verify that the WGSL we produced is valid.
35 tint::Source::File srcFile("", wgsl);
36 tint::Program program(tint::wgsl::reader::Parse(&srcFile, options));
37
38 if (program.Diagnostics().ContainsErrors()) {
39 // The program isn't valid WGSL.
40 if (appendError) {
41 // Report the error via SkDEBUGFAIL and append the generated program for
42 // ease of debugging. We don't do this for our golden test output because
43 // it can change too often
44 tint::diag::Formatter diagFormatter;
45 std::string diagOutput = diagFormatter.Format(program.Diagnostics()).Plain();
46 diagOutput += "\n";
47 diagOutput += wgsl;
48 SkDEBUGFAILF("%s", diagOutput.c_str());
49 } else {
50 reporter.error(Position(),
51 std::string("Tint compilation failed.\n\n") + std::string(wgsl));
52 }
53 return false;
54 }
55
56 if (!program.Diagnostics().empty()) {
57 // The program contains warnings. Report them as-is.
58 tint::diag::Formatter diagFormatter;
59 *warnings = diagFormatter.Format(program.Diagnostics()).Plain();
60 }
61 return true;
62 }
63
64 bool ValidateWGSL(ErrorReporter& reporter, std::string_view wgsl, std::string* warnings) {
65 return validate_wgsl(reporter, wgsl, false, warnings);
66 }
67
68 bool ValidateWGSLVerbose(ErrorReporter& reporter, std::string_view wgsl, std::string* warnings) {
69 return validate_wgsl(reporter, wgsl, true, warnings);
70 }
71
72 } // namespace SkSL