Skip to content

Commit

Permalink
Platform independent commands
Browse files Browse the repository at this point in the history
  • Loading branch information
shg8 committed Feb 22, 2024
1 parent ef16ef8 commit 3e67418
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
22 changes: 13 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand Down
47 changes: 47 additions & 0 deletions cmake/embedfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdlib.h>
#include <stdio.h>

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;
}
6 changes: 3 additions & 3 deletions vulkan/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class Shader {
filename(std::move(filename)) {
}

Shader(const std::shared_ptr<VulkanContext>& _context, unsigned char *data, size_t size)
Shader(const std::shared_ptr<VulkanContext>& _context, const unsigned char * data, size_t size)
: context(_context),
filename(""),
data(data),
size(size) {
}

Shader(const std::shared_ptr<VulkanContext>& context, const std::string& filename, unsigned char* data, size_t size)
Shader(const std::shared_ptr<VulkanContext>& context, const std::string& filename, const unsigned char * data, size_t size)
: filename(filename),
context(context),
data(data),
Expand All @@ -34,7 +34,7 @@ class Shader {
private:
const std::string filename;
std::shared_ptr<VulkanContext> context;
unsigned char* data;
const unsigned char* data = nullptr;
size_t size;
};

Expand Down
2 changes: 1 addition & 1 deletion vulkan/pipelines/ComputePipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "ComputePipeline.h"

ComputePipeline::ComputePipeline(const std::shared_ptr<VulkanContext>& context, std::shared_ptr<Shader> shader): Pipeline(context), shader(std::move(shader)) {
shader->load();
this->shader->load();
}

void ComputePipeline::build() {
Expand Down

0 comments on commit 3e67418

Please sign in to comment.