blob: eacb25168f0a251d6c12dcb06e8c46ec9737d920 [file] [log] [blame]
dan sinclair7fec38c2023-10-19 13:56:42 +00001// Copyright 2023 The Dawn & Tint Authors
Ben Claytonf1b8a012023-10-11 17:15:52 +00002//
dan sinclair7fec38c2023-10-19 13:56:42 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ben Claytonf1b8a012023-10-11 17:15:52 +00005//
dan sinclair7fec38c2023-10-19 13:56:42 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ben Claytonf1b8a012023-10-11 17:15:52 +00008//
dan sinclair7fec38c2023-10-19 13:56:42 +00009// 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 Claytonf1b8a012023-10-11 17:15:52 +000027
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
37namespace tint::glsl::validate {
38
39namespace {
40
41EShLanguage 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 Clayton7cdaffe2024-05-08 16:24:07 +000051 // MSVC considered the inlined call:
52 // `lang = PipelineStageToEshLanguage()` as potentially uninitialized
53 TINT_MSVC_ONLY(return EShLangCompute;)
Ben Claytonf1b8a012023-10-11 17:15:52 +000054 }
55}
56
57} // namespace
58
dan sinclairefda2802024-01-16 17:53:23 +000059Result<SuccessType> Validate(const std::string& source, tint::ast::PipelineStage stage) {
Ben Claytonf1b8a012023-10-11 17:15:52 +000060 TINT_STATIC_INIT(glslang::InitializeProcess());
61
dan sinclairefda2802024-01-16 17:53:23 +000062 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 Claytonf1b8a012023-10-11 17:15:52 +000078 }
79
80 return Success;
81}
82
83} // namespace tint::glsl::validate