diff --git a/.ccls b/.ccls index ab6261583..d79e21d79 100644 --- a/.ccls +++ b/.ccls @@ -17,6 +17,8 @@ -I./python/pybind11/include/ -I./external/Vulkan-Headers/include/ -I./external/googletest/googletest/include/ +-I./external/shaderc/libshaderc/include/ +-I./external/shaderc/libshaderc_util/include/ -I./external/spdlog/include/ -I./src/include/ -I./single_include/ diff --git a/.gitmodules b/.gitmodules index 7b7ada44f..086a6f8fd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,7 @@ path = python/pybind11 url = https://github.com/pybind/pybind11 branch = v2.6.1 +[submodule "external/shaderc"] + path = external/shaderc + url = https://github.com/google/shaderc + branch = v2020.4 diff --git a/Makefile b/Makefile index f77970759..c2b667fd4 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,7 @@ mk_cmake: -DKOMPUTE_OPT_ENABLE_SPDLOG=1 \ -DSPDLOG_INSTALL=1 \ -DKOMPUTE_OPT_CODE_COVERAGE=1 \ + -DSHADERC_SKIP_TESTS=1 \ -G "Unix Makefiles" mk_build_all: @@ -88,7 +89,7 @@ mk_run_docs: mk_build_docs (cd build/docs/sphinx && python2.7 -m SimpleHTTPServer) mk_run_tests: mk_build_tests - ./build/test/test_kompute $(FILTER_TESTS) + ./build/test/test_kompute --gtest_filter=$(FILTER_TESTS) mk_build_swiftshader_library: git clone https://github.com/google/swiftshader || echo "Assuming already cloned" @@ -99,6 +100,9 @@ mk_build_swiftshader_library: mk_run_tests_cpu: export VK_ICD_FILENAMES=$(PWD)/swiftshader/build/vk_swiftshader_icd.json mk_run_tests_cpu: mk_build_swiftshader_library mk_build_tests mk_run_tests_cpu_only +mk_run_tests_only: + ./build/test/test_kompute --gtest_filter="-TestAsyncOperations.*" + mk_run_tests_cpu_only: ./build/test/test_kompute --gtest_filter="TestLogisticRegressionAlgorithm.*" ./build/test/test_kompute --gtest_filter="TestManager.*" @@ -133,6 +137,7 @@ vs_cmake: -DKOMPUTE_OPT_BUILD_SINGLE_HEADER=1 \ -DKOMPUTE_OPT_ENABLE_SPDLOG=1 \ -DSPDLOG_INSTALL=1 \ + -DSHADERC_SKIP_TESTS=1 \ -G "Visual Studio 16 2019" vs_build_all: diff --git a/external/shaderc b/external/shaderc new file mode 160000 index 000000000..0dbd89994 --- /dev/null +++ b/external/shaderc @@ -0,0 +1 @@ +Subproject commit 0dbd899941a43ffd55df527d65128b3b66e75c9c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b38f7bdae..77d0d1c4b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,7 @@ +##################################################### +#################### GETEST ####################### +##################################################### enable_testing() if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) add_subdirectory(${PROJECT_SOURCE_DIR}/external/googletest EXCLUDE_FROM_ALL @@ -35,6 +38,35 @@ target_link_libraries(test_kompute PRIVATE kompute) add_test(NAME test_kompute COMMAND test_kompute) +##################################################### +#################### PYSHADERC ####################### +##################################################### + +if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) + add_subdirectory(${PROJECT_SOURCE_DIR}/external/shaderc EXCLUDE_FROM_ALL + ${CMAKE_CURRENT_BINARY_DIR}/kompute_shaderc) + + target_include_directories( + test_kompute PRIVATE + ${shaderc_SOURCE_DIR}/include) + + target_link_libraries(test_kompute PRIVATE + shaderc_combined) +else() + find_library(SHADERC_LIB shaderc_combined) + + find_path(SHADERC_INCLUDE_DIRS shaderc) + + target_include_directories( + test_kompute PRIVATE + ${SHADERC_INCLUDE_DIRS}/include) + + MESSAGE(STATUS "INclude dirs:" ${SHADERC_INCLUDE_DIRS}) + + target_link_libraries(test_kompute PRIVATE + shaderc_combined) +endif() + ##################################################### #################### CODECOV ####################### ##################################################### diff --git a/test/TestSpecializationConstant.cpp b/test/TestSpecializationConstant.cpp new file mode 100644 index 000000000..05fb19420 --- /dev/null +++ b/test/TestSpecializationConstant.cpp @@ -0,0 +1,51 @@ + +#include "gtest/gtest.h" + +#include "kompute/Kompute.hpp" + + +TEST(TestSpecializationConstants, TestTwoConstants) +{ + std::shared_ptr tensorA{ new kp::Tensor({ 0, 0, 0 }) }; + std::shared_ptr tensorB{ new kp::Tensor({ 0, 0, 0 }) }; + + std::string shader(R"( + #version 450 + layout (constant_id = 0) const float cOne = 1; + layout (constant_id = 1) const float cTwo = 1; + layout (local_size_x = 1) in; + layout(set = 0, binding = 0) buffer a { float pa[]; }; + layout(set = 0, binding = 1) buffer b { float pb[]; }; + void main() { + uint index = gl_GlobalInvocationID.x; + pa[index] = cOne; + pb[index] = cTwo; + })"); + + { + std::shared_ptr sq = nullptr; + + { + kp::Manager mgr; + + mgr.rebuild({ tensorA, tensorB }); + + sq = mgr.sequence(); + + auto spec = kp::Constants({5.0, 0.3}); + + sq->begin(); + sq->record( + { tensorA, tensorB }, + std::vector(shader.begin(), shader.end()), + kp::Workgroup(), spec); + sq->end(); + + sq->eval(); + + mgr.evalOpDefault({ tensorA, tensorB }); + } + } + EXPECT_EQ(tensorA->data(), std::vector({ 5, 5, 5 })); + EXPECT_EQ(tensorB->data(), std::vector({ 0.3, 0.3, 0.3 })); +} diff --git a/test/TestUtils.cpp b/test/TestUtils.cpp new file mode 100644 index 000000000..59444f5f8 --- /dev/null +++ b/test/TestUtils.cpp @@ -0,0 +1,27 @@ + +#include +#include + +#include + +static std::vector spirv_from_string(const std::string& source, + shaderc_optimization_level optimization = shaderc_optimization_level_size, + std::vector> definitions = {}) { + shaderc::Compiler compiler; + shaderc::CompileOptions options; + + for (const std::pair& def : definitions) { + options.AddMacroDefinition(def.first, def.second); + } + if (optimization) options.SetOptimizationLevel(optimization); + + std::string errorTag = "kompute"; + shaderc::SpvCompilationResult module = + compiler.CompileGlslToSpv(source, shaderc_glsl_default_compute_shader, errorTag.c_str(), options); + + if (module.GetCompilationStatus() != shaderc_compilation_status_success) { + throw std::runtime_error("Shader string invalid: " + module.GetErrorMessage()); + } + + return {module.cbegin(), module.cend()}; +}