#ifndef LIB_SHADER_FILE_H #define LIB_SHADER_FILE_H #include /**********************************************************************************************************************************/ /* public interface */ /**********************************************************************************************************************************/ #define LSF_LOGLEVEL_DEBUG 0 #define LSF_LOGLEVEL_INFO 1 #define LSF_LOGLEVEL_WARNING 2 #define LSF_LOGLEVEL_ERROR 3 #define LSF_ERR_NONE 0x00000000 #define LSF_ERR_NOT_INIT 0x00000001 #define LSF_ERR_INVALID_HANDLE_SHADER_FILE 0x00000010 #define LSF_ERR_INVALID_HANDLE_SHADER_GENERATOR 0x00000011 #define LSF_ERR_INVALID_GENERATOR_NAME 0x00000020 #define LSF_ERR_INVALID_PROPERTY_INDEX 0x00000021 #define LSF_ERR_INVALID_PROPERTY_NAME 0x00000022 #define LSF_ERR_GENERATOR_NOT_ASSIGNED_TO_FILE 0x00000023 #define LSF_ERR_UNKNOWN_IDENTFIFIER 0x00001000 #define LSF_ERR_DUPLICATE_IDENTIFIER 0x00001001 #define LSF_ERR_OUT_OF_RANGE 0x00001002 #define LSF_ERR_INVALID_IDENTIFIER 0x00001003 #define LSF_ERR_INVALID_PARAMTER_COUNT 0x00001004 #define LSF_ERR_INVALID_PARAMTER 0x00001005 #define LSF_ERR_UNEXPECTED_TOKEN 0x00001006 #define LSF_ERR_INVALID_TOKEN 0x00001007 #define LSF_ERR_EXPRESSION_INTERNAL 0x00001008 #define LSF_ERR_EXPRESSION 0x00001009 #define LSF_ERR_SHADER_PART_INTERNAL 0x0000100A #define LSF_ERR_SHADER_PART 0x0000100B #define LSF_ERR_INVALID_LIBRARY_NAME 0x00002000 #define LSF_ERR_INVALID_LIBRARY_HANDLE 0x00002001 #define LSF_ERR_INVALID_METHOD_NAME 0x00002002 #define LSF_ERR_UNKNOWN 0xFFFFFFFF #if __MINGW32__ # define WINAPI __stdcall #else # define WINAPI #endif typedef uint32_t lsf_error_code_t; typedef uint32_t lsf_log_level_t; typedef void* lsf_shader_file_handle_t; typedef void* lsf_shader_generator_handle_t; typedef void (*lsf_shader_file_log_callback_t)(const lsf_log_level_t loglevel, const char* msg, const void* userargs); /** create a new shader file handle * @returns shader file handle or NULL (check lsf_get_last_error_* for more details) */ lsf_shader_file_handle_t (WINAPI *lsf_shader_file_create) (); /** set the callback for log messages of a specific shader file handle * @param handle shader file handle to set callback for * @param callback pointer to callback function * @param userargs user defined arguments * @returns error code (see LSF_ERR_*) */ lsf_error_code_t (WINAPI *lsf_shader_file_set_log_callback) (const lsf_shader_file_handle_t handle, const lsf_shader_file_log_callback_t callback, const void* userargs); /** load shader file code from a given file * @param handle handle of shader file to load from file * @param filename file to load shader code from * @returns error code (see LSF_ERR_*) */ lsf_error_code_t (WINAPI *lsf_shader_file_load_from_file) (const lsf_shader_file_handle_t handle, const char* filename); /** save shader file code to a given file * @param handle handle of shader file to save to file * @param filename file to save shader code to * @returns error code (see LSF_ERR_*) */ lsf_error_code_t (WINAPI *lsf_shader_file_save_to_file) (const lsf_shader_file_handle_t handle, const char* filename); /** generate a list of all code generators in a shader file * @param handle shader file handle to generate list for * @returns list of code generators (seperated by linebreak) or NULL (check lsf_get_last_error_* for more details) */ const char* (WINAPI *lsf_shader_file_get_generator_names) (const lsf_shader_file_handle_t handle); /** destroy a shader file handle * @param handle shader file handle to destroy * @returns error code (see LSF_ERR_*) */ lsf_error_code_t (WINAPI *lsf_shader_file_destroy) (const lsf_shader_file_handle_t handle); /** create a new code generator from a given shader file. the generator is owned by the shader file handle. * if you destroy a shader file handle, all associated generator handles will be destroyed to. * @param handle shader file handle to create generator from * @param name name of the generator (see lsf_shader_file_get_generator_names to get a list of all valid names) * @return generator handle of NULL (check lsf_get_last_error_* for more details) */ lsf_shader_generator_handle_t (WINAPI *lsf_generator_create) (const lsf_shader_file_handle_t handle, const char* name); /** generate a list of all known properties within a given generator handle * @handle generator handle to get properties from * @return list of all known properties (seperated by linebreak) or NULL (check lsf_get_last_error_* for more details) */ const char* (WINAPI *lsf_generator_get_property_names) (const lsf_shader_generator_handle_t handle); /** get a property value by its index * @param handle generator handle * @param index index of the property (see lsf_generator_get_property_names to calculate property index) * @return property value or NULL (check lsf_get_last_error_* for more details) */ const char* (WINAPI *lsf_generator_get_property) (const lsf_shader_generator_handle_t handle, const int index); /** get a property value by its name * @param handle generator handle * @param name name of the property * @return property value or NULL (check lsf_get_last_error_* for more details) */ const char* (WINAPI *lsf_generator_get_property_by_name) (const lsf_shader_generator_handle_t handle, const char* name); /** set a property value by its index * @param handle generator handle * @param index index of the property (see lsf_generator_get_property_names to calculate property index) * @param value new value of the property * @return property value or NULL (check lsf_get_last_error_* for more details) */ lsf_error_code_t (WINAPI *lsf_generator_set_property) (const lsf_shader_generator_handle_t handle, const int index, const char* value); /** set a property value by its name * @param handle generator handle * @param name name of the property * @param value new value of the property * @return property value or NULL (check lsf_get_last_error_* for more details) */ lsf_error_code_t (WINAPI *lsf_generator_set_property_by_name) (const lsf_shader_generator_handle_t handle, const char* name, const char* value); /** generate shader code * @param handle generator handle * @return generated shader code or NULL (check lsf_get_last_error_* for more details) */ const char* (WINAPI *lsf_generator_generate_code) (const lsf_shader_generator_handle_t handle); /** destroy a generator handle * @param handle generator handle to destroy * @returns error code (see LSF_ERR_*) */ lsf_error_code_t (WINAPI *lsf_generator_destroy) (const lsf_shader_generator_handle_t handle); /** get last error code * @return error code of last failed operation */ lsf_error_code_t (WINAPI *lsf_get_last_error_code) (); /** get last error message * @return error message of last failed operation */ const char* (WINAPI *lsf_get_last_error_msg) (); /** get last error trace * @return error message and a trace of last failed operation */ const char* (WINAPI *lsf_get_last_error_trace) (); /** initialize the library * @param libname name of the shared library to load data from * @return 0 on success, LSF_ERR_* otherwise */ int lsf_init(const char* libname); /** finalize the library * @return 0 on success, LSF_ERR_* otherwise */ int lsf_finish(void); /**********************************************************************************************************************************/ /* internal initialization code */ /**********************************************************************************************************************************/ //#define LSF_DEBUG #ifdef LSF_DEBUG # include #endif #if WIN32 || WIN64 || _WIN32 || _WIN64 # include typedef HMODULE lib_handle_t; lib_handle_t open_lib(const char* name) { return LoadLibrary(name); }; void* get_addr(lib_handle_t handle, const char* name) { return GetProcAddress(handle, name); }; int close_lib(lib_handle_t handle) { return FreeLibrary(handle); }; #elif LINUX || __linux__ # include typedef void* lib_handle_t; lib_handle_t open_lib(const char* name) { return dlopen(name, RTLD_LAZY); }; void* get_addr(lib_handle_t handle, const char* name) { return dlsym(handle, name); }; int close_lib(lib_handle_t handle) { return !dlclose(handle); }; #else # error "unknown operation system" #endif #ifndef LSF_DEBUG # define LoadProc(proc, name) \ proc = get_addr(handle, name); \ if (!proc) \ return LSF_ERR_INVALID_METHOD_NAME #else # define LoadProc(proc, name) \ proc = get_addr(handle, name); \ printf("load method '%s' (addr=0x%016x)\n", name, proc); \ if (!proc) \ return LSF_ERR_INVALID_METHOD_NAME #endif lsf_error_code_t (WINAPI *lsf_init_intern)(); lsf_error_code_t (WINAPI *lsf_finish_intern)(); lib_handle_t handle = NULL; int lsf_init(const char* libname) { #ifdef LSF_DEBUG printf("loading library '%s'\n", libname); #endif handle = open_lib(libname); if (!handle) return LSF_ERR_INVALID_LIBRARY_NAME; #ifdef LSF_DEBUG printf(" done (handle=0x%016x)\n", handle); #endif LoadProc(lsf_shader_file_create, "lsf_ShaderFile_create"); LoadProc(lsf_shader_file_set_log_callback, "lsf_ShaderFile_setLogCallback"); LoadProc(lsf_shader_file_load_from_file, "lsf_ShaderFile_loadFromFile"); LoadProc(lsf_shader_file_save_to_file, "lsf_ShaderFile_saveToFile"); LoadProc(lsf_shader_file_get_generator_names, "lsf_ShaderFile_getGeneratorNames"); LoadProc(lsf_shader_file_destroy, "lsf_ShaderFile_destroy"); LoadProc(lsf_generator_create, "lsf_Generator_create"); LoadProc(lsf_generator_get_property_names, "lsf_Generator_getPropertyNames"); LoadProc(lsf_generator_get_property, "lsf_Generator_getProperty"); LoadProc(lsf_generator_get_property_by_name, "lsf_Generator_getPropertyByName"); LoadProc(lsf_generator_set_property, "lsf_Generator_setProperty"); LoadProc(lsf_generator_set_property_by_name, "lsf_Generator_setPropertyByName"); LoadProc(lsf_generator_generate_code, "lsf_Generator_generateCode"); LoadProc(lsf_generator_destroy, "lsf_Generator_destroy"); LoadProc(lsf_init_intern, "lsf_init"); LoadProc(lsf_get_last_error_code, "lsf_getLastErrorCode"); LoadProc(lsf_get_last_error_msg, "lsf_getLastErrorMsg"); LoadProc(lsf_get_last_error_trace, "lsf_getLastErrorTrace"); LoadProc(lsf_finish_intern, "lsf_finish"); return lsf_init_intern(); } int lsf_finish(void) { lsf_error_code_t err = lsf_finish_intern(); if (!close_lib(handle)) return LSF_ERR_INVALID_LIBRARY_HANDLE; return err; } #endif /* LIB_SHADER_FILE_H */