main_test.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "stdlib.h"
  2. #include "SDL2/SDL.h"
  3. #include "math.h"
  4. #include "time.h"
  5. #include <CL/cl.h>
  6. const int WIDTH = 800;
  7. const int HEIGHT = 800;
  8. const int POLYGON_SIZE = sizeof(float) * 3 * 3;
  9. const int POLYGONS = 5;
  10. int is_intersecting(int a, int b, int c, int d, int p, int q, int r, int s) {
  11. float det, gamma, lambda;
  12. det = (c - a) * (s - q) - (r - p) * (d - b);
  13. if (det == 0) {
  14. return 1;
  15. }
  16. else {
  17. lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
  18. gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
  19. return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1);
  20. }
  21. }
  22. void norm(float dest[2], float a[2]){
  23. float magnitude = sqrt((pow(a[0], 2) + pow(a[1], 2)));
  24. dest[0] = a[0] / magnitude;
  25. dest[1] = a[1] / magnitude;
  26. }
  27. void sub(float dest[2], float a[2], float b[2]){
  28. dest[0] = a[0] - b[0];
  29. dest[1] = a[1] - b[1];
  30. }
  31. void add(float dest[2], float a[2], float b[2]){
  32. dest[0] = a[0] + b[0];
  33. dest[1] = a[1] + b[1];
  34. }
  35. const char* kernel_source =
  36. "__kernel void raster_kernel(__global float* polygons, __global uint* frame_buffer)\n"
  37. "int id = get_global_id(0); \n"
  38. " \n"
  39. " \n"
  40. "frame_buffer[id] = 80085; \n"
  41. " \n"
  42. "}\n";
  43. int main(){
  44. srand(time(NULL));
  45. float polygons[POLYGONS][3][3];
  46. cl_uint frame_buffer[WIDTH * HEIGHT];
  47. float z_buffer[WIDTH * HEIGHT];
  48. // ----- Check for Valid Platforms & GPUs
  49. cl_platform_id platform;
  50. cl_device_id device;
  51. cl_uint number_of_platforms, number_of_devices;
  52. clGetPlatformIDs(1, &platform, &number_of_platforms);
  53. if(number_of_platforms == 0){
  54. printf("No OpenCL Platforms");
  55. }
  56. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, &number_of_devices);
  57. if (number_of_devices == 0){
  58. printf("No GPU's Found");
  59. }
  60. // -----
  61. // ----- Setup OpenCL
  62. cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
  63. cl_command_queue queue = clCreateCommandQueue(context, device, 0, NULL);
  64. cl_mem input_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * 3 * 3 * POLYGONS, polygons, NULL);
  65. cl_mem output_memory_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_uint) * POLYGONS, NULL, NULL);
  66. cl_program kernel_program = clCreateProgramWithSource(context, 1, &kernel_source, NULL, NULL);
  67. clBuildProgram(kernel_program, 1, &device, NULL, NULL, NULL);
  68. cl_kernel compiled_kernel = clCreateKernel(kernel_program, "raster_kernel", NULL);
  69. clSetKernelArg(compiled_kernel, 0, sizeof(cl_mem), &input_memory_buffer);
  70. clSetKernelArg(compiled_kernel, 1, sizeof(cl_mem), &output_memory_buffer);
  71. size_t size = POLYGONS;
  72. cl_uint pattern = 121212;
  73. // -----
  74. SDL_Init(SDL_INIT_VIDEO);
  75. SDL_Window* window = SDL_CreateWindow("Rasterizer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
  76. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  77. SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
  78. int running = 1;
  79. int frame = 0;
  80. Uint32 start_time, frame_time;
  81. float fps;
  82. while (running) {
  83. start_time = SDL_GetTicks();
  84. //if (frame % 1 == 0){
  85. for (int p = 0; p < POLYGONS; p++){
  86. for (int point = 0; point < 3; point++){
  87. for (int i = 0; i < 3; i++){
  88. polygons[p][point][i] = rand() % WIDTH + 1;
  89. }
  90. }
  91. }
  92. //}
  93. memset(&z_buffer, 0, sizeof(float) * WIDTH * HEIGHT);
  94. clEnqueueFillBuffer(queue, output_memory_buffer, &pattern, sizeof(cl_uint), 0, sizeof(cl_uint) * POLYGONS, 0, NULL, NULL);
  95. clEnqueueNDRangeKernel(queue, compiled_kernel, 1, NULL, &size, NULL, 0, NULL, NULL);
  96. clFinish(queue);
  97. clEnqueueReadBuffer(queue, output_memory_buffer, CL_TRUE, 0, sizeof(cl_uint) * POLYGONS, &frame_buffer, 0, NULL, NULL);
  98. for (int i = 0; i < POLYGONS; i++){
  99. printf("%u\n", frame_buffer[i]);
  100. }
  101. SDL_Event event;
  102. while (SDL_PollEvent(&event)){
  103. switch (event.type){
  104. case SDL_QUIT:
  105. running = 0;
  106. }
  107. }
  108. // for (int polygon = 0; polygon < POLYGONS; polygon++){
  109. // float x0 = polygons[polygon][0][0];
  110. // float y0 = polygons[polygon][0][1];
  111. // float z0 = polygons[polygon][0][2];
  112. // float x1 = polygons[polygon][1][0];
  113. // float y1 = polygons[polygon][1][1];
  114. // float z1 = polygons[polygon][1][2];
  115. // float x2 = polygons[polygon][2][0];
  116. // float y2 = polygons[polygon][2][1];
  117. // float z2 = polygons[polygon][2][2];
  118. // float smallest_x = x0;
  119. // float largest_x = x0;
  120. // float smallest_y = y0;
  121. // float largest_y = y0;
  122. // for (int point = 0; point < 3; point++){
  123. // float x = polygons[polygon][point][0];
  124. // float y = polygons[polygon][point][1];
  125. // if (x > largest_x){
  126. // largest_x = x;
  127. // }
  128. // if (x < smallest_x){
  129. // smallest_x = x;
  130. // }
  131. // if (y > largest_y){
  132. // largest_y = y;
  133. // }
  134. // if (y < smallest_y){
  135. // smallest_y = y;
  136. // }
  137. // }
  138. // smallest_x = fmin(smallest_x, 0);
  139. // largest_x = fmax(largest_x, WIDTH);
  140. // smallest_y = fmin(smallest_y, 0);
  141. // largest_y = fmax(largest_y, HEIGHT);
  142. // // test every pixel in a rect around the triangle. If it's inside, color it.
  143. // for (int x = (int)smallest_x; x < largest_x; x++){
  144. // for (int y = (int)smallest_y; y < largest_y; y++){
  145. // int intersections = 0;
  146. // for (int i = 0; i < 3; i++){
  147. // intersections += is_intersecting(x, y, 10000, 100000, polygons[polygon][i][0], polygons[polygon][i][1], polygons[polygon][(i + 1) % 3][0], polygons[polygon][(i + 1) % 3][1]);
  148. // }
  149. // if (intersections % 2 == 0){
  150. // continue;
  151. // }
  152. // float denominator = (y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2);
  153. // float w0 = ((y1 - y2) * (x - x2) + (x2 - x1) * (y - y2)) / denominator;
  154. // float w1 = ((y2 - y0) * (x - x0) + (x0 - x2) * (y - y2)) / denominator;
  155. // float w2 = 1.0 - w0 - w1;
  156. // if (denominator < 0) {
  157. // w0 = -w0;
  158. // w1 = -w1;
  159. // w2 = -w2;
  160. // denominator = -denominator;
  161. // }
  162. // float z = w0 * z0 + w1 * z1 + w2 * z2;
  163. // if (z < z_buffer[y * WIDTH + x]){
  164. // z_buffer[y * WIDTH + x] = z;
  165. // }
  166. // else {
  167. // continue;
  168. // }
  169. // if (z < 0){
  170. // z *= -1;
  171. // }
  172. // frame_buffer[y * WIDTH + x] = 0xFFFFFFFF / POLYGONS * (polygon + 1);
  173. // }
  174. // }
  175. // }
  176. SDL_UpdateTexture(texture, NULL, frame_buffer, WIDTH * sizeof(cl_uint));
  177. SDL_RenderClear(renderer);
  178. SDL_RenderCopy(renderer, texture, NULL, NULL);
  179. SDL_RenderPresent(renderer);
  180. frame++;
  181. frame_time = SDL_GetTicks()-start_time;
  182. fps = (frame_time > 0) ? 1000.0f / frame_time : 0.0f;
  183. // printf("%f fps\n", fps);
  184. // printf("%d polygons\n", POLYGONS);
  185. }
  186. clReleaseMemObject(input_memory_buffer);
  187. clReleaseMemObject(output_memory_buffer);
  188. clReleaseKernel(compiled_kernel);
  189. clReleaseProgram(kernel_program);
  190. clReleaseCommandQueue(queue);
  191. clReleaseContext(context);
  192. SDL_DestroyTexture(texture);
  193. SDL_DestroyRenderer(renderer);
  194. SDL_DestroyWindow(window);
  195. SDL_Quit();
  196. }