Added initial iteration of shaderc

This commit is contained in:
Alejandro Saucedo 2021-02-18 21:05:17 +00:00
parent b9e0b5e988
commit c1b53cda9d
7 changed files with 123 additions and 1 deletions

View file

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

View file

@ -0,0 +1,51 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
TEST(TestSpecializationConstants, TestTwoConstants)
{
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor({ 0, 0, 0 }) };
std::shared_ptr<kp::Tensor> 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<kp::Sequence> sq = nullptr;
{
kp::Manager mgr;
mgr.rebuild({ tensorA, tensorB });
sq = mgr.sequence();
auto spec = kp::Constants({5.0, 0.3});
sq->begin();
sq->record<kp::OpAlgoBase>(
{ tensorA, tensorB },
std::vector<char>(shader.begin(), shader.end()),
kp::Workgroup(), spec);
sq->end();
sq->eval();
mgr.evalOpDefault<kp::OpTensorSyncLocal>({ tensorA, tensorB });
}
}
EXPECT_EQ(tensorA->data(), std::vector<float>({ 5, 5, 5 }));
EXPECT_EQ(tensorB->data(), std::vector<float>({ 0.3, 0.3, 0.3 }));
}

27
test/TestUtils.cpp Normal file
View file

@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <shaderc/shaderc.hpp>
static std::vector<uint32_t> spirv_from_string(const std::string& source,
shaderc_optimization_level optimization = shaderc_optimization_level_size,
std::vector<std::pair<std::string,std::string>> definitions = {}) {
shaderc::Compiler compiler;
shaderc::CompileOptions options;
for (const std::pair<std::string,std::string>& 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()};
}