Skip to content

Commit

Permalink
[SGE] Added vulkan_shader_compile_and_load for runtime compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi688 committed Jul 23, 2024
1 parent be3abac commit e6cbac8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
1 change: 0 additions & 1 deletion include/sge/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ typedef struct memory_allocator_t memory_allocator_t;
#define IF_DEBUG(x) IF_DEBUG_MODE(x)

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE u32 max(u32 v1, u32 v2) { return (v1 > v2) ? v1 : v2; }
static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE u32 min(u32 v1, u32 v2) { return (v1 < v2) ? v1 : v2; }


#define DYNAMIC_CAST(target_type, ptr) CAST_TO(target_type, ptr)
Expand Down
7 changes: 7 additions & 0 deletions include/sge/internal/vulkan/vulkan_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <sge/internal/vulkan/vulkan_object.h>
#include <sge/event.h>
#include <glslcommon/glsl_types.h>
#include <shader_compiler/compiler/compiler.h>

#define VERTEX_ATTRIB(value, index) ((value) << ((index) * 5))
enum
{
Expand Down Expand Up @@ -204,6 +206,11 @@ BEGIN_CPP_COMPATIBLE
SGE_API vulkan_shader_t* vulkan_shader_new(memory_allocator_t* allocator);
SGE_API vulkan_shader_t* vulkan_shader_create(vulkan_renderer_t* renderer, vulkan_shader_create_info_t* create_info);
SGE_API vulkan_shader_t* vulkan_shader_load(vulkan_renderer_t* renderer, vulkan_shader_load_info_t* file_info);
/* renderer: pointer to the vulkan_renderer_t object
* source: null-terminated source text
* file_load_callback: this is a function pointer to issue a callback for loading resolved include files
* user_data: is a pointer to the arbitrary data which might be needed by file_load_callback (and it is also passed as the second argument) */
SGE_API vulkan_shader_t* vulkan_shader_compile_and_load(vulkan_renderer_t* renderer, const char* source, sc_file_load_callback_t file_load_callback, sc_file_close_callback_t file_close_callback, void* user_data);
SGE_API void vulkan_shader_destroy(vulkan_shader_t* shader);
SGE_API void vulkan_shader_release_resources(vulkan_shader_t* shader);

Expand Down
1 change: 1 addition & 0 deletions include/sge/internal/vulkan/vulkan_shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ SGE_API void vulkan_shader_library_release_resources(vulkan_shader_library_t* li

SGE_API vulkan_shader_handle_t vulkan_shader_library_create_shader(vulkan_shader_library_t* library, vulkan_shader_create_info_t* create_info, const char* shader_name);
SGE_API vulkan_shader_handle_t vulkan_shader_library_load_shader(vulkan_shader_library_t* library, vulkan_shader_load_info_t* load_info, const char* shader_name);
SGE_API vulkan_shader_handle_t vulkan_shader_library_compile_and_load_shader(vulkan_shader_library_t* library, const char* source, const char* shader_name);
SGE_API bool vulkan_shader_library_destroy_shader(vulkan_shader_library_t* library, const char* shader_name);
SGE_API bool vulkan_shader_library_destroy_shaderH(vulkan_shader_library_t* library, vulkan_shader_handle_t handle);

Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ DYNAMIC_LIB_NAME = sge.dll
EXECUTABLE_NAME = main
MAIN_SOURCE_LANG = c
MAIN_SOURCES=source/main.c
EXTERNAL_LIBRARIES = -L./external-dependency-libs
EXTERNAL_LIBRARIES = -L./external-dependency-libs -L./toolchain/shader_compiler/external-dependency-libs -lshaderc_shared
EXTERNAL_INCLUDES = -I./dependencies/ -I./shared-dependencies -I./include/freetype
DEPENDENCIES = ../toolchain/shader_compiler ECS MeshLib GLSLCommon Common MeshLib/dependencies/DiskManager HPML SafeMemory SafeMemory/shared-dependencies/CallTrace TemplateSystem MeshLib/dependencies/DiskManager ttf2mesh ../shared-dependencies/BufferLib
DEPENDENCY_LIBS = ECS/lib/ecs.a MeshLib/lib/meshlib.a GLSLCommon/lib/glslcommon.a Common/lib/common.a MeshLib/dependencies/DiskManager/lib/diskmanager.a HPML/lib/hpml.a SafeMemory/lib/safemem.a ttf2mesh/lib/ttf2mesh.a ../shared-dependencies/BufferLib/lib/bufferlib.a SafeMemory/shared-dependencies/CallTrace/lib/calltrace.a
DEPENDENCY_LIBS = ../toolchain/shader_compiler/lib/shader_compiler.a PhyMacParser/lib/ppsr.a ECS/lib/ecs.a MeshLib/lib/meshlib.a GLSLCommon/lib/glslcommon.a Common/lib/common.a MeshLib/dependencies/DiskManager/lib/diskmanager.a HPML/lib/hpml.a SafeMemory/lib/safemem.a ttf2mesh/lib/ttf2mesh.a ../shared-dependencies/BufferLib/lib/bufferlib.a SafeMemory/shared-dependencies/CallTrace/lib/calltrace.a
DEPENDENCIES_DIR = ./dependencies
SHARED_DEPENDENCIES = BufferLib
SHARED_DEPENDENCY_LIBS = BufferLib/lib/bufferlib.a
Expand Down
55 changes: 44 additions & 11 deletions source/sge/vulkan/vulkan_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include <sge/dictionary.h>
#include <sge/render_window.h>

#include <shader_compiler/compiler/compiler.h> /* for sc_compile() and udat_layout_t */

#include <string.h> // for strlen()
#include <math.h> // for log2(), and ceil()

Expand Down Expand Up @@ -1482,21 +1484,13 @@ typedef struct shader_binary_read_context_t
#define ARRAY_BIT BIT32(30)
#define LAYOUT_FORCE_BIT BIT8(7)

/* see the SB2023.2 spec */
typedef enum udat_layout_t
{
SCALAR = 0,
STD430 = 1,
STD140 = 2
} udat_layout_t;

static memory_layout_provider_callbacks_t get_glsl_memory_layout_callbacks_from_udat_layout(u8 udat_layout)
{
switch(udat_layout & 0x7fu)
{
case SCALAR: return glsl_scalar_memory_layout_callbacks();
case STD430: return glsl_std430_memory_layout_callbacks();
case STD140: return glsl_std140_memory_layout_callbacks();
case SCALAR_LAYOUT: return glsl_scalar_memory_layout_callbacks();
case STD430_LAYOUT: return glsl_std430_memory_layout_callbacks();
case STD140_LAYOUT: return glsl_std140_memory_layout_callbacks();
default: DEBUG_LOG_FETAL_ERROR("Unrecognized udat layout: %u", udat_layout);
}
return glsl_scalar_memory_layout_callbacks();
Expand Down Expand Up @@ -2247,6 +2241,45 @@ SGE_API vulkan_shader_t* vulkan_shader_load(vulkan_renderer_t* renderer, vulkan_
return shader;
}

SGE_API vulkan_shader_t* vulkan_shader_compile_and_load(vulkan_renderer_t* renderer, const char* source, sc_file_load_callback_t file_load_callback, sc_file_close_callback_t file_close_callback, void* user_data)
{
/* prepare input data */
const char* include_path = "/";
sc_compiler_input_t input =
{
.file_load_callback = file_load_callback,
.file_close_callback = file_close_callback,
.user_data = user_data,
.src = source,
.src_len = strlen(source),
.src_path = "",
.exe_path = "",
.cwd = "",
.include_paths = &include_path,
.include_path_count = 1
};
/* compile the source string */
sc_compiler_output_t output = sc_compile(&input, NULL);

/* if compilation has been successful then proceed to create vulkan_shader_t object */
if(output.is_success)
{
vulkan_shader_load_info_t load_info =
{
.data = output.sb_bytes,
.data_size = output.sb_byte_count
};
AUTO shader = vulkan_shader_load(renderer, &load_info);
return shader;
}
/* otherwise issue an error what gone wrong and return NULL value */
else
{
debug_log_error("Failed to compile source string, Reason: %s", output.log);
return NULL;
}
}

SGE_API void vulkan_shader_destroy(vulkan_shader_t* shader)
{
event_unsubscribe(shader->renderer->window->on_resize_event, shader->pipeline_recreate_handle);
Expand Down
17 changes: 17 additions & 0 deletions source/sge/vulkan/vulkan_shader_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <sge/debug.h>
#include <string.h>

#include <sge/pygen/shaders.h>

/* constructors & destructors */
SGE_API vulkan_shader_library_t* vulkan_shader_library_new(memory_allocator_t* allocator)
{
Expand Down Expand Up @@ -138,6 +140,21 @@ SGE_API vulkan_shader_handle_t vulkan_shader_library_load_shader(vulkan_shader_l
return vulkan_shader_library_add(library, vulkan_shader_load(library->renderer, load_info), shader_name);
}

static buffer_t* load_file(const char* file_path, void* user_data)
{
return NULL;
}

static void close_file(buffer_t* data, void* user_data)
{

}

SGE_API vulkan_shader_handle_t vulkan_shader_library_compile_and_load_shader(vulkan_shader_library_t* library, const char* source, const char* shader_name)
{
return vulkan_shader_library_add(library, vulkan_shader_compile_and_load(library->renderer, source, load_file, close_file, NULL), shader_name);
}

static bool vulkan_shader_library_remove(vulkan_shader_library_t* library, const char* vulkan_shader_name)
{
NOT_IMPLEMENTED_FUNCTION();
Expand Down

0 comments on commit e6cbac8

Please sign in to comment.