From bb90806d79a00055032983759364c7621db6814b Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 23 Aug 2020 12:30:56 +0100 Subject: [PATCH] Updated to include and work with cpp header shaders --- .ccls | 1 + Makefile | 4 ++-- {src => external}/shaders/computeheadless.hpp | 8 +++++--- {src => external}/shaders/machinelearning.hpp | 8 +++++--- {src => external}/shaders/opmult.hpp | 8 +++++--- scripts/convert_shaders.py | 10 ++++++++-- src/OpMult.cpp | 12 ++++++------ src/OpMult.hpp | 4 +--- src/shaders/shader_util.hpp | 7 ------- 9 files changed, 33 insertions(+), 29 deletions(-) rename {src => external}/shaders/computeheadless.hpp (97%) rename {src => external}/shaders/machinelearning.hpp (97%) rename {src => external}/shaders/opmult.hpp (97%) delete mode 100644 src/shaders/shader_util.hpp diff --git a/.ccls b/.ccls index bc4e5017a..da9461360 100644 --- a/.ccls +++ b/.ccls @@ -11,6 +11,7 @@ %h c++-header -DDEBUG=1 +-DKOMPUTE_INCLUDE_FOR_SYNTAX -I./external/ diff --git a/Makefile b/Makefile index 09f77f1c3..e472944c4 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,6 @@ build: clean build_shaders -std=c++17 \ -DDEBUG=1 \ -I"./external/" \ - -I"./src/" \ -I"C:\\VulkanSDK\\1.2.141.2\\Include\\" \ -L"C:\\VulkanSDK\\1.2.141.2\\Lib\\" \ -lvulkan-1 \ @@ -46,7 +45,7 @@ build_shaders: python scripts/convert_shaders.py \ --shader-path shaders/glsl \ --shader-binary $(SCMP) \ - --header-path src/shaders \ + --header-path external/shaders \ -v docker_seldon_run: @@ -80,6 +79,7 @@ format: $(CF) -i -style="{BasedOnStyle: mozilla, IndentWidth: 4}" src/*.cpp src/*.hpp src/*.h clean: + find src -name "*gch" -exec rm {} \; || "No ghc files" rm ./bin/main.exe || echo "No main.exe" run: diff --git a/src/shaders/computeheadless.hpp b/external/shaders/computeheadless.hpp similarity index 97% rename from src/shaders/computeheadless.hpp rename to external/shaders/computeheadless.hpp index 6902ebcfc..6ed7527c3 100644 --- a/src/shaders/computeheadless.hpp +++ b/external/shaders/computeheadless.hpp @@ -1,8 +1,9 @@ -#pragma once +#ifndef SHADEROP_COMPUTEHEADLESS_HPP +#define SHADEROP_COMPUTEHEADLESS_HPP namespace kp { namespace shader_data { -unsigned char shaders_glsl_computeheadless_comp_spv[] = { +static unsigned const char shaders_glsl_computeheadless_comp_spv[] = { 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, @@ -172,6 +173,7 @@ unsigned char shaders_glsl_computeheadless_comp_spv[] = { 0x12, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x02, 0x00, 0x13, 0x00, 0x00, 0x00, 0x38, 0x00, 0x01, 0x00 }; -unsigned int shaders_glsl_computeheadless_comp_spv_len = 2008; +static unsigned const int shaders_glsl_computeheadless_comp_spv_len = 2008; } } +#endif // define SHADEROP_COMPUTEHEADLESS_HPP diff --git a/src/shaders/machinelearning.hpp b/external/shaders/machinelearning.hpp similarity index 97% rename from src/shaders/machinelearning.hpp rename to external/shaders/machinelearning.hpp index 3460e51e9..48829f445 100644 --- a/src/shaders/machinelearning.hpp +++ b/external/shaders/machinelearning.hpp @@ -1,8 +1,9 @@ -#pragma once +#ifndef SHADEROP_MACHINELEARNING_HPP +#define SHADEROP_MACHINELEARNING_HPP namespace kp { namespace shader_data { -unsigned char shaders_glsl_machinelearning_comp_spv[] = { +static unsigned const char shaders_glsl_machinelearning_comp_spv[] = { 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, @@ -122,6 +123,7 @@ unsigned char shaders_glsl_machinelearning_comp_spv[] = { 0x34, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 }; -unsigned int shaders_glsl_machinelearning_comp_spv_len = 1408; +static unsigned const int shaders_glsl_machinelearning_comp_spv_len = 1408; } } +#endif // define SHADEROP_MACHINELEARNING_HPP diff --git a/src/shaders/opmult.hpp b/external/shaders/opmult.hpp similarity index 97% rename from src/shaders/opmult.hpp rename to external/shaders/opmult.hpp index fe59e963e..8340b1029 100644 --- a/src/shaders/opmult.hpp +++ b/external/shaders/opmult.hpp @@ -1,8 +1,9 @@ -#pragma once +#ifndef SHADEROP_OPMULT_HPP +#define SHADEROP_OPMULT_HPP namespace kp { namespace shader_data { -unsigned char shaders_glsl_opmult_comp_spv[] = { +static unsigned const char shaders_glsl_opmult_comp_spv[] = { 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, @@ -113,6 +114,7 @@ unsigned char shaders_glsl_opmult_comp_spv[] = { 0x16, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00, 0x27, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 }; -unsigned int shaders_glsl_opmult_comp_spv_len = 1308; +static unsigned const int shaders_glsl_opmult_comp_spv_len = 1308; } } +#endif // define SHADEROP_OPMULT_HPP diff --git a/scripts/convert_shaders.py b/scripts/convert_shaders.py index 6780fdeaf..4b365eef6 100644 --- a/scripts/convert_shaders.py +++ b/scripts/convert_shaders.py @@ -79,18 +79,24 @@ def run_cli( if header_path: logger.debug(f"Header path provided. Converting bin files to hpp.") logger.debug(f"Output header path: {shader_path}") + for file in spirv_files: header_data = str(sh.xxd("-i", file)) + # Ensuring the variable is a static unsigned const + header_data = header_data.replace("unsigned", "static unsigned const") file_name = file.split("/")[-1] header_file = file_name.replace(".comp.spv", ".hpp") + header_file_define = "SHADEROP_" + header_file.replace(".", "_").upper() 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(f"#ifndef {header_file_define}\n") + fstream.write(f"#define {header_file_define}\n\n") fstream.write("namespace kp {\n") fstream.write("namespace shader_data {\n") - fstream.write(header_data) + fstream.write(f"{header_data}") fstream.write("}\n") fstream.write("}\n") + fstream.write(f"#endif // define {header_file_define}\n") if __name__ == "__main__": diff --git a/src/OpMult.cpp b/src/OpMult.cpp index a4764e98a..1b41948c7 100644 --- a/src/OpMult.cpp +++ b/src/OpMult.cpp @@ -1,15 +1,15 @@ +#ifndef OPMULT_CPP +#define OPMULT_CPP #include "Tensor.hpp" -#include "shaders/opmult.hpp" +#include // Only defining hpp file for syntax validation in editors -#ifndef OPMULT_H +#ifndef OPMULT_HPP #include "OpMult.hpp" #endif -#ifndef OPMULT_CPP -#define OPMULT_CPP namespace kp { @@ -99,8 +99,8 @@ OpMult::init(std::vector> tensors) this->mCommandBuffer); 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); + shader_data::shaders_glsl_opmult_comp_spv, + shader_data::shaders_glsl_opmult_comp_spv + shader_data::shaders_glsl_opmult_comp_spv_len); this->mAlgorithm->init(shaderFileData, tensors); } diff --git a/src/OpMult.hpp b/src/OpMult.hpp index 52218f296..e3de9cdc4 100644 --- a/src/OpMult.hpp +++ b/src/OpMult.hpp @@ -1,6 +1,6 @@ // Defining OPMULT_H to ensure cpp class doesn't reimport -#define OPMULT_H #pragma once +#define OPMULT_HPP #include #include @@ -15,8 +15,6 @@ #include "Algorithm.hpp" #include "Tensor.hpp" -#include "shaders/opmult.hpp" - #include "OpBase.hpp" namespace kp { diff --git a/src/shaders/shader_util.hpp b/src/shaders/shader_util.hpp deleted file mode 100644 index 3010ec3dd..000000000 --- a/src/shaders/shader_util.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -namespace kp { -namespace shader_data { - -} -}