diff options
author | alan-baker <33432579+alan-baker@users.noreply.github.com> | 2019-07-15 21:19:11 -0400 |
---|---|---|
committer | dan sinclair <dsinclair@google.com> | 2019-07-15 21:19:11 -0400 |
commit | bbe15eabade03277cb6f614697133b0dcf1f68e3 (patch) | |
tree | 162e24fe13e4d711382d5d0e194d7df5106eaa71 /src | |
parent | ed9971b45f46d35f8bcdf7190de34680d743e630 (diff) | |
download | amber-bbe15eabade03277cb6f614697133b0dcf1f68e3.tar.gz |
Support parsing OpenCL C kernels (#578)
* New shader format: OpenCLC
* Add parser tests
* Build fixes for clspv
* add include director
* add link libraries
* add macro definition
* Add clspv helper for compiling
Fixes #428
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 15 | ||||
-rw-r--r-- | src/amberscript/parser.cc | 2 | ||||
-rw-r--r-- | src/amberscript/parser_shader_test.cc | 24 | ||||
-rw-r--r-- | src/clspv_helper.cc | 35 | ||||
-rw-r--r-- | src/clspv_helper.h | 34 | ||||
-rw-r--r-- | src/shader_compiler.cc | 23 | ||||
-rw-r--r-- | src/shader_compiler.h | 2 |
7 files changed, 135 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ff1a15e..03d183a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -55,6 +55,10 @@ if (${AMBER_ENABLE_DXC}) list(APPEND AMBER_SOURCES dxc_helper.cc) endif() +if (${AMBER_ENABLE_CLSPV}) + list(APPEND AMBER_SOURCES clspv_helper.cc) +endif() + add_library(libamber ${AMBER_SOURCES}) amber_default_compile_options(libamber) target_include_directories(libamber PRIVATE "${CMAKE_BINARY_DIR}") @@ -76,6 +80,17 @@ if (${AMBER_ENABLE_DXC}) ) endif() +if (${AMBER_ENABLE_CLSPV}) + target_include_directories(libamber PRIVATE + "${PROJECT_SOURCE_DIR}/third_party/clspv/include" + ) + + target_link_libraries(libamber + clspv_core + clspv_passes + ) +endif() + if (${AMBER_ENABLE_SPIRV_TOOLS}) target_link_libraries(libamber SPIRV-Tools) endif() diff --git a/src/amberscript/parser.cc b/src/amberscript/parser.cc index 2c1fc05..412f33f 100644 --- a/src/amberscript/parser.cc +++ b/src/amberscript/parser.cc @@ -187,6 +187,8 @@ Result Parser::ToShaderFormat(const std::string& str, ShaderFormat* fmt) { *fmt = kShaderFormatSpirvAsm; else if (str == "SPIRV-HEX") *fmt = kShaderFormatSpirvHex; + else if (str == "OPENCL-C") + *fmt = kShaderFormatOpenCLC; else return Result("unknown shader format: " + str); return {}; diff --git a/src/amberscript/parser_shader_test.cc b/src/amberscript/parser_shader_test.cc index 47f3c1d..f9fd957 100644 --- a/src/amberscript/parser_shader_test.cc +++ b/src/amberscript/parser_shader_test.cc @@ -340,5 +340,29 @@ END)"; EXPECT_EQ("7: duplicate shader name provided", r.Error()); } +TEST_F(AmberScriptParserTest, OpenCLCKernel) { + std::string in = R"( +SHADER compute my_shader OPENCL-C +# shader +END +)"; + + Parser parser; + Result r = parser.Parse(in); + ASSERT_TRUE(r.IsSuccess()); +} + +TEST_F(AmberScriptParserTest, OpenCLCMultiKernel) { + std::string in = R"( +SHADER multi my_shader OPENCL-C +# shader +END +)"; + + Parser parser; + Result r = parser.Parse(in); + ASSERT_TRUE(r.IsSuccess()); +} + } // namespace amberscript } // namespace amber diff --git a/src/clspv_helper.cc b/src/clspv_helper.cc new file mode 100644 index 0000000..056b5d5 --- /dev/null +++ b/src/clspv_helper.cc @@ -0,0 +1,35 @@ +// Copyright 2019 The Amber 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/clspv_helper.h" + +#include "clspv/Compiler.h" + +namespace amber { +namespace clspvhelper { + +Result Compile(const std::string& src_str, + std::vector<uint32_t>* generated_binary) { + // TODO(alan-baker): Parse the descriptor map. + std::vector<clspv::version0::DescriptorMapEntry> entries; + if (clspv::CompileFromSourceString(src_str, "", "", generated_binary, + &entries)) { + return Result("Clspv compile failed"); + } + + return Result(); +} + +} // namespace clspvhelper +} // namespace amber diff --git a/src/clspv_helper.h b/src/clspv_helper.h new file mode 100644 index 0000000..3672532 --- /dev/null +++ b/src/clspv_helper.h @@ -0,0 +1,34 @@ +// Copyright 2019 The Amber 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_CLSPV_HELPER_H_ +#define SRC_CLSPV_HELPER_H_ + +#include <string> +#include <vector> + +#include "amber/result.h" + +namespace amber { +namespace clspvhelper { + +// Passes the OpenCL C source code to Clspv. +// Returns the generated SPIR-V binary via |generated_binary| argument. +Result Compile(const std::string& src_str, + std::vector<uint32_t>* generated_binary); + +} // namespace clspvhelper +} // namespace amber + +#endif // SRC_CLSPV_HELPER_H_ diff --git a/src/shader_compiler.cc b/src/shader_compiler.cc index 674da4a..a0c737e 100644 --- a/src/shader_compiler.cc +++ b/src/shader_compiler.cc @@ -38,6 +38,10 @@ #include "src/dxc_helper.h" #endif // AMBER_ENABLE_DXC +#ifdef AMBER_ENABLE_CLSPV +#include "src/clspv_helper.h" +#endif // AMBER_ENABLE_CLSPV + namespace amber { ShaderCompiler::ShaderCompiler() = default; @@ -116,6 +120,13 @@ std::pair<Result, std::vector<uint32_t>> ShaderCompiler::Compile( } #endif // AMBER_ENABLE_SPIRV_TOOLS +#if AMBER_ENABLE_CLSPV + } else if (shader->GetFormat() == kShaderFormatOpenCLC) { + Result r = CompileOpenCLC(shader, &results); + if (!r.IsSuccess()) + return {r, {}}; +#endif // AMBER_ENABLE_CLSPV + } else { return {Result("Invalid shader format"), results}; } @@ -218,4 +229,16 @@ Result ShaderCompiler::CompileHlsl(const Shader*, } #endif // AMBER_ENABLE_DXC +#if AMBER_ENABLE_CLSPV +Result ShaderCompiler::CompileOpenCLC(const Shader* shader, + std::vector<uint32_t>* result) const { + return clspvhelper::Compile(shader->GetData(), result); +} +#else +Result ShaderCompiler::CompileOpenCLC(const Shader*, + std::vector<uint32_t>*) const { + return {}; +} +#endif // AMBER_ENABLE_CLSPV + } // namespace amber diff --git a/src/shader_compiler.h b/src/shader_compiler.h index 3c0067d..52fd027 100644 --- a/src/shader_compiler.h +++ b/src/shader_compiler.h @@ -44,6 +44,8 @@ class ShaderCompiler { Result ParseHex(const std::string& data, std::vector<uint32_t>* result) const; Result CompileGlsl(const Shader* shader, std::vector<uint32_t>* result) const; Result CompileHlsl(const Shader* shader, std::vector<uint32_t>* result) const; + Result CompileOpenCLC(const Shader* shader, + std::vector<uint32_t>* result) const; std::string spv_env_; }; |