mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-28 19:18:34 +02:00
+ | TopLevel builded
This commit is contained in:
5
Makefile
5
Makefile
@ -11,7 +11,7 @@ ifeq ($(OS),Windows_NT)
|
|||||||
LINE_CLR = \33[2K\r
|
LINE_CLR = \33[2K\r
|
||||||
RM := del /S /Q
|
RM := del /S /Q
|
||||||
DIR_DUP = if not exist "$(@D)" mkdir "$(@D)"
|
DIR_DUP = if not exist "$(@D)" mkdir "$(@D)"
|
||||||
CC := g++ -O3
|
CC := g++ -g
|
||||||
IFLAGS := -I./includes -I./includes/RT -I./includes/imgui
|
IFLAGS := -I./includes -I./includes/RT -I./includes/imgui
|
||||||
LDFLAGS := -L./lib -lglfw3 -lopengl32 -lgdi32 -lcglm
|
LDFLAGS := -L./lib -lglfw3 -lopengl32 -lgdi32 -lcglm
|
||||||
else
|
else
|
||||||
@ -28,7 +28,7 @@ else
|
|||||||
RM := rm -rf
|
RM := rm -rf
|
||||||
DIR_DUP = mkdir -p $(@D)
|
DIR_DUP = mkdir -p $(@D)
|
||||||
CC := clang++
|
CC := clang++
|
||||||
CFLAGS := -Wall -Wextra -Werror -g -O3
|
CFLAGS := -Wall -Wextra -Werror -g
|
||||||
IFLAGS := -I./includes -I./includes/RT -I./includes/imgui -I/usr/include
|
IFLAGS := -I./includes -I./includes/RT -I./includes/imgui -I/usr/include
|
||||||
LDFLAGS := -L/usr/lib/x86_64-linux-gnu -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lstdc++
|
LDFLAGS := -L/usr/lib/x86_64-linux-gnu -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lstdc++
|
||||||
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)
|
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)
|
||||||
@ -55,6 +55,7 @@ ALL_SRCS := $(IMGUI_SRCS) \
|
|||||||
class/SceneParser.cpp \
|
class/SceneParser.cpp \
|
||||||
class/ObjParser.cpp \
|
class/ObjParser.cpp \
|
||||||
class/BVH.cpp \
|
class/BVH.cpp \
|
||||||
|
class/TopBVH.cpp \
|
||||||
|
|
||||||
SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%)
|
SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%)
|
||||||
OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o))
|
OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o))
|
||||||
|
18
imgui.ini
18
imgui.ini
@ -3,26 +3,22 @@ Pos=60,60
|
|||||||
Size=400,400
|
Size=400,400
|
||||||
|
|
||||||
[Window][Camera]
|
[Window][Camera]
|
||||||
Pos=1643,7
|
Pos=1636,1
|
||||||
Size=259,200
|
Size=259,208
|
||||||
Collapsed=1
|
|
||||||
|
|
||||||
[Window][Material]
|
[Window][Material]
|
||||||
Pos=1642,29
|
Pos=1634,219
|
||||||
Size=266,299
|
Size=266,290
|
||||||
Collapsed=1
|
|
||||||
|
|
||||||
[Window][Fog settings]
|
[Window][Fog settings]
|
||||||
Pos=1641,52
|
Pos=1625,788
|
||||||
Size=247,130
|
Size=247,130
|
||||||
Collapsed=1
|
|
||||||
|
|
||||||
[Window][Debug]
|
[Window][Debug]
|
||||||
Pos=1642,668
|
Pos=1642,668
|
||||||
Size=260,143
|
Size=260,143
|
||||||
|
|
||||||
[Window][Debug BVH]
|
[Window][Debug BVH]
|
||||||
Pos=1641,72
|
Pos=1625,930
|
||||||
Size=274,205
|
Size=274,137
|
||||||
Collapsed=1
|
|
||||||
|
|
||||||
|
@ -62,7 +62,25 @@ struct Vertex {
|
|||||||
# include "Scene.hpp"
|
# include "Scene.hpp"
|
||||||
# include "SceneParser.hpp"
|
# include "SceneParser.hpp"
|
||||||
# include "ObjParser.hpp"
|
# include "ObjParser.hpp"
|
||||||
|
|
||||||
|
struct AABB
|
||||||
|
{
|
||||||
|
glm::vec3 min;
|
||||||
|
glm::vec3 max;
|
||||||
|
|
||||||
|
AABB(glm::vec3 min, glm::vec3 max) : min(min), max(max) {}
|
||||||
|
|
||||||
|
void grow( glm::vec3 p ) { min = glm::min( min, p ), max = glm::max( max, p ); }
|
||||||
|
|
||||||
|
float area()
|
||||||
|
{
|
||||||
|
glm::vec3 e = max - min;
|
||||||
|
return (e.x * e.y + e.y * e.z + e.z * e.x);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
# include "BVH.hpp"
|
# include "BVH.hpp"
|
||||||
|
# include "TopBVH.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,22 +18,7 @@
|
|||||||
struct GPUTriangle;
|
struct GPUTriangle;
|
||||||
struct GPUObject;
|
struct GPUObject;
|
||||||
struct GPUBvh;
|
struct GPUBvh;
|
||||||
|
struct AABB;
|
||||||
struct AABB
|
|
||||||
{
|
|
||||||
glm::vec3 min;
|
|
||||||
glm::vec3 max;
|
|
||||||
|
|
||||||
AABB(glm::vec3 min, glm::vec3 max) : min(min), max(max) {}
|
|
||||||
|
|
||||||
void grow( glm::vec3 p ) { min = glm::min( min, p ), max = glm::max( max, p ); }
|
|
||||||
|
|
||||||
float area()
|
|
||||||
{
|
|
||||||
glm::vec3 e = max - min;
|
|
||||||
return (e.x * e.y + e.y * e.z + e.z * e.x);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BVHStats
|
struct BVHStats
|
||||||
{
|
{
|
||||||
|
54
includes/RT/TopBVH.hpp
Normal file
54
includes/RT/TopBVH.hpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* TopBVH.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/01/16 21:36:19 by TheRed #+# #+# */
|
||||||
|
/* Updated: 2025/01/17 18:59:28 by ycontre ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef RT_TOPBVH__HPP
|
||||||
|
# define RT_TOPBVH__HPP
|
||||||
|
|
||||||
|
# include "RT.hpp"
|
||||||
|
|
||||||
|
struct GPUBvhData;
|
||||||
|
struct GPUTopBvh;
|
||||||
|
struct AABB;
|
||||||
|
|
||||||
|
class TopBVH
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TopBVH(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs, int first_bvh, int bvh_count);
|
||||||
|
|
||||||
|
void updateBounds(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs);
|
||||||
|
void subdivide(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs);
|
||||||
|
|
||||||
|
float evaluateSah(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs, int axis, float pos);
|
||||||
|
|
||||||
|
// int getSize();
|
||||||
|
// int getLeaves();
|
||||||
|
|
||||||
|
// void flatten(std::vector<GPUTopBVH> &TopBVHs, int ¤tIndex);
|
||||||
|
// GPUTopBVH toGPUTopBVH();
|
||||||
|
|
||||||
|
// const AABB &getAABB() const;
|
||||||
|
// std::vector<GPUTopBVH> getGPUTopBVHs();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
AABB _aabb;
|
||||||
|
|
||||||
|
TopBVH *_left;
|
||||||
|
TopBVH *_right;
|
||||||
|
|
||||||
|
bool _is_leaf;
|
||||||
|
|
||||||
|
int _first_bvh;
|
||||||
|
int _bvh_count;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -24,8 +24,8 @@ pl 0 -2 0 0 1 0 2 // floor
|
|||||||
|
|
||||||
qu -1 1.999 -1 2 0 0 0 0 2 6
|
qu -1 1.999 -1 2 0 0 0 0 2 6
|
||||||
|
|
||||||
OBJ obj/Dragon_800K.obj -0.5 0 0.55 5 0 90 0
|
OBJ scenes/obj/Dragon_800K.obj -0.5 0 0.55 5 0 90 0
|
||||||
OBJ obj/Dragon_800K.obj 0.5 0 -0.55 5 0 -90 0
|
OBJ scenes/obj/Dragon_800K.obj 0.5 0 -0.55 5 0 -90 0
|
||||||
|
|
||||||
|
|
||||||
# OBJ obj/Model.obj
|
# OBJ obj/Model.obj
|
||||||
|
@ -24,7 +24,8 @@ qu -2.5 0 -2.5 0 5 0 15 0 0 3
|
|||||||
qu 11.5 0 -2.5 0 5 0 0 0 15 1
|
qu 11.5 0 -2.5 0 5 0 0 0 15 1
|
||||||
qu -2.5 0 11.5 0 5 0 15 0 0 4
|
qu -2.5 0 11.5 0 5 0 15 0 0 4
|
||||||
|
|
||||||
|
OBJ obj/Dragon_8K.obj -5 0.38 0 1
|
||||||
|
OBJ obj/Dragon_8K.obj 5 0.38 0 1
|
||||||
|
|
||||||
OBJ obj/Dragon_8K.obj 0 0.38 0 1
|
OBJ obj/Dragon_8K.obj 0 0.38 0 1
|
||||||
OBJ obj/Dragon_8K.obj 0 0.38 1 1
|
OBJ obj/Dragon_8K.obj 0 0.38 1 1
|
||||||
|
@ -212,44 +212,6 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
|
|||||||
return (hit);
|
return (hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
mat3 rotateX(float angleRadians) {
|
|
||||||
float c = cos(angleRadians);
|
|
||||||
float s = sin(angleRadians);
|
|
||||||
return mat3(
|
|
||||||
1.0, 0.0, 0.0,
|
|
||||||
0.0, c, -s,
|
|
||||||
0.0, s, c
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
mat3 rotateY(float angleRadians) {
|
|
||||||
float c = cos(angleRadians);
|
|
||||||
float s = sin(angleRadians);
|
|
||||||
return mat3(
|
|
||||||
c, 0.0, s,
|
|
||||||
0.0, 1.0, 0.0,
|
|
||||||
-s, 0.0, c
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
mat3 rotateZ(float angleRadians) {
|
|
||||||
float c = cos(angleRadians);
|
|
||||||
float s = sin(angleRadians);
|
|
||||||
return mat3(
|
|
||||||
c, -s, 0.0,
|
|
||||||
s, c, 0.0,
|
|
||||||
0.0, 0.0, 1.0
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
mat3 createTransformMatrix(vec3 rotationAngles, float scale) {
|
|
||||||
mat3 rotMatrix = rotateZ(rotationAngles.z) *
|
|
||||||
rotateY(rotationAngles.y) *
|
|
||||||
rotateX(rotationAngles.x);
|
|
||||||
|
|
||||||
return rotMatrix * scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
hitInfo traverseBVHs(Ray ray, inout Stats stats)
|
hitInfo traverseBVHs(Ray ray, inout Stats stats)
|
||||||
{
|
{
|
||||||
hitInfo hit;
|
hitInfo hit;
|
||||||
|
@ -20,8 +20,8 @@ int main(int argc, char **argv)
|
|||||||
return (1);
|
return (1);
|
||||||
|
|
||||||
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
|
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
|
||||||
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
||||||
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
||||||
|
|
||||||
GLint max_gpu_size;
|
GLint max_gpu_size;
|
||||||
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
||||||
|
@ -58,6 +58,9 @@ bool Scene::parseScene(char *name)
|
|||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
|
TopBVH *top_bvh = new TopBVH(_gpu_bvh_data, _gpu_bvh, 0, _gpu_bvh_data.size());
|
||||||
|
(void) top_bvh;
|
||||||
|
|
||||||
|
|
||||||
std::cout << "Parsing done" << std::endl;
|
std::cout << "Parsing done" << std::endl;
|
||||||
|
|
||||||
@ -187,7 +190,6 @@ void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset, float sc
|
|||||||
std::cout << "\tMin triangles per leaf: " << stats.min_triangles << std::endl;
|
std::cout << "\tMin triangles per leaf: " << stats.min_triangles << std::endl;
|
||||||
std::cout << "\tMax triangles per leaf: " << stats.max_triangles << std::endl;
|
std::cout << "\tMax triangles per leaf: " << stats.max_triangles << std::endl;
|
||||||
std::cout << "\tAverage triangles per leaf: " << stats.average_triangles << std::endl << std::endl;
|
std::cout << "\tAverage triangles per leaf: " << stats.average_triangles << std::endl << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::addMaterial(Material *material)
|
void Scene::addMaterial(Material *material)
|
||||||
|
144
srcs/class/TopBVH.cpp
Normal file
144
srcs/class/TopBVH.cpp
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* TopBvh.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/01/21 21:46:45 by TheRed #+# #+# */
|
||||||
|
/* Updated: 2025/01/21 21:46:45 by TheRed ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "TopBVH.hpp"
|
||||||
|
|
||||||
|
TopBVH::TopBVH(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs, int first_bvh, int bvh_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
|
||||||
|
{
|
||||||
|
_left = nullptr;
|
||||||
|
_right = nullptr;
|
||||||
|
|
||||||
|
_is_leaf = true;
|
||||||
|
|
||||||
|
_first_bvh = first_bvh;
|
||||||
|
_bvh_count = bvh_count;
|
||||||
|
|
||||||
|
updateBounds(bvhs_data, bvhs);
|
||||||
|
subdivide(bvhs_data, bvhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TopBVH::updateBounds(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _bvh_count; i++)
|
||||||
|
{
|
||||||
|
GPUBvhData bvh_data = bvhs_data[_first_bvh + i];
|
||||||
|
GPUBvh root_bvh = bvhs[bvh_data.bvh_start_index];
|
||||||
|
|
||||||
|
glm::vec3 min = root_bvh.min - bvh_data.offset;
|
||||||
|
glm::vec3 max = root_bvh.max - bvh_data.offset;
|
||||||
|
|
||||||
|
_aabb.min = glm::min(_aabb.min, min);
|
||||||
|
_aabb.max = glm::max(_aabb.max, max);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float TopBVH::evaluateSah(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs, int axis, float pos)
|
||||||
|
{
|
||||||
|
AABB left_box(glm::vec3(1e30f), glm::vec3(-1e30f));
|
||||||
|
AABB right_box(glm::vec3(1e30f), glm::vec3(-1e30f));
|
||||||
|
|
||||||
|
int left_count = 0;
|
||||||
|
int right_count = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < _bvh_count; i++)
|
||||||
|
{
|
||||||
|
GPUBvhData bvh_data = bvhs_data[_first_bvh + i];
|
||||||
|
GPUBvh bvh_root = bvhs[bvh_data.bvh_start_index];
|
||||||
|
|
||||||
|
glm::vec3 min = bvh_root.min - bvh_data.offset;
|
||||||
|
glm::vec3 max = bvh_root.max - bvh_data.offset;
|
||||||
|
|
||||||
|
if (min[axis] < pos)
|
||||||
|
{
|
||||||
|
left_count++;
|
||||||
|
left_box.grow( min );
|
||||||
|
left_box.grow( max );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
right_count++;
|
||||||
|
right_box.grow( min );
|
||||||
|
right_box.grow( max );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (left_box.area() * left_count + right_box.area() * right_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TopBVH::subdivide(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs)
|
||||||
|
{
|
||||||
|
if (_bvh_count <= 1)
|
||||||
|
return ;
|
||||||
|
|
||||||
|
|
||||||
|
const int num_test_per_axis = 5;
|
||||||
|
|
||||||
|
int best_axis = 0;
|
||||||
|
float best_pos = 0.0f;
|
||||||
|
float best_cost = 1e30f;
|
||||||
|
|
||||||
|
for (int axis = 0; axis < 3; axis++)
|
||||||
|
{
|
||||||
|
float start_pos = _aabb.min[axis];
|
||||||
|
float end_pos = _aabb.max[axis];
|
||||||
|
|
||||||
|
for (int i = 0; i < num_test_per_axis; i++)
|
||||||
|
{
|
||||||
|
float split_t = (i + 1) / (num_test_per_axis + 1.0f);
|
||||||
|
float candidate_pos = start_pos + (end_pos - start_pos) * split_t;
|
||||||
|
|
||||||
|
float cost = evaluateSah(bvhs_data, bvhs, axis, candidate_pos);
|
||||||
|
|
||||||
|
if (cost < best_cost)
|
||||||
|
{
|
||||||
|
best_pos = candidate_pos;
|
||||||
|
best_axis = axis;
|
||||||
|
best_cost = cost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int axis = best_axis;
|
||||||
|
float split_pos = best_pos;
|
||||||
|
|
||||||
|
int i = _first_bvh;
|
||||||
|
int j = _first_bvh + _bvh_count - 1;
|
||||||
|
|
||||||
|
while (i <= j)
|
||||||
|
{
|
||||||
|
GPUBvhData bvh_data = bvhs_data[i];
|
||||||
|
GPUBvh root = bvhs[bvh_data.bvh_start_index];
|
||||||
|
glm::vec3 min = root.min - bvh_data.offset;
|
||||||
|
glm::vec3 max = root.max - bvh_data.offset;
|
||||||
|
glm::vec3 centroid = (min + max) * 0.5f;
|
||||||
|
|
||||||
|
if (centroid[axis] < split_pos)
|
||||||
|
i++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::swap(bvhs_data[i], bvhs_data[j]);
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int left_count = i - _first_bvh;
|
||||||
|
|
||||||
|
if (left_count == 0 || left_count == _bvh_count)
|
||||||
|
return ;
|
||||||
|
|
||||||
|
_is_leaf = false;
|
||||||
|
|
||||||
|
_left = new TopBVH(bvhs_data, bvhs, _first_bvh, left_count);
|
||||||
|
_right = new TopBVH(bvhs_data, bvhs, i, _bvh_count - left_count);
|
||||||
|
|
||||||
|
_bvh_count = 0;
|
||||||
|
}
|
Reference in New Issue
Block a user