Refactor path tracing and material rendering system

Added a new enum `path_state` and a struct `path_output` in `Common.h` to manage path tracing states and outputs.
Added a new `simple_lit_render_loop` function in `SimpleLit.c` to handle rendering for simple lit materials, integrating light contributions and BSDF evaluations.

Changed the function signatures in `Light.h` to use `path_output` instead of `vec3s` for BSDF evaluations.
Changed the `evaluate_bsdf_directional` function in `LightEvaluation.h` to return `path_output` instead of `vec3s`.
Changed the `evaluate_bsdf_sky` function in `LightEvaluation.h` to return `path_output` and updated its implementation accordingly.
Changed the function signature of `evaluate_bsdf_const_sky` in `SkyLight.h` to return `path_output` and modified its implementation to match.

Removed several old BSDF-related functions in `Material.c` that are no longer needed due to the new structure.

Updated the `material_create` function in `Material.c` to accept the new render loop function pointer.
Updated the `path_trace` function in `PathTracing.c` to utilize the new `path_output` structure for managing light contributions and state transitions during path tracing.
Updated the `evaluate_bsdf_directional` function in `LightEvaluation.c` to return `path_output` and adjusted its internal logic to populate the output structure.
Updated the `evaluate_bsdf_const_sky` function in `SkyLight.c` to return `path_output` and modified its logic to handle light contributions correctly.
Updated the intensity of the sky light to `0.0f` during scene setup in `main.c`, effectively disabling it.
This commit is contained in:
2025-05-01 01:21:39 +09:00
parent 2c0d5a2364
commit 0061609267
13 changed files with 240 additions and 201 deletions

View File

@@ -1,8 +1,10 @@
#ifndef MATERIAL_H
#define MATERIAL_H
#include "Algorithm/BVH.h"
#include "Algorithm/Sobol.h"
#include "Common.h"
#include "Lighting/Light.h"
#include "Rendering/Texture.h"
#define PROPERTY_SIZE 64
@@ -13,18 +15,21 @@ typedef struct
vec3s position;
vec3s normal;
vec3s tangent;
vec2s uv;
vec3s wi;
vec3s wo;
vec3s throughput;
vec2s uv;
uint32_t sample_index;
uint32_t bounce_depth;
const bvh_tree_t* bvh_tree;
const triangle_collection_t* triangles;
const light_collection_t* lights;
const texture_collection_t* textures;
} shading_context_t;
typedef void (*compute_surface_data_f)(const shading_context_t* context, const void* properties, void* surface_data_out);
typedef vec3s (*sample_bsdf_f)(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data, uint32_t index, uint32_t bounce, float* pdf_out);
typedef float (*sample_bsdf_pdf_f)(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
typedef vec3s (*evaluate_bsdf_f)(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
typedef path_output (*material_render_loop_f)(const void* properties, const shading_context_t* context);
typedef struct
{
@@ -33,10 +38,7 @@ typedef struct
// TODO: alpha, displacement, etc.
vec3s emission; // We have to have emission outside of data because data is only for bsdf, and emission is not part of bsdf. Maybe another shading properties struct is needed if the number of properties increases.
compute_surface_data_f compute_surface_data;
sample_bsdf_f sample_bsdf;
sample_bsdf_pdf_f sample_bsdf_pdf;
evaluate_bsdf_f evaluate_bsdf;
material_render_loop_f render_loop;
} material_t;
typedef struct
@@ -57,11 +59,8 @@ bool material_collection_init(uint8_t size, material_collection_t* materials);
void material_collection_resize(material_collection_t* materials, size_t size);
void material_collection_free(material_collection_t* materials);
material_entity_t material_create(const void* properties, size_t properties_size, compute_surface_data_f surface_data, sample_bsdf_f sample, sample_bsdf_pdf_f sample_pdf, evaluate_bsdf_f evaluate, material_collection_t* collection);
vec3s sample_material_bsdf(const material_t* material, const shading_context_t* context, uint32_t sample_index, uint32_t bounce, float* pdf_out);
float sample_material_bsdf_pdf(const material_t* material, const shading_context_t* context);
vec3s evaluate_material_bsdf(const material_t* material, const shading_context_t* context);
material_entity_t material_create(const void* properties, size_t properties_size, material_render_loop_f render_loop, material_collection_t* collection);
// void material_free(material_entity_t entity, material_collection_t* collection);
inline material_entity_t invalid_material_entity()
{
@@ -73,4 +72,14 @@ inline bool is_material_entity_valid(material_entity_t entity)
return entity.id != INVALID_MATERIAL_ID;
}
inline path_output render_material(const material_t* material, const shading_context_t* context)
{
if (material == NULL || material->render_loop == NULL)
{
return (path_output){0};
}
return material->render_loop(material->properties, context);
}
#endif // MATERIAL_H

View File

@@ -0,0 +1,6 @@
#ifndef SHADING_CONTEXT_H
#define SHADING_CONTEXT_H
#endif // SHADING_CONTEXT_H

View File

@@ -11,7 +11,7 @@ typedef struct
vec3s normal;
float roughness;
float metallic;
} simple_lit_data_t;
} surface_data_t;
typedef struct
{
@@ -25,20 +25,15 @@ typedef struct
texture_entity_t metallic_texture;
} simple_lit_properties_t;
void simple_lit_data_default(const shading_context_t* context, const void* properties, void* data_out);
// vec3s sample_bsdf_simple_lit(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data, uint32_t sample_index, uint32_t bounce, float* pdf_out);
// vec3s evaluate_bsdf_simple_lit(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
// float sample_bsdf_pdf_simple_lit(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
vec3s sample_bsdf_simple_lit(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data, uint32_t sample_index, uint32_t bounce, float* pdf_out);
vec3s evaluate_bsdf_simple_lit(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
float sample_bsdf_pdf_simple_lit(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
inline material_entity_t material_create_simple_lit(const simple_lit_properties_t* properties, compute_surface_data_f surface_data, material_collection_t* collection)
{
return material_create(properties, sizeof(simple_lit_properties_t), surface_data, sample_bsdf_simple_lit, sample_bsdf_pdf_simple_lit, evaluate_bsdf_simple_lit, collection);
}
path_output simple_lit_render_loop(const void* properties, const shading_context_t* context);
inline material_entity_t material_create_simple_lit_default(const simple_lit_properties_t* properties, material_collection_t* collection)
{
return material_create(properties, sizeof(simple_lit_properties_t), simple_lit_data_default, sample_bsdf_simple_lit, sample_bsdf_pdf_simple_lit, evaluate_bsdf_simple_lit, collection);
return material_create(properties, sizeof(simple_lit_properties_t), simple_lit_render_loop, collection);
}
#endif // SIMPLE_LIT_H