Browse Source

rasterizer skips triangles that are out of view, removed redundant transformation code

iver 7 months ago
parent
commit
f2ac614fef

BIN
builds/final binaries/librasteriver.so


BIN
builds/final binaries/main.bin


+ 4 - 36
src/RasterIver/kernels/master_kernel.h

@@ -132,9 +132,9 @@ void rotate_euler(float *x, float *y, float *z, float r_x, float r_y, float r_z)
     *z = temp_z;\
 };\
 \
-__kernel void raster_kernel(__global Object* objects, __global float* verticies, __global float* normals, __global float* uvs, __global int* triangles, __global uint* frame_buffer, __global uchar* textures, __global int* texture_info, int object_count, int width, int height, int show_buffer, int frame, float fov, int total_triangle_count, int total_vertex_count){ \
-    int id_x = get_global_id(0) - width / 2; \
-    int id_y = get_global_id(1) - height / 2; \
+__kernel void raster_kernel(__global Object* objects, __global float* verticies, __global float* normals, __global float* uvs, __global int* triangles, __global uint* frame_buffer, __global uchar* textures, __global int* texture_info, int object_count, int width, int height, int show_buffer, int frame, float fov, int total_triangle_count, int total_vertex_count, int h_width, int h_height){ \
+    int id_x = get_global_id(0) - h_width; \
+    int id_y = get_global_id(1) - h_height; \
     \
     float vertical_fov_factor = height / tan(0.5 * fov);\
     float horizontal_fov_factor = width / tan(0.5 * fov);\
@@ -200,27 +200,6 @@ __kernel void raster_kernel(__global Object* objects, __global float* verticies,
             float x2 = verticies[i2 + 0];\
             float y2 = verticies[i2 + 1];\
             \
-            if (object_r_w <= -9999999){\
-                rotate_euler(&x0, &y0, &z0, object_r_x, object_r_y, object_r_z);\
-                rotate_euler(&x1, &y1, &z1, object_r_x, object_r_y, object_r_z);\
-                rotate_euler(&x2, &y2, &z2, object_r_x, object_r_y, object_r_z);\
-            }\
-            else{\
-                rotate_euler(&x0, &y0, &z0, object_r_x, object_r_y, object_r_z);\
-                rotate_euler(&x1, &y1, &z1, object_r_x, object_r_y, object_r_z);\
-                rotate_euler(&x2, &y2, &z2, object_r_x, object_r_y, object_r_z);\
-            }\
-            \
-            z0 = (z0 * object_s_z + object_z);\
-            x0 = (x0 * object_s_x + object_x) / z0 * horizontal_fov_factor;\
-            y0 = (y0 * object_s_y + object_y) / z0 * vertical_fov_factor;\
-            z1 = (z1 * object_s_z + object_z);\
-            x1 = (x1 * object_s_x + object_x) / z1 * horizontal_fov_factor;\
-            y1 = (y1 * object_s_y + object_y) / z1 * vertical_fov_factor;\
-            z2 = (z2 * object_s_z + object_z);\
-            y2 = (y2 * object_s_y + object_y) / z2 * horizontal_fov_factor;\
-            x2 = (x2 * object_s_x + object_x) / z2 * vertical_fov_factor;\
-            \
             if (i3 < 0 || i4 < 0 || i5 < 0){\
                 has_normals = 0;\
             }\
@@ -307,17 +286,6 @@ __kernel void raster_kernel(__global Object* objects, __global float* verticies,
                     float u_y2 = uvs[i8 + 1];\
                     float u_z2 = uvs[i8 + 2];\
                     \
-                    if (object_r_w <= -9999999){\
-                        rotate_euler(&n_x0, &n_y0, &n_z0, object_r_x, object_r_y, object_r_z);\
-                        rotate_euler(&n_x1, &n_y1, &n_z1, object_r_x, object_r_y, object_r_z);\
-                        rotate_euler(&n_x2, &n_y2, &n_z2, object_r_x, object_r_y, object_r_z);\
-                    }\
-                    else{\
-                        rotate_euler(&n_x0, &n_y0, &n_z0, object_r_x, object_r_y, object_r_z);\
-                        rotate_euler(&n_x1, &n_y1, &n_z1, object_r_x, object_r_y, object_r_z);\
-                        rotate_euler(&n_x2, &n_y2, &n_z2, object_r_x, object_r_y, object_r_z);\
-                    }\
-                    \
                     switch (show_buffer){\
                         case 0:{\
                             double ux = (w0 * (u_x0 / z0) + w1 * (u_x1 / z1) + w2 * (u_x2 / z2)) / w_over_z;\
@@ -400,7 +368,7 @@ __kernel void raster_kernel(__global Object* objects, __global float* verticies,
         }\
     }\
     \
-    int pixel_coord = (height * 0.5 - id_y) * width + id_x + width * 0.5;\
+    int pixel_coord = (h_height - id_y) * width + id_x + h_width;\
     \
     if (pixel_coord >= width * height || pixel_coord < 0){\
         return;\

+ 2 - 2
src/RasterIver/kernels/transformer.h

@@ -100,7 +100,7 @@ void rotate_euler(float *x, float *y, float *z, float r_x, float r_y, float r_z)
     *z = temp_z;\
 };\
 \
-__kernel void transformer_kernel(__global Object* objects, __global float* verticies, __global float* normals, __global int* triangles, __global float* transformed_verticies, __global float* transformed_normals, float fov, int width, int height, __global uint* frame_buffer){ \
+__kernel void transformer_kernel(__global Object* objects, __global float* verticies, __global float* normals, __global int* triangles, __global float* transformed_verticies, __global float* transformed_normals, float fov, int width, int height, __global uint* frame_buffer, int ri_h_width, int ri_h_height){ \
     int id_x = get_global_id(0);\
     \
     float vertical_fov_factor = height / tan(0.5 * fov);\
@@ -183,7 +183,7 @@ __kernel void transformer_kernel(__global Object* objects, __global float* verti
         y2 = (y2 * object_s_y + object_y) / z2 * horizontal_fov_factor;\
         x2 = (x2 * object_s_x + object_x) / z2 * vertical_fov_factor;\
         \
-        if ((x0 < 0 && x1 < 0 && x2 < 0) || (y0 < 0 && y1 < 0 && y2 < 0) || (x0 >= width && x1 >= width && x2 >= width) || (y0 >= height && y1 >= height && y2 >= height)){\
+        if ((x0 < -ri_h_width && x1 < -ri_h_width && x2 < -ri_h_width) || (y0 < -ri_h_height && y1 < -ri_h_height && y2 < -ri_h_height) || (x0 >= ri_h_width && x1 >= ri_h_width && x2 >= ri_h_width) || (y0 >= ri_h_height && y1 >= ri_h_height && y2 >= ri_h_height)){\
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = 999999999;\
         }\
         else{\

+ 19 - 8
src/RasterIver/source code/rasteriver.c

@@ -14,6 +14,9 @@
 int ri_width;
 int ri_height;
 
+int ri_h_width;
+int ri_h_height;
+
 float highest_z = 0;
 
 int polygon_count;
@@ -1196,11 +1199,10 @@ RI_result RI_Tick(){
         y2 = (y2 * object_s_y + object_y) / z2 * horizontal_fov_factor;
         x2 = (x2 * object_s_x + object_x) / z2 * vertical_fov_factor;
         
-        // this needs to be fixed becuase the edge of the screen is -width/2 and width/2 not 0 and width
-        // if ((x0 < 0 && x1 < 0 && x2 < 0) || (y0 < 0 && y1 < 0 && y2 < 0) || (x0 >= ri_width && x1 >= ri_width && x2 >= ri_width) || (y0 >= ri_height && y1 >= ri_height && y2 >= ri_height)){
-        //     transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = 9999;
-        // }
-        // else{
+        if ((x0 < -ri_h_width && x1 < -ri_h_width && x2 < -ri_h_width) || (y0 < -ri_h_height && y1 < -ri_h_height && y2 < -ri_h_height) || (x0 >= ri_h_width && x1 >= ri_h_width && x2 >= ri_h_width) || (y0 >= ri_h_height && y1 >= ri_h_height && y2 >= ri_h_height)){
+            transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = 9999;
+        }
+        else{
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = x0;
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 1] = y0;
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 2] = z0;
@@ -1220,11 +1222,11 @@ RI_result RI_Tick(){
             transformed_normals[(triangles[triangle_base + 2] + transformed_normal_index) * 3 + 0] = n_x2;
             transformed_normals[(triangles[triangle_base + 2] + transformed_normal_index) * 3 + 1] = n_y2;
             transformed_normals[(triangles[triangle_base + 2] + transformed_normal_index) * 3 + 2] = n_z2;
-        // }
+        }
     }}
 
-            for (int id_y = -ri_height / 2; id_y < ri_height / 2; id_y++){
-                for (int id_x = -ri_width / 2; id_x < ri_width / 2; id_x++){
+            for (int id_y = -ri_h_height; id_y < ri_h_height; id_y++){
+                for (int id_x = -ri_h_width; id_x < ri_h_width; id_x++){
                     float z_pixel = INFINITY; 
                     unsigned int frame_pixel = 0x22222222; 
                     
@@ -1487,6 +1489,8 @@ RI_result RI_Tick(){
             erchk(clSetKernelArg(compiled_kernel_transformer, 7, sizeof(int), (void*)&ri_width));
             erchk(clSetKernelArg(compiled_kernel_transformer, 8, sizeof(int), (void*)&ri_height));
             erchk(clSetKernelArg(compiled_kernel_transformer, 9, sizeof(cl_mem), &output_memory_buffer));
+            erchk(clSetKernelArg(compiled_kernel_transformer, 10, sizeof(int), (void*)&ri_h_width));
+            erchk(clSetKernelArg(compiled_kernel_transformer, 11, sizeof(int), (void*)&ri_h_height));
 
             size_t size_1d[1] = {object_count};            
  
@@ -1511,6 +1515,8 @@ RI_result RI_Tick(){
             erchk(clSetKernelArg(compiled_kernel_master, 13, sizeof(float), (void*)&fov)); 
             erchk(clSetKernelArg(compiled_kernel_master, 14, sizeof(int), (void*)&face_count)); 
             erchk(clSetKernelArg(compiled_kernel_master, 15, sizeof(int), (void*)&vertex_count)); 
+            erchk(clSetKernelArg(compiled_kernel_master, 16, sizeof(int), (void*)&ri_h_width));
+            erchk(clSetKernelArg(compiled_kernel_master, 17, sizeof(int), (void*)&ri_h_height));
 
             // size_t local_size_2d[2] = {sqrt(local_size), sqrt(local_size)};
             size_t local_size_2d[2] = {16, 16};
@@ -1554,6 +1560,8 @@ RI_result RI_Tick(){
             erchk(clSetKernelArg(compiled_kernel_non_master, 4, sizeof(int), (void*)&ri_height));
             erchk(clSetKernelArg(compiled_kernel_non_master, 5, sizeof(int), (void*)&show_buffer)); 
             erchk(clSetKernelArg(compiled_kernel_non_master, 6, sizeof(float), (void*)&highest_z));
+            erchk(clSetKernelArg(compiled_kernel_non_master, 7, sizeof(int), (void*)&ri_h_width));
+            erchk(clSetKernelArg(compiled_kernel_non_master, 8, sizeof(int), (void*)&ri_h_height));
 
             erchk(clEnqueueWriteBuffer(queue, input_memory_buffer, CL_TRUE, 0, sizeof(float) * 3 * 3 * polygon_count, polygons, 0, NULL, NULL));
             erchk(clFinish(queue));
@@ -1964,6 +1972,9 @@ RI_result RI_Init(int RI_WindowWidth, int RI_WindowHeight, char *RI_WindowTitle)
     ri_width = RI_WindowWidth;
     ri_height = RI_WindowHeight;
 
+    ri_h_width = RI_WindowWidth * 0.5;
+    ri_h_height = RI_WindowHeight * 0.5;
+
     if (!use_cpu && OpenCL_init() == RI_ERROR){
         if (!use_cpu){
             return RI_ERROR;