Added handle system and improve resource management.
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
#ifndef LIGHT_H
|
||||
#define LIGHT_H
|
||||
|
||||
#include "Algorithm/BVH.h"
|
||||
#include "Algorithm/Sobol.h"
|
||||
#include "Geometry/Triangle.h"
|
||||
#include "Rendering/Texture.h"
|
||||
#include "cglm/struct/vec3.h"
|
||||
|
||||
struct scene_t;
|
||||
struct bvh_tree_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -21,9 +20,9 @@ typedef struct
|
||||
uint16_t bounce_depth;
|
||||
|
||||
const struct scene_t* scene;
|
||||
const struct bvh_tree_t* bvh_tree;
|
||||
|
||||
const bvh_tree_t* bvh_tree;
|
||||
const texture_collection_t* textures;
|
||||
const texture_slot_map_t* textures;
|
||||
|
||||
float spread_angle;
|
||||
} light_shading_context_t;
|
||||
@@ -63,100 +62,81 @@ typedef struct
|
||||
float angular_diameter;
|
||||
} directional_light_t;
|
||||
|
||||
SLOT_MAP_DEF(punctual_light_t, punctual_light)
|
||||
SLOT_MAP_DEF(directional_light_t, directional_light)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t punctual_light_count;
|
||||
uint32_t directional_light_count;
|
||||
uint32_t max_punctual_lights;
|
||||
uint32_t max_directional_lights;
|
||||
|
||||
sky_light_t sky_light;
|
||||
punctual_light_t* punctual_lights;
|
||||
directional_light_t* directional_lights;
|
||||
punctual_light_slot_map_t punctual_lights;
|
||||
directional_light_slot_map_t directional_lights;
|
||||
} light_collection_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t id;
|
||||
uint32_t generation;
|
||||
bool is_punctual;
|
||||
} light_entity_t;
|
||||
} light_handle_t;
|
||||
|
||||
inline bool light_collection_create(uint32_t max_punctual_lights, uint32_t max_directional_lights, light_collection_t* collection)
|
||||
inline result_t light_collection_create(light_collection_t* collection, uint32_t punctual_lights_capacity, uint32_t directional_lights_capacity)
|
||||
{
|
||||
if (max_punctual_lights == 0 || max_directional_lights == 0)
|
||||
result_t r = punctual_light_slot_map_init(&collection->punctual_lights, punctual_lights_capacity);
|
||||
if (r != RESULT_SUCCESS)
|
||||
{
|
||||
return false;
|
||||
return r;
|
||||
}
|
||||
|
||||
collection->max_punctual_lights = max_punctual_lights;
|
||||
collection->max_directional_lights = max_directional_lights;
|
||||
collection->punctual_light_count = 0;
|
||||
collection->directional_light_count = 0;
|
||||
|
||||
collection->punctual_lights = (punctual_light_t*)malloc(max_punctual_lights * sizeof(punctual_light_t));
|
||||
if (collection->punctual_lights == NULL)
|
||||
r = directional_light_slot_map_init(&collection->directional_lights, directional_lights_capacity);
|
||||
if (r != RESULT_SUCCESS)
|
||||
{
|
||||
return false;
|
||||
punctual_light_slot_map_free(&collection->punctual_lights);
|
||||
return r;
|
||||
}
|
||||
|
||||
collection->directional_lights = (directional_light_t*)malloc(max_directional_lights * sizeof(directional_light_t));
|
||||
if (collection->directional_lights == NULL)
|
||||
{
|
||||
free(collection->punctual_lights);
|
||||
return false;
|
||||
}
|
||||
collection->sky_light.data = NULL;
|
||||
collection->sky_light.data_size = 0;
|
||||
collection->sky_light.evaluate_bsdf_sky = NULL;
|
||||
collection->sky_light.free_sky_data = NULL;
|
||||
|
||||
return true;
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
inline light_entity_t light_create_punctual_light(light_collection_t* collection)
|
||||
inline light_handle_t light_add_punctual_light(light_collection_t* collection, punctual_light_t* light_data)
|
||||
{
|
||||
if (collection->punctual_light_count >= collection->max_punctual_lights)
|
||||
light_handle_t handle;
|
||||
handle.is_punctual = true;
|
||||
if (punctual_light_slot_map_add(&collection->punctual_lights, *light_data, &handle.id, &handle.generation) != RESULT_SUCCESS)
|
||||
{
|
||||
return (light_entity_t){0};
|
||||
return INVALID_HANDLE(light_handle_t);
|
||||
}
|
||||
|
||||
punctual_light_t light = {0};
|
||||
collection->punctual_lights[collection->punctual_light_count] = light;
|
||||
light_entity_t entity = {.id = collection->punctual_light_count, .is_punctual = true};
|
||||
collection->punctual_light_count++;
|
||||
|
||||
return entity;
|
||||
return handle;
|
||||
}
|
||||
|
||||
inline light_entity_t light_create_directional_light(light_collection_t* collection)
|
||||
inline light_handle_t light_add_directional_light(light_collection_t* collection, directional_light_t* light_data)
|
||||
{
|
||||
if (collection->directional_light_count >= collection->max_directional_lights)
|
||||
light_handle_t handle;
|
||||
handle.is_punctual = false;
|
||||
if (directional_light_slot_map_add(&collection->directional_lights, *light_data, &handle.id, &handle.generation) != RESULT_SUCCESS)
|
||||
{
|
||||
return (light_entity_t){0};
|
||||
return INVALID_HANDLE(light_handle_t);
|
||||
}
|
||||
|
||||
directional_light_t light = {0};
|
||||
collection->directional_lights[collection->directional_light_count] = light;
|
||||
light_entity_t entity = {.id = collection->directional_light_count, .is_punctual = false};
|
||||
collection->directional_light_count++;
|
||||
|
||||
return entity;
|
||||
return handle;
|
||||
}
|
||||
|
||||
inline void light_collection_free(light_collection_t* collection)
|
||||
{
|
||||
free(collection->punctual_lights);
|
||||
free(collection->directional_lights);
|
||||
punctual_light_slot_map_free(&collection->punctual_lights);
|
||||
directional_light_slot_map_free(&collection->directional_lights);
|
||||
|
||||
if (collection->sky_light.free_sky_data != NULL)
|
||||
{
|
||||
collection->sky_light.free_sky_data(collection->sky_light.data);
|
||||
collection->sky_light.data = NULL;
|
||||
collection->sky_light.data_size = 0;
|
||||
}
|
||||
free(collection->sky_light.data);
|
||||
|
||||
collection->max_punctual_lights = 0;
|
||||
collection->max_directional_lights = 0;
|
||||
collection->punctual_light_count = 0;
|
||||
collection->directional_light_count = 0;
|
||||
collection->punctual_lights = NULL;
|
||||
collection->directional_lights = NULL;
|
||||
collection->sky_light.data = NULL;
|
||||
}
|
||||
|
||||
#endif // LIGHT_H
|
||||
|
||||
@@ -31,7 +31,7 @@ typedef struct
|
||||
sky_light_t sky_create_constant_sky(const constant_sky_data_t* data);
|
||||
path_output evaluate_bsdf_const_sky(const void* data, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index);
|
||||
|
||||
sky_light_t sky_create_hdr_sky(const texture_collection_t* textures, texture_handle_t hdri_entity, float intensity);
|
||||
sky_light_t sky_create_hdr_sky(const texture_slot_map_t* textures, texture_handle_t hdri_entity, float intensity);
|
||||
path_output evaluate_bsdf_hdr_sky(const void* data, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index);
|
||||
void hdr_sky_free(hdr_sky_data_t* data);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user