From f3b28e6b088a3ad9933e8a73d168cc65d239ea90 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 23 Aug 2020 10:56:14 +0100 Subject: [PATCH] Added initial implementation of shaders as hpp files --- scripts/convert_shaders.py | 3 ++- src/Algorithm.cpp | 35 +++++---------------------------- src/Algorithm.hpp | 4 ++-- src/OpMult.cpp | 11 ++++++----- src/OpMult.hpp | 2 ++ src/shaders/computeheadless.hpp | 2 ++ src/shaders/machinelearning.hpp | 2 ++ src/shaders/opmult.hpp | 2 ++ src/shaders/shader_util.hpp | 7 +++++++ 9 files changed, 30 insertions(+), 38 deletions(-) create mode 100644 src/shaders/shader_util.hpp diff --git a/scripts/convert_shaders.py b/scripts/convert_shaders.py index e478e03..6780fde 100644 --- a/scripts/convert_shaders.py +++ b/scripts/convert_shaders.py @@ -80,11 +80,12 @@ def run_cli( logger.debug(f"Header path provided. Converting bin files to hpp.") logger.debug(f"Output header path: {shader_path}") for file in spirv_files: - logger.debug(f"Converting to hpp: {file}") header_data = str(sh.xxd("-i", file)) file_name = file.split("/")[-1] header_file = file_name.replace(".comp.spv", ".hpp") + logger.debug(f"Converting to hpp: {file_name}") with open(os.path.join(header_path, header_file), "w+") as fstream: + fstream.write("#pragma once\n\n") fstream.write("namespace kp {\n") fstream.write("namespace shader_data {\n") fstream.write(header_data) diff --git a/src/Algorithm.cpp b/src/Algorithm.cpp index 3920b08..d788ffe 100644 --- a/src/Algorithm.cpp +++ b/src/Algorithm.cpp @@ -30,14 +30,14 @@ Algorithm::~Algorithm() } void -Algorithm::init(std::string shaderFilePath, +Algorithm::init(const std::vector& shaderFileData, std::vector> tensorParams) { SPDLOG_DEBUG("Kompute Algorithm init started"); // TODO: Move to util function this->createParameters(tensorParams); - this->createShaderModule(shaderFilePath); + this->createShaderModule(shaderFileData); this->createPipeline(); } @@ -103,22 +103,6 @@ Algorithm::createParameters(std::vector>& tensorParams) this->mDevice->allocateDescriptorSets(&descriptorSetAllocateInfo, this->mDescriptorSet.get()); - ////std::vector descriptorBufferInfos; - ////for (size_t i = 0; i < tensorParams.size(); i++) { - //// descriptorBufferInfos.push_back(tensorParams[i]->constructDescriptorBufferInfo()); - ////} - ////std::vector computeWriteDescriptorSets; - - ////computeWriteDescriptorSets.push_back( - //// vk::WriteDescriptorSet(*this->mDescriptorSet, - //// 0, // Destination binding - //// 0, // Destination array element - //// 1, // Descriptor count - //// vk::DescriptorType::eStorageBuffer, - //// nullptr, // Descriptor image info - //// descriptorBufferInfos.data() - //// )); - // TODO: Explore design exposing the destination array element for (size_t i = 0; i < tensorParams.size(); i++) { std::vector computeWriteDescriptorSets; @@ -146,24 +130,15 @@ Algorithm::createParameters(std::vector>& tensorParams) } void -Algorithm::createShaderModule(std::string shaderFilePath) +Algorithm::createShaderModule(const std::vector& shaderFileData) { SPDLOG_DEBUG("Kompute Algorithm createShaderModule started"); - std::ifstream fileStream(shaderFilePath, - std::ios::binary | std::ios::in | std::ios::ate); - - size_t shaderFileSize = fileStream.tellg(); - fileStream.seekg(0, std::ios::beg); - char* shaderFileData = new char[shaderFileSize]; - fileStream.read(shaderFileData, shaderFileSize); - fileStream.close(); - vk::ShaderModuleCreateInfo shaderModuleInfo( - vk::ShaderModuleCreateFlags(), shaderFileSize, (uint32_t*)shaderFileData); + vk::ShaderModuleCreateFlags(), shaderFileData.size(), (uint32_t*)shaderFileData.data()); SPDLOG_DEBUG("Kompute Algorithm Creating shader module. ShaderFileSize: {}", - shaderFileSize); + shaderFileData.size()); this->mFreeShaderModule = true; this->mShaderModule = std::make_shared(); this->mDevice->createShaderModule( diff --git a/src/Algorithm.hpp b/src/Algorithm.hpp index 934e9ba..e2228e7 100644 --- a/src/Algorithm.hpp +++ b/src/Algorithm.hpp @@ -24,7 +24,7 @@ class Algorithm // TODO: Add specialisation data // TODO: Explore other ways of passing shader (ie raw bytes) - void init(std::string shaderFilePath, + void init(const std::vector& shaderFileData, std::vector> tensorParams); ~Algorithm(); @@ -55,7 +55,7 @@ class Algorithm bool mFreePipeline = false; // Create util functions - void createShaderModule(std::string shaderFilePath); + void createShaderModule(const std::vector& shaderFileData); void createPipeline(); // Parameters void createParameters(std::vector>& tensorParams); diff --git a/src/OpMult.cpp b/src/OpMult.cpp index d4c7a48..a4764e9 100644 --- a/src/OpMult.cpp +++ b/src/OpMult.cpp @@ -1,9 +1,8 @@ -#include -#include - #include "Tensor.hpp" +#include "shaders/opmult.hpp" + // Only defining hpp file for syntax validation in editors #ifndef OPMULT_H #include "OpMult.hpp" @@ -99,8 +98,10 @@ OpMult::init(std::vector> tensors) this->mDevice, this->mCommandBuffer); - // TODO: Make this path configurable - this->mAlgorithm->init("shaders/glsl/opmult.comp.spv", tensors); + std::vector shaderFileData( + kp::shader_data::shaders_glsl_opmult_comp_spv_len, + kp::shader_data::shaders_glsl_opmult_comp_spv + kp::shader_data::shaders_glsl_opmult_comp_spv_len); + this->mAlgorithm->init(shaderFileData, tensors); } template diff --git a/src/OpMult.hpp b/src/OpMult.hpp index b7cceb7..52218f2 100644 --- a/src/OpMult.hpp +++ b/src/OpMult.hpp @@ -15,6 +15,8 @@ #include "Algorithm.hpp" #include "Tensor.hpp" +#include "shaders/opmult.hpp" + #include "OpBase.hpp" namespace kp { diff --git a/src/shaders/computeheadless.hpp b/src/shaders/computeheadless.hpp index 3c3f794..6902ebc 100644 --- a/src/shaders/computeheadless.hpp +++ b/src/shaders/computeheadless.hpp @@ -1,3 +1,5 @@ +#pragma once + namespace kp { namespace shader_data { unsigned char shaders_glsl_computeheadless_comp_spv[] = { diff --git a/src/shaders/machinelearning.hpp b/src/shaders/machinelearning.hpp index d2a0b2d..3460e51 100644 --- a/src/shaders/machinelearning.hpp +++ b/src/shaders/machinelearning.hpp @@ -1,3 +1,5 @@ +#pragma once + namespace kp { namespace shader_data { unsigned char shaders_glsl_machinelearning_comp_spv[] = { diff --git a/src/shaders/opmult.hpp b/src/shaders/opmult.hpp index 0a63ca0..fe59e96 100644 --- a/src/shaders/opmult.hpp +++ b/src/shaders/opmult.hpp @@ -1,3 +1,5 @@ +#pragma once + namespace kp { namespace shader_data { unsigned char shaders_glsl_opmult_comp_spv[] = { diff --git a/src/shaders/shader_util.hpp b/src/shaders/shader_util.hpp new file mode 100644 index 0000000..3010ec3 --- /dev/null +++ b/src/shaders/shader_util.hpp @@ -0,0 +1,7 @@ +#include + +namespace kp { +namespace shader_data { + +} +}