From 13575499008d3b8994e0b12f9501a58f9559563b Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Tue, 20 Jul 2021 19:22:51 +0100 Subject: [PATCH 1/8] Updated glslang as core dependency --- .ccls | 1 + CHANGELOG.md | 2 +- CMakeLists.txt | 19 ++--- README.md | 2 +- docs/overview/advanced-examples.rst | 8 +-- docs/overview/reference.rst | 4 +- docs/overview/shaders-to-headers.rst | 16 ----- examples/array_multiplication/src/Main.cpp | 2 +- .../kompute_summator/KomputeSummatorNode.cpp | 2 +- .../gdnative_shared/src/KomputeSummator.cpp | 2 +- python/src/main.cpp | 28 -------- python/test/test_kompute.py | 17 +++-- python/test/test_tensor_types.py | 11 +-- single_include/AggregateHeaders.cpp | 1 - single_include/kompute/Kompute.hpp | 72 ------------------- src/CMakeLists.txt | 35 --------- test/CMakeLists.txt | 40 ++++++++++- test/TestAsyncOperations.cpp | 6 +- test/TestDestroy.cpp | 8 ++- test/TestMultipleAlgoExecutions.cpp | 12 ++-- test/TestOpShadersFromStringAndFile.cpp | 4 +- test/TestOpTensorCopy.cpp | 2 + test/TestPushConstant.cpp | 8 ++- test/TestSequence.cpp | 6 +- test/TestShaderResources.cpp | 10 +-- test/TestSpecializationConstant.cpp | 4 +- {src => test/utils/kompute_test}/Shader.cpp | 8 +-- .../utils/kompute_test}/Shader.hpp | 6 +- 28 files changed, 119 insertions(+), 217 deletions(-) rename {src => test/utils/kompute_test}/Shader.cpp (97%) rename {src/include/kompute => test/utils/kompute_test}/Shader.hpp (94%) diff --git a/.ccls b/.ccls index da06a976..dd11e052 100644 --- a/.ccls +++ b/.ccls @@ -24,4 +24,5 @@ -I./single_include/ -I./vk_ndk_wrapper_include/ -I./test/compiled_shaders_include/ +-I./test/utils/ diff --git a/CHANGELOG.md b/CHANGELOG.md index b85f4e1d..3a48d1fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,7 @@ **Closed issues:** - Update memory barriers to align with tensor staging/primary memory revamp [\#181](https://github.com/EthicalML/vulkan-kompute/issues/181) -- Move shader defaultResource inside kp::Shader class [\#175](https://github.com/EthicalML/vulkan-kompute/issues/175) +- Move shader defaultResource inside kp_test_utils::Shader class [\#175](https://github.com/EthicalML/vulkan-kompute/issues/175) - Reach at least 90% code coverage on tests [\#170](https://github.com/EthicalML/vulkan-kompute/issues/170) - Add functionality to re-record sequence as now it's possible to update the underlying algorithm [\#169](https://github.com/EthicalML/vulkan-kompute/issues/169) - Use numpy arrays as default return value [\#166](https://github.com/EthicalML/vulkan-kompute/issues/166) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37162eb9..3a9c5b2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,6 @@ option(KOMPUTE_OPT_ENABLE_SPDLOG "Extra compile flags for Kompute, see docs for option(KOMPUTE_OPT_REPO_SUBMODULE_BUILD "Use the submodule repos instead of external package manager" 0) option(KOMPUTE_OPT_ANDROID_BUILD "Enable android compilation flags required" 0) option(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS "Explicitly disable debug layers even on debug" 0) -option(KOMPUTE_OPT_DISABLE_SHADER_UTILS "Remove shader util code and dependencies including glslang" 0) option(KOMPUTE_OPT_DEPENDENCIES_SHARED_LIBS "Whether to use shared libraries for dependencies for install" 0) option(KOMPUTE_OPT_BUILD_AS_SHARED_LIB "Whether to build kompute as shared library" 0) # Build flags @@ -55,19 +54,15 @@ if(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS) set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DKOMPUTE_DISABLE_VK_DEBUG_LAYERS=1") endif() -if(NOT KOMPUTE_OPT_DISABLE_SHADER_UTILS) - if(KOMPUTE_OPT_INSTALL) - # Enable install parameters for glslang (overrides parameters passed) - # When install is enabled the glslang libraries become shared - set(ENABLE_GLSLANG_INSTALL ON CACHE BOOL "Enables install of glslang" FORCE) +if(KOMPUTE_OPT_INSTALL) + # Enable install parameters for glslang (overrides parameters passed) + # When install is enabled the glslang libraries become shared + set(ENABLE_GLSLANG_INSTALL ON CACHE BOOL "Enables install of glslang" FORCE) - # By default we enable shared library based installation - if(KOMPUTE_OPT_DEPENDENCIES_SHARED_LIBS) - set(BUILD_SHARED_LIBS ON CACHE BOOL "Enables build of shared libraries" FORCE) - endif() + # By default we enable shared library based installation + if(KOMPUTE_OPT_DEPENDENCIES_SHARED_LIBS) + set(BUILD_SHARED_LIBS ON CACHE BOOL "Enables build of shared libraries" FORCE) endif() -else() - set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DKOMPUTE_DISABLE_SHADER_UTILS=1") endif() set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG=1 ${KOMPUTE_EXTRA_CXX_FLAGS} -DUSE_DEBUG_EXTENTIONS") diff --git a/README.md b/README.md index aed87c9f..699928ea 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ void kompute(const std::string& shader) { kp::Constants pushConstsB({ 3.0 }); auto algorithm = mgr.algorithm(params, - kp::Shader::compileSource(shader), + kp_test_utils::Shader::compileSource(shader), workgroup, specConsts, pushConstsA); diff --git a/docs/overview/advanced-examples.rst b/docs/overview/advanced-examples.rst index e4cc8304..f2fbe219 100644 --- a/docs/overview/advanced-examples.rst +++ b/docs/overview/advanced-examples.rst @@ -55,7 +55,7 @@ The example below shows how you can enable the "VK_EXT_shader_atomic_float" exte atomicAdd(pa[2], pcs.z); })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr sq = nullptr; @@ -102,7 +102,7 @@ We also provide tools that allow you to `convert shaders into C++ headers spirv = kp::Shader::compileSource(R"( + std::vector spirv = kp_test_utils::Shader::compileSource(R"( #version 450 layout(set = 0, binding = 0) buffer tensorLhs { @@ -215,7 +215,7 @@ In this case we create a shader that should take a couple of milliseconds to run } )"); - auto algo = mgr.algorithm({tensor}, kp::Shader::compileSource(shader)); + auto algo = mgr.algorithm({tensor}, kp_test_utils::Shader::compileSource(shader)); Now we are able to run the await function on the default sequence. @@ -361,7 +361,7 @@ Similar to the asyncrhonous usecase above, we can still run synchronous commands } )"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr algo = mgr.algorithm({tensorA, tenssorB}, spirv); diff --git a/docs/overview/reference.rst b/docs/overview/reference.rst index 76d16514..7ea80518 100644 --- a/docs/overview/reference.rst +++ b/docs/overview/reference.rst @@ -122,9 +122,9 @@ The :class:`kp::OpMemoryBarrier` is a tensor only operation which adds memory ba Shader -------- -The :class:`kp::Shader` class contains a set of utilities to compile and process shaders. +The :class:`kp_test_utils::Shader` class contains a set of utilities to compile and process shaders. -.. doxygenclass:: kp::Shader +.. doxygenclass:: kp_test_utils::Shader :members: diff --git a/docs/overview/shaders-to-headers.rst b/docs/overview/shaders-to-headers.rst index 531e7495..0b36777b 100644 --- a/docs/overview/shaders-to-headers.rst +++ b/docs/overview/shaders-to-headers.rst @@ -3,22 +3,6 @@ Processing Shaders with Kompute ===================== -Kompute allows for two main ways of interacting with shaders - namely: - -* Integration with [glslang](https://github.com/KhronosGroup/glslang) for online/runtime shader compilation -* A CLI that coverts shaders into C++ header files - -Processing Shaders Online via Kompute Shader Utils ---------------- - -Kompute provides a set of helper functions that expose the C++ functionality of the glslang Khronos framework to process shader sources online during runtime. - -It's worth emphasising that the suggested approach is to process shaders offline, so the section below is suggested to convert shaders to either their respective SPV format, or convert them into C++ sources that would be embedded as part of the resulting binary. - -The Shader utility function can be skipped on build time through compiler flags - for more information on this you should read the `build section `_. - -More details on the shader utils can be found in the :class:`kp::Shader` section of the `C++ reference page `_. - Converting Shaders into C / C++ Header Files ---------------------------------- diff --git a/examples/array_multiplication/src/Main.cpp b/examples/array_multiplication/src/Main.cpp index 95e0781a..3dd48c39 100755 --- a/examples/array_multiplication/src/Main.cpp +++ b/examples/array_multiplication/src/Main.cpp @@ -39,7 +39,7 @@ int main() std::vector> params = { tensorInA, tensorInB, tensorOut }; - std::shared_ptr algo = mgr.algorithm(params, kp::Shader::compileSource(shader)); + std::shared_ptr algo = mgr.algorithm(params, kp_test_utils::Shader::compileSource(shader)); mgr.sequence() ->record(params) diff --git a/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp b/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp index e901ef81..1a42761b 100644 --- a/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp +++ b/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp @@ -54,7 +54,7 @@ void KomputeSummatorNode::_init() { std::shared_ptr algo = mgr.algorithm( { this->mPrimaryTensor, this->mSecondaryTensor }, - kp::Shader::compileSource(shader)); + kp_test_utils::Shader::compileSource(shader)); // First we ensure secondary tensor loads to GPU diff --git a/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp index 99aabb33..ffca61dc 100644 --- a/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp +++ b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp @@ -58,7 +58,7 @@ void KomputeSummator::_init() { // Then we run the operation with both tensors this->mSequence->record( { this->mPrimaryTensor, this->mSecondaryTensor }, - kp::Shader::compileSource(shader)); + kp_test_utils::Shader::compileSource(shader)); // We map the result back to local this->mSequence->record( diff --git a/python/src/main.cpp b/python/src/main.cpp index d9f87e93..846576ad 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -33,34 +33,6 @@ PYBIND11_MODULE(kp, m) { .value("storage", kp::Tensor::TensorTypes::eStorage, DOC(kp, Tensor, TensorTypes, eStorage)) .export_values(); -#if !defined(KOMPUTE_DISABLE_SHADER_UTILS) || !KOMPUTE_DISABLE_SHADER_UTILS - py::class_(m, "Shader", "Shader class") - .def_static("compile_source", []( - const std::string& source, - const std::string& entryPoint, - const std::vector>& definitions) { - std::vector spirv = kp::Shader::compileSource(source, entryPoint, definitions); - return py::bytes((const char*)spirv.data(), spirv.size() * sizeof(uint32_t)); - }, - DOC(kp, Shader, compileSource), - py::arg("source"), - py::arg("entryPoint") = "main", - py::arg("definitions") = std::vector>() ) - .def_static("compile_sources", []( - const std::vector& source, - const std::vector& files, - const std::string& entryPoint, - const std::vector>& definitions) { - std::vector spirv = kp::Shader::compileSources(source, files, entryPoint, definitions); - return py::bytes((const char*)spirv.data(), spirv.size() * sizeof(uint32_t)); - }, - DOC(kp, Shader, compileSources), - py::arg("sources"), - py::arg("files") = std::vector(), - py::arg("entryPoint") = "main", - py::arg("definitions") = std::vector>() ); -#endif // KOMPUTE_DISABLE_SHADER_UTILS - py::class_>(m, "OpBase", DOC(kp, OpBase)); py::class_>( diff --git a/python/test/test_kompute.py b/python/test/test_kompute.py index 8c2c1ac5..9dd8040d 100644 --- a/python/test/test_kompute.py +++ b/python/test/test_kompute.py @@ -5,10 +5,13 @@ import logging import pyshader as ps +import pyshaderc + DIRNAME = os.path.dirname(os.path.abspath(__file__)) kp_log = logging.getLogger("kp") + def test_end_to_end(): mgr = kp.Manager() @@ -52,7 +55,7 @@ def test_end_to_end(): push_consts_a = [2] push_consts_b = [3] - algo = mgr.algorithm(params, kp.Shader.compile_source(shader), workgroup, spec_consts, push_consts_a) + algo = mgr.algorithm(params, pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp"), workgroup, spec_consts, push_consts_a) (mgr.sequence() .record(kp.OpTensorSyncDevice(params)) @@ -88,7 +91,7 @@ def test_shader_str(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") mgr = kp.Manager() @@ -108,6 +111,7 @@ def test_shader_str(): assert tensor_out.data().tolist() == [2.0, 4.0, 6.0] + def test_sequence(): """ Test basic OpAlgoBase operation @@ -127,7 +131,7 @@ def test_sequence(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") mgr = kp.Manager(0) @@ -164,9 +168,10 @@ def test_sequence(): assert tensor_in_b.is_init() == False assert tensor_out.is_init() == False + def test_pushconsts(): - spirv = kp.Shader.compile_source(""" + spirv = pyshaderc.compile_into_spirv(""" #version 450 layout(push_constant) uniform PushConstants { float x; @@ -180,7 +185,7 @@ def test_pushconsts(): pa[1] += pcs.y; pa[2] += pcs.z; } - """) + """.encode("utf-8"), "comp", "shader.comp") mgr = kp.Manager() @@ -197,6 +202,7 @@ def test_pushconsts(): assert np.all(tensor.data() == np.array([0.4, 0.4, 0.4], dtype=np.float32)) + def test_workgroup(): mgr = kp.Manager(0) @@ -227,6 +233,7 @@ def compute_shader_wg(gl_idx=("input", "GlobalInvocationId", ps.ivec3), assert np.all(tensor_a.data() == np.stack([np.arange(16)]*8, axis=1).ravel()) assert np.all(tensor_b.data() == np.stack([np.arange(8)]*16, axis=0).ravel()) + def test_mgr_utils(): mgr = kp.Manager() diff --git a/python/test/test_tensor_types.py b/python/test/test_tensor_types.py index ee645d85..e2a71250 100644 --- a/python/test/test_tensor_types.py +++ b/python/test/test_tensor_types.py @@ -1,4 +1,5 @@ import pyshader as ps +import pyshaderc import os import pytest import kp @@ -22,7 +23,7 @@ def test_type_float(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") arr_in_a = np.array([123., 153., 231.], dtype=np.float32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.float32) @@ -61,7 +62,7 @@ def test_type_float_double_incorrect(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") arr_in_a = np.array([123., 153., 231.], dtype=np.float32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.uint32) @@ -103,7 +104,7 @@ def test_type_double(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") arr_in_a = np.array([123., 153., 231.], dtype=np.float64) arr_in_b = np.array([9482, 1208, 1238], dtype=np.float64) @@ -143,7 +144,7 @@ def test_type_int(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") arr_in_a = np.array([123, 153, 231], dtype=np.int32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.int32) @@ -183,7 +184,7 @@ def test_type_unsigned_int(): } """ - spirv = kp.Shader.compile_source(shader) + spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") arr_in_a = np.array([123, 153, 231], dtype=np.uint32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.uint32) diff --git a/single_include/AggregateHeaders.cpp b/single_include/AggregateHeaders.cpp index e4d1014e..e1c7f4af 100644 --- a/single_include/AggregateHeaders.cpp +++ b/single_include/AggregateHeaders.cpp @@ -2,7 +2,6 @@ #include "kompute/shaders/shaderopmult.hpp" #include "kompute/shaders/shaderlogisticregression.hpp" #include "kompute/Core.hpp" -#include "kompute/Shader.hpp" #include "kompute/Tensor.hpp" #include "kompute/Algorithm.hpp" #include "kompute/operations/OpBase.hpp" diff --git a/single_include/kompute/Kompute.hpp b/single_include/kompute/Kompute.hpp index a1be4765..4d252f51 100755 --- a/single_include/kompute/Kompute.hpp +++ b/single_include/kompute/Kompute.hpp @@ -725,78 +725,6 @@ extern py::object kp_debug, kp_info, kp_warning, kp_error; #endif // KOMPUTE_SPDLOG_ENABLED #endif // KOMPUTE_LOG_OVERRIDE -#if !defined(KOMPUTE_DISABLE_SHADER_UTILS) || !KOMPUTE_DISABLE_SHADER_UTILS -#include -#include - -#ifdef USE_EXTERNAL_GLSLANG -#include -#else -#include -#endif - -#include -#include - -namespace kp { - -/** - Shader utily class with functions to compile and process glsl files. -*/ -class Shader -{ - public: - // The default resource limit for the GLSL compiler, can be overwritten - // Has been adopted by: - // https://github.com/KhronosGroup/glslang/blob/master/StandAlone/ResourceLimits.cpp - const static TBuiltInResource defaultResource; - - /** - * Compile multiple sources with optional filenames. Currently this function - * uses the glslang C++ interface which is not thread safe so this funciton - * should not be called from multiple threads concurrently. If you have a - * online shader processing multithreading use-case that can't use offline - * compilation please open an issue. - * - * @param sources A list of raw glsl shaders in string format - * @param files A list of file names respective to each of the sources - * @param entryPoint The function name to use as entry point - * @param definitions List of pairs containing key value definitions - * @param resourcesLimit A list that contains the resource limits for the - * GLSL compiler - * @return The compiled SPIR-V binary in unsigned int32 format - */ - static std::vector compileSources( - const std::vector& sources, - const std::vector& files = {}, - const std::string& entryPoint = "main", - std::vector> definitions = {}, - const TBuiltInResource& resources = Shader::defaultResource); - - /** - * Compile a single glslang source from string value. Currently this - * function uses the glslang C++ interface which is not thread safe so this - * funciton should not be called from multiple threads concurrently. If you - * have a online shader processing multithreading use-case that can't use - * offline compilation please open an issue. - * - * @param source An individual raw glsl shader in string format - * @param entryPoint The function name to use as entry point - * @param definitions List of pairs containing key value definitions - * @param resourcesLimit A list that contains the resource limits for the - * GLSL compiler - * @return The compiled SPIR-V binary in unsigned int32 format - */ - static std::vector compileSource( - const std::string& source, - const std::string& entryPoint = "main", - std::vector> definitions = {}, - const TBuiltInResource& resources = Shader::defaultResource); -}; - -} -#endif // DKOMPUTE_DISABLE_SHADER_UTILS - namespace kp { /** diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 26bf47e6..5adfddc2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -141,41 +141,6 @@ if(KOMPUTE_OPT_BUILD_SINGLE_HEADER) build_single_header) endif() -##################################################### -#################### GLSLANG ####################### -##################################################### - -if(NOT KOMPUTE_OPT_DISABLE_SHADER_UTILS) - if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) - add_subdirectory(${PROJECT_SOURCE_DIR}/external/glslang - ${CMAKE_CURRENT_BINARY_DIR}/kompute_glslang) - - target_include_directories( - kompute PRIVATE - ${PROJECT_SOURCE_DIR}/external/glslang) - - target_link_libraries(kompute - # Not including hlsl support - # HLSL - # glslang includes OGLCompiler, OSDependent, MachineIndependent - glslang - SPIRV) - else() - find_package(glslang CONFIG REQUIRED) - - target_include_directories( - kompute PRIVATE - ${GLSLANG_GENERATED_INCLUDEDIR}) - - target_link_libraries(kompute - # Not including hlsl support - # glslang::HLSL - # Adding explicit dependencies to match above - glslang::glslang - glslang::SPIRV) - endif() -endif() - add_library(kompute::kompute ALIAS kompute) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a30792aa..8c4731a7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,6 +12,7 @@ endif() file(GLOB test_kompute_CPP "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/utils/kompute_test/*.cpp" ) add_executable(test_kompute ${test_kompute_CPP}) @@ -20,6 +21,7 @@ target_include_directories( test_kompute PUBLIC $ $ + $ ) if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) @@ -27,17 +29,51 @@ if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) test_kompute PRIVATE ${gtest_SOURCE_DIR}/include) - target_link_libraries(test_kompute PRIVATE + target_link_libraries(test_kompute gtest_main) else() target_link_libraries(test_kompute PRIVATE GTest::gtest) endif() -target_link_libraries(test_kompute PRIVATE kompute) +target_link_libraries(test_kompute kompute) add_test(NAME test_kompute COMMAND test_kompute) +##################################################### +#################### GLSLANG ####################### +##################################################### + +if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) + add_subdirectory(${PROJECT_SOURCE_DIR}/external/glslang + ${CMAKE_CURRENT_BINARY_DIR}/kompute_glslang) + + target_include_directories( + test_kompute PRIVATE + ${PROJECT_SOURCE_DIR}/external/glslang) + + target_link_libraries(test_kompute + # Not including hlsl support + # HLSL + # glslang includes OGLCompiler, OSDependent, MachineIndependent + glslang + SPIRV) +else() + find_package(glslang CONFIG REQUIRED) + + target_include_directories( + test_kompute PRIVATE + ${GLSLANG_GENERATED_INCLUDEDIR}) + + target_link_libraries(test_kompute + # Not including hlsl support + # glslang::HLSL + # Adding explicit dependencies to match above + glslang::glslang + glslang::SPIRV) +endif() + + ##################################################### #################### CODECOV ####################### ##################################################### diff --git a/test/TestAsyncOperations.cpp b/test/TestAsyncOperations.cpp index 2e3edcd3..1c5fe277 100644 --- a/test/TestAsyncOperations.cpp +++ b/test/TestAsyncOperations.cpp @@ -5,6 +5,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestAsyncOperations, TestManagerParallelExecution) { // This test is built for NVIDIA 1650. It assumes: @@ -37,7 +39,7 @@ TEST(TestAsyncOperations, TestManagerParallelExecution) } )"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::vector data(size, 0.0); std::vector resultSync(size, 100000000); @@ -145,7 +147,7 @@ TEST(TestAsyncOperations, TestManagerAsyncExecution) } )"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::vector data(size, 0.0); std::vector resultAsync(size, 100000000); diff --git a/test/TestDestroy.cpp b/test/TestDestroy.cpp index 36127f84..72225f9c 100644 --- a/test/TestDestroy.cpp +++ b/test/TestDestroy.cpp @@ -3,6 +3,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestDestroy, TestDestroyTensorSingle) { std::shared_ptr> tensorA = nullptr; @@ -16,7 +18,7 @@ TEST(TestDestroy, TestDestroyTensorSingle) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); { std::shared_ptr sq = nullptr; @@ -58,7 +60,7 @@ TEST(TestDestroy, TestDestroyTensorVector) pa[index] = pa[index] + 1; pb[index] = pb[index] + 2; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); { std::shared_ptr sq = nullptr; @@ -103,7 +105,7 @@ TEST(TestDestroy, TestDestroySequenceSingle) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); { std::shared_ptr sq = nullptr; diff --git a/test/TestMultipleAlgoExecutions.cpp b/test/TestMultipleAlgoExecutions.cpp index df4af2f0..2206e126 100644 --- a/test/TestMultipleAlgoExecutions.cpp +++ b/test/TestMultipleAlgoExecutions.cpp @@ -3,6 +3,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) { @@ -51,7 +53,7 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) kp::Constants pushConstsB({ 3.0 }); auto algorithm = mgr.algorithm(params, - kp::Shader::compileSource(shader), + kp_test_utils::Shader::compileSource(shader), workgroup, specConsts, pushConstsA); @@ -89,7 +91,7 @@ TEST(TestMultipleAlgoExecutions, SingleSequenceRecord) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); { // A sharedMemoryBarrier is required as the shader is not thread-safe:w @@ -130,7 +132,7 @@ TEST(TestMultipleAlgoExecutions, MultipleCmdBufRecords) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr algorithm = mgr.algorithm({ tensorA }, spirv); @@ -166,7 +168,7 @@ TEST(TestMultipleAlgoExecutions, MultipleSequences) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr algorithm = mgr.algorithm({ tensorA }, spirv); @@ -201,7 +203,7 @@ TEST(TestMultipleAlgoExecutions, SingleRecordMultipleEval) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr algorithm = mgr.algorithm({ tensorA }, spirv); diff --git a/test/TestOpShadersFromStringAndFile.cpp b/test/TestOpShadersFromStringAndFile.cpp index bf2ed858..cfbd9b00 100644 --- a/test/TestOpShadersFromStringAndFile.cpp +++ b/test/TestOpShadersFromStringAndFile.cpp @@ -5,6 +5,8 @@ #include "kompute_test/shaders/shadertest_op_custom_shader.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestOpAlgoCreate, ShaderRawDataFromConstructor) { kp::Manager mgr; @@ -27,7 +29,7 @@ TEST(TestOpAlgoCreate, ShaderRawDataFromConstructor) } )"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::vector> params = { tensorA, tensorB }; diff --git a/test/TestOpTensorCopy.cpp b/test/TestOpTensorCopy.cpp index 6978eeee..1cd87fc7 100644 --- a/test/TestOpTensorCopy.cpp +++ b/test/TestOpTensorCopy.cpp @@ -3,6 +3,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestOpTensorCopy, CopyDeviceToDeviceTensor) { diff --git a/test/TestPushConstant.cpp b/test/TestPushConstant.cpp index 66f798af..51985ef1 100644 --- a/test/TestPushConstant.cpp +++ b/test/TestPushConstant.cpp @@ -2,6 +2,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + #include "fmt/ranges.h" TEST(TestPushConstants, TestConstantsAlgoDispatchOverride) @@ -22,7 +24,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchOverride) pa[2] += pcs.z; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr sq = nullptr; @@ -67,7 +69,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchNoOverride) pa[2] += pcs.z; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr sq = nullptr; @@ -112,7 +114,7 @@ TEST(TestPushConstants, TestConstantsWrongSize) pa[2] += pcs.z; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr sq = nullptr; diff --git a/test/TestSequence.cpp b/test/TestSequence.cpp index 19d96c89..c1f5734d 100644 --- a/test/TestSequence.cpp +++ b/test/TestSequence.cpp @@ -3,6 +3,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestSequence, SequenceDestructorViaManager) { std::shared_ptr sq = nullptr; @@ -66,7 +68,7 @@ TEST(TestSequence, RerecordSequence) sq->eval({ tensorA, tensorB, tensorOut }); - std::vector spirv = kp::Shader::compileSource(R"( + std::vector spirv = kp_test_utils::Shader::compileSource(R"( #version 450 layout (local_size_x = 1) in; @@ -116,7 +118,7 @@ TEST(TestSequence, SequenceTimestamps) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); auto seq = mgr.sequence(0, 100); // 100 timestamps seq->record({ tensorA }) diff --git a/test/TestShaderResources.cpp b/test/TestShaderResources.cpp index 6faddb39..41a979f6 100644 --- a/test/TestShaderResources.cpp +++ b/test/TestShaderResources.cpp @@ -2,6 +2,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + static const std::string shaderString = (R"( #version 450 @@ -28,7 +30,7 @@ void compileShaderWithGivenResources(const std::string shaderString, const TBuiltInResource resources) { - kp::Shader::compileSource( + kp_test_utils::Shader::compileSource( shaderString, std::string("main"), std::vector>({}), @@ -37,7 +39,7 @@ compileShaderWithGivenResources(const std::string shaderString, TEST(TestShaderResources, TestNoMaxLight) { - TBuiltInResource noMaxLightResources = kp::Shader::defaultResource; + TBuiltInResource noMaxLightResources = kp_test_utils::Shader::defaultResource; noMaxLightResources.maxLights = 0; EXPECT_NO_THROW( @@ -47,7 +49,7 @@ TEST(TestShaderResources, TestNoMaxLight) TEST(TestShaderResources, TestSmallComputeWorkGroupSizeX) { TBuiltInResource smallComputeWorkGroupSizeXResources = - kp::Shader::defaultResource; + kp_test_utils::Shader::defaultResource; smallComputeWorkGroupSizeXResources.maxComputeWorkGroupSizeX = 0; ASSERT_THROW(compileShaderWithGivenResources( @@ -57,7 +59,7 @@ TEST(TestShaderResources, TestSmallComputeWorkGroupSizeX) TEST(TestShaderResources, TestNoWhileLoopLimit) { - TBuiltInResource noWhileLoopLimitResources = kp::Shader::defaultResource; + TBuiltInResource noWhileLoopLimitResources = kp_test_utils::Shader::defaultResource; noWhileLoopLimitResources.limits.whileLoops = 0; ASSERT_THROW( diff --git a/test/TestSpecializationConstant.cpp b/test/TestSpecializationConstant.cpp index 7654c8aa..2279b46b 100644 --- a/test/TestSpecializationConstant.cpp +++ b/test/TestSpecializationConstant.cpp @@ -2,6 +2,8 @@ #include "kompute/Kompute.hpp" +#include "kompute_test/Shader.hpp" + TEST(TestSpecializationConstants, TestTwoConstants) { { @@ -18,7 +20,7 @@ TEST(TestSpecializationConstants, TestTwoConstants) pb[index] = cTwo; })"); - std::vector spirv = kp::Shader::compileSource(shader); + std::vector spirv = kp_test_utils::Shader::compileSource(shader); std::shared_ptr sq = nullptr; diff --git a/src/Shader.cpp b/test/utils/kompute_test/Shader.cpp similarity index 97% rename from src/Shader.cpp rename to test/utils/kompute_test/Shader.cpp index 293752a9..2fb9209a 100644 --- a/src/Shader.cpp +++ b/test/utils/kompute_test/Shader.cpp @@ -1,8 +1,9 @@ -#if !defined(KOMPUTE_DISABLE_SHADER_UTILS) || !KOMPUTE_DISABLE_SHADER_UTILS -#include "kompute/Shader.hpp" +#include "kompute/Kompute.hpp" -namespace kp { +#include "kompute_test/Shader.hpp" + +namespace kp_test_utils { std::vector Shader::compileSources( @@ -215,4 +216,3 @@ const TBuiltInResource Shader::defaultResource = { }; } -#endif // DKOMPUTE_DISABLE_SHADER_UTILS diff --git a/src/include/kompute/Shader.hpp b/test/utils/kompute_test/Shader.hpp similarity index 94% rename from src/include/kompute/Shader.hpp rename to test/utils/kompute_test/Shader.hpp index d670d63a..a24234e3 100644 --- a/src/include/kompute/Shader.hpp +++ b/test/utils/kompute_test/Shader.hpp @@ -1,6 +1,5 @@ #pragma once -#if !defined(KOMPUTE_DISABLE_SHADER_UTILS) || !KOMPUTE_DISABLE_SHADER_UTILS #include #include @@ -13,9 +12,7 @@ #include #include -#include "kompute/Core.hpp" - -namespace kp { +namespace kp_test_utils { /** Shader utily class with functions to compile and process glsl files. @@ -72,4 +69,3 @@ class Shader }; } -#endif // DKOMPUTE_DISABLE_SHADER_UTILS From 598c843545e5460f5f773550cf04de7ecc5c39cb Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Tue, 20 Jul 2021 21:53:50 +0100 Subject: [PATCH 2/8] Updated glslang as core dependency --- .gitignore | 2 +- .gitmodules | 4 - Makefile | 2 +- python/test/requirements-dev.txt | 1 + test/CMakeLists.txt | 35 +---- test/TestShaderResources.cpp | 68 --------- test/utils/kompute_test/Shader.cpp | 219 ++--------------------------- test/utils/kompute_test/Shader.hpp | 54 +------ 8 files changed, 23 insertions(+), 362 deletions(-) delete mode 100644 test/TestShaderResources.cpp diff --git a/.gitignore b/.gitignore index 4d67efdd..f72d9ff7 100644 --- a/.gitignore +++ b/.gitignore @@ -186,5 +186,5 @@ release/ # Kompute swiftshader/ vk_swiftshader_icd.json - +tmp_kp_shader.comp.spv diff --git a/.gitmodules b/.gitmodules index eeb2b007..3c710d61 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,10 +14,6 @@ path = python/pybind11 url = https://github.com/pybind/pybind11 branch = v2.6.1 -[submodule "external/glslang"] - path = external/glslang - url = https://github.com/KhronosGroup/glslang/ - branch = 11.1.0 [submodule "external/fmt"] path = external/fmt url = https://github.com/fmtlib/fmt diff --git a/Makefile b/Makefile index f9498f8a..c912f95c 100644 --- a/Makefile +++ b/Makefile @@ -194,7 +194,7 @@ build_single_header: "single_include/kompute/Kompute.hpp" win_build_xxd: - cd external/bin/ && gcc -o xxd.exe xxd.c -DCYGWIN + cd external/bin/ && gcc.exe -o xxd.exe xxd.c -DCYGWIN format: $(CLANG_FORMAT_BIN) -i -style="{BasedOnStyle: mozilla, IndentWidth: 4}" src/*.cpp src/include/kompute/*.hpp test/*cpp diff --git a/python/test/requirements-dev.txt b/python/test/requirements-dev.txt index 8d5576a7..e44a9b53 100644 --- a/python/test/requirements-dev.txt +++ b/python/test/requirements-dev.txt @@ -1,3 +1,4 @@ pyshader==0.7.0 numpy==1.19.5 pytest==6.2.1 +pyshaderc==1.1.2 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8c4731a7..ebe565b1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -32,7 +32,7 @@ if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) target_link_libraries(test_kompute gtest_main) else() - target_link_libraries(test_kompute PRIVATE + target_link_libraries(test_kompute GTest::gtest) endif() @@ -40,39 +40,6 @@ target_link_libraries(test_kompute kompute) add_test(NAME test_kompute COMMAND test_kompute) -##################################################### -#################### GLSLANG ####################### -##################################################### - -if(KOMPUTE_OPT_REPO_SUBMODULE_BUILD) - add_subdirectory(${PROJECT_SOURCE_DIR}/external/glslang - ${CMAKE_CURRENT_BINARY_DIR}/kompute_glslang) - - target_include_directories( - test_kompute PRIVATE - ${PROJECT_SOURCE_DIR}/external/glslang) - - target_link_libraries(test_kompute - # Not including hlsl support - # HLSL - # glslang includes OGLCompiler, OSDependent, MachineIndependent - glslang - SPIRV) -else() - find_package(glslang CONFIG REQUIRED) - - target_include_directories( - test_kompute PRIVATE - ${GLSLANG_GENERATED_INCLUDEDIR}) - - target_link_libraries(test_kompute - # Not including hlsl support - # glslang::HLSL - # Adding explicit dependencies to match above - glslang::glslang - glslang::SPIRV) -endif() - ##################################################### #################### CODECOV ####################### diff --git a/test/TestShaderResources.cpp b/test/TestShaderResources.cpp deleted file mode 100644 index 41a979f6..00000000 --- a/test/TestShaderResources.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "gtest/gtest.h" - -#include "kompute/Kompute.hpp" - -#include "kompute_test/Shader.hpp" - -static const std::string shaderString = (R"( - #version 450 - - layout (local_size_x = 1) in; - - // The input tensors bind index is relative to index in parameter passed - layout(set = 0, binding = 0) buffer bina { float tina[]; }; - layout(set = 0, binding = 1) buffer binb { float tinb[]; }; - layout(set = 0, binding = 2) buffer bout { float tout[]; }; - - void main() { - uint index = gl_GlobalInvocationID.x; - - int i = 1; - - while(i < 200) { - tout[index] += tina[index] * tinb[index]; - i++; - } - } -)"); - -void -compileShaderWithGivenResources(const std::string shaderString, - const TBuiltInResource resources) -{ - kp_test_utils::Shader::compileSource( - shaderString, - std::string("main"), - std::vector>({}), - resources); -} - -TEST(TestShaderResources, TestNoMaxLight) -{ - TBuiltInResource noMaxLightResources = kp_test_utils::Shader::defaultResource; - noMaxLightResources.maxLights = 0; - - EXPECT_NO_THROW( - compileShaderWithGivenResources(shaderString, noMaxLightResources)); -} - -TEST(TestShaderResources, TestSmallComputeWorkGroupSizeX) -{ - TBuiltInResource smallComputeWorkGroupSizeXResources = - kp_test_utils::Shader::defaultResource; - smallComputeWorkGroupSizeXResources.maxComputeWorkGroupSizeX = 0; - - ASSERT_THROW(compileShaderWithGivenResources( - shaderString, smallComputeWorkGroupSizeXResources), - std::runtime_error); -} - -TEST(TestShaderResources, TestNoWhileLoopLimit) -{ - TBuiltInResource noWhileLoopLimitResources = kp_test_utils::Shader::defaultResource; - noWhileLoopLimitResources.limits.whileLoops = 0; - - ASSERT_THROW( - compileShaderWithGivenResources(shaderString, noWhileLoopLimitResources), - std::runtime_error); -} diff --git a/test/utils/kompute_test/Shader.cpp b/test/utils/kompute_test/Shader.cpp index 2fb9209a..a4791432 100644 --- a/test/utils/kompute_test/Shader.cpp +++ b/test/utils/kompute_test/Shader.cpp @@ -5,214 +5,23 @@ namespace kp_test_utils { -std::vector -Shader::compileSources( - const std::vector& sources, - const std::vector& files, - const std::string& entryPoint, - std::vector> definitions, - const TBuiltInResource& resources) -{ - - // Initialize glslang library. - glslang::InitializeProcess(); - - // Currently we don't support other shader types nor plan to - const EShLanguage language = EShLangCompute; - glslang::TShader shader(language); - - std::vector filesCStr(files.size()), - sourcesCStr(sources.size()); - for (size_t i = 0; i < sources.size(); i++) - sourcesCStr[i] = sources[i].c_str(); - - if (files.size() > 1) { - assert(files.size() == sources.size()); - for (size_t i = 0; i < files.size(); i++) - filesCStr[i] = files[i].c_str(); - shader.setStringsWithLengthsAndNames( - sourcesCStr.data(), nullptr, filesCStr.data(), filesCStr.size()); - } else { - filesCStr = { "" }; - shader.setStringsWithLengthsAndNames( - sourcesCStr.data(), nullptr, filesCStr.data(), sourcesCStr.size()); - } - - shader.setEntryPoint(entryPoint.c_str()); - shader.setSourceEntryPoint(entryPoint.c_str()); - - std::string info_log = ""; - const EShMessages messages = static_cast( - EShMsgDefault | EShMsgVulkanRules | EShMsgSpvRules); - if (!shader.parse(&resources, 100, false, messages)) { - info_log = std::string(shader.getInfoLog()) + "\n" + - std::string(shader.getInfoDebugLog()); - KP_LOG_ERROR("Kompute Shader Error: {}", info_log); - throw std::runtime_error(info_log); - } - - // Add shader to new program object. - glslang::TProgram program; - program.addShader(&shader); - // Link program. - if (!program.link(messages)) { - info_log = std::string(program.getInfoLog()) + "\n" + - std::string(program.getInfoDebugLog()); - KP_LOG_ERROR("Kompute Shader Error: {}", info_log); - throw std::runtime_error(info_log); - } - - // Save any info log that was generated. - if (shader.getInfoLog()) { - info_log += std::string(shader.getInfoLog()) + "\n" + - std::string(shader.getInfoDebugLog()) + "\n"; - KP_LOG_INFO("Kompute Shader Information: {}", info_log); - } - - glslang::TIntermediate* intermediate = program.getIntermediate(language); - // Translate to SPIRV. - if (!intermediate) { - info_log += "Failed to get shared intermediate code.\n"; - KP_LOG_ERROR("Kompute Shader Error: {}", info_log); - throw std::runtime_error(info_log); - } - - spv::SpvBuildLogger logger; - std::vector spirv; - glslang::GlslangToSpv(*intermediate, spirv, &logger); - - if (shader.getInfoLog()) { - info_log += logger.getAllMessages() + "\n"; - KP_LOG_DEBUG("Kompute Shader all result messages: {}", info_log); - } - - // Shutdown glslang library. - glslang::FinalizeProcess(); - - return spirv; -} - std::vector Shader::compileSource( - const std::string& source, - const std::string& entryPoint, - std::vector> definitions, - const TBuiltInResource& resource) + const std::string& source) { - return compileSources({ source }, - std::vector({}), - entryPoint, - definitions, - resource); + std::string cmd = "glslc -fshader-stage=compute -o tmp_kp_shader.comp.spv - << END\n" + source + "\nEND"; + system(cmd.c_str()); + + std::ifstream fileStream("tmp_kp_shader.comp.spv", + std::ios::binary | std::ios::in | std::ios::ate); + + size_t shaderFileSize = fileStream.tellg(); + fileStream.seekg(0, std::ios::beg); + char* shaderDataRaw = new char[shaderFileSize]; + fileStream.read(shaderDataRaw, shaderFileSize); + fileStream.close(); + std::vector tmpResult = {(uint32_t*)shaderDataRaw, (uint32_t*)(shaderDataRaw + shaderFileSize)}; + return tmpResult; } -const TBuiltInResource Shader::defaultResource = { - /* .MaxLights = */ 0, - /* .MaxClipPlanes = */ 0, - /* .MaxTextureUnits = */ 0, - /* .MaxTextureCoords = */ 0, - /* .MaxVertexAttribs = */ 64, - /* .MaxVertexUniformComponents = */ 4096, - /* .MaxVaryingFloats = */ 64, - /* .MaxVertexTextureImageUnits = */ 0, - /* .MaxCombinedTextureImageUnits = */ 0, - /* .MaxTextureImageUnits = */ 0, - /* .MaxFragmentUniformComponents = */ 0, - /* .MaxDrawBuffers = */ 0, - /* .MaxVertexUniformVectors = */ 128, - /* .MaxVaryingVectors = */ 8, - /* .MaxFragmentUniformVectors = */ 0, - /* .MaxVertexOutputVectors = */ 16, - /* .MaxFragmentInputVectors = */ 0, - /* .MinProgramTexelOffset = */ -8, - /* .MaxProgramTexelOffset = */ 7, - /* .MaxClipDistances = */ 8, - /* .MaxComputeWorkGroupCountX = */ 65535, - /* .MaxComputeWorkGroupCountY = */ 65535, - /* .MaxComputeWorkGroupCountZ = */ 65535, - /* .MaxComputeWorkGroupSizeX = */ 1024, - /* .MaxComputeWorkGroupSizeY = */ 1024, - /* .MaxComputeWorkGroupSizeZ = */ 64, - /* .MaxComputeUniformComponents = */ 1024, - /* .MaxComputeTextureImageUnits = */ 16, - /* .MaxComputeImageUniforms = */ 8, - /* .MaxComputeAtomicCounters = */ 8, - /* .MaxComputeAtomicCounterBuffers = */ 1, - /* .MaxVaryingComponents = */ 60, - /* .MaxVertexOutputComponents = */ 64, - /* .MaxGeometryInputComponents = */ 64, - /* .MaxGeometryOutputComponents = */ 128, - /* .MaxFragmentInputComponents = */ 0, - /* .MaxImageUnits = */ 0, - /* .MaxCombinedImageUnitsAndFragmentOutputs = */ 0, - /* .MaxCombinedShaderOutputResources = */ 8, - /* .MaxImageSamples = */ 0, - /* .MaxVertexImageUniforms = */ 0, - /* .MaxTessControlImageUniforms = */ 0, - /* .MaxTessEvaluationImageUniforms = */ 0, - /* .MaxGeometryImageUniforms = */ 0, - /* .MaxFragmentImageUniforms = */ 0, - /* .MaxCombinedImageUniforms = */ 0, - /* .MaxGeometryTextureImageUnits = */ 0, - /* .MaxGeometryOutputVertices = */ 256, - /* .MaxGeometryTotalOutputComponents = */ 1024, - /* .MaxGeometryUniformComponents = */ 1024, - /* .MaxGeometryVaryingComponents = */ 64, - /* .MaxTessControlInputComponents = */ 128, - /* .MaxTessControlOutputComponents = */ 128, - /* .MaxTessControlTextureImageUnits = */ 0, - /* .MaxTessControlUniformComponents = */ 1024, - /* .MaxTessControlTotalOutputComponents = */ 4096, - /* .MaxTessEvaluationInputComponents = */ 128, - /* .MaxTessEvaluationOutputComponents = */ 128, - /* .MaxTessEvaluationTextureImageUnits = */ 16, - /* .MaxTessEvaluationUniformComponents = */ 1024, - /* .MaxTessPatchComponents = */ 120, - /* .MaxPatchVertices = */ 32, - /* .MaxTessGenLevel = */ 64, - /* .MaxViewports = */ 16, - /* .MaxVertexAtomicCounters = */ 0, - /* .MaxTessControlAtomicCounters = */ 0, - /* .MaxTessEvaluationAtomicCounters = */ 0, - /* .MaxGeometryAtomicCounters = */ 0, - /* .MaxFragmentAtomicCounters = */ 0, - /* .MaxCombinedAtomicCounters = */ 8, - /* .MaxAtomicCounterBindings = */ 1, - /* .MaxVertexAtomicCounterBuffers = */ 0, - /* .MaxTessControlAtomicCounterBuffers = */ 0, - /* .MaxTessEvaluationAtomicCounterBuffers = */ 0, - /* .MaxGeometryAtomicCounterBuffers = */ 0, - /* .MaxFragmentAtomicCounterBuffers = */ 0, - /* .MaxCombinedAtomicCounterBuffers = */ 1, - /* .MaxAtomicCounterBufferSize = */ 16384, - /* .MaxTransformFeedbackBuffers = */ 4, - /* .MaxTransformFeedbackInterleavedComponents = */ 64, - /* .MaxCullDistances = */ 8, - /* .MaxCombinedClipAndCullDistances = */ 8, - /* .MaxSamples = */ 4, - /* .maxMeshOutputVerticesNV = */ 256, - /* .maxMeshOutputPrimitivesNV = */ 512, - /* .maxMeshWorkGroupSizeX_NV = */ 32, - /* .maxMeshWorkGroupSizeY_NV = */ 1, - /* .maxMeshWorkGroupSizeZ_NV = */ 1, - /* .maxTaskWorkGroupSizeX_NV = */ 32, - /* .maxTaskWorkGroupSizeY_NV = */ 1, - /* .maxTaskWorkGroupSizeZ_NV = */ 1, - /* .maxMeshViewCountNV = */ 4, - /* .maxDualSourceDrawBuffersEXT = */ 1, - - /* .limits = */ - { - /* .nonInductiveForLoops = */ 1, - /* .whileLoops = */ 1, - /* .doWhileLoops = */ 1, - /* .generalUniformIndexing = */ 1, - /* .generalAttributeMatrixVectorIndexing = */ 1, - /* .generalVaryingIndexing = */ 1, - /* .generalSamplerIndexing = */ 1, - /* .generalVariableIndexing = */ 1, - /* .generalConstantMatrixVectorIndexing = */ 1, - } -}; - } diff --git a/test/utils/kompute_test/Shader.hpp b/test/utils/kompute_test/Shader.hpp index a24234e3..b6df40dc 100644 --- a/test/utils/kompute_test/Shader.hpp +++ b/test/utils/kompute_test/Shader.hpp @@ -3,15 +3,6 @@ #include #include -#ifdef USE_EXTERNAL_GLSLANG -#include -#else -#include -#endif - -#include -#include - namespace kp_test_utils { /** @@ -20,52 +11,17 @@ namespace kp_test_utils { class Shader { public: - // The default resource limit for the GLSL compiler, can be overwritten - // Has been adopted by: - // https://github.com/KhronosGroup/glslang/blob/master/StandAlone/ResourceLimits.cpp - const static TBuiltInResource defaultResource; - - /** - * Compile multiple sources with optional filenames. Currently this function - * uses the glslang C++ interface which is not thread safe so this funciton - * should not be called from multiple threads concurrently. If you have a - * online shader processing multithreading use-case that can't use offline - * compilation please open an issue. - * - * @param sources A list of raw glsl shaders in string format - * @param files A list of file names respective to each of the sources - * @param entryPoint The function name to use as entry point - * @param definitions List of pairs containing key value definitions - * @param resourcesLimit A list that contains the resource limits for the - * GLSL compiler - * @return The compiled SPIR-V binary in unsigned int32 format - */ - static std::vector compileSources( - const std::vector& sources, - const std::vector& files = {}, - const std::string& entryPoint = "main", - std::vector> definitions = {}, - const TBuiltInResource& resources = Shader::defaultResource); - /** - * Compile a single glslang source from string value. Currently this - * function uses the glslang C++ interface which is not thread safe so this - * funciton should not be called from multiple threads concurrently. If you - * have a online shader processing multithreading use-case that can't use - * offline compilation please open an issue. + * Compile a single glslang source from string value. This is only meant + * to be used for testing as it's non threadsafe, and it had to be removed + * from the glslang dependency and now can only run the CLI directly due to + * license issues: see https://github.com/EthicalML/vulkan-kompute/pull/235 * * @param source An individual raw glsl shader in string format - * @param entryPoint The function name to use as entry point - * @param definitions List of pairs containing key value definitions - * @param resourcesLimit A list that contains the resource limits for the - * GLSL compiler * @return The compiled SPIR-V binary in unsigned int32 format */ static std::vector compileSource( - const std::string& source, - const std::string& entryPoint = "main", - std::vector> definitions = {}, - const TBuiltInResource& resources = Shader::defaultResource); + const std::string& source); }; } From 7fdb8687903821a48f25f32041204e7a066f0a3b Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Wed, 21 Jul 2021 08:08:05 +0100 Subject: [PATCH 3/8] Updated to simplify logic of shader cli --- test/utils/kompute_test/Shader.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/test/utils/kompute_test/Shader.cpp b/test/utils/kompute_test/Shader.cpp index a4791432..0e551747 100644 --- a/test/utils/kompute_test/Shader.cpp +++ b/test/utils/kompute_test/Shader.cpp @@ -9,19 +9,11 @@ std::vector Shader::compileSource( const std::string& source) { - std::string cmd = "glslc -fshader-stage=compute -o tmp_kp_shader.comp.spv - << END\n" + source + "\nEND"; - system(cmd.c_str()); - - std::ifstream fileStream("tmp_kp_shader.comp.spv", - std::ios::binary | std::ios::in | std::ios::ate); - - size_t shaderFileSize = fileStream.tellg(); - fileStream.seekg(0, std::ios::beg); - char* shaderDataRaw = new char[shaderFileSize]; - fileStream.read(shaderDataRaw, shaderFileSize); - fileStream.close(); - std::vector tmpResult = {(uint32_t*)shaderDataRaw, (uint32_t*)(shaderDataRaw + shaderFileSize)}; - return tmpResult; + system(std::string("glslc -fshader-stage=compute -o tmp_kp_shader.comp.spv - << END\n" + source + "\nEND").c_str()); + std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); + std::vector buffer; + buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); + return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; } } From 71b08d9f580e278a28f52a15ac6b476d15849308 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Wed, 21 Jul 2021 08:15:49 +0100 Subject: [PATCH 4/8] Updated glslang as core dependency --- external/glslang | 1 - 1 file changed, 1 deletion(-) delete mode 160000 external/glslang diff --git a/external/glslang b/external/glslang deleted file mode 160000 index c594de23..00000000 --- a/external/glslang +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c594de23cdd790d64ad5f9c8b059baae0ee2941d From b7fd1b4d7977993e3c5725a37fbe6522566c0c76 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Wed, 21 Jul 2021 18:16:29 +0100 Subject: [PATCH 5/8] Updated python and cpp with working gslslangvalidator --- .github/workflows/cpp_tests.yml | 2 +- .github/workflows/python_tests.yml | 2 +- docker-builders/KomputeBuilder.Dockerfile | 7 ++++++- docker-builders/Makefile | 4 ++-- python/test/__init__.py | 0 python/test/requirements-dev.txt | 1 - python/test/test_kompute.py | 12 ++++++------ python/test/test_tensor_types.py | 14 +++++++------- python/test/utils.py | 7 +++++++ test/utils/kompute_test/Shader.cpp | 3 ++- 10 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 python/test/__init__.py create mode 100644 python/test/utils.py diff --git a/.github/workflows/cpp_tests.yml b/.github/workflows/cpp_tests.yml index ecb0d419..53e0c94d 100644 --- a/.github/workflows/cpp_tests.yml +++ b/.github/workflows/cpp_tests.yml @@ -10,7 +10,7 @@ jobs: cpp-tests: runs-on: ubuntu-18.04 - container: axsauze/kompute-builder:0.2 + container: axsauze/kompute-builder:0.3 steps: - uses: actions/checkout@v2 diff --git a/.github/workflows/python_tests.yml b/.github/workflows/python_tests.yml index 1bd60ac2..e241979f 100644 --- a/.github/workflows/python_tests.yml +++ b/.github/workflows/python_tests.yml @@ -10,7 +10,7 @@ jobs: python-tests: runs-on: ubuntu-18.04 - container: axsauze/kompute-builder:0.2 + container: axsauze/kompute-builder:0.3 steps: - uses: actions/checkout@v2 diff --git a/docker-builders/KomputeBuilder.Dockerfile b/docker-builders/KomputeBuilder.Dockerfile index 4cb31c9f..a426be55 100644 --- a/docker-builders/KomputeBuilder.Dockerfile +++ b/docker-builders/KomputeBuilder.Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:18.04 +FROM ubuntu:20.04 # Base packages from default ppa RUN apt-get update -y @@ -26,9 +26,14 @@ RUN apt-get install -y libxext-dev COPY --from=axsauze/swiftshader:0.1 /swiftshader/ /swiftshader/ +# GLSLANG tools for tests +RUN apt-get install -y glslang-tools + # Setup Python RUN apt-get install -y python3-pip +RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 10 + RUN mkdir builder WORKDIR /builder diff --git a/docker-builders/Makefile b/docker-builders/Makefile index ef79b4df..8ded9ed2 100644 --- a/docker-builders/Makefile +++ b/docker-builders/Makefile @@ -1,9 +1,9 @@ build_kompute_builder: - docker build .. -f KomputeBuilder.Dockerfile -t axsauze/kompute-builder:0.2 + docker build .. -f KomputeBuilder.Dockerfile -t axsauze/kompute-builder:0.3 push_kompute_builder: build_kompute_builder - docker push axsauze/kompute-builder:0.2 + docker push axsauze/kompute-builder:0.3 build_swiftshader: docker build .. -f Swiftshader.Dockerfile -t axsauze/swiftshader:0.1 diff --git a/python/test/__init__.py b/python/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/test/requirements-dev.txt b/python/test/requirements-dev.txt index e44a9b53..8d5576a7 100644 --- a/python/test/requirements-dev.txt +++ b/python/test/requirements-dev.txt @@ -1,4 +1,3 @@ pyshader==0.7.0 numpy==1.19.5 pytest==6.2.1 -pyshaderc==1.1.2 diff --git a/python/test/test_kompute.py b/python/test/test_kompute.py index 9dd8040d..eaf6b28d 100644 --- a/python/test/test_kompute.py +++ b/python/test/test_kompute.py @@ -5,7 +5,7 @@ import logging import pyshader as ps -import pyshaderc +from .utils import compile_source DIRNAME = os.path.dirname(os.path.abspath(__file__)) @@ -55,7 +55,7 @@ def test_end_to_end(): push_consts_a = [2] push_consts_b = [3] - algo = mgr.algorithm(params, pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp"), workgroup, spec_consts, push_consts_a) + algo = mgr.algorithm(params, compile_source(shader), workgroup, spec_consts, push_consts_a) (mgr.sequence() .record(kp.OpTensorSyncDevice(params)) @@ -91,7 +91,7 @@ def test_shader_str(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) mgr = kp.Manager() @@ -131,7 +131,7 @@ def test_sequence(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) mgr = kp.Manager(0) @@ -171,7 +171,7 @@ def test_sequence(): def test_pushconsts(): - spirv = pyshaderc.compile_into_spirv(""" + spirv = compile_source(""" #version 450 layout(push_constant) uniform PushConstants { float x; @@ -185,7 +185,7 @@ def test_pushconsts(): pa[1] += pcs.y; pa[2] += pcs.z; } - """.encode("utf-8"), "comp", "shader.comp") + """) mgr = kp.Manager() diff --git a/python/test/test_tensor_types.py b/python/test/test_tensor_types.py index e2a71250..379346f2 100644 --- a/python/test/test_tensor_types.py +++ b/python/test/test_tensor_types.py @@ -1,10 +1,10 @@ -import pyshader as ps -import pyshaderc import os import pytest import kp import numpy as np +from .utils import compile_source + VK_ICD_FILENAMES = os.environ.get("VK_ICD_FILENAMES", "") def test_type_float(): @@ -23,7 +23,7 @@ def test_type_float(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) arr_in_a = np.array([123., 153., 231.], dtype=np.float32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.float32) @@ -62,7 +62,7 @@ def test_type_float_double_incorrect(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) arr_in_a = np.array([123., 153., 231.], dtype=np.float32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.uint32) @@ -104,7 +104,7 @@ def test_type_double(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) arr_in_a = np.array([123., 153., 231.], dtype=np.float64) arr_in_b = np.array([9482, 1208, 1238], dtype=np.float64) @@ -144,7 +144,7 @@ def test_type_int(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) arr_in_a = np.array([123, 153, 231], dtype=np.int32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.int32) @@ -184,7 +184,7 @@ def test_type_unsigned_int(): } """ - spirv = pyshaderc.compile_into_spirv(shader.encode("utf-8"), "comp", "shader.comp") + spirv = compile_source(shader) arr_in_a = np.array([123, 153, 231], dtype=np.uint32) arr_in_b = np.array([9482, 1208, 1238], dtype=np.uint32) diff --git a/python/test/utils.py b/python/test/utils.py new file mode 100644 index 00000000..4a711ba1 --- /dev/null +++ b/python/test/utils.py @@ -0,0 +1,7 @@ +import os + + +def compile_source(source): + os.system("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND") + return open("tmp_kp_shader.comp.spv", "rb").read() + diff --git a/test/utils/kompute_test/Shader.cpp b/test/utils/kompute_test/Shader.cpp index 0e551747..691c9715 100644 --- a/test/utils/kompute_test/Shader.cpp +++ b/test/utils/kompute_test/Shader.cpp @@ -9,7 +9,8 @@ std::vector Shader::compileSource( const std::string& source) { - system(std::string("glslc -fshader-stage=compute -o tmp_kp_shader.comp.spv - << END\n" + source + "\nEND").c_str()); + if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND").c_str())) + throw std::runtime_error("Error running glslangValidator command"); std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); std::vector buffer; buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); From 15346ee5052f4a39881f65e93edcad18bc6157bd Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Wed, 21 Jul 2021 20:30:21 +0100 Subject: [PATCH 6/8] Updated tests to not use namespaace for compileShader --- test/CMakeLists.txt | 1 - test/TestAsyncOperations.cpp | 4 +-- test/TestDestroy.cpp | 6 ++-- test/TestMultipleAlgoExecutions.cpp | 10 +++---- test/TestOpShadersFromStringAndFile.cpp | 2 +- test/TestPushConstant.cpp | 6 ++-- test/TestSequence.cpp | 4 +-- test/TestSpecializationConstant.cpp | 2 +- test/utils/kompute_test/Shader.cpp | 20 ------------- test/utils/kompute_test/Shader.hpp | 38 ++++++++++++------------- 10 files changed, 36 insertions(+), 57 deletions(-) delete mode 100644 test/utils/kompute_test/Shader.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ebe565b1..b97e549b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,7 +12,6 @@ endif() file(GLOB test_kompute_CPP "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/utils/kompute_test/*.cpp" ) add_executable(test_kompute ${test_kompute_CPP}) diff --git a/test/TestAsyncOperations.cpp b/test/TestAsyncOperations.cpp index 1c5fe277..7bda7a10 100644 --- a/test/TestAsyncOperations.cpp +++ b/test/TestAsyncOperations.cpp @@ -39,7 +39,7 @@ TEST(TestAsyncOperations, TestManagerParallelExecution) } )"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::vector data(size, 0.0); std::vector resultSync(size, 100000000); @@ -147,7 +147,7 @@ TEST(TestAsyncOperations, TestManagerAsyncExecution) } )"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::vector data(size, 0.0); std::vector resultAsync(size, 100000000); diff --git a/test/TestDestroy.cpp b/test/TestDestroy.cpp index 72225f9c..b415024e 100644 --- a/test/TestDestroy.cpp +++ b/test/TestDestroy.cpp @@ -18,7 +18,7 @@ TEST(TestDestroy, TestDestroyTensorSingle) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); { std::shared_ptr sq = nullptr; @@ -60,7 +60,7 @@ TEST(TestDestroy, TestDestroyTensorVector) pa[index] = pa[index] + 1; pb[index] = pb[index] + 2; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); { std::shared_ptr sq = nullptr; @@ -105,7 +105,7 @@ TEST(TestDestroy, TestDestroySequenceSingle) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); { std::shared_ptr sq = nullptr; diff --git a/test/TestMultipleAlgoExecutions.cpp b/test/TestMultipleAlgoExecutions.cpp index 2206e126..c364d084 100644 --- a/test/TestMultipleAlgoExecutions.cpp +++ b/test/TestMultipleAlgoExecutions.cpp @@ -53,7 +53,7 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) kp::Constants pushConstsB({ 3.0 }); auto algorithm = mgr.algorithm(params, - kp_test_utils::Shader::compileSource(shader), + compileSource(shader), workgroup, specConsts, pushConstsA); @@ -91,7 +91,7 @@ TEST(TestMultipleAlgoExecutions, SingleSequenceRecord) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); { // A sharedMemoryBarrier is required as the shader is not thread-safe:w @@ -132,7 +132,7 @@ TEST(TestMultipleAlgoExecutions, MultipleCmdBufRecords) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr algorithm = mgr.algorithm({ tensorA }, spirv); @@ -168,7 +168,7 @@ TEST(TestMultipleAlgoExecutions, MultipleSequences) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr algorithm = mgr.algorithm({ tensorA }, spirv); @@ -203,7 +203,7 @@ TEST(TestMultipleAlgoExecutions, SingleRecordMultipleEval) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr algorithm = mgr.algorithm({ tensorA }, spirv); diff --git a/test/TestOpShadersFromStringAndFile.cpp b/test/TestOpShadersFromStringAndFile.cpp index cfbd9b00..09b354cc 100644 --- a/test/TestOpShadersFromStringAndFile.cpp +++ b/test/TestOpShadersFromStringAndFile.cpp @@ -29,7 +29,7 @@ TEST(TestOpAlgoCreate, ShaderRawDataFromConstructor) } )"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::vector> params = { tensorA, tensorB }; diff --git a/test/TestPushConstant.cpp b/test/TestPushConstant.cpp index 51985ef1..df098065 100644 --- a/test/TestPushConstant.cpp +++ b/test/TestPushConstant.cpp @@ -24,7 +24,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchOverride) pa[2] += pcs.z; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr sq = nullptr; @@ -69,7 +69,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchNoOverride) pa[2] += pcs.z; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr sq = nullptr; @@ -114,7 +114,7 @@ TEST(TestPushConstants, TestConstantsWrongSize) pa[2] += pcs.z; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr sq = nullptr; diff --git a/test/TestSequence.cpp b/test/TestSequence.cpp index c1f5734d..3e829159 100644 --- a/test/TestSequence.cpp +++ b/test/TestSequence.cpp @@ -68,7 +68,7 @@ TEST(TestSequence, RerecordSequence) sq->eval({ tensorA, tensorB, tensorOut }); - std::vector spirv = kp_test_utils::Shader::compileSource(R"( + std::vector spirv = compileSource(R"( #version 450 layout (local_size_x = 1) in; @@ -118,7 +118,7 @@ TEST(TestSequence, SequenceTimestamps) pa[index] = pa[index] + 1; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); auto seq = mgr.sequence(0, 100); // 100 timestamps seq->record({ tensorA }) diff --git a/test/TestSpecializationConstant.cpp b/test/TestSpecializationConstant.cpp index 2279b46b..79e829f5 100644 --- a/test/TestSpecializationConstant.cpp +++ b/test/TestSpecializationConstant.cpp @@ -20,7 +20,7 @@ TEST(TestSpecializationConstants, TestTwoConstants) pb[index] = cTwo; })"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + std::vector spirv = compileSource(shader); std::shared_ptr sq = nullptr; diff --git a/test/utils/kompute_test/Shader.cpp b/test/utils/kompute_test/Shader.cpp deleted file mode 100644 index 691c9715..00000000 --- a/test/utils/kompute_test/Shader.cpp +++ /dev/null @@ -1,20 +0,0 @@ - -#include "kompute/Kompute.hpp" - -#include "kompute_test/Shader.hpp" - -namespace kp_test_utils { - -std::vector -Shader::compileSource( - const std::string& source) -{ - if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND").c_str())) - throw std::runtime_error("Error running glslangValidator command"); - std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); - std::vector buffer; - buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); - return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; -} - -} diff --git a/test/utils/kompute_test/Shader.hpp b/test/utils/kompute_test/Shader.hpp index b6df40dc..884efe5f 100644 --- a/test/utils/kompute_test/Shader.hpp +++ b/test/utils/kompute_test/Shader.hpp @@ -2,26 +2,26 @@ #include #include - -namespace kp_test_utils { +#include /** - Shader utily class with functions to compile and process glsl files. -*/ -class Shader + * Compile a single glslang source from string value. This is only meant + * to be used for testing as it's non threadsafe, and it had to be removed + * from the glslang dependency and now can only run the CLI directly due to + * license issues: see https://github.com/EthicalML/vulkan-kompute/pull/235 + * + * @param source An individual raw glsl shader in string format + * @return The compiled SPIR-V binary in unsigned int32 format + */ +static +std::vector +compileSource( + const std::string& source) { - public: - /** - * Compile a single glslang source from string value. This is only meant - * to be used for testing as it's non threadsafe, and it had to be removed - * from the glslang dependency and now can only run the CLI directly due to - * license issues: see https://github.com/EthicalML/vulkan-kompute/pull/235 - * - * @param source An individual raw glsl shader in string format - * @return The compiled SPIR-V binary in unsigned int32 format - */ - static std::vector compileSource( - const std::string& source); -}; - + if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND").c_str())) + throw std::runtime_error("Error running glslangValidator command"); + std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); + std::vector buffer; + buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); + return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; } From 96768a33022a32b6259c1ed2185ab4d1281663a4 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Wed, 21 Jul 2021 20:30:38 +0100 Subject: [PATCH 7/8] UPdated examples and documentations to not require glsl deps --- README.md | 6 +++-- docs/overview/advanced-examples.rst | 12 ++++++---- docs/overview/build-system.rst | 2 -- docs/overview/ci-tests.rst | 5 +--- docs/overview/reference.rst | 9 -------- docs/overview/shaders-to-headers.rst | 23 +++++++++++++++++++ examples/array_multiplication/src/Main.cpp | 14 ++++++++++- .../kompute_summator/KomputeSummatorNode.cpp | 18 ++++++++++++--- .../gdnative_shared/src/KomputeSummator.cpp | 14 ++++++++++- examples/neural_network_vgg7/sh_conv.py | 9 +++++++- 10 files changed, 85 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 699928ea..765b6d6e 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,8 @@ void kompute(const std::string& shader) { kp::Constants pushConstsB({ 3.0 }); auto algorithm = mgr.algorithm(params, - kp_test_utils::Shader::compileSource(shader), + // See documentation shader section for compileSource + compileSource(shader), workgroup, specConsts, pushConstsA); @@ -165,6 +166,7 @@ def kompute(shader): push_consts_a = [2] push_consts_b = [3] + # See documentation shader section for compile_source spirv = kp.Shader.compile_source(shader) algo = mgr.algorithm(params, spirv, workgroup, spec_consts, push_consts_a) @@ -372,7 +374,7 @@ You can also access the spirv = kp_test_utils::Shader::compileSource(shader); + // See shader documentation section for compileSource + std::vector spirv = compileSource(shader); std::shared_ptr sq = nullptr; @@ -102,7 +103,8 @@ We also provide tools that allow you to `convert shaders into C++ headers spirv = kp_test_utils::Shader::compileSource(R"( + // See shader documentation section for compileSource + std::vector spirv = compileSource(R"( #version 450 layout(set = 0, binding = 0) buffer tensorLhs { @@ -215,7 +217,8 @@ In this case we create a shader that should take a couple of milliseconds to run } )"); - auto algo = mgr.algorithm({tensor}, kp_test_utils::Shader::compileSource(shader)); + // See shader documentation section for compileSource + auto algo = mgr.algorithm({tensor}, compileSource(shader)); Now we are able to run the await function on the default sequence. @@ -361,7 +364,8 @@ Similar to the asyncrhonous usecase above, we can still run synchronous commands } )"); - std::vector spirv = kp_test_utils::Shader::compileSource(shader); + // See shader documentation section for compileSource + std::vector spirv = compileSource(shader); std::shared_ptr algo = mgr.algorithm({tensorA, tenssorB}, spirv); diff --git a/docs/overview/build-system.rst b/docs/overview/build-system.rst index 55b7a19d..bdc66469 100644 --- a/docs/overview/build-system.rst +++ b/docs/overview/build-system.rst @@ -69,8 +69,6 @@ Compile Flags - Enable debug build including debug flags (enabled by cmake debug build) * - -DKOMPUTE_DISABLE_VK_DEBUG_LAYERS - Disable the debug Vulkan SDK Layers, mainly used for android builds - * - -DKOMPUTE_DISABLE_SHADER_UTILS - - Disable the shader utils and skip adding glslang as dependency Other CMake Flags ~~~~~~~~~~~~~~~~~ diff --git a/docs/overview/ci-tests.rst b/docs/overview/ci-tests.rst index e65464f9..3dc0e15e 100644 --- a/docs/overview/ci-tests.rst +++ b/docs/overview/ci-tests.rst @@ -38,10 +38,7 @@ Running on the CPU We use `Swiftshader `_ to enable us to run the Kompute framework directly on the CPU for the CI tests. -Even though Swiftshader is optimized to function as a high-performance CPU backend for the Vulkan SDK, there are several limitations, the most notable in context of Kompute are: - -* Loading files (spirv or text) leads to segfault -* Loading raw text string shaders leads to segfault +Even though Swiftshader is optimized to function as a high-performance CPU backend for the Vulkan SDK, there are several limitations, the most notable are limitations in extensions. This is one of the main reason why only a subset of the tests are run in the CI. diff --git a/docs/overview/reference.rst b/docs/overview/reference.rst index 7ea80518..339dc14a 100644 --- a/docs/overview/reference.rst +++ b/docs/overview/reference.rst @@ -119,13 +119,4 @@ The :class:`kp::OpMemoryBarrier` is a tensor only operation which adds memory ba .. doxygenclass:: kp::OpTensorSyncDevice :members: -Shader --------- - -The :class:`kp_test_utils::Shader` class contains a set of utilities to compile and process shaders. - -.. doxygenclass:: kp_test_utils::Shader - :members: - - diff --git a/docs/overview/shaders-to-headers.rst b/docs/overview/shaders-to-headers.rst index 0b36777b..e805c3f8 100644 --- a/docs/overview/shaders-to-headers.rst +++ b/docs/overview/shaders-to-headers.rst @@ -3,6 +3,29 @@ Processing Shaders with Kompute ===================== +Demo / testing function to compile shaders +---------------------------------- + +GLSLANG was initially integrated as part of the framework but it now has been removed due to the license of the glslang pre-processor being under a custom NVIDIA license which explicitly excludes grant of any licenses to NVIDIA's patents in the preprocessor. + +For users that are looking to quickly test the processors it is possible to use the function that is provided in the examples which provides a (non-thread-safe / non-robust) implementation that compiles a shader string into spirv bytes. It is not recommended to use in production but it does enable for faster iteration cycles during development. + +.. code-block:: cpp + :linenos: + static std::vector + compileSource( + const std::string& source) + { + if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END + " + source + " + END").c_str())) + throw std::runtime_error("Error running glslangValidator command"); + std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); + std::vector buffer; + buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); + return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; + } + Converting Shaders into C / C++ Header Files ---------------------------------- diff --git a/examples/array_multiplication/src/Main.cpp b/examples/array_multiplication/src/Main.cpp index 3dd48c39..a97fdbf2 100755 --- a/examples/array_multiplication/src/Main.cpp +++ b/examples/array_multiplication/src/Main.cpp @@ -5,6 +5,18 @@ #include "kompute/Kompute.hpp" +static std::vector +compileSource( + const std::string& source) +{ + if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND").c_str())) + throw std::runtime_error("Error running glslangValidator command"); + std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); + std::vector buffer; + buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); + return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; +} + int main() { #if KOMPUTE_ENABLE_SPDLOG @@ -39,7 +51,7 @@ int main() std::vector> params = { tensorInA, tensorInB, tensorOut }; - std::shared_ptr algo = mgr.algorithm(params, kp_test_utils::Shader::compileSource(shader)); + std::shared_ptr algo = mgr.algorithm(params, kp_test_utils::compileSource(shader)); mgr.sequence() ->record(params) diff --git a/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp b/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp index 1a42761b..e02a08d8 100644 --- a/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp +++ b/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp @@ -4,6 +4,18 @@ #include "KomputeSummatorNode.h" +static std::vector +compileSource( + const std::string& source) +{ + if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND").c_str())) + throw std::runtime_error("Error running glslangValidator command"); + std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); + std::vector buffer; + buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); + return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; +} + KomputeSummatorNode::KomputeSummatorNode() { this->_init(); } @@ -52,9 +64,9 @@ void KomputeSummatorNode::_init() { )"); std::shared_ptr algo = - mgr.algorithm( + this->mManager.algorithm( { this->mPrimaryTensor, this->mSecondaryTensor }, - kp_test_utils::Shader::compileSource(shader)); + compileSource(shader)); // First we ensure secondary tensor loads to GPU @@ -63,7 +75,7 @@ void KomputeSummatorNode::_init() { { this->mSecondaryTensor }); // Then we run the operation with both tensors - sq->record(algo) + sq->record(algo); // We map the result back to local sq->record( diff --git a/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp index ffca61dc..ab406769 100644 --- a/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp +++ b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp @@ -5,6 +5,18 @@ #include "KomputeSummator.hpp" +static std::vector +compileSource( + const std::string& source) +{ + if (system(std::string("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND").c_str())) + throw std::runtime_error("Error running glslangValidator command"); + std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary); + std::vector buffer; + buffer.insert(buffer.begin(), std::istreambuf_iterator(fileStream), {}); + return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())}; +} + namespace godot { KomputeSummator::KomputeSummator() { @@ -58,7 +70,7 @@ void KomputeSummator::_init() { // Then we run the operation with both tensors this->mSequence->record( { this->mPrimaryTensor, this->mSecondaryTensor }, - kp_test_utils::Shader::compileSource(shader)); + compileSource(shader)); // We map the result back to local this->mSequence->record( diff --git a/examples/neural_network_vgg7/sh_conv.py b/examples/neural_network_vgg7/sh_conv.py index dea3722c..e284a616 100644 --- a/examples/neural_network_vgg7/sh_conv.py +++ b/examples/neural_network_vgg7/sh_conv.py @@ -1,8 +1,15 @@ +import os + import kp +def compile_source(source): + os.system("glslangValidator --stdin -S comp -V -o tmp_kp_shader.comp.spv << END\n" + source + "\nEND") + return open("tmp_kp_shader.comp.spv", "rb").read() + + # This is the convolution & leakyrelu shader. global conv_shader -conv_shader = kp.Shader.compile_source(""" +conv_shader = compile_source(""" #version 450 layout (local_size_x = 8, local_size_y = 2) in; From 4e7248e09bbc2be3b9fcf75d9a78e604ab04e2ba Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Wed, 21 Jul 2021 20:44:34 +0100 Subject: [PATCH 8/8] Updated section on shaders --- docs/overview/shaders-to-headers.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/overview/shaders-to-headers.rst b/docs/overview/shaders-to-headers.rst index e805c3f8..53817c85 100644 --- a/docs/overview/shaders-to-headers.rst +++ b/docs/overview/shaders-to-headers.rst @@ -6,12 +6,13 @@ Processing Shaders with Kompute Demo / testing function to compile shaders ---------------------------------- -GLSLANG was initially integrated as part of the framework but it now has been removed due to the license of the glslang pre-processor being under a custom NVIDIA license which explicitly excludes grant of any licenses to NVIDIA's patents in the preprocessor. +GLSLANG was initially integrated as part of the framework but it now has been removed due to the license of the glslang pre-processor being under a custom NVIDIA license which explicitly excludes grant of any licenses to NVIDIA's patents in the preprocessor. This is covered in more detail here: https://github.com/EthicalML/vulkan-kompute/pull/235 For users that are looking to quickly test the processors it is possible to use the function that is provided in the examples which provides a (non-thread-safe / non-robust) implementation that compiles a shader string into spirv bytes. It is not recommended to use in production but it does enable for faster iteration cycles during development. .. code-block:: cpp :linenos: + static std::vector compileSource( const std::string& source)