Refactor some of the code.
This commit is contained in:
@@ -8,7 +8,7 @@ typedef enum
|
|||||||
{
|
{
|
||||||
PROGRESSIVE = 0,
|
PROGRESSIVE = 0,
|
||||||
TILE_BASED = 1,
|
TILE_BASED = 1,
|
||||||
} rendering_type_t;
|
} rendering_mode_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@@ -20,15 +20,14 @@ typedef struct
|
|||||||
uint32_t bucket_size;
|
uint32_t bucket_size;
|
||||||
} rendering_config_t;
|
} rendering_config_t;
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
scene_t* scene;
|
scene_t* scene;
|
||||||
render_target_t* render_target;
|
render_target_t* render_target;
|
||||||
const rendering_config_t* config;
|
const rendering_config_t* config;
|
||||||
|
|
||||||
rendering_type_t rendering_type;
|
rendering_mode_t rendering_type;
|
||||||
debug_flag_t rendering_flag;
|
debug_flag_t debug_flag;
|
||||||
bool is_done;
|
bool is_done;
|
||||||
} render_job_t;
|
} render_job_t;
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ typedef struct
|
|||||||
light_collection_t lights;
|
light_collection_t lights;
|
||||||
} scene_t;
|
} scene_t;
|
||||||
|
|
||||||
|
|
||||||
bool scene_init(scene_t* scene, uint64_t triangle_count, uint8_t material_count, uint32_t punctual_light_count);
|
bool scene_init(scene_t* scene, uint64_t triangle_count, uint8_t material_count, uint32_t punctual_light_count);
|
||||||
bool scene_build_bvh(scene_t* scene);
|
bool scene_build_bvh(scene_t* scene);
|
||||||
void scene_free(scene_t* scene);
|
void scene_free(scene_t* scene);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "Geometry/Triangle.h"
|
#include "Geometry/Triangle.h"
|
||||||
#include "cglm/struct/vec3.h"
|
#include "cglm/struct/vec3.h"
|
||||||
|
|
||||||
|
// TODO: We still have small amount of block dots in current implementation. It's because of floating point precision. May need to fall back to double or handle it in a different way.
|
||||||
hit_result_t ray_intersect_triangle(ray_t ray, triangle_t triangle)
|
hit_result_t ray_intersect_triangle(ray_t ray, triangle_t triangle)
|
||||||
{
|
{
|
||||||
hit_result_t result = {0};
|
hit_result_t result = {0};
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ void material_collection_free(material_collection_t* materials)
|
|||||||
{
|
{
|
||||||
if (materials->buffer != NULL)
|
if (materials->buffer != NULL)
|
||||||
{
|
{
|
||||||
for (uint8_t i; i < materials->count; i++)
|
for (uint8_t i = 0; i < materials->count; i++)
|
||||||
{
|
{
|
||||||
void* data = materials->buffer[i].data;
|
void* data = materials->buffer[i].data;
|
||||||
if (data != NULL)
|
if (data != NULL)
|
||||||
|
|||||||
@@ -4,13 +4,13 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
bool render_target_init(uint32_t width, uint32_t height, render_target_t* render_target)
|
bool render_target_init(uint32_t width, uint32_t height, render_target_t* render_target)
|
||||||
|
|
||||||
{
|
{
|
||||||
render_target->width = width;
|
render_target->width = width;
|
||||||
render_target->height = height;
|
render_target->height = height;
|
||||||
|
|
||||||
size_t size_of_pixel = sizeof(vec4s);
|
size_t size_of_pixel = sizeof(vec4s);
|
||||||
size_t buffer_size = (size_t)width * height * size_of_pixel;
|
size_t image_size = (size_t)width * height;
|
||||||
|
size_t buffer_size = image_size * size_of_pixel;
|
||||||
vec4s* buffer = (vec4s*)malloc(buffer_size);
|
vec4s* buffer = (vec4s*)malloc(buffer_size);
|
||||||
if (buffer == NULL)
|
if (buffer == NULL)
|
||||||
{
|
{
|
||||||
@@ -19,7 +19,7 @@ bool render_target_init(uint32_t width, uint32_t height, render_target_t* render
|
|||||||
|
|
||||||
memset(buffer, 0, buffer_size);
|
memset(buffer, 0, buffer_size);
|
||||||
|
|
||||||
for (size_t i = 0; i < buffer_size / size_of_pixel; i++)
|
for (size_t i = 0; i < image_size; i++)
|
||||||
{
|
{
|
||||||
buffer[i].w = 1.0;
|
buffer[i].w = 1.0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ static inline uint16_t get_sample_count(uint16_t sample_count, int flag)
|
|||||||
return sample_count;
|
return sample_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void render_pixel(const rendering_config_t* config, scene_t* scene, vec3s coord, uint32_t x, uint32_t y, int flag, vec4s* pixel_color)
|
static void render_pixel(const rendering_config_t* config, scene_t* scene, vec3s coord, uint32_t x, uint32_t y, debug_flag_t flag, vec4s* pixel_color)
|
||||||
{
|
{
|
||||||
vec4s accumulated_color = glms_vec4_zero();
|
vec4s accumulated_color = glms_vec4_zero();
|
||||||
uint32_t pixel_id = y * config->width + x;
|
uint32_t pixel_id = y * config->width + x;
|
||||||
@@ -118,7 +118,7 @@ void renderer_start(render_job_t* job)
|
|||||||
}
|
}
|
||||||
|
|
||||||
vec4s pixel_color;
|
vec4s pixel_color;
|
||||||
render_pixel(job->config, job->scene, coord, (uint32_t)x, (uint32_t)y, job->rendering_flag, &pixel_color);
|
render_pixel(job->config, job->scene, coord, (uint32_t)x, (uint32_t)y, job->debug_flag, &pixel_color);
|
||||||
render_target_set_pixel(job->render_target, (uint32_t)x, (uint32_t)y, pixel_color);
|
render_target_set_pixel(job->render_target, (uint32_t)x, (uint32_t)y, pixel_color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,22 +58,10 @@ static bool load_assets(scene_t* scene)
|
|||||||
|
|
||||||
static bool initialize_renderer(const rendering_config_t* config, render_job_t** outJob, render_target_t* outImg, scene_t* outScene)
|
static bool initialize_renderer(const rendering_config_t* config, render_job_t** outJob, render_target_t* outImg, scene_t* outScene)
|
||||||
{
|
{
|
||||||
if (!scene_setup(outScene))
|
if (!scene_setup(outScene)
|
||||||
{
|
|| !load_assets(outScene)
|
||||||
return false;
|
|| !scene_build_bvh(outScene)
|
||||||
}
|
|| !render_target_init(config->width, config->height, outImg))
|
||||||
|
|
||||||
if (!load_assets(outScene))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!scene_build_bvh(outScene))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!render_target_init(config->width, config->height, outImg))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -90,7 +78,7 @@ static bool initialize_renderer(const rendering_config_t* config, render_job_t**
|
|||||||
.config = config,
|
.config = config,
|
||||||
|
|
||||||
.rendering_type = TILE_BASED,
|
.rendering_type = TILE_BASED,
|
||||||
.rendering_flag = DEBUG_NONE,
|
.debug_flag = DEBUG_NONE,
|
||||||
.is_done = false,
|
.is_done = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -174,20 +162,15 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
|
|||||||
render_job_t* job = NULL;
|
render_job_t* job = NULL;
|
||||||
|
|
||||||
rendering_config_t config = {
|
rendering_config_t config = {
|
||||||
.width = 1920 / 4,
|
.width = 1920 / 2,
|
||||||
.height = 1080 / 4,
|
.height = 1080 / 2,
|
||||||
.sample_count = 64,
|
.sample_count = 64 * 4,
|
||||||
.max_depth = 4,
|
.max_depth = 4,
|
||||||
.bucket_size = 64,
|
.bucket_size = 64,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!initialize_renderer(&config, &job, &img, &scene))
|
if (!initialize_renderer(&config, &job, &img, &scene)
|
||||||
{
|
|| !window_create(TITLE, hInstance, config.width, config.height, job))
|
||||||
shutdown_renderer(job, &img, &scene);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!window_create(TITLE, hInstance, config.width, config.height, job))
|
|
||||||
{
|
{
|
||||||
shutdown_renderer(job, &img, &scene);
|
shutdown_renderer(job, &img, &scene);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user