rasteriver.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. #include <stdlib.h>
  2. #include <SDL2/SDL.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <CL/cl.h>
  6. #include "../headers/rasteriver.h"
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <SDL2/SDL_ttf.h>
  10. // ----- Internal Variables
  11. int width;
  12. int height;
  13. float highest_z = 0;
  14. int polygon_count;
  15. RI_polygons polygons = NULL;
  16. int object_count;
  17. RI_objects objects;
  18. RI_verticies verticies;
  19. RI_verticies normals;
  20. RI_verticies uvs;
  21. RI_triangles triangles;
  22. RI_textures textures;
  23. int textures_size;
  24. int texture_count;
  25. int running = 1;
  26. int frame = 0;
  27. int show_buffer = RI_BUFFER_COMPLETE;
  28. int show_debug = 0;
  29. int debug_level = RI_DEBUG_LOW;
  30. int show_fps = 0;
  31. int debug_fps = 0;
  32. int clean_polygons = 0;
  33. int populate_polygons = 0;
  34. int be_master_renderer = 0;
  35. int debug_frame = 0;
  36. int show_frame = 0;
  37. int show_info = 0;
  38. int debug_tick = 0;
  39. int use_cpu = 0;
  40. Uint64 start_time;
  41. double frame_time_ms;
  42. double fps;
  43. double elapsed_ticks;
  44. double delta_time;
  45. int fps_cap = -1;
  46. // ----- Internal Variables
  47. // ----- Rendering Vars
  48. SDL_Window *window;
  49. SDL_Renderer *renderer;
  50. SDL_Texture *texture;
  51. int *texture_info;
  52. RI_uint *frame_buffer;
  53. char *font_file = "src/fonts/OxygenMono.ttf";
  54. int font_size = 24;
  55. TTF_Font *font;
  56. SDL_Color font_color = {255, 255, 255, 255};
  57. SDL_Surface *text_surface;
  58. SDL_Texture *text_texture;
  59. SDL_Rect text_rect;
  60. // ----- Rendering Vars
  61. // ----- OpenCL Vars
  62. cl_platform_id platform;
  63. cl_device_id device;
  64. RI_uint number_of_platforms, number_of_devices;
  65. cl_int error;
  66. cl_context context;
  67. cl_command_queue queue;
  68. cl_mem input_memory_buffer;
  69. cl_mem output_memory_buffer;
  70. cl_mem object_memory_buffer;
  71. cl_mem triangles_memory_buffer;
  72. cl_mem verticies_memory_buffer;
  73. cl_mem normals_memory_buffer;
  74. cl_mem uvs_memory_buffer;
  75. cl_mem textures_memory_buffer;
  76. cl_mem texture_info_memory_buffer;
  77. cl_program kernel_program_non_master;
  78. cl_kernel compiled_kernel_non_master;
  79. cl_program kernel_program_master;
  80. cl_kernel compiled_kernel_master;
  81. size_t size_2d[2];
  82. size_t local_size;
  83. RI_uint pattern;
  84. // ----- OpenCL Vars
  85. // ----- Internal Functions
  86. RI_result debug(int verbose, char *string, ...){
  87. if (!show_debug || verbose > debug_level){
  88. return RI_ERROR;
  89. }
  90. va_list args;
  91. va_start(args, string);
  92. char prefix[100] = "[RasterIver] ";
  93. strcat(prefix, string);
  94. vprintf(prefix, args);
  95. printf("\n");
  96. va_end(args);
  97. return RI_SUCCESS;
  98. }
  99. RI_result debug_tick_func(int verbose, char *string, ...)
  100. {
  101. if (!show_debug || (verbose && debug_level != RI_DEBUG_HIGH) || !debug_tick){
  102. return RI_ERROR;
  103. }
  104. va_list args;
  105. va_start(args, string);
  106. char prefix[100] = "[RasterIver] ";
  107. strcat(prefix, string);
  108. vprintf(prefix, args);
  109. printf("\n");
  110. va_end(args);
  111. return RI_SUCCESS;
  112. }
  113. RI_result erchk_func(cl_int error, int line, char *file)
  114. {
  115. if (error != CL_SUCCESS)
  116. {
  117. debug(RI_DEBUG_LOW, "OpenCL Error: %d at line %d at file %s", error, line, file);
  118. RI_Stop(0);
  119. }
  120. return RI_SUCCESS;
  121. }
  122. #define erchk(error) erchk_func(error, __LINE__, __FILE__)
  123. // ----- Internal Functions
  124. // ----- Value Return Functions
  125. RI_result RI_IsRunning()
  126. {
  127. if (running)
  128. {
  129. return RI_RUNNING;
  130. }
  131. else
  132. {
  133. return RI_NOT_RUNNING;
  134. }
  135. }
  136. RI_result RI_ListFlags(){
  137. printf("RI_FLAG_DEBUG: Turns debugging on or off\n");
  138. printf("RI_FLAG_DEBUG_VERBOSE: If debugging and verbose is on, print extra data\n");
  139. printf("RI_FLAG_SHOW_Z_BUFFER: Whether or not to render the Z buffer");
  140. printf("RI_FLAG_SHOW_FPS: Render FPS on screen");
  141. printf("RI_FLAG_DEBUG_FPS: Debug FPS into the console");
  142. printf("RI_FLAG_CLEAN_POLYGONS: When requesting polygons, write INF to the array. (INF means a triangle doesn't exist. Useful for if you allocate more space than there are triangles, but inefficient if you call RI_RequestPolygons frequently\n)");
  143. printf("RI_FLAG_POPULATE_POLYGONS: When requesting polygons, populate the array with random triangles (useful for testing/benchmark)\n");
  144. printf("RI_FLAG_BE_MASTER_RENDERER: If this is 0, RasterIver only acts as a polygon renderer. If 1, you get all the features like objects and rotations.\n");
  145. return RI_SUCCESS;
  146. }
  147. // ----- Value Return Functions
  148. // ----- Set Value Functions
  149. RI_result RI_SetFlag(RI_flag RI_FlagToSet, int RI_Value){
  150. debug(RI_DEBUG_HIGH, "Called RI_SetFlag");
  151. switch (RI_FlagToSet)
  152. {
  153. case RI_FLAG_DEBUG:
  154. show_debug = RI_Value;
  155. break;
  156. case RI_FLAG_DEBUG_LEVEL:
  157. debug_level = RI_Value;
  158. break;
  159. case RI_FLAG_SHOW_BUFFER:
  160. show_buffer = RI_Value;
  161. break;
  162. case RI_FLAG_SHOW_FPS:
  163. show_fps = RI_Value;
  164. break;
  165. case RI_FLAG_DEBUG_FPS:
  166. debug_fps = RI_Value;
  167. break;
  168. case RI_FLAG_CLEAN_POLYGONS:
  169. clean_polygons = RI_Value;
  170. break;
  171. case RI_FLAG_POPULATE_POLYGONS:
  172. populate_polygons = RI_Value;
  173. break;
  174. case RI_FLAG_BE_MASTER_RENDERER:
  175. be_master_renderer = RI_Value;
  176. break;
  177. case RI_FLAG_DEBUG_FRAME:
  178. debug_frame = RI_Value;
  179. break;
  180. case RI_FLAG_SHOW_FRAME:
  181. show_frame = RI_Value;
  182. break;
  183. case RI_FLAG_SHOW_INFO:
  184. show_info = RI_Value;
  185. break;
  186. case RI_FLAG_DEBUG_TICK:
  187. debug_tick = RI_Value;
  188. break;
  189. case RI_FLAG_USE_CPU:
  190. use_cpu = RI_Value;
  191. break;
  192. default:
  193. return RI_INVALID_FLAG;
  194. }
  195. return RI_SUCCESS;
  196. }
  197. RI_result RI_SetBackground(RI_uint RI_BackgroundColor){
  198. debug(RI_DEBUG_HIGH, "Called RI_SetBackground");
  199. pattern = RI_BackgroundColor;
  200. return RI_SUCCESS;
  201. }
  202. RI_polygons RI_RequestPolygons(int RI_PolygonsToRequest){
  203. debug(RI_DEBUG_HIGH, "Called RI_RequestPolygons");
  204. polygon_count = RI_PolygonsToRequest;
  205. int size = sizeof(float) * 3 * 3 * polygon_count;
  206. debug(RI_DEBUG_MEDIUM, "Requesting %d Polygons... (%d bytes)", polygon_count, size);
  207. if (polygons != NULL)
  208. {
  209. free(polygons);
  210. }
  211. polygons = malloc(size);
  212. if (polygons == NULL)
  213. {
  214. debug(RI_DEBUG_LOW, "Malloc Error");
  215. return (float*)RI_ERROR;
  216. }
  217. for (int i_polygon = 0; i_polygon < polygon_count * 9; i_polygon += 3){
  218. if (clean_polygons){
  219. polygons[i_polygon] = INFINITY;
  220. polygons[i_polygon + 1] = INFINITY;
  221. polygons[i_polygon + 2] = INFINITY;
  222. }
  223. else if (populate_polygons){
  224. polygons[i_polygon] = rand() % width;
  225. polygons[i_polygon + 1] = rand() % height;
  226. polygons[i_polygon + 2] = rand() % ((width + height) / 2);
  227. }
  228. }
  229. input_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, size, polygons, &error);
  230. if (input_memory_buffer == NULL)
  231. {
  232. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Requested Polygons");
  233. }
  234. erchk(error);
  235. debug(RI_DEBUG_MEDIUM, "Request for %d Polygons Granted", polygon_count);
  236. return polygons;
  237. }
  238. int vertex_count = 0;
  239. int normal_count = 0;
  240. int uv_count = 0;
  241. int face_count = 0;
  242. int loading_object_current_verticies_count = 0;
  243. int loading_object_current_normals_count = 0;
  244. int loading_object_current_uvs_count = 0;
  245. int loading_object_current_faces_count = 0;
  246. void slice(char *string, char *result, int start, int end){
  247. debug(RI_DEBUG_HIGH, "Called slice");
  248. strncpy(result, string + start, end - start);
  249. }
  250. void malloc_objects(int objects, char **file_names){
  251. debug(RI_DEBUG_HIGH, "Called malloc_objects");
  252. for (int i = 0; i < objects; i++){
  253. debug(RI_DEBUG_HIGH, "file_names[i] is %s", file_names[i]);
  254. FILE *file = fopen(file_names[i], "r");
  255. if(file == NULL){
  256. debug(RI_DEBUG_LOW, "Error Opening Object File");
  257. RI_Stop(0);
  258. }
  259. char line[256];
  260. while (fgets(line, sizeof(line), file)) {
  261. if (line[0] == 'f' && line[1] == ' ') {
  262. face_count++;
  263. }
  264. else if (line[0] == 'v' && line[1] == ' ') {
  265. vertex_count++;
  266. }
  267. else if (line[0] == 'v' && line[1] == 'n') {
  268. normal_count++;
  269. }
  270. else if (line[0] == 'v' && line[1] == 't') {
  271. uv_count++;
  272. }
  273. }
  274. fclose(file);
  275. }
  276. if(verticies != NULL){
  277. free(verticies);
  278. }
  279. if(normals != NULL){
  280. free(normals);
  281. }
  282. if(uvs != NULL){
  283. free(uvs);
  284. }
  285. if(triangles != NULL){
  286. free(triangles);
  287. }
  288. if (vertex_count > 0){
  289. verticies = malloc(sizeof(RI_verticies) * vertex_count * vs);
  290. }
  291. if (normal_count > 0){
  292. normals = malloc(sizeof(RI_verticies) * normal_count * vs);
  293. }
  294. if (uv_count > 0){
  295. uvs = malloc(sizeof(RI_verticies) * uv_count * vs);
  296. }
  297. if (face_count > 0){
  298. triangles = malloc(sizeof(RI_triangles) * face_count * ts);
  299. }
  300. debug(RI_DEBUG_HIGH, "Left malloc_objects");
  301. return;
  302. }
  303. load_object_return load_object(char *object_path, int object_offset, int base){
  304. debug(RI_DEBUG_HIGH, "Called load_object");
  305. debug(RI_DEBUG_MEDIUM, "Loading Object #%d...", object_offset + 1);
  306. debug(RI_DEBUG_MEDIUM, "Opening File \"%s\"...", object_path);
  307. FILE *file = fopen(object_path, "r");
  308. if(file == NULL){
  309. debug(RI_DEBUG_LOW, "Error Opening Object File");
  310. RI_Stop(0);
  311. }
  312. char line[256];
  313. // current values
  314. int ct = 0;
  315. int cn = 0;
  316. int cv = 0;
  317. int cu = 0;
  318. int obj_face_type = 0;
  319. while (fgets(line, sizeof(line), file)) {
  320. if (line[0] == 'f' && line[1] == ' ') {
  321. int matches = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d/",
  322. &triangles[(ct + loading_object_current_faces_count) * ts + 0], &triangles[(ct + loading_object_current_faces_count) * ts + 6], &triangles[(ct + loading_object_current_faces_count) * ts + 3],
  323. &triangles[(ct + loading_object_current_faces_count) * ts + 1], &triangles[(ct + loading_object_current_faces_count) * ts + 7], &triangles[(ct + loading_object_current_faces_count) * ts + 4],
  324. &triangles[(ct + loading_object_current_faces_count) * ts + 2], &triangles[(ct + loading_object_current_faces_count) * ts + 8], &triangles[(ct + loading_object_current_faces_count) * ts + 5]);
  325. if (matches != 9){
  326. triangles[(ct + loading_object_current_faces_count) * ts + 0] = -100;
  327. triangles[(ct + loading_object_current_faces_count) * ts + 1] = -100;
  328. triangles[(ct + loading_object_current_faces_count) * ts + 2] = -100;
  329. triangles[(ct + loading_object_current_faces_count) * ts + 3] = -100;
  330. triangles[(ct + loading_object_current_faces_count) * ts + 4] = -100;
  331. triangles[(ct + loading_object_current_faces_count) * ts + 5] = -100;
  332. triangles[(ct + loading_object_current_faces_count) * ts + 6] = -100;
  333. triangles[(ct + loading_object_current_faces_count) * ts + 7] = -100;
  334. triangles[(ct + loading_object_current_faces_count) * ts + 8] = -100;
  335. if (strchr(line, '/')){
  336. obj_face_type = 1;
  337. sscanf(line, "f %d//%d %d//%d %d//%d",
  338. &triangles[(ct + loading_object_current_faces_count) * ts + 0], &triangles[(ct + loading_object_current_faces_count) * ts + 3],
  339. &triangles[(ct + loading_object_current_faces_count) * ts + 1], &triangles[(ct + loading_object_current_faces_count) * ts + 4],
  340. &triangles[(ct + loading_object_current_faces_count) * ts + 2], &triangles[(ct + loading_object_current_faces_count) * ts + 5]);
  341. }
  342. else {
  343. obj_face_type = 2;
  344. sscanf(line, "f %d %d %d",
  345. &triangles[(ct + loading_object_current_faces_count) * ts + 0],
  346. &triangles[(ct + loading_object_current_faces_count) * ts + 1],
  347. &triangles[(ct + loading_object_current_faces_count) * ts + 2]);
  348. }
  349. }
  350. ct++;
  351. }
  352. else if (line[0] == 'v' && line[1] == ' ') {
  353. sscanf(line, "v %f %f %f",
  354. &verticies[(cv + loading_object_current_verticies_count) * vs + 0],
  355. &verticies[(cv + loading_object_current_verticies_count) * vs + 1],
  356. &verticies[(cv + loading_object_current_verticies_count) * vs + 2]);
  357. cv++;
  358. }
  359. else if (line[0] == 'v' && line[1] == 'n') {
  360. sscanf(line, "vn %f %f %f",
  361. &normals[(cn + loading_object_current_normals_count) * vs + 0],
  362. &normals[(cn + loading_object_current_normals_count) * vs + 1],
  363. &normals[(cn + loading_object_current_normals_count) * vs + 2]);
  364. cn++;
  365. }
  366. else if (line[0] == 'v' && line[1] == 't') {
  367. sscanf(line, "vt %f %f %f",
  368. &uvs[(cu + loading_object_current_uvs_count) * vs + 0],
  369. &uvs[(cu + loading_object_current_uvs_count) * vs + 1],
  370. &uvs[(cu + loading_object_current_uvs_count) * vs + 2]);
  371. cu++;
  372. }
  373. }
  374. switch(obj_face_type){
  375. case 0:
  376. debug(RI_DEBUG_MEDIUM, "OBJ file uses X/X/X X/X/X X/X/X");
  377. break;
  378. case 1:
  379. debug(RI_DEBUG_MEDIUM, "OBJ file uses X//X X//X X//X");
  380. break;
  381. case 2:
  382. debug(RI_DEBUG_MEDIUM, "OBJ file uses X X X");
  383. break;
  384. default:
  385. break;
  386. }
  387. debug(RI_DEBUG_MEDIUM, "%d Triangles", ct);
  388. debug(RI_DEBUG_MEDIUM, "%d Verticies", cv);
  389. debug(RI_DEBUG_MEDIUM, "%d Normals", cn);
  390. debug(RI_DEBUG_MEDIUM, "%d UVS", cu);
  391. objects[base + 9] = ct; // triangle count
  392. loading_object_current_faces_count += ct;
  393. loading_object_current_verticies_count += cv;
  394. loading_object_current_normals_count += cn;
  395. loading_object_current_uvs_count += cu;
  396. fclose(file);
  397. load_object_return return_values = {
  398. 0, 0, 0, 0, 0
  399. };
  400. debug(RI_DEBUG_HIGH, "Left load_object");
  401. return return_values;
  402. }
  403. RI_objects RI_RequestObjects(RI_newObject *RI_ObjectBuffer, int RI_ObjectsToRequest){
  404. debug(RI_DEBUG_HIGH, "Called RI_RequestObjects");
  405. object_count = RI_ObjectsToRequest;
  406. debug(RI_DEBUG_MEDIUM, "Requesting %d Objects...", object_count);
  407. if (objects != NULL)
  408. {
  409. free(objects);
  410. }
  411. int object_arary_size = sizeof(float) * object_size * RI_ObjectsToRequest;
  412. objects = malloc(object_arary_size);
  413. if (objects == NULL){
  414. debug(RI_DEBUG_LOW, "Malloc Error");
  415. }
  416. char **file_names = malloc(RI_ObjectsToRequest * sizeof(char *));
  417. char **texture_names = malloc(RI_ObjectsToRequest * sizeof(char *));
  418. for (int i_object = 0; i_object < object_count; i_object++){
  419. file_names[i_object] = RI_ObjectBuffer[i_object].file_path;
  420. debug(RI_DEBUG_HIGH, "file_names[object] is %s", file_names[i_object]);
  421. texture_names[i_object] = "blahblahblah placeholder (this is some salt)";
  422. }
  423. malloc_objects(RI_ObjectsToRequest, file_names);
  424. free(file_names);
  425. loading_object_current_verticies_count = 0;
  426. loading_object_current_normals_count = 0;
  427. loading_object_current_uvs_count = 0;
  428. loading_object_current_faces_count = 0;
  429. texture_count = 0;
  430. int is_this_texture_name_already_in_the_texture_names_array = 0;
  431. textures_size = 0;
  432. for (int i_object = 0; i_object < object_count; i_object++){
  433. RI_newObject *loading_object_current_object = &RI_ObjectBuffer[i_object];
  434. int base = i_object * object_size;
  435. objects[base + 10] = loading_object_current_faces_count; // triangle offset
  436. objects[base + 11] = loading_object_current_verticies_count; // vertex offset
  437. objects[base + 12] = loading_object_current_normals_count; // normal offset
  438. objects[base + 13] = loading_object_current_uvs_count; // uvs offset
  439. is_this_texture_name_already_in_the_texture_names_array = 0;
  440. for (int i_object_texture = 0; i_object_texture < object_count; i_object_texture++){
  441. if (strcmp(texture_names[i_object_texture], loading_object_current_object->texture) == 0){
  442. debug(RI_DEBUG_HIGH, "Not Reloading Texture \"%s\" (texture #%d) (compared %s to %s)", loading_object_current_object->texture, i_object_texture, texture_names[i_object_texture], loading_object_current_object->texture);
  443. objects[base + 14] = i_object_texture; // texture offset
  444. is_this_texture_name_already_in_the_texture_names_array = 1;
  445. break;
  446. }
  447. }
  448. if (!is_this_texture_name_already_in_the_texture_names_array){
  449. texture_names[texture_count] = loading_object_current_object->texture;
  450. objects[base + 14] = texture_count; // texture offset
  451. texture_count++;
  452. int texture_width, texture_height, channels;
  453. stbi_load(loading_object_current_object->texture, &texture_width, &texture_height, &channels, 4);
  454. debug(RI_DEBUG_MEDIUM, "Texture (%s) Loaded With Size %dx%d (%d channels)", loading_object_current_object->texture, texture_width, texture_height, channels);
  455. textures_size += texture_width * texture_height;
  456. }
  457. load_object((char *)loading_object_current_object->file_path, i_object, base);
  458. objects[base + 0] = loading_object_current_object->x; // x
  459. objects[base + 1] = loading_object_current_object->y; // y
  460. objects[base + 2] = loading_object_current_object->z; // z
  461. objects[base + 3] = loading_object_current_object->r_x; // rotation x
  462. objects[base + 4] = loading_object_current_object->r_y; // rotation y
  463. objects[base + 5] = loading_object_current_object->r_z; // rotation z
  464. objects[base + 15] = loading_object_current_object->r_w; // rotation w
  465. objects[base + 6] = loading_object_current_object->s_x; // scale x
  466. objects[base + 7] = loading_object_current_object->s_y; // scale y
  467. objects[base + 8] = loading_object_current_object->s_z; // scale z
  468. debug(RI_DEBUG_MEDIUM, "Triangle Index: %d", objects[base + 10]);
  469. debug(RI_DEBUG_MEDIUM, "Vertex Index: %d", objects[base + 11]);
  470. debug(RI_DEBUG_MEDIUM, "Normal Index: %d", objects[base + 12]);
  471. debug(RI_DEBUG_MEDIUM, "UV Index: %d", objects[base + 13]);
  472. }
  473. debug(RI_DEBUG_MEDIUM, "Allocating Texture Buffers");
  474. textures = malloc(sizeof(unsigned char) * textures_size * 4);
  475. texture_info = malloc(sizeof(int) * tis * texture_count);
  476. if (!textures){
  477. debug(RI_DEBUG_MEDIUM, "Failed to Allocate Texture Buffer");
  478. }
  479. if (!texture_info){
  480. debug(RI_DEBUG_MEDIUM, "Failed to Allocate Texture Info Buffer");
  481. }
  482. if (!use_cpu && texture_count > 0){
  483. textures_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(unsigned char) * textures_size * 4, textures, &error);
  484. erchk(error);
  485. if (textures_memory_buffer == NULL){
  486. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Textures cl_mem Buffer");
  487. }
  488. texture_info_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * tis * texture_count, texture_info, &error);
  489. erchk(error);
  490. if (texture_info_memory_buffer == NULL){
  491. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Texture Info cl_mem Buffer");
  492. }
  493. }
  494. int value_offset = 0;
  495. for (int i_current_texture = 0; i_current_texture < texture_count; i_current_texture++){
  496. char *current_texture_name = texture_names[i_current_texture];
  497. int temp_width, temp_height;
  498. RI_textures temp_texture = stbi_load(current_texture_name, &temp_width, &temp_height, NULL, 4);
  499. texture_info[i_current_texture * tis] = temp_width;
  500. texture_info[i_current_texture * tis + 1] = temp_height;
  501. texture_info[i_current_texture * tis + 2] = value_offset;
  502. debug(RI_DEBUG_HIGH, "Texture Info for Texture #%d: width: %d, height: %d, offset: %d", i_current_texture, texture_info[i_current_texture * tis], texture_info[i_current_texture * tis + 1], texture_info[i_current_texture * tis + 2]);
  503. for (int i_current_value = 0; i_current_value < temp_width * temp_height * 4; i_current_value++){
  504. textures[i_current_value + value_offset] = temp_texture[i_current_value];
  505. }
  506. value_offset += temp_width * temp_height * 4;
  507. }
  508. debug(RI_DEBUG_MEDIUM, "%d Textures Totalling %d Bytes", texture_count, sizeof(unsigned char) * textures_size * 4);
  509. for (int i = 0; i < face_count * 9; i++){
  510. triangles[i]--;
  511. }
  512. debug(RI_DEBUG_MEDIUM, "Allocated %d Bytes for Objects", sizeof(unsigned char) * textures_size * 4 + sizeof(RI_verticies) * uv_count * vs + sizeof(RI_triangles) * face_count * vs + sizeof(RI_verticies) * vertex_count * vs + sizeof(RI_verticies) * normal_count * vs + object_arary_size);
  513. debug(RI_DEBUG_HIGH, "clCreateBuffer object_arary_size: %d", object_arary_size);
  514. if (!use_cpu){
  515. object_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, object_arary_size, objects, &error);
  516. erchk(error);
  517. }
  518. if (!use_cpu && object_memory_buffer == NULL){
  519. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Objects cl_mem Buffer");
  520. }
  521. if (!use_cpu && face_count > 0){
  522. triangles_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(RI_triangles) * face_count * ts, triangles, &error);
  523. erchk(error);
  524. if (triangles_memory_buffer == NULL){
  525. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Triangles cl_mem Buffer");
  526. }
  527. }
  528. if (!use_cpu && vertex_count > 0){
  529. verticies_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(RI_verticies) * vertex_count * vs, verticies, &error);
  530. erchk(error);
  531. if (verticies_memory_buffer == NULL){
  532. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Verticies cl_mem Buffer");
  533. }
  534. }
  535. if (!use_cpu && normal_count > 0){
  536. normals_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(RI_verticies) * normal_count * vs, normals, &error);
  537. erchk(error);
  538. if (normals_memory_buffer == NULL){
  539. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for Normals cl_mem Buffer");
  540. }
  541. }
  542. if (!use_cpu && uv_count > 0){
  543. uvs_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(RI_verticies) * uv_count * vs, uvs, &error);
  544. erchk(error);
  545. if (uvs_memory_buffer == NULL){
  546. debug(RI_DEBUG_LOW, "clCreateBuffer Failed for UVS cl_mem Buffer");
  547. }
  548. }
  549. debug(RI_DEBUG_MEDIUM, "Request for %d Objects Granted", object_count);
  550. debug(RI_DEBUG_HIGH, "Left RI_RequestObjects");
  551. return objects;
  552. }
  553. RI_result RI_SetFpsCap(int RI_FpsCap){
  554. debug(RI_DEBUG_HIGH, "Called RI_SetFpsCap");
  555. fps_cap = RI_FpsCap;
  556. return RI_SUCCESS;
  557. }
  558. RI_result RI_SetFontColor(RI_uint RI_FontColorRGBA){
  559. debug(RI_DEBUG_HIGH, "Called RI_SetFontColor");
  560. font_color.r = (RI_FontColorRGBA >> 24) & 0xFF;
  561. font_color.g = (RI_FontColorRGBA >> 16) & 0xFF;
  562. font_color.b = (RI_FontColorRGBA >> 8) & 0xFF;
  563. font_color.a = RI_FontColorRGBA & 0xFF;
  564. return RI_SUCCESS;
  565. }
  566. RI_result RI_SetFontSize(int RI_FontSize){
  567. debug(RI_DEBUG_HIGH, "Called RI_SetFontSize");
  568. font_size = RI_FontSize;
  569. return RI_SUCCESS;
  570. }
  571. RI_result RI_SetFontFile(char *RI_PathToFontFile){
  572. debug(RI_DEBUG_HIGH, "Called RI_SetFontFile");
  573. TTF_Font* font_check = TTF_OpenFont(RI_PathToFontFile, 24);
  574. if (font_check == NULL){
  575. return RI_ERROR;
  576. }
  577. font_file = RI_PathToFontFile;
  578. font = TTF_OpenFont(font_file, font_size);
  579. return RI_SUCCESS;
  580. }
  581. // ----- Set Value Functions
  582. int is_intersecting(float a, float b, float c, float d, float p, float q, float r, float s) {
  583. float det, gamma, lambda;
  584. det = (c - a) * (s - q) - (r - p) * (d - b);
  585. if (det == 0) {
  586. return 1;
  587. }
  588. else {
  589. lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
  590. gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
  591. return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1);
  592. }
  593. }
  594. void norm(float dest[2], float a[2]){
  595. float magnitude = sqrt(a[0] * a[0] + a[1] * a[1]);
  596. dest[0] = a[0] / magnitude;
  597. dest[1] = a[1] / magnitude;
  598. }
  599. void sub(float dest[2], float a[2], float b[2]){
  600. dest[0] = a[0] - b[0];
  601. dest[1] = a[1] - b[1];
  602. }
  603. void add(float dest[2], float a[2], float b[2]){
  604. dest[0] = a[0] + b[0];
  605. dest[1] = a[1] + b[1];
  606. }
  607. int clamppp(int x, int lower, int upper) {
  608. return x < lower ? lower : (x > upper ? upper : x);
  609. }
  610. float clamppp_float(float x, float lower, float upper) {
  611. return x < lower ? lower : (x > upper ? upper : x);
  612. }
  613. void rotate_quaternion(float *x, float *y, float *z, float r_x, float r_y, float r_z){
  614. };
  615. void rotate_euler(float *x, float *y, float *z, float r_x, float r_y, float r_z){
  616. float cx = cos(r_x), sx = sin(r_x);
  617. float cy = cos(r_y), sy = sin(r_y);
  618. float cz = cos(r_z), sz = sin(r_z);
  619. float matrix[3][3] = {
  620. {
  621. cy * cz,
  622. -cy * sz,
  623. sy
  624. },
  625. {
  626. sx * sy * cz + cx * sz,
  627. -sx * sy * sz + cx * cz,
  628. -sx * cy
  629. },
  630. {
  631. -cx * sy * cz + sx * sz,
  632. cx * sy * sz + sx * cz,
  633. cx * cy
  634. }
  635. };
  636. float temp_x = matrix[0][0] * *x + matrix[0][1] * *y + matrix[0][2] * *z;
  637. float temp_y = matrix[1][0] * *x + matrix[1][1] * *y + matrix[1][2] * *z;
  638. float temp_z = matrix[2][0] * *x + matrix[2][1] * *y + matrix[2][2] * *z;
  639. *x = temp_x;
  640. *y = temp_y;
  641. *z = temp_z;
  642. };
  643. // ----- Renderer Action Functions
  644. RI_result RI_Tick(){
  645. if (show_fps || debug_fps){
  646. start_time = SDL_GetPerformanceCounter();
  647. }
  648. debug_tick_func(1, "Called RI_Tick");
  649. if (running)
  650. {
  651. if (frame_buffer == NULL)
  652. {
  653. debug_tick_func(0, "Frame Buffer is not Allocated");
  654. return RI_ERROR;
  655. }
  656. if (use_cpu){
  657. for (int id_y = -height / 2; id_y < height / 2; id_y++){
  658. for (int id_x = -width / 2; id_x < width / 2; id_x++){
  659. float z_pixel = INFINITY;
  660. unsigned int frame_pixel = 0x22222222;
  661. float highest_z = 800;
  662. float lowest_z = 0;
  663. int has_normals = 1;
  664. int has_uvs = 1;
  665. float w0;
  666. float w1;
  667. float w2;
  668. for (int i_object = 0; i_object < object_count; i_object++){
  669. int base = i_object * 16;
  670. float object_x = objects[base + 0];
  671. float object_y = objects[base + 1];
  672. float object_z = objects[base + 2];
  673. float object_r_x = objects[base + 3];
  674. float object_r_y = objects[base + 4];
  675. float object_r_z = objects[base + 5];
  676. float object_r_w = objects[base + 15];
  677. float object_s_x = objects[base + 6];
  678. float object_s_y = objects[base + 7];
  679. float object_s_z = objects[base + 8];
  680. int triangle_count = (int)objects[base + 9];
  681. int triangle_index = (int)objects[base + 10];
  682. int vertex_index = (int)objects[base + 11];
  683. int normal_index = (int)objects[base + 12];
  684. int uv_index = (int)objects[base + 13];
  685. int texture_index = (int)objects[base + 14];
  686. for (int i_triangle = 0; i_triangle < triangle_count; i_triangle++){
  687. int triangle_base = (i_triangle + triangle_index) * 9;
  688. int i0 = (vertex_index + triangles[triangle_base + 0]) * 3;
  689. int i1 = (vertex_index + triangles[triangle_base + 1]) * 3;
  690. int i2 = (vertex_index + triangles[triangle_base + 2]) * 3;
  691. int i3 = (normal_index + triangles[triangle_base + 3]) * 3;
  692. int i4 = (normal_index + triangles[triangle_base + 4]) * 3;
  693. int i5 = (normal_index + triangles[triangle_base + 5]) * 3;
  694. int i6 = (uv_index + triangles[triangle_base + 6]) * 3;
  695. int i7 = (uv_index + triangles[triangle_base + 7]) * 3;
  696. int i8 = (uv_index + triangles[triangle_base + 8]) * 3;
  697. float z0 = verticies[i0 + 2];
  698. float x0 = verticies[i0 + 0];
  699. float y0 = verticies[i0 + 1];
  700. float z1 = verticies[i1 + 2];
  701. float x1 = verticies[i1 + 0];
  702. float y1 = verticies[i1 + 1];
  703. float z2 = verticies[i2 + 2];
  704. float x2 = verticies[i2 + 0];
  705. float y2 = verticies[i2 + 1];
  706. if (object_r_w <= -9999999){
  707. rotate_euler(&x0, &y0, &z0, object_r_x, object_r_y, object_r_z);
  708. rotate_euler(&x1, &y1, &z1, object_r_x, object_r_y, object_r_z);
  709. rotate_euler(&x2, &y2, &z2, object_r_x, object_r_y, object_r_z);
  710. }
  711. else{
  712. rotate_euler(&x0, &y0, &z0, object_r_x, object_r_y, object_r_z);
  713. rotate_euler(&x1, &y1, &z1, object_r_x, object_r_y, object_r_z);
  714. rotate_euler(&x2, &y2, &z2, object_r_x, object_r_y, object_r_z);
  715. }
  716. z0 = (z0 * object_s_z + object_z);
  717. x0 = (x0 * object_s_x + object_x) / z0 * width;
  718. y0 = (y0 * object_s_y + object_y) / z0 * height;
  719. z1 = (z1 * object_s_z + object_z);
  720. x1 = (x1 * object_s_x + object_x) / z1 * width;
  721. y1 = (y1 * object_s_y + object_y) / z1 * height;
  722. z2 = (z2 * object_s_z + object_z);
  723. x2 = (x2 * object_s_x + object_x) / z2 * width;
  724. y2 = (y2 * object_s_y + object_y) / z2 * height;
  725. if (i3 < 0 || i4 < 0 || i5 < 0){
  726. has_normals = 0;
  727. }
  728. if (i6 < 0 || i7 < 0 || i8 < 0){
  729. has_uvs = 0;
  730. }
  731. if (isinf(x0) || isinf(y0) || isinf(z0) || isinf(x1) || isinf(y1) || isinf(z1) || isinf(x2) || isinf(y2) || isinf(z2)){
  732. continue;
  733. }
  734. float smallest_x = x0;
  735. float largest_x = x0;
  736. float smallest_y = y0;
  737. float largest_y = y0;
  738. if (x0 > largest_x) largest_x = x0;
  739. if (x1 > largest_x) largest_x = x1;
  740. if (x2 > largest_x) largest_x = x2;
  741. if (x0 < smallest_x) smallest_x = x0;
  742. if (x1 < smallest_x) smallest_x = x1;
  743. if (x2 < smallest_x) smallest_x = x2;
  744. if (y0 > largest_y) largest_y = y0;
  745. if (y1 > largest_y) largest_y = y1;
  746. if (y2 > largest_y) largest_y = y2;
  747. if (y0 < smallest_y) smallest_y = y0;
  748. if (y1 < smallest_y) smallest_y = y1;
  749. if (y2 < smallest_y) smallest_y = y2;
  750. smallest_x = fmin(smallest_x, 0);
  751. largest_x = fmax(largest_x, width);
  752. smallest_y = fmin(smallest_y, 0);
  753. largest_y = fmax(largest_y, height);
  754. if (id_x >= smallest_x && id_x <= largest_x && id_y >= smallest_y && id_y <= largest_y){
  755. int intersections = 0;
  756. intersections += is_intersecting(id_x, id_y, 10000, 100000, x0, y0, x1, y1);
  757. intersections += is_intersecting(id_x, id_y, 10000, 100000, x1, y1, x2, y2);
  758. intersections += is_intersecting(id_x, id_y, 10000, 100000, x2, y2, x0, y0);
  759. if (intersections % 2 == 0){
  760. continue;
  761. }
  762. float denominator = (y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2);
  763. if (denominator >= 0) {
  764. continue;
  765. }
  766. w0 = ((y1 - y2) * (id_x - x2) + (x2 - x1) * (id_y - y2)) / denominator;
  767. w1 = ((y2 - y0) * (id_x - x0) + (x0 - x2) * (id_y - y0)) / denominator;
  768. w2 = 1.0 - w0 - w1;
  769. float z = w0 * z0 + w1 * z1 + w2 * z2;
  770. if (z < z_pixel){
  771. z_pixel = z;
  772. float n_x0 = normals[i3 + 0];
  773. float n_y0 = normals[i3 + 1];
  774. float n_z0 = normals[i3 + 2];
  775. float n_x1 = normals[i4 + 0];
  776. float n_y1 = normals[i4 + 1];
  777. float n_z1 = normals[i4 + 2];
  778. float n_x2 = normals[i5 + 0];
  779. float n_y2 = normals[i5 + 1];
  780. float n_z2 = normals[i5 + 2];
  781. float u_x0 = uvs[i6 + 0];
  782. float u_y0 = uvs[i6 + 1];
  783. float u_z0 = uvs[i6 + 2];
  784. float u_x1 = uvs[i7 + 0];
  785. float u_y1 = uvs[i7 + 1];
  786. float u_z1 = uvs[i7 + 2];
  787. float u_x2 = uvs[i8 + 0];
  788. float u_y2 = uvs[i8 + 1];
  789. float u_z2 = uvs[i8 + 2];
  790. if (object_r_w <= -9999999){
  791. rotate_euler(&n_x0, &n_y0, &n_z0, object_r_x, object_r_y, object_r_z);
  792. rotate_euler(&n_x1, &n_y1, &n_z1, object_r_x, object_r_y, object_r_z);
  793. rotate_euler(&n_x2, &n_y2, &n_z2, object_r_x, object_r_y, object_r_z);
  794. }
  795. else{
  796. rotate_euler(&n_x0, &n_y0, &n_z0, object_r_x, object_r_y, object_r_z);
  797. rotate_euler(&n_x1, &n_y1, &n_z1, object_r_x, object_r_y, object_r_z);
  798. rotate_euler(&n_x2, &n_y2, &n_z2, object_r_x, object_r_y, object_r_z);
  799. }
  800. switch (show_buffer){
  801. case 0:{
  802. double ux = w0 * u_x0 + w1 * u_x1 + w2 * u_x2;
  803. double uy = w0 * u_y0 + w1 * u_y1 + w2 * u_y2;
  804. int texture_width = texture_info[texture_index * 3];
  805. int texture_height = texture_info[texture_index * 3 + 1];
  806. int texture_value_offset = texture_info[texture_index * 3 + 2];
  807. int ix = fmax((int)(ux * texture_width), 0);
  808. int iy = fmax((int)(uy * texture_height), 0);
  809. int uv_pixel = (iy * texture_width + ix) * 4 + texture_value_offset;
  810. unsigned char r = textures[uv_pixel + 0];
  811. unsigned char g = textures[uv_pixel + 1];
  812. unsigned char b = textures[uv_pixel + 2];
  813. unsigned char a = textures[uv_pixel + 3];
  814. frame_pixel = (a << 24) | (r << 16) | (g << 8) | b;
  815. break;}
  816. case 1:{
  817. float z = clamppp_float(z_pixel, 0.0f, highest_z);
  818. float norm_z = z / highest_z;
  819. unsigned char intensity = (unsigned char)(norm_z * 255.0f);
  820. frame_pixel = 0xFF000000 | (intensity << 16) | (intensity << 8) | intensity;
  821. break;}
  822. case 2:{
  823. float nx = w0 * n_x0 + w1 * n_x1 + w2 * n_x2;
  824. float ny = w0 * n_y0 + w1 * n_y1 + w2 * n_y2;
  825. float nz = w0 * n_z0 + w1 * n_z1 + w2 * n_z2;
  826. nx = clamppp_float((nx * 0.5f + 0.5f) * 255.0f, 0.0f, 255.0f);
  827. ny = clamppp_float((ny * 0.5f + 0.5f) * 255.0f, 0.0f, 255.0f);
  828. nz = clamppp_float((nz * 0.5f + 0.5f) * 255.0f, 0.0f, 255.0f);
  829. unsigned char r = (unsigned char)nx;
  830. unsigned char g = (unsigned char)ny;
  831. unsigned char b = (unsigned char)nz;
  832. if (!has_normals){
  833. r = 20;
  834. g = 20;
  835. b = 20;
  836. }
  837. frame_pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
  838. break;}
  839. case 3:{
  840. float ux = w0 * u_x0 + w1 * u_x1 + w2 * u_x2;
  841. float uy = w0 * u_y0 + w1 * u_y1 + w2 * u_y2;
  842. unsigned char r = (unsigned char)clamppp_float(ux * 255.0f, 0.0f, 255.0f);
  843. unsigned char g = (unsigned char)clamppp_float(uy * 255.0f, 0.0f, 255.0f);
  844. unsigned char b = 0;
  845. if (!has_uvs){
  846. r = 20;
  847. g = 20;
  848. b = 20;
  849. }
  850. frame_pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
  851. break;}
  852. default:{
  853. frame_pixel = 0xFF00FFFF;
  854. break;}
  855. }
  856. }
  857. }
  858. }
  859. }
  860. int pixel_coord = (height * 0.5 - id_y) * width + id_x + width * 0.5;
  861. if (pixel_coord >= width * height || pixel_coord < 0){
  862. continue;
  863. }
  864. frame_buffer[pixel_coord] = frame_pixel;
  865. }}}
  866. else if (be_master_renderer){
  867. erchk(clSetKernelArg(compiled_kernel_master, 0, sizeof(cl_mem), &object_memory_buffer));
  868. erchk(clSetKernelArg(compiled_kernel_master, 1, sizeof(cl_mem), &verticies_memory_buffer));
  869. erchk(clSetKernelArg(compiled_kernel_master, 2, sizeof(cl_mem), &normals_memory_buffer));
  870. erchk(clSetKernelArg(compiled_kernel_master, 3, sizeof(cl_mem), &uvs_memory_buffer));
  871. erchk(clSetKernelArg(compiled_kernel_master, 4, sizeof(cl_mem), &triangles_memory_buffer));
  872. erchk(clSetKernelArg(compiled_kernel_master, 5, sizeof(cl_mem), &output_memory_buffer));
  873. erchk(clSetKernelArg(compiled_kernel_master, 6, sizeof(cl_mem), &textures_memory_buffer));
  874. erchk(clSetKernelArg(compiled_kernel_master, 7, sizeof(cl_mem), &texture_info_memory_buffer));
  875. erchk(clSetKernelArg(compiled_kernel_master, 8, sizeof(int), (void*)&object_count));
  876. erchk(clSetKernelArg(compiled_kernel_master, 9, sizeof(int), (void*)&width));
  877. erchk(clSetKernelArg(compiled_kernel_master, 10, sizeof(int), (void*)&height));
  878. erchk(clSetKernelArg(compiled_kernel_master, 11, sizeof(int), (void*)&show_buffer));
  879. erchk(clSetKernelArg(compiled_kernel_master, 12, sizeof(int), (void*)&frame));
  880. if (object_count > 0) {
  881. erchk(clEnqueueWriteBuffer(queue, object_memory_buffer, CL_TRUE, 0, sizeof(float) * object_size * object_count, objects, 0, NULL, NULL));
  882. erchk(clFinish(queue));
  883. debug_tick_func(1, "Wrote Objects Buffer");
  884. }
  885. if (vertex_count > 0){
  886. erchk(clEnqueueWriteBuffer(queue, verticies_memory_buffer, CL_TRUE, 0, sizeof(float) * vs * vertex_count, verticies, 0, NULL, NULL));
  887. erchk(clFinish(queue));
  888. debug_tick_func(1, "Wrote Verticies Buffer");
  889. }
  890. if (normal_count > 0){
  891. erchk(clEnqueueWriteBuffer(queue, normals_memory_buffer, CL_TRUE, 0, sizeof(float) * vs * normal_count, normals, 0, NULL, NULL));
  892. erchk(clFinish(queue));
  893. debug_tick_func(1, "Wrote Normals Buffer");
  894. }
  895. if (uv_count > 0){
  896. erchk(clEnqueueWriteBuffer(queue, uvs_memory_buffer, CL_TRUE, 0, sizeof(float) * vs * uv_count, uvs, 0, NULL, NULL));
  897. erchk(clFinish(queue));
  898. debug_tick_func(1, "Wrote UVS Buffer");
  899. }
  900. if (face_count > 0){
  901. erchk(clEnqueueWriteBuffer(queue, triangles_memory_buffer, CL_TRUE, 0, sizeof(int) * ts * face_count, triangles, 0, NULL, NULL));
  902. erchk(clFinish(queue));
  903. debug_tick_func(1, "Wrote Triangles Buffer");
  904. }
  905. if (texture_count > 0){
  906. erchk(clEnqueueWriteBuffer(queue, textures_memory_buffer, CL_TRUE, 0, sizeof(unsigned char) * 4 * textures_size, textures, 0, NULL, NULL));
  907. erchk(clFinish(queue));
  908. erchk(clEnqueueWriteBuffer(queue, texture_info_memory_buffer, CL_TRUE, 0, sizeof(int) * tis * texture_count, texture_info, 0, NULL, NULL));
  909. erchk(clFinish(queue));
  910. debug_tick_func(1, "Wrote Textures Buffer and Texture Info Buffer");
  911. }
  912. erchk(clEnqueueFillBuffer(queue, output_memory_buffer, &pattern, sizeof(RI_uint), 0, sizeof(RI_uint) * width * height, 0, NULL, NULL));
  913. erchk(clFinish(queue));
  914. debug_tick_func(1, "Cleared Frame Buffer");
  915. size_t local_size_2d[2] = {sqrt(local_size), sqrt(local_size)};
  916. erchk(clEnqueueNDRangeKernel(queue, compiled_kernel_master, 2, NULL, size_2d, local_size_2d, 0, NULL, NULL));
  917. erchk(clFinish(queue));
  918. erchk(clEnqueueReadBuffer(queue, output_memory_buffer, CL_TRUE, 0, sizeof(RI_uint) * width * height, frame_buffer, 0, NULL, NULL));
  919. erchk(clFinish(queue));
  920. debug_tick_func(1, "Read Frame Buffer");
  921. }
  922. else{
  923. if (polygons == NULL)
  924. {
  925. debug_tick_func(0, "Polygons is not Allocated");
  926. return RI_ERROR;
  927. }
  928. if (frame_buffer == NULL)
  929. {
  930. debug_tick_func(0, "Frame Buffer is not Allocated");
  931. return RI_ERROR;
  932. }
  933. if (show_buffer == RI_BUFFER_Z){
  934. for (int i_polygon = 2; i_polygon < polygon_count * 9; i_polygon+=3){
  935. if (polygons[i_polygon] > highest_z){
  936. highest_z = polygons[i_polygon];
  937. }
  938. }
  939. debug_tick_func(1, "Highest Z: %f", highest_z);
  940. }
  941. erchk(clSetKernelArg(compiled_kernel_non_master, 0, sizeof(cl_mem), &input_memory_buffer));
  942. erchk(clSetKernelArg(compiled_kernel_non_master, 1, sizeof(cl_mem), &output_memory_buffer));
  943. erchk(clSetKernelArg(compiled_kernel_non_master, 2, sizeof(int), (void*)&polygon_count));
  944. erchk(clSetKernelArg(compiled_kernel_non_master, 3, sizeof(int), (void*)&width));
  945. erchk(clSetKernelArg(compiled_kernel_non_master, 4, sizeof(int), (void*)&height));
  946. erchk(clSetKernelArg(compiled_kernel_non_master, 5, sizeof(int), (void*)&show_buffer));
  947. erchk(clSetKernelArg(compiled_kernel_non_master, 6, sizeof(float), (void*)&highest_z));
  948. erchk(clEnqueueWriteBuffer(queue, input_memory_buffer, CL_TRUE, 0, sizeof(float) * 3 * 3 * polygon_count, polygons, 0, NULL, NULL));
  949. erchk(clFinish(queue));
  950. debug_tick_func(1, "Wrote Polygon Buffer");
  951. erchk(clEnqueueFillBuffer(queue, output_memory_buffer, &pattern, sizeof(RI_uint), 0, sizeof(RI_uint) * width * height, 0, NULL, NULL));
  952. erchk(clFinish(queue));
  953. debug_tick_func(1, "Cleared Frame Buffer");
  954. size_t local_size_2d[2] = {sqrt(local_size), sqrt(local_size)};
  955. erchk(clEnqueueNDRangeKernel(queue, compiled_kernel_non_master, 2, NULL, size_2d, local_size_2d, 0, NULL, NULL));
  956. erchk(clFinish(queue));
  957. erchk(clEnqueueReadBuffer(queue, output_memory_buffer, CL_TRUE, 0, sizeof(RI_uint) * width * height, frame_buffer, 0, NULL, NULL));
  958. erchk(clFinish(queue));
  959. debug_tick_func(1, "Read Frame Buffer");
  960. }
  961. SDL_Event event;
  962. while (SDL_PollEvent(&event))
  963. {
  964. switch (event.type)
  965. {
  966. case SDL_QUIT:
  967. running = 0;
  968. }
  969. }
  970. SDL_UpdateTexture(texture, NULL, frame_buffer, width * sizeof(RI_uint));
  971. SDL_RenderClear(renderer);
  972. SDL_RenderCopy(renderer, texture, NULL, NULL);
  973. int total_text_height = 0;
  974. if (show_fps){
  975. char fps_string[50];
  976. sprintf(fps_string, "%.0f FPS", fps);
  977. text_surface = TTF_RenderText_Blended(font, fps_string, font_color);
  978. text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
  979. text_rect.x = 5;
  980. text_rect.y = 0;
  981. text_rect.h = text_surface->h;
  982. text_rect.w = text_surface->w;
  983. total_text_height += text_surface->h;
  984. SDL_RenderCopy(renderer, text_texture, NULL, &text_rect);
  985. }
  986. if (debug_frame){
  987. debug_tick_func(0, "Frame: %d", frame);
  988. }
  989. if (show_frame){
  990. char frame_string[50];
  991. sprintf(frame_string, "Frame #%d", frame);
  992. text_surface = TTF_RenderText_Blended(font, frame_string, font_color);
  993. text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
  994. text_rect.x = 5;
  995. text_rect.y = total_text_height;
  996. text_rect.h = text_surface->h;
  997. text_rect.w = text_surface->w;
  998. total_text_height += text_surface->h;
  999. SDL_RenderCopy(renderer, text_texture, NULL, &text_rect);
  1000. }
  1001. if (show_info){
  1002. char frame_string[256];
  1003. sprintf(frame_string, "%d objects, %d triangles, %d verticies, %d normals, %d UVS, %d pixels (%dx%d), FPS cap: %d", object_count, face_count, vertex_count, normal_count, uv_count, width * height, width, height, fps_cap);
  1004. text_surface = TTF_RenderText_Blended_Wrapped(font, frame_string, font_color, width);
  1005. text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
  1006. text_rect.x = 5;
  1007. text_rect.y = total_text_height;
  1008. text_rect.h = text_surface->h;
  1009. text_rect.w = text_surface->w;
  1010. total_text_height += text_surface->h;
  1011. SDL_RenderCopy(renderer, text_texture, NULL, &text_rect);
  1012. }
  1013. SDL_RenderPresent(renderer);
  1014. frame++;
  1015. if (fps_cap > 0 && fps > fps_cap){
  1016. elapsed_ticks = SDL_GetPerformanceCounter() - start_time;
  1017. delta_time = elapsed_ticks / (double)SDL_GetPerformanceFrequency();
  1018. double target_frame_time = 1.0 / fps_cap;
  1019. SDL_Delay((Uint32)((target_frame_time - delta_time) * 1000.0));
  1020. }
  1021. if (show_fps || debug_fps){
  1022. elapsed_ticks = SDL_GetPerformanceCounter() - start_time;
  1023. delta_time = elapsed_ticks / (double)SDL_GetPerformanceFrequency();
  1024. fps = 1.0 / delta_time;
  1025. }
  1026. if (debug_fps){
  1027. debug_tick_func(0, "FPS: %lf (%d polygons, %d pixels)", fps, polygon_count, width * height);
  1028. }
  1029. debug_tick_func(1, "Ticked");
  1030. return RI_SUCCESS;
  1031. }
  1032. else
  1033. {
  1034. return RI_ERROR;
  1035. }
  1036. }
  1037. RI_result RI_Stop(int quit){
  1038. debug(RI_DEBUG_HIGH, "Called RI_Stop");
  1039. debug(RI_DEBUG_LOW, "Stopping...");
  1040. running = 0;
  1041. if (!use_cpu){
  1042. clReleaseMemObject(input_memory_buffer);
  1043. clReleaseMemObject(output_memory_buffer);
  1044. clReleaseMemObject(object_memory_buffer);
  1045. clReleaseMemObject(verticies_memory_buffer);
  1046. clReleaseMemObject(normals_memory_buffer);
  1047. clReleaseMemObject(uvs_memory_buffer);
  1048. clReleaseMemObject(triangles_memory_buffer);
  1049. clReleaseMemObject(textures_memory_buffer);
  1050. clReleaseMemObject(texture_info_memory_buffer);
  1051. clReleaseKernel(compiled_kernel_non_master);
  1052. clReleaseProgram(kernel_program_non_master);
  1053. clReleaseKernel(compiled_kernel_master);
  1054. clReleaseProgram(kernel_program_master);
  1055. clReleaseCommandQueue(queue);
  1056. clReleaseContext(context);
  1057. }
  1058. SDL_FreeSurface(text_surface);
  1059. SDL_DestroyTexture(text_texture);
  1060. SDL_DestroyTexture(texture);
  1061. SDL_DestroyRenderer(renderer);
  1062. SDL_DestroyWindow(window);
  1063. SDL_Quit();
  1064. TTF_Quit();
  1065. if (polygons != NULL)
  1066. free(polygons);
  1067. else
  1068. debug(RI_DEBUG_LOW, "Polygons Was Unset on Stop");
  1069. if (frame_buffer != NULL)
  1070. free(frame_buffer);
  1071. else
  1072. debug(RI_DEBUG_LOW, "Frame-Buffer Was Unset on Stop");
  1073. if (objects != NULL)
  1074. free(objects);
  1075. else
  1076. debug(RI_DEBUG_LOW, "Objects Was Unset on Stop");
  1077. if (verticies != NULL)
  1078. free(verticies);
  1079. else
  1080. debug(RI_DEBUG_LOW, "Verticies Was Unset on Stop");
  1081. if (normals != NULL)
  1082. free(normals);
  1083. else
  1084. debug(RI_DEBUG_LOW, "Normals Was Unset on Stop");
  1085. if (uvs != NULL)
  1086. free(uvs);
  1087. else
  1088. debug(RI_DEBUG_LOW, "UVS Was Unset on Stop");
  1089. if (textures != NULL)
  1090. free(textures);
  1091. else
  1092. debug(RI_DEBUG_LOW, "Textures Was Unset on Stop");
  1093. if (texture_info != NULL)
  1094. free(texture_info);
  1095. else
  1096. debug(RI_DEBUG_LOW, "Texture Info Was Unset on Stop");
  1097. debug(RI_DEBUG_LOW, "Stopped");
  1098. if (quit){
  1099. exit(0);
  1100. }
  1101. return RI_SUCCESS;
  1102. }
  1103. // ----- Renderer Action Functions
  1104. // ----- INIT
  1105. RI_result Rendering_init(char *title){
  1106. debug(RI_DEBUG_HIGH, "Called Rendering_init");
  1107. debug(RI_DEBUG_LOW, "Initializing Rendering...");
  1108. frame_buffer = malloc(sizeof(RI_uint) * width * height);
  1109. if (frame_buffer == NULL)
  1110. {
  1111. debug(RI_DEBUG_LOW, "Couldn't Allocate Frame Buffer");
  1112. return RI_ERROR;
  1113. }
  1114. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  1115. {
  1116. debug(RI_DEBUG_LOW, "SDL_Init Failed: %s", SDL_GetError());
  1117. return RI_ERROR;
  1118. }
  1119. if (TTF_Init() == -1) {
  1120. debug(RI_DEBUG_LOW, "TFF_Init Failed: %s", TTF_GetError());
  1121. SDL_Quit();
  1122. return -1;
  1123. }
  1124. if (width <= 0 || height <= 0)
  1125. {
  1126. debug(RI_DEBUG_LOW, "Invalid width or height");
  1127. return RI_ERROR;
  1128. }
  1129. window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
  1130. if (!window)
  1131. {
  1132. debug(RI_DEBUG_LOW, "SDL_CreateWindow Failed");
  1133. return RI_ERROR;
  1134. }
  1135. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  1136. if (!renderer)
  1137. {
  1138. debug(RI_DEBUG_LOW, "SDL_CreateRenderer Failed");
  1139. return RI_ERROR;
  1140. }
  1141. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
  1142. if (!texture)
  1143. {
  1144. debug(RI_DEBUG_LOW, "SDL_CreateTexture Failed");
  1145. return RI_ERROR;
  1146. }
  1147. font = TTF_OpenFont(font_file, font_size);
  1148. text_surface = TTF_RenderText_Solid(font, "FPS", font_color);
  1149. if (text_surface == NULL){
  1150. debug(RI_DEBUG_LOW, "TTF_RenderText_Solid Failed: %s", TTF_GetError());
  1151. return RI_ERROR;
  1152. }
  1153. text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
  1154. if (text_texture == NULL){
  1155. debug(RI_DEBUG_LOW, "SDL_CreateTextureFromSurface Failed");
  1156. return RI_ERROR;
  1157. }
  1158. debug(RI_DEBUG_LOW, "Initialized Rendering");
  1159. return RI_SUCCESS;
  1160. }
  1161. RI_result OpenCL_init(){
  1162. debug(RI_DEBUG_HIGH, "Called OpenCL_init");
  1163. debug(RI_DEBUG_LOW, "Initialiing OpenCL...");
  1164. clGetPlatformIDs(1, &platform, &number_of_platforms);
  1165. if (number_of_platforms == 0)
  1166. {
  1167. debug(RI_DEBUG_LOW, "No OpenCL Platforms. Switching to CPU");
  1168. use_cpu = 1;
  1169. return RI_ERROR;
  1170. }
  1171. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, &number_of_devices);
  1172. if (number_of_devices == 0)
  1173. {
  1174. debug(RI_DEBUG_LOW, "No Valid GPU's Found. Switching to CPU");
  1175. use_cpu = 1;
  1176. return RI_ERROR;
  1177. }
  1178. context = clCreateContext(NULL, 1, &device, NULL, NULL, &error);
  1179. erchk(error);
  1180. queue = clCreateCommandQueue(context, device, 0, &error);
  1181. erchk(error);
  1182. output_memory_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(RI_uint) * width * height, NULL, &error);
  1183. erchk(error);
  1184. kernel_program_non_master = clCreateProgramWithSource(context, 1, &kernel_source_non_master, NULL, &error);
  1185. erchk(error);
  1186. error = clBuildProgram(kernel_program_non_master, 1, &device, NULL, NULL, NULL);
  1187. if (error == -11){
  1188. size_t log_size;
  1189. clGetProgramBuildInfo(kernel_program_non_master, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  1190. char *log = malloc(log_size);
  1191. clGetProgramBuildInfo(kernel_program_non_master, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
  1192. fprintf(stderr, "Build log:\n%s\n", log);
  1193. free(log);
  1194. RI_Stop(0);
  1195. }
  1196. compiled_kernel_non_master = clCreateKernel(kernel_program_non_master, "raster_kernel", &error);
  1197. erchk(error);
  1198. kernel_program_master = clCreateProgramWithSource(context, 1, &kernel_source_master, NULL, &error);
  1199. erchk(error);
  1200. error = clBuildProgram(kernel_program_master, 1, &device, NULL, NULL, NULL);
  1201. if (error == -11){
  1202. size_t log_size;
  1203. clGetProgramBuildInfo(kernel_program_master, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  1204. char *log = malloc(log_size);
  1205. clGetProgramBuildInfo(kernel_program_master, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
  1206. fprintf(stderr, "Build log:\n%s\n", log);
  1207. free(log);
  1208. RI_Stop(0);
  1209. }
  1210. compiled_kernel_master = clCreateKernel(kernel_program_master, "raster_kernel", &error);
  1211. erchk(error);
  1212. erchk(clGetKernelWorkGroupInfo(compiled_kernel_master, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local_size), &local_size, NULL));
  1213. debug(RI_DEBUG_MEDIUM, "Local Size: %d", local_size);
  1214. size_2d[0] = width;
  1215. size_2d[1] = height;
  1216. pattern = 0x22222222;
  1217. debug(RI_DEBUG_LOW, "Initialized OpenCL");
  1218. return RI_SUCCESS;
  1219. }
  1220. RI_result RI_Init(int RI_WindowWidth, int RI_WindowHeight, char *RI_WindowTitle){
  1221. debug(RI_DEBUG_HIGH, "Called RI_Init");
  1222. srand(time(NULL));
  1223. width = RI_WindowWidth;
  1224. height = RI_WindowHeight;
  1225. if (!use_cpu && OpenCL_init() == RI_ERROR){
  1226. if (!use_cpu){
  1227. return RI_ERROR;
  1228. }
  1229. }
  1230. if (Rendering_init(RI_WindowTitle) == RI_ERROR){
  1231. return RI_ERROR;
  1232. }
  1233. return RI_SUCCESS;
  1234. }
  1235. // ----- INIT