Skip to content

Commit

Permalink
Make the runtime compiler lookup cf_shader_directory for include
Browse files Browse the repository at this point in the history
  • Loading branch information
bullno1 committed Nov 22, 2024
1 parent 0636d2a commit 9acef93
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 246 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/samples/hello_triangle_data/tri_vs.h
COMMAND cute-shaderc
-I${CMAKE_CURRENT_SOURCE_DIR}/samples/hello_triangle_data
-type=vertex
-varname=s_tri_vs_bytecode
-oheader=${CMAKE_CURRENT_SOURCE_DIR}/samples/hello_triangle_data/tri_vs.h
Expand Down
1 change: 1 addition & 0 deletions samples/hello_triangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int main(int argc, char* argv[])
// Therefor, the material is just empty in this case.
CF_Material material = cf_make_material();
#ifdef CF_RUNTIME_SHADER_COMPILATION
cf_shader_directory("/hello_triangle_data");
CF_Shader shader = cf_make_shader("/hello_triangle_data/tri_vs.shd", "/hello_triangle_data/tri_fs.shd");
#else
CF_Shader shader = cf_make_shader_from_bytecode(s_tri_vs_bytecode, s_tri_fs_bytecode);
Expand Down
29 changes: 26 additions & 3 deletions src/cute_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ void cf_shader_watch()
s_shader_watch_recursive("/");
}

#ifdef CF_RUNTIME_SHADER_COMPILATION
static char* s_cute_shader_vfs_read(const char* path, size_t* len, void* context) {
CF_UNUSED(context);
return (char*)fs_read_entire_file_to_memory(path, len);
}

static void s_cute_shader_vfs_free(char* content, void* context) {
CF_UNUSED(context);
cf_free(content);
}

static cute_shader_vfs_t s_cute_shader_vfs = {
.read_file_content = s_cute_shader_vfs_read,
.free_file_content = s_cute_shader_vfs_free,
};
#endif

const dyna uint8_t* cf_compile_shader_to_bytecode_internal(const char* shader_src, CF_ShaderStage cf_stage, const char* user_shd)
{
#ifdef CF_RUNTIME_SHADER_COMPILATION
Expand All @@ -350,15 +367,21 @@ const dyna uint8_t* cf_compile_shader_to_bytecode_internal(const char* shader_sr
shader_stub.content = user_shd != NULL ? user_shd : s_shader_stub;
builtin_includes[num_builtin_includes++] = shader_stub;

int num_include_dirs = 0;
const char* include_dirs[1];
if (app->shader_directory_set) {
include_dirs[num_include_dirs++] = app->shader_directory.c_str();
}

cute_shader_config_t config;
config.automatic_include_guard = true;
config.num_builtin_includes = num_builtin_includes;
config.builtin_includes = builtin_includes;
config.num_include_dirs = 0;
config.include_dirs = NULL;
config.num_include_dirs = num_include_dirs;
config.include_dirs = include_dirs;
config.num_builtin_defines = 0;
config.builtin_defines = NULL;
config.vfs = NULL;
config.vfs = &s_cute_shader_vfs;

cute_shader_result_t result = cute_shader_compile(shader_src, stage, config);
if (result.success) {
Expand Down
147 changes: 0 additions & 147 deletions src/cute_shader/DirStackFileIncluder.h

This file was deleted.

Loading

0 comments on commit 9acef93

Please sign in to comment.