[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

@@ -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