diff --git a/CMakeLists.txt b/CMakeLists.txt index e05044e..d0b9632 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,9 +89,18 @@ if (APPLE) list(APPEND GLSLC_DEFINE "-DAPPLE") endif () +add_executable(embedfile cmake/embedfile.c) + set(SHADER_HEADER "${PROJECT_BINARY_DIR}/shaders/shaders.h") message(STATUS "Shader header file: ${SHADER_HEADER}") +# Delete old header file +add_custom_command( + OUTPUT ${SHADER_HEADER} + COMMAND ${CMAKE_COMMAND} -E remove ${SHADER_HEADER} + DEPENDS ${GLSL_SOURCE_FILES} +) + foreach (GLSL ${GLSL_SOURCE_FILES}) get_filename_component(FILE_NAME ${GLSL} NAME_WE) string(TOUPPER ${FILE_NAME} FILE_NAME_UPPER) @@ -106,19 +115,14 @@ foreach (GLSL ${GLSL_SOURCE_FILES}) list(APPEND SPIRV_BINARY_FILES ${SPIRV}) add_custom_command( - OUTPUT ${TEMP_HEADER} - COMMAND xxd -i -n ${FILE_NAME_UPPER} ${SPIRV} > ${TEMP_HEADER} - DEPENDS ${SPIRV}) + OUTPUT ${SHADER_HEADER} + COMMAND embedfile ${FILE_NAME_UPPER} ${SPIRV} ${SHADER_HEADER} + DEPENDS ${SPIRV} + APPEND) list(APPEND TEMP_HEADERS ${TEMP_HEADER}) endforeach (GLSL) -add_custom_command( - OUTPUT ${SHADER_HEADER} - COMMAND cat ${TEMP_HEADERS} > ${SHADER_HEADER} - DEPENDS ${TEMP_HEADERS} -) - add_custom_target( Shaders DEPENDS ${SPIRV_BINARY_FILES} ${SHADER_HEADER} diff --git a/cmake/embedfile.c b/cmake/embedfile.c new file mode 100644 index 0000000..ad6b3ae --- /dev/null +++ b/cmake/embedfile.c @@ -0,0 +1,47 @@ +#include +#include + +FILE* open_or_exit(const char* fname, const char* mode) +{ + FILE* f = fopen(fname, mode); + if (f == NULL) { + perror(fname); + exit(EXIT_FAILURE); + } + return f; +} + +int main(int argc, char** argv) +{ + if (argc < 3) { + fprintf(stderr, "USAGE: %s {sym} {rsrc}\n\n" + " Creates {sym}.c from the contents of {rsrc}\n", + argv[0]); + return EXIT_FAILURE; + } + + const char* sym = argv[1]; + FILE* in = open_or_exit(argv[2], "r"); + FILE* out = open_or_exit(argv[3], "a"); + fprintf(out, "static const unsigned char %s[] = {\n", sym); + + unsigned char buf[256]; + size_t nread = 0; + size_t linecount = 0; + do { + nread = fread(buf, 1, sizeof(buf), in); + size_t i; + for (i=0; i < nread; i++) { + fprintf(out, "0x%02x, ", buf[i]); + if (++linecount == 10) { fprintf(out, "\n"); linecount = 0; } + } + } while (nread > 0); + if (linecount > 0) fprintf(out, "\n"); + fprintf(out, "};\n"); + fprintf(out, "static const size_t %s_len = sizeof(%s);\n\n",sym,sym); + + fclose(in); + fclose(out); + + return EXIT_SUCCESS; +} diff --git a/vulkan/Shader.h b/vulkan/Shader.h index 9508420..883d678 100644 --- a/vulkan/Shader.h +++ b/vulkan/Shader.h @@ -14,14 +14,14 @@ class Shader { filename(std::move(filename)) { } - Shader(const std::shared_ptr& _context, unsigned char *data, size_t size) + Shader(const std::shared_ptr& _context, const unsigned char * data, size_t size) : context(_context), filename(""), data(data), size(size) { } - Shader(const std::shared_ptr& context, const std::string& filename, unsigned char* data, size_t size) + Shader(const std::shared_ptr& context, const std::string& filename, const unsigned char * data, size_t size) : filename(filename), context(context), data(data), @@ -34,7 +34,7 @@ class Shader { private: const std::string filename; std::shared_ptr context; - unsigned char* data; + const unsigned char* data = nullptr; size_t size; }; diff --git a/vulkan/pipelines/ComputePipeline.cpp b/vulkan/pipelines/ComputePipeline.cpp index 83cad78..48cac70 100644 --- a/vulkan/pipelines/ComputePipeline.cpp +++ b/vulkan/pipelines/ComputePipeline.cpp @@ -3,7 +3,7 @@ #include "ComputePipeline.h" ComputePipeline::ComputePipeline(const std::shared_ptr& context, std::shared_ptr shader): Pipeline(context), shader(std::move(shader)) { - shader->load(); + this->shader->load(); } void ComputePipeline::build() {