Add hlsl::TestHelperBase::Validate()

Looks for DXC on PATH.
If found, it will invoke DXC with the shader program generated by the writer to verify the shader can compile.

Change-Id: Iad8b4021bac16d01214b500ddb89b5f743927ce9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41943
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-02-18 16:19:09 +00:00 committed by Commit Bot service account
parent ce7e18e87c
commit 41085503e1
4 changed files with 129 additions and 0 deletions

View File

@ -1265,6 +1265,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_unary_op_test.cc", "src/writer/hlsl/generator_impl_unary_op_test.cc",
"src/writer/hlsl/generator_impl_variable_decl_statement_test.cc", "src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
"src/writer/hlsl/namer_test.cc", "src/writer/hlsl/namer_test.cc",
"src/writer/hlsl/test_helper.cc",
"src/writer/hlsl/test_helper.h", "src/writer/hlsl/test_helper.h",
] ]

View File

@ -785,6 +785,7 @@ if(${TINT_BUILD_TESTS})
writer/hlsl/generator_impl_unary_op_test.cc writer/hlsl/generator_impl_unary_op_test.cc
writer/hlsl/generator_impl_variable_decl_statement_test.cc writer/hlsl/generator_impl_variable_decl_statement_test.cc
writer/hlsl/namer_test.cc writer/hlsl/namer_test.cc
writer/hlsl/test_helper.cc
writer/hlsl/test_helper.h writer/hlsl/test_helper.h
) )
endif() endif()

View File

@ -0,0 +1,96 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/writer/hlsl/test_helper.h"
#include "src/utils/command.h"
#include "src/utils/tmpfile.h"
namespace tint {
namespace writer {
namespace hlsl {
CompileResult Compile(Program* program, GeneratorImpl* generator) {
CompileResult result;
auto dxc = utils::Command::LookPath("dxc");
if (!dxc.Found()) {
result.status = CompileResult::Status::kDXCNotFound;
return result;
}
std::ostringstream hlsl;
if (!generator->Generate(hlsl)) {
result.output = generator->error();
result.status = CompileResult::Status::kFailed;
return result;
}
result.hlsl = hlsl.str();
utils::TmpFile file;
file << result.hlsl;
bool found_an_entrypoint = false;
for (auto* func : program->AST().Functions()) {
if (func->IsEntryPoint()) {
found_an_entrypoint = true;
const char* profile = "";
switch (func->pipeline_stage()) {
case ast::PipelineStage::kNone:
result.output = "Invalid PipelineStage";
result.status = CompileResult::Status::kFailed;
return result;
case ast::PipelineStage::kVertex:
profile = "-T vs_6_0";
break;
case ast::PipelineStage::kFragment:
profile = "-T ps_6_0";
break;
case ast::PipelineStage::kCompute:
profile = "-T cs_6_0";
break;
}
auto res = dxc(profile, file.Path());
if (!res.out.empty()) {
if (!result.output.empty()) {
result.output += "\n";
}
result.output += res.out;
}
if (!res.err.empty()) {
if (!result.output.empty()) {
result.output += "\n";
}
result.output += res.err;
}
result.status = (res.error_code == 0) ? CompileResult::Status::kSuccess
: CompileResult::Status::kFailed;
}
}
if (!found_an_entrypoint) {
result.output = "No entrypoint found";
result.status = CompileResult::Status::kFailed;
return result;
}
return result;
}
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -31,6 +31,24 @@ namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
/// The return structure of Compile()
struct CompileResult {
/// Status is an enumerator of status codes from Compile()
enum class Status { kSuccess, kFailed, kDXCNotFound };
/// The resulting status of the compile
Status status;
/// Output of DXC.
std::string output;
/// The HLSL source that was compiled
std::string hlsl;
};
/// Compile attempts to compile the shader with DXC if found on PATH.
/// @param program the HLSL program
/// @param generator the HLSL generator
/// @return the result of the compile
CompileResult Compile(Program* program, GeneratorImpl* generator);
/// Helper class for testing /// Helper class for testing
template <typename BODY> template <typename BODY>
class TestHelperBase : public BODY, public ProgramBuilder { class TestHelperBase : public BODY, public ProgramBuilder {
@ -89,6 +107,19 @@ class TestHelperBase : public BODY, public ProgramBuilder {
return *gen_; return *gen_;
} }
/// Validate passes the generated HLSL from the generator to the DXC compiler
/// on `PATH` for checking the program can be compiled.
/// If DXC finds problems the test will fail.
/// If DXC is not on `PATH` then Validate() does nothing.
void Validate() const {
auto res = Compile(program.get(), gen_.get());
if (res.status == CompileResult::Status::kFailed) {
FAIL() << "HLSL Validation failed.\n\n"
<< res.hlsl << "\n\n"
<< res.output;
}
}
/// @returns the result string /// @returns the result string
std::string result() const { return out.str(); } std::string result() const { return out.str(); }