25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

343 satır
16 KiB

  1. #ifndef LIB_SHADER_FILE_H
  2. #define LIB_SHADER_FILE_H
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. /**********************************************************************************************************************************/
  6. /* public interface */
  7. /**********************************************************************************************************************************/
  8. #define LSF_LOGLEVEL_DEBUG 0
  9. #define LSF_LOGLEVEL_INFO 1
  10. #define LSF_LOGLEVEL_WARNING 2
  11. #define LSF_LOGLEVEL_ERROR 3
  12. #define LSF_ERR_NONE 0x00000000
  13. #define LSF_ERR_NOT_INIT 0x00000001
  14. #define LSF_ERR_INVALID_HANDLE_SHADER_FILE 0x00000010
  15. #define LSF_ERR_INVALID_HANDLE_SHADER_GENERATOR 0x00000011
  16. #define LSF_ERR_INVALID_HANDLE_STREAM 0x00000012
  17. #define LSF_ERR_INVALID_GENERATOR_NAME 0x00000020
  18. #define LSF_ERR_INVALID_PROPERTY_INDEX 0x00000021
  19. #define LSF_ERR_INVALID_PROPERTY_NAME 0x00000022
  20. #define LSF_ERR_GENERATOR_NOT_ASSIGNED_TO_FILE 0x00000023
  21. #define LSF_ERR_UNKNOWN_IDENTFIFIER 0x00001000
  22. #define LSF_ERR_DUPLICATE_IDENTIFIER 0x00001001
  23. #define LSF_ERR_OUT_OF_RANGE 0x00001002
  24. #define LSF_ERR_INVALID_IDENTIFIER 0x00001003
  25. #define LSF_ERR_INVALID_PARAMTER_COUNT 0x00001004
  26. #define LSF_ERR_INVALID_PARAMTER 0x00001005
  27. #define LSF_ERR_UNEXPECTED_TOKEN 0x00001006
  28. #define LSF_ERR_INVALID_TOKEN 0x00001007
  29. #define LSF_ERR_EXPRESSION_INTERNAL 0x00001008
  30. #define LSF_ERR_EXPRESSION 0x00001009
  31. #define LSF_ERR_SHADER_PART_INTERNAL 0x0000100A
  32. #define LSF_ERR_SHADER_PART 0x0000100B
  33. #define LSF_ERR_INVALID_LIBRARY_NAME 0x00002000
  34. #define LSF_ERR_INVALID_LIBRARY_HANDLE 0x00002001
  35. #define LSF_ERR_INVALID_METHOD_NAME 0x00002002
  36. #define LSF_ERR_UNKNOWN 0xFFFFFFFF
  37. #define LSF_SEEK_BEG 0
  38. #define LSF_SEEK_CUR 1
  39. #define LSF_SEEK_END 2
  40. #if __MINGW32__
  41. # define WINAPI __stdcall
  42. #else
  43. # define WINAPI
  44. #endif
  45. typedef uint32_t lsf_error_code_t;
  46. typedef uint32_t lsf_log_level_t;
  47. typedef uint32_t lsf_seek_origin_t;
  48. typedef void* lsf_shader_file_handle_t;
  49. typedef void* lsf_shader_generator_handle_t;
  50. typedef void* lsf_stream_handle_t;
  51. /** function pointer to write log messages
  52. * @param loglevel log level of current message (see LSF_LOGLEVEL_*)
  53. * @param msg message to log
  54. * @param userargs user defined arguments */
  55. typedef void (WINAPI *lsf_shader_file_log_callback_t)(const lsf_log_level_t loglevel, const char* msg, const void* userargs);
  56. /** function pointer to save shader code
  57. * @param filename filename to save shader code to
  58. * @param handle handle to stream data (use lsf_stream_* methods to read/write data)
  59. * @param userargs user defined arguments */
  60. typedef void (WINAPI *lsf_save_data_callback_t)(const char* filename, const lsf_stream_handle_t handle, const void* userargs);
  61. /** function pointer to load shader code
  62. * @param filename filename to load shader code from
  63. * @param handle handle to stream data (use lsf_stream_* methods to read/write data)
  64. * @param userargs user defined arguments */
  65. typedef bool (WINAPI *lsf_load_data_callback_t)(const char* filename, const lsf_stream_handle_t handle, const void* userargs);
  66. /** create a new shader file handle
  67. * @returns shader file handle or NULL (check lsf_get_last_error_* for more details) */
  68. lsf_shader_file_handle_t (WINAPI *lsf_shader_file_create) ();
  69. /** set the callback for log messages of a specific shader file handle
  70. * @param handle shader file handle to set callback for
  71. * @param callback pointer to callback function
  72. * @param userargs user defined arguments
  73. * @returns error code (see LSF_ERR_*) */
  74. 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);
  75. /** load shader file code from a given file
  76. * @param handle handle of shader file to load from file
  77. * @param filename file to load shader code from
  78. * @returns error code (see LSF_ERR_*) */
  79. lsf_error_code_t (WINAPI *lsf_shader_file_load_from_file) (const lsf_shader_file_handle_t handle, const char* filename);
  80. /** load shader code from function
  81. * @param handle handle of shader file to load
  82. * @param filename name of the file to load code from (will be passed to callback; can be used as key to identify data to load)
  83. * @param callback function to call to request data
  84. * @param userargs user defined arguments (will be passed to callback)
  85. * @returns error code (see LSF_ERR_) */
  86. lsf_error_code_t (WINAPI *lsf_shader_file_load_from_func) (const lsf_shader_file_handle_t handle, const char* filename, const lsf_load_data_callback_t callback, const void* userargs);
  87. /** save shader file code to a given file
  88. * @param handle handle of shader file to save to file
  89. * @param filename file to save shader code to
  90. * @returns error code (see LSF_ERR_*) */
  91. lsf_error_code_t (WINAPI *lsf_shader_file_save_to_file) (const lsf_shader_file_handle_t handle, const char* filename);
  92. /** save shader code to function
  93. * @param handle handle of shader file to load
  94. * @param filename name of the file to save code to (will be passed to callback; can be used as key to identify data to save)
  95. * @param callback function to call to save data
  96. * @param userargs user defined arguments (will be passed to callback)
  97. * @returns error code (see LSF_ERR_) */
  98. lsf_error_code_t (WINAPI *lsf_shader_file_save_to_func) (const lsf_shader_file_handle_t handle, const char* filename, const lsf_load_data_callback_t callback, const void* userargs);
  99. /** generate a list of all code generators in a shader file
  100. * @param handle shader file handle to generate list for
  101. * @returns list of code generators (seperated by linebreak) or NULL (check lsf_get_last_error_* for more details) */
  102. const char* (WINAPI *lsf_shader_file_get_generator_names) (const lsf_shader_file_handle_t handle);
  103. /** destroy a shader file handle
  104. * @param handle shader file handle to destroy
  105. * @returns error code (see LSF_ERR_*) */
  106. lsf_error_code_t (WINAPI *lsf_shader_file_destroy) (const lsf_shader_file_handle_t handle);
  107. /** create a new code generator from a given shader file. the generator is owned by the shader file handle.
  108. * if you destroy a shader file handle, all associated generator handles will be destroyed to.
  109. * @param handle shader file handle to create generator from
  110. * @param name name of the generator (see lsf_shader_file_get_generator_names to get a list of all valid names)
  111. * @return generator handle of NULL (check lsf_get_last_error_* for more details) */
  112. lsf_shader_generator_handle_t (WINAPI *lsf_generator_create) (const lsf_shader_file_handle_t handle, const char* name);
  113. /** generate a list of all known properties within a given generator handle
  114. * @handle generator handle to get properties from
  115. * @return list of all known properties (seperated by linebreak) or NULL (check lsf_get_last_error_* for more details) */
  116. const char* (WINAPI *lsf_generator_get_property_names) (const lsf_shader_generator_handle_t handle);
  117. /** get a property value by its index
  118. * @param handle generator handle
  119. * @param index index of the property (see lsf_generator_get_property_names to calculate property index)
  120. * @return property value or NULL (check lsf_get_last_error_* for more details) */
  121. const char* (WINAPI *lsf_generator_get_property) (const lsf_shader_generator_handle_t handle, const int index);
  122. /** get a property value by its name
  123. * @param handle generator handle
  124. * @param name name of the property
  125. * @return property value or NULL (check lsf_get_last_error_* for more details) */
  126. const char* (WINAPI *lsf_generator_get_property_by_name) (const lsf_shader_generator_handle_t handle, const char* name);
  127. /** set a property value by its index
  128. * @param handle generator handle
  129. * @param index index of the property (see lsf_generator_get_property_names to calculate property index)
  130. * @param value new value of the property
  131. * @return error code (see LSF_ERR_*) */
  132. lsf_error_code_t (WINAPI *lsf_generator_set_property) (const lsf_shader_generator_handle_t handle, const int index, const char* value);
  133. /** set a property value by its name
  134. * @param handle generator handle
  135. * @param name name of the property
  136. * @param value new value of the property
  137. * @return error code (see LSF_ERR_*) */
  138. lsf_error_code_t (WINAPI *lsf_generator_set_property_by_name) (const lsf_shader_generator_handle_t handle, const char* name, const char* value);
  139. /** generate shader code
  140. * @param handle generator handle
  141. * @return generated shader code or NULL (check lsf_get_last_error_* for more details) */
  142. const char* (WINAPI *lsf_generator_generate_code) (const lsf_shader_generator_handle_t handle);
  143. /** destroy a generator handle
  144. * @param handle generator handle to destroy
  145. * @returns error code (see LSF_ERR_*) */
  146. lsf_error_code_t (WINAPI *lsf_generator_destroy) (const lsf_shader_generator_handle_t handle);
  147. /** get size of stream
  148. * @param handle stream handle to get size from
  149. * @returns stream size on success, -1 otherwise (check lsf_get_last_error_* for more details) */
  150. int (WINAPI *lsf_stream_get_size) (const lsf_stream_handle_t handle);
  151. /** set size of stream
  152. * @param handle stream handle to set size from
  153. * @returns error code (see LSF_ERR_*) */
  154. lsf_error_code_t (WINAPI *lsf_stream_set_size) (const lsf_stream_handle_t handle, const int size);
  155. /** seek stream
  156. * @param handle stream handle to seek
  157. * @param offset number of bytes to use as offset
  158. * @param origin seek origin
  159. * @returns new position on success, -1 otherwise (check lsf_get_last_error_* for more details) */
  160. int (WINAPI *lsf_stream_seek) (const lsf_stream_handle_t handle, const int offset, const lsf_seek_origin_t origin);
  161. /** read data from stream
  162. * @param handle stream handle to read data from
  163. * @param buffer buffer to copy data into
  164. * @param size number of bytes to read
  165. * @returns number of read bytes on success, -1 otherwise (check lsf_get_last_error_* for more details) */
  166. int (WINAPI *lsf_stream_read) (const lsf_stream_handle_t handle, const void *buffer, const int size);
  167. /** write data to stream
  168. * @param handle stream handle to write data to
  169. * @param buffer buffer to copy data from
  170. * @param size number of bytes to write
  171. * @returns number of written bytes on success, -1 otherwise (check lsf_get_last_error_* for more details) */
  172. int (WINAPI *lsf_stream_write) (const lsf_stream_handle_t handle, const void *buffer, const int size);
  173. /** get last error code
  174. * @return error code of last failed operation */
  175. lsf_error_code_t (WINAPI *lsf_get_last_error_code) ();
  176. /** get last error message
  177. * @return error message of last failed operation */
  178. const char* (WINAPI *lsf_get_last_error_msg) ();
  179. /** get last error trace
  180. * @return error message and a trace of last failed operation */
  181. const char* (WINAPI *lsf_get_last_error_trace) ();
  182. /** initialize the library
  183. * @param libname name of the shared library to load data from
  184. * @return 0 on success, LSF_ERR_* otherwise */
  185. int lsf_init(const char* libname);
  186. /** finalize the library
  187. * @return 0 on success, LSF_ERR_* otherwise */
  188. int lsf_finish(void);
  189. /**********************************************************************************************************************************/
  190. /* internal initialization code */
  191. /**********************************************************************************************************************************/
  192. //#define LSF_DEBUG
  193. #ifdef LSF_DEBUG
  194. # include <stdio.h>
  195. #endif
  196. #if WIN32 || WIN64 || _WIN32 || _WIN64
  197. # include <windows.h>
  198. typedef HMODULE lib_handle_t;
  199. lib_handle_t open_lib(const char* name)
  200. { return LoadLibrary(name); };
  201. void* get_addr(lib_handle_t handle, const char* name)
  202. { return GetProcAddress(handle, name); };
  203. int close_lib(lib_handle_t handle)
  204. { return FreeLibrary(handle); };
  205. #elif LINUX || __linux__
  206. # include <dlfcn.h>
  207. typedef void* lib_handle_t;
  208. lib_handle_t open_lib(const char* name)
  209. { return dlopen(name, RTLD_LAZY); };
  210. void* get_addr(lib_handle_t handle, const char* name)
  211. { return dlsym(handle, name); };
  212. int close_lib(lib_handle_t handle)
  213. { return !dlclose(handle); };
  214. #else
  215. # error "unknown operation system"
  216. #endif
  217. #ifndef LSF_DEBUG
  218. # define LoadProc(proc, name) \
  219. proc = get_addr(handle, name); \
  220. if (!proc) \
  221. return LSF_ERR_INVALID_METHOD_NAME
  222. #else
  223. # define LoadProc(proc, name) \
  224. proc = get_addr(handle, name); \
  225. printf("load method '%s' (addr=0x%016x)\n", name, proc); \
  226. if (!proc) \
  227. return LSF_ERR_INVALID_METHOD_NAME
  228. #endif
  229. lsf_error_code_t (WINAPI *lsf_init_intern)();
  230. lsf_error_code_t (WINAPI *lsf_finish_intern)();
  231. lib_handle_t handle = NULL;
  232. int lsf_init(const char* libname)
  233. {
  234. #ifdef LSF_DEBUG
  235. printf("loading library '%s'\n", libname);
  236. #endif
  237. handle = open_lib(libname);
  238. if (!handle)
  239. return LSF_ERR_INVALID_LIBRARY_NAME;
  240. #ifdef LSF_DEBUG
  241. printf(" done (handle=0x%016x)\n", handle);
  242. #endif
  243. LoadProc(lsf_shader_file_create, "lsf_ShaderFile_create");
  244. LoadProc(lsf_shader_file_set_log_callback, "lsf_ShaderFile_setLogCallback");
  245. LoadProc(lsf_shader_file_load_from_file, "lsf_ShaderFile_loadFromFile");
  246. LoadProc(lsf_shader_file_save_to_file, "lsf_ShaderFile_saveToFile");
  247. LoadProc(lsf_shader_file_get_generator_names, "lsf_ShaderFile_getGeneratorNames");
  248. LoadProc(lsf_shader_file_destroy, "lsf_ShaderFile_destroy");
  249. LoadProc(lsf_generator_create, "lsf_Generator_create");
  250. LoadProc(lsf_generator_get_property_names, "lsf_Generator_getPropertyNames");
  251. LoadProc(lsf_generator_get_property, "lsf_Generator_getProperty");
  252. LoadProc(lsf_generator_get_property_by_name, "lsf_Generator_getPropertyByName");
  253. LoadProc(lsf_generator_set_property, "lsf_Generator_setProperty");
  254. LoadProc(lsf_generator_set_property_by_name, "lsf_Generator_setPropertyByName");
  255. LoadProc(lsf_generator_generate_code, "lsf_Generator_generateCode");
  256. LoadProc(lsf_generator_destroy, "lsf_Generator_destroy");
  257. LoadProc(lsf_stream_get_size, "lsf_Stream_getSize");
  258. LoadProc(lsf_stream_set_size, "lsf_Stream_setSize");
  259. LoadProc(lsf_stream_seek, "lsf_Stream_seek");
  260. LoadProc(lsf_stream_read, "lsf_Stream_read");
  261. LoadProc(lsf_stream_write, "lsf_Stream_write");
  262. LoadProc(lsf_init_intern, "lsf_init");
  263. LoadProc(lsf_get_last_error_code, "lsf_getLastErrorCode");
  264. LoadProc(lsf_get_last_error_msg, "lsf_getLastErrorMsg");
  265. LoadProc(lsf_get_last_error_trace, "lsf_getLastErrorTrace");
  266. LoadProc(lsf_finish_intern, "lsf_finish");
  267. return lsf_init_intern();
  268. }
  269. int lsf_finish(void)
  270. {
  271. lsf_error_code_t err = lsf_finish_intern();
  272. if (!close_lib(handle))
  273. return LSF_ERR_INVALID_LIBRARY_HANDLE;
  274. return err;
  275. }
  276. #endif /* LIB_SHADER_FILE_H */