dan sinclair | 7fec38c | 2023-10-19 13:56:42 +0000 | [diff] [blame] | 1 | // Copyright 2023 The Dawn & Tint Authors |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 2 | // |
dan sinclair | 7fec38c | 2023-10-19 13:56:42 +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: |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 5 | // |
dan sinclair | 7fec38c | 2023-10-19 13:56:42 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 8 | // |
dan sinclair | 7fec38c | 2023-10-19 13:56:42 +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. |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 27 | |
| 28 | #include "src/tint/lang/glsl/validate/validate.h" |
| 29 | |
| 30 | #include <string> |
| 31 | |
| 32 | #include "glslang/Public/ResourceLimits.h" |
| 33 | #include "glslang/Public/ShaderLang.h" |
| 34 | #include "src/tint/utils/macros/static_init.h" |
| 35 | #include "src/tint/utils/text/string_stream.h" |
| 36 | |
| 37 | namespace tint::glsl::validate { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | EShLanguage PipelineStageToEshLanguage(tint::ast::PipelineStage stage) { |
| 42 | switch (stage) { |
| 43 | case tint::ast::PipelineStage::kFragment: |
| 44 | return EShLangFragment; |
| 45 | case tint::ast::PipelineStage::kVertex: |
| 46 | return EShLangVertex; |
| 47 | case tint::ast::PipelineStage::kCompute: |
| 48 | return EShLangCompute; |
| 49 | default: |
| 50 | TINT_UNREACHABLE(); |
Ben Clayton | 7cdaffe | 2024-05-08 16:24:07 +0000 | [diff] [blame] | 51 | // MSVC considered the inlined call: |
| 52 | // `lang = PipelineStageToEshLanguage()` as potentially uninitialized |
| 53 | TINT_MSVC_ONLY(return EShLangCompute;) |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
dan sinclair | efda280 | 2024-01-16 17:53:23 +0000 | [diff] [blame] | 59 | Result<SuccessType> Validate(const std::string& source, tint::ast::PipelineStage stage) { |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 60 | TINT_STATIC_INIT(glslang::InitializeProcess()); |
| 61 | |
dan sinclair | efda280 | 2024-01-16 17:53:23 +0000 | [diff] [blame] | 62 | const char* strings[1] = {source.c_str()}; |
| 63 | int lengths[1] = {static_cast<int>(source.length())}; |
| 64 | |
| 65 | EShLanguage lang = PipelineStageToEshLanguage(stage); |
| 66 | glslang::TShader shader(lang); |
| 67 | shader.setStringsWithLengths(strings, lengths, 1); |
| 68 | shader.setEntryPoint("main"); |
| 69 | |
| 70 | bool result = shader.parse(GetDefaultResources(), 310, EEsProfile, false, false, EShMsgDefault); |
| 71 | |
| 72 | if (!result) { |
| 73 | StringStream err; |
| 74 | err << "Error parsing GLSL shader:\n" |
| 75 | << shader.getInfoLog() << "\n" |
| 76 | << shader.getInfoDebugLog() << "\n"; |
| 77 | return Failure{err.str()}; |
Ben Clayton | f1b8a01 | 2023-10-11 17:15:52 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return Success; |
| 81 | } |
| 82 | |
| 83 | } // namespace tint::glsl::validate |