Browse Source

added flags for clearing freshly allocated polygons

IverMartinson 8 months ago
parent
commit
23ff6d44b5

+ 2 - 1
.vscode/settings.json

@@ -9,6 +9,7 @@
         "tuple": "c",
         "utility": "c",
         "cl.h": "c",
-        "stdio.h": "c"
+        "stdio.h": "c",
+        "stdint.h": "c"
     }
 }

BIN
builds/final binaries/example.bin


BIN
builds/final binaries/librasteriver.so


+ 7 - 5
src/RasterIver/headers/rasteriver.h

@@ -19,11 +19,13 @@ typedef enum {
 
 // RI_flag
 typedef enum {
-    RI_FLAG_DEBUG           = 0,
-    RI_FLAG_DEBUG_VERBOSE   = 1,
-    RI_FLAG_SHOW_Z_BUFFER   = 2,
-    RI_FLAG_SHOW_FPS        = 3,
-    RI_FLAG_DEBUG_FPS       = 4,
+    RI_FLAG_DEBUG               = 0,
+    RI_FLAG_DEBUG_VERBOSE       = 1,
+    RI_FLAG_SHOW_Z_BUFFER       = 2,
+    RI_FLAG_SHOW_FPS            = 3,
+    RI_FLAG_DEBUG_FPS           = 4,
+    RI_FLAG_CLEAN_POLYGONS      = 5,
+    RI_FLAG_POPULATE_POLYGONS   = 6,
 } RI_flag_enum;
 
 RI_result   RI_Init();

+ 29 - 4
src/RasterIver/source code/rasteriver.c

@@ -60,6 +60,10 @@ __kernel void raster_kernel(__global float* polygons, __global uint* frame_buffe
         float y2 = polygons[base + 7]; \
         float z2 = polygons[base + 8]; \
         \
+        if (isinf(x0) || isinf(y0) || isinf(z0) || isinf(x1) || isinf(y1) || isinf(z1) || isinf(x2) || isinf(y2) || isinf(z2)){\
+            return;\
+        }\
+        \
         float smallest_x = x0; \
         float largest_x = x0; \
         float smallest_y = y0; \
@@ -165,6 +169,8 @@ int show_debug = 0;
 int debug_verbose = 0;
 int show_fps = 0;
 int debug_fps = 0;
+int clean_polygons = 0;
+int populate_polygons = 0;
 
 Uint64 start_time;
 double frame_time_ms;
@@ -262,6 +268,8 @@ RI_result RI_ListFlags(){
     printf("RI_FLAG_SHOW_Z_BUFFER: Whether or not to render the Z buffer");
     printf("RI_FLAG_SHOW_FPS: Render FPS on screen");
     printf("RI_FLAG_DEBUG_FPS: Debug FPS into the console");
+    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)");
+    printf("RI_FLAG_POPULATE_POLYGONS: When requesting polygons, populate the array with random triangles (useful for testing/benchmark)\n");
 
     return RI_SUCCESS;
 }
@@ -292,6 +300,14 @@ RI_result RI_SetFlag(RI_flag RI_FlagToSet, int RI_Value)
         debug_fps = RI_Value;
         break;
 
+    case RI_FLAG_CLEAN_POLYGONS:
+        clean_polygons = RI_Value;
+        break;
+    
+    case RI_FLAG_POPULATE_POLYGONS:
+        populate_polygons = RI_Value;
+        break;
+
     default:
         return RI_INVALID_FLAG;
     }
@@ -326,9 +342,18 @@ RI_polygons RI_RequestPolygons(int RI_PolygonsToRequest){
         return (float*)RI_ERROR;
     }
     
-    for (int p = 0; p < polygon_count * 9; p++)
-    {
-        polygons[p] = rand() % 800;
+    
+    for (int p = 0; p < polygon_count * 9; p += 3){     
+        if (clean_polygons){
+            polygons[p] = INFINITY;
+            polygons[p + 1] = INFINITY;
+            polygons[p + 2] = INFINITY;
+        }
+        else if (populate_polygons){
+            polygons[p] = rand() % width;
+            polygons[p + 1] = rand() % height;
+            polygons[p + 2] = rand() % ((width + height) / 2);
+        }
     }
     
     input_memory_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, size, polygons, &error);
@@ -595,7 +620,7 @@ RI_result OpenCL_init(){
 
 RI_result RI_Init(int RI_WindowWidth, int RI_WindowHeight, char *RI_WindowTitle)
 {
-    srand(time(NULL));
+    srand(time(NULL));                                                         
 
     width = RI_WindowWidth;
     height = RI_WindowHeight;

+ 9 - 4
src/test programs/example.c

@@ -1,23 +1,28 @@
 #include <stdio.h>
 #include "../RasterIver/headers/rasteriver.h"
+#include <time.h>
+#include <stdlib.h>
+
+int main(){ 
+    srand(time(NULL));                                                         
 
-int main(){
     RI_SetFlag(RI_FLAG_DEBUG, 1);
     RI_SetFlag(RI_FLAG_DEBUG_VERBOSE, 0);
     RI_SetFlag(RI_FLAG_DEBUG_FPS, 1);
-    RI_SetFlag(RI_FLAG_SHOW_Z_BUFFER, 1);
+    RI_SetFlag(RI_FLAG_SHOW_Z_BUFFER, 0);
+    RI_SetFlag(RI_FLAG_CLEAN_POLYGONS, 1);
     RI_SetFpsCap(60);
     
     if (RI_Init(800, 800, "Rasteriver Test") == RI_ERROR){
         return 1;
     }
 
-    RI_polygons polygons = RI_RequestPolygons(1);
+    RI_polygons polygons = RI_RequestPolygons(rand() % 10000);  
 
     polygons[0] = 622;
     polygons[1] = 213;
     polygons[2] = 123;
-
+    
     while (RI_IsRunning() == RI_RUNNING){
         RI_Tick();
     }