[hlsl-writer] Scaffold the HLSL backend.

This CL adds the scaffolding for the HLSL backend.

Bug: tint:7
Change-Id: Iaf9f5159bc409f3ac71fcec281229258bdfa021b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25000
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-20 22:13:37 +00:00
parent 5dee499c57
commit feffa9d887
13 changed files with 310 additions and 8 deletions

View File

@ -62,6 +62,12 @@ config("tint_public_config") {
defines += [ "TINT_BUILD_MSL_WRITER=0" ]
}
if (tint_build_hlsl_writer) {
defines += [ "TINT_BUILD_HLSL_WRITER=1" ]
} else {
defines += [ "TINT_BUILD_HLSL_WRITER=0" ]
}
include_dirs = [
"${tint_root_dir}/",
"${tint_root_dir}/include/",
@ -513,6 +519,23 @@ source_set("libtint_msl_writer_src") {
public_deps = [ ":libtint_core_src" ]
}
source_set("libtint_hlsl_writer_src") {
sources = [
"src/writer/hlsl/generator.cc",
"src/writer/hlsl/generator.h",
"src/writer/hlsl/generator_impl.cc",
"src/writer/hlsl/generator_impl.h",
]
configs += [ ":tint_common_config" ]
public_configs = [ ":tint_public_config" ]
if (build_with_chromium) {
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [ "//build/config/compiler:no_chromium_code" ]
}
}
source_set("libtint") {
deps = [ ":libtint_core_src" ]
@ -536,6 +559,10 @@ source_set("libtint") {
deps += [ ":libtint_msl_writer_src" ]
}
if (tint_build_hlsl_writer) {
deps += [ ":libtint_hlsl_writer_src" ]
}
configs += [ ":tint_common_config" ]
public_configs = [ ":tint_public_config" ]
@ -982,6 +1009,21 @@ source_set("tint_unittests_msl_writer_src") {
]
}
source_set("tint_unittests_hlsl_writer_src") {
sources = [ "src/writer/hlsl/generator_impl_test.cc" ]
configs += [
":tint_common_config",
":tint_unittests_config",
]
public_configs = [ ":tint_public_config" ]
if (build_with_chromium) {
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [ "//build/config/compiler:no_chromium_code" ]
}
}
source_set("tint_unittests_src") {
testonly = true

View File

@ -29,6 +29,7 @@ endif()
option(TINT_BUILD_DOCS "Build documentation" ON)
option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" ON)
option(TINT_BUILD_WGSL_READER "Builde the WGSL input reader" ON)
option(TINT_BUILD_HLSL_WRITER "Build the HLSL output writer" ON)
option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" ON)
option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
@ -51,6 +52,7 @@ endif(WIN32)
message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
message(STATUS "Tint build HLSL writer: ${TINT_BUILD_HLSL_WRITER}")
message(STATUS "Tint build MSL writer: ${TINT_BUILD_MSL_WRITER}")
message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
@ -120,6 +122,8 @@ function(tint_default_compile_options TARGET)
-DTINT_BUILD_SPV_READER=$<BOOL:${TINT_BUILD_SPV_READER}>)
target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_WGSL_READER=$<BOOL:${TINT_BUILD_WGSL_READER}>)
target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_HLSL_WRITER=$<BOOL:${TINT_BUILD_HLSL_WRITER}>)
target_compile_definitions(${TARGET} PRIVATE
-DTINT_BUILD_MSL_WRITER=$<BOOL:${TINT_BUILD_MSL_WRITER}>)
target_compile_definitions(${TARGET} PRIVATE

View File

@ -46,4 +46,8 @@
#include "src/writer/msl/generator.h"
#endif // TINT_BUILD_MSL_WRITER
#if TINT_BUILD_HLSL_WRITER
#include "src/writer/hlsl/generator.h"
#endif // TINT_BUILD_HLSL_WRITER
#endif // INCLUDE_TINT_TINT_H_

View File

@ -33,6 +33,7 @@ enum class Format {
kSpvAsm,
kWgsl,
kMsl,
kHlsl,
};
struct Options {
@ -50,13 +51,14 @@ struct Options {
const char kUsage[] = R"(Usage: tint [options] <input-file>
options:
--format <spirv|spvasm|wgsl|msl> -- Output format.
--format <spirv|spvasm|wgsl|msl|hlsl> -- Output format.
If not provided, will be inferred from output
filename extension:
.spvasm -> spvasm
.spv -> spirv
.wgsl -> wgsl
.metal -> msl
.spv -> spirv
.wgsl -> wgsl
.metal -> msl
.hlsl -> hlsl
If none matches, then default to SPIR-V assembly.
--output-file <name> -- Output file name. Use "-" for standard output
-o <name> -- Output file name. Use "-" for standard output
@ -86,6 +88,11 @@ Format parse_format(const std::string& fmt) {
return Format::kMsl;
#endif // TINT_BUILD_MSL_WRITER
#if TINT_BUILD_HLSL_WRITER
if (fmt == "hlsl")
return Format::kHlsl;
#endif // TINT_BUILD_HLSL_WRITER
return Format::kNone;
}
@ -453,7 +460,13 @@ int main(int argc, const char** argv) {
if (options.format == Format::kMsl) {
writer = std::make_unique<tint::writer::msl::Generator>(std::move(mod));
}
#endif // TINT_BUILDER_MSL_WRITER
#endif // TINT_BUILD_MSL_WRITER
#if TINT_BUILD_HLSL_WRITER
if (options.format == Format::kHlsl) {
writer = std::make_unique<tint::writer::hlsl::Generator>(std::move(mod));
}
#endif // TINT_BUILD_HLSL_WRITER
if (!writer) {
std::cerr << "Unknown output format specified" << std::endl;

View File

@ -260,6 +260,15 @@ if(${TINT_BUILD_MSL_WRITER})
)
endif()
if(${TINT_BUILD_HLSL_WRITER})
list(APPEND TINT_LIB_SRCS
writer/hlsl/generator.cc
writer/hlsl/generator.h
writer/hlsl/generator_impl.cc
writer/hlsl/generator_impl.h
)
endif()
set(TINT_TEST_SRCS
ast/array_accessor_expression_test.cc
ast/as_expression_test.cc
@ -524,6 +533,12 @@ if(${TINT_BUILD_MSL_WRITER})
)
endif()
if (${TINT_BUILD_HLSL_WRITER})
list(APPEND TINT_TEST_SRCS
writer/hlsl/generator_impl_test.cc
)
endif()
add_executable(tint_unittests ${TINT_TEST_SRCS})
if(NOT MSVC)

View File

@ -0,0 +1,45 @@
// Copyright 2020 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/generator.h"
#include <utility>
namespace tint {
namespace writer {
namespace hlsl {
Generator::Generator(ast::Module module) : Text(std::move(module)) {}
Generator::~Generator() = default;
bool Generator::Generate() {
auto ret = impl_.Generate(module_);
if (!ret) {
error_ = impl_.error();
}
return ret;
}
std::string Generator::result() const {
return impl_.result();
}
std::string Generator::error() const {
return impl_.error();
}
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,53 @@
// Copyright 2020 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.
#ifndef SRC_WRITER_HLSL_GENERATOR_H_
#define SRC_WRITER_HLSL_GENERATOR_H_
#include <string>
#include "src/writer/hlsl/generator_impl.h"
#include "src/writer/text.h"
namespace tint {
namespace writer {
namespace hlsl {
/// Class to generate HLSL source
class Generator : public Text {
public:
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module module);
~Generator() override;
/// Generates the result data
/// @returns true on successful generation; false otherwise
bool Generate() override;
/// @returns the result data
std::string result() const override;
/// @returns the error
std::string error() const;
private:
GeneratorImpl impl_;
};
} // namespace hlsl
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_HLSL_GENERATOR_H_

View File

@ -0,0 +1,31 @@
// Copyright 2020 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/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
GeneratorImpl::GeneratorImpl() = default;
GeneratorImpl::~GeneratorImpl() = default;
bool GeneratorImpl::Generate(const ast::Module&) {
return true;
}
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,42 @@
// Copyright 2020 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.
#ifndef SRC_WRITER_HLSL_GENERATOR_IMPL_H_
#define SRC_WRITER_HLSL_GENERATOR_IMPL_H_
#include "src/ast/module.h"
#include "src/writer/text_generator.h"
namespace tint {
namespace writer {
namespace hlsl {
/// Implementation class for HLSL generator
class GeneratorImpl : public TextGenerator {
public:
/// Constructor
GeneratorImpl();
~GeneratorImpl();
/// Generates the result data
/// @param module the module to generate
/// @returns true on successful generation; false otherwise
bool Generate(const ast::Module& module);
};
} // namespace hlsl
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_HLSL_GENERATOR_IMPL_H_

View File

@ -0,0 +1,53 @@
// Copyright 2020 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/generator_impl.h"
#include <memory>
#include "gtest/gtest.h"
#include "src/ast/entry_point.h"
#include "src/ast/function.h"
#include "src/ast/module.h"
#include "src/ast/type/void_type.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
TEST_F(HlslGeneratorImplTest, DISABLED_Generate) {
ast::type::VoidType void_type;
ast::Module m;
m.AddFunction(std::make_unique<ast::Function>("my_func", ast::VariableList{},
&void_type));
m.AddEntryPoint(std::make_unique<ast::EntryPoint>(
ast::PipelineStage::kFragment, "my_func", ""));
GeneratorImpl g;
ASSERT_TRUE(g.Generate(m)) << g.error();
EXPECT_EQ(g.result(), R"(#import <metal_lib>
void my_func() {
}
)");
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -24,7 +24,7 @@ namespace tint {
namespace writer {
namespace msl {
/// Class to generate WGSL source from a WGSL module
/// Class to generate MSL source
class Generator : public Text {
public:
/// Constructor

View File

@ -32,7 +32,7 @@ namespace tint {
namespace writer {
namespace msl {
/// Implementation class for WGSL generator
/// Implementation class for MSL generator
class GeneratorImpl : public TextGenerator {
public:
/// Constructor

View File

@ -24,7 +24,7 @@ namespace tint {
namespace writer {
namespace wgsl {
/// Class to generate WGSL source from a WGSL module
/// Class to generate WGSL source
class Generator : public Text {
public:
/// Constructor