Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/wgsl/reader/reader.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
Ben Clayton | cc63696 | 2024-01-30 16:55:59 +0000 | [diff] [blame] | 30 | #include <limits> |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | #include <utility> |
| 32 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 33 | #include "src/tint/lang/wgsl/reader/lower/lower.h" |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 34 | #include "src/tint/lang/wgsl/reader/parser/parser.h" |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 35 | #include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h" |
Ben Clayton | 915ceca | 2023-07-29 13:12:58 +0000 | [diff] [blame] | 36 | #include "src/tint/lang/wgsl/resolver/resolve.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 38 | namespace tint::wgsl::reader { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | |
James Price | 78da664 | 2023-11-06 18:42:21 +0000 | [diff] [blame] | 40 | Program Parse(const Source::File* file, const Options& options) { |
Ben Clayton | cc63696 | 2024-01-30 16:55:59 +0000 | [diff] [blame] | 41 | if (TINT_UNLIKELY(file->content.data.size() > |
| 42 | static_cast<size_t>(std::numeric_limits<uint32_t>::max()))) { |
| 43 | ProgramBuilder b; |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 44 | b.Diagnostics().AddError(tint::Source{}) << "WGSL source must be 0xffffffff bytes or fewer"; |
Ben Clayton | cc63696 | 2024-01-30 16:55:59 +0000 | [diff] [blame] | 45 | return Program(std::move(b)); |
| 46 | } |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 47 | Parser parser(file); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | parser.Parse(); |
James Price | 51f19e3 | 2024-06-12 14:38:34 +0000 | [diff] [blame] | 49 | return resolver::Resolve(parser.builder(), options.allowed_features, options.mode); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | } |
| 51 | |
James Price | 78da664 | 2023-11-06 18:42:21 +0000 | [diff] [blame] | 52 | Result<core::ir::Module> WgslToIR(const Source::File* file, const Options& options) { |
| 53 | Program program = Parse(file, options); |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 54 | auto module = ProgramToIR(program); |
Ben Clayton | 89274f7 | 2024-01-03 10:53:42 +0000 | [diff] [blame] | 55 | if (module != Success) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 56 | return module.Failure(); |
| 57 | } |
| 58 | // WGSL-dialect -> core-dialect |
Ben Clayton | 89274f7 | 2024-01-03 10:53:42 +0000 | [diff] [blame] | 59 | if (auto res = Lower(module.Get()); res != Success) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 60 | return res.Failure(); |
| 61 | } |
| 62 | return module; |
| 63 | } |
| 64 | |
dan sinclair | 24b8109 | 2023-11-21 16:02:47 +0000 | [diff] [blame] | 65 | tint::Result<core::ir::Module> ProgramToLoweredIR(const Program& program) { |
| 66 | auto ir = tint::wgsl::reader::ProgramToIR(program); |
Ben Clayton | 89274f7 | 2024-01-03 10:53:42 +0000 | [diff] [blame] | 67 | if (ir != Success) { |
dan sinclair | 24b8109 | 2023-11-21 16:02:47 +0000 | [diff] [blame] | 68 | return ir.Failure(); |
| 69 | } |
| 70 | |
| 71 | // Lower from WGSL-dialect to core-dialect |
| 72 | auto res = tint::wgsl::reader::Lower(ir.Get()); |
Ben Clayton | 89274f7 | 2024-01-03 10:53:42 +0000 | [diff] [blame] | 73 | if (res != Success) { |
dan sinclair | 24b8109 | 2023-11-21 16:02:47 +0000 | [diff] [blame] | 74 | return res.Failure(); |
| 75 | } |
| 76 | |
| 77 | return ir; |
| 78 | } |
| 79 | |
Ryan Harrison | c7d4205 | 2024-05-22 04:38:49 +0000 | [diff] [blame] | 80 | bool IsUnsupportedByIR(const ast::Enable* enable) { |
| 81 | for (auto ext : enable->extensions) { |
| 82 | switch (ext->name) { |
| 83 | case tint::wgsl::Extension::kChromiumExperimentalFramebufferFetch: |
| 84 | case tint::wgsl::Extension::kChromiumExperimentalPixelLocal: |
| 85 | case tint::wgsl::Extension::kChromiumExperimentalPushConstant: |
Ryan Harrison | c7d4205 | 2024-05-22 04:38:49 +0000 | [diff] [blame] | 86 | case tint::wgsl::Extension::kChromiumInternalRelaxedUniformLayout: |
| 87 | return true; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 95 | } // namespace tint::wgsl::reader |