Changed function signatures to remove const qualifiers

Changed several function signatures across multiple files to remove the `const` qualifier from parameters of type `vec3s` for improved flexibility.
Changed `material_collection_create` to `material_collection_init` for better initialization handling.
Changed `scene_create` to `scene_init` to return a boolean indicating success or failure.
Changed `render_target_create` to `render_target_init` for consistent initialization practices.
Changed `window_create` to remove `const` from its parameters for consistency.
Changed `evaluate_bsdf_directional` and `evaluate_bsdf_const_sky` to remove `const` from their parameters.
Changed `sample_bsdf_simple_lit` and `sample_bsdf_pdf_simple_lit` to remove `const` from the `normal` parameter.
Changed `scene_render` to take a pointer to `render_target_t` instead of returning it directly.
Updated `main.c` to reflect new initialization functions for better memory management.
This commit is contained in:
2025-04-18 10:51:46 +09:00
parent bfc94f0008
commit 1162575545
27 changed files with 231 additions and 166 deletions

View File

@@ -3,27 +3,33 @@
#include <string.h>
#include <math.h>
render_target_t render_target_create(uint32_t width, uint32_t height)
bool render_target_init(uint32_t width, uint32_t height, render_target_t* render_target)
{
render_target_t target;
target.width = width;
target.height = height;
*render_target = (render_target_t){0};
render_target->width = width;
render_target->height = height;
size_t size_of_pixel = sizeof(vec4s);
size_t buffer_size = (size_t)width * height * size_of_pixel;
target.buffer = (vec4s*)malloc(buffer_size);
vec4s* buffer = (vec4s*)malloc(buffer_size);
if (buffer == NULL)
{
return false;
}
memset(target.buffer, 0, buffer_size);
memset(buffer, 0, buffer_size);
for (size_t i = 0; i < buffer_size / size_of_pixel; i++)
{
target.buffer[i].w = 1.0;
buffer[i].w = 1.0;
}
return target;
render_target->buffer = buffer;
return true;
}
vec4s render_target_get_pixel(render_target_t* render_target, uint32_t x, uint32_t y)
vec4s render_target_get_pixel(const render_target_t* render_target, uint32_t x, uint32_t y)
{
if (x < render_target->width && y < render_target->height)
{
@@ -47,6 +53,10 @@ unsigned char* render_target_to_char(render_target_t* render_target)
{
size_t buffer_size = (size_t)render_target->width * render_target->height * 4; // 4 bytes for RGBA
unsigned char* char_buffer = (unsigned char*)malloc(buffer_size);
if (char_buffer == NULL)
{
return NULL;
}
for (uint32_t y = 0; y < render_target->height; y++)
{

View File

@@ -3,26 +3,7 @@
#include "Rendering/Scene.h"
#include "Algorithm/PathTracing.h"
scene_t scene_create(const uint64_t triangle_count, const uint8_t material_count, const uint32_t punctual_light_count)
{
scene_t scene = {0};
scene.triangles = triangle_collection_create(triangle_count);
scene.materials = material_collection_create(material_count);
scene.lights = light_collection_create(punctual_light_count, 16); // NOTE: We just fixed the max directional light count to 16.
scene.camera = camera_create(
(vec3s){0.0f, 0.0f, 5.0f},
(vec3s){0.0f, 0.0f, -1.0f},
(vec3s){0.0f, 1.0f, 0.0f},
0.025f,
0.036f,
16.0f / 9.0f
);
return scene;
}
static inline void ensure_camera_aspect_ratio(camera_t* camera, const rendering_config_t config)
static inline void ensure_camera_aspect_ratio(camera_t* camera, rendering_config_t config)
{
if (fabsf((float)config.width / config.height - camera->aspect_ratio) > FLT_EPSILON)
{
@@ -49,7 +30,7 @@ static inline vec2s compute_ndc(float x, float y, uint32_t width, uint32_t heigh
};
}
static void screne_render_pixel(scene_t* scene, const rendering_config_t config, const vec3s coord, const uint32_t x, const uint32_t y, vec4s* pixel_color)
static void screne_render_pixel(scene_t* scene, rendering_config_t config, vec3s coord, uint32_t x, uint32_t y, vec4s* pixel_color)
{
vec4s accumulated_color = glms_vec4_zero();
*pixel_color = accumulated_color;
@@ -84,8 +65,8 @@ static void screne_render_pixel(scene_t* scene, const rendering_config_t config,
*pixel_color = glms_vec4_scale(accumulated_color, 1.0f / (float)config.sample_count);
}
bool scene_render_tile(scene_t* scene, rendering_context_t* ctx, render_target_t* render_target,
const rendering_config_t config, const uint32_t tile_index, tile_t* tile_out)
bool scene_render_tile(scene_t* scene, rendering_context_t* ctx, render_target_t* render_target,
rendering_config_t config, uint32_t tile_index, tile_t* tile_out)
{
if (ctx->is_done)
{
@@ -140,12 +121,19 @@ bool scene_render_tile(scene_t* scene, rendering_context_t* ctx, render_target_t
return true;
}
render_target_t scene_render(scene_t* scene, const rendering_config_t config)
bool scene_render(scene_t* scene, rendering_config_t config, render_target_t* render_target)
{
ensure_camera_aspect_ratio(&scene->camera, config);
// The actual float buffer inside the render target is on the heap, copy return shoudl be fine.
render_target_t render_target = render_target_create(config.width, config.height);
if (render_target->buffer != NULL)
{
render_target_free(render_target);
}
if (render_target_init(config.width, config.height, render_target))
{
return false;
}
uint32_t tile_count_x = (config.width + config.bucket_size - 1) / config.bucket_size;
uint32_t tile_count_y = (config.height + config.bucket_size - 1) / config.bucket_size;
@@ -173,17 +161,10 @@ render_target_t scene_render(scene_t* scene, const rendering_config_t config)
{
vec4s pixel_color;
screne_render_pixel(scene, config_copy, coord, (uint32_t)x, (uint32_t)y, &pixel_color);
render_target_set_pixel(&render_target, (uint32_t)x, (uint32_t)y, pixel_color);
render_target_set_pixel(render_target, (uint32_t)x, (uint32_t)y, pixel_color);
}
}
}
return render_target;
}
void scene_free(scene_t* scene)
{
triangle_collection_free(&scene->triangles);
material_collection_free(&scene->materials);
light_collection_free(&scene->lights);
return true;
}