~ | Working Toplevel bvh for untransformed obj

This commit is contained in:
TheRedShip
2025-01-22 23:46:27 +01:00
parent cd65cff03f
commit bdcafaf4f0
10 changed files with 1183 additions and 146 deletions

View File

@ -11,7 +11,7 @@ ifeq ($(OS),Windows_NT)
LINE_CLR = \33[2K\r
RM := del /S /Q
DIR_DUP = if not exist "$(@D)" mkdir "$(@D)"
CC := g++ -g
CC := g++ -g -O3
IFLAGS := -I./includes -I./includes/RT -I./includes/imgui
LDFLAGS := -L./lib -lglfw3 -lopengl32 -lgdi32 -lcglm
else
@ -28,7 +28,7 @@ else
RM := rm -rf
DIR_DUP = mkdir -p $(@D)
CC := clang++
CFLAGS := -Wall -Wextra -Werror -g
CFLAGS := -Wall -Wextra -Werror -g -O3
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++
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)

BIN
RT Normal file

Binary file not shown.

View File

@ -19,6 +19,6 @@ Pos=1642,668
Size=260,143
[Window][Debug BVH]
Pos=1625,930
Pos=1625,924
Size=274,137

View File

@ -3,7 +3,7 @@ v 0 50 0
v 50 50 0
v 50 0 0
mtllib obj/test.mtl
# mtllib scenes/obj/test.mtl
usemtl patate
# usemtl patate
f 1 2 3 4

File diff suppressed because it is too large Load Diff

View File

@ -61,6 +61,20 @@ struct GPUVolume
int enabled;
};
struct GPUTopBvh
{
vec3 min;
vec3 max;
int left_index;
int right_index;
int is_leaf;
int first_bvh;
int bvh_count;
};
struct GPUBvhData
{
mat4 transform;
@ -95,22 +109,27 @@ layout(std430, binding = 2) buffer TriangleBuffer
GPUTriangle triangles[];
};
layout(std430, binding = 3) buffer BvhDataBuffer
layout(std430, binding = 3) buffer TopBvhBuffer
{
GPUTopBvh TopBvh[];
};
layout(std430, binding = 4) buffer BvhDataBuffer
{
GPUBvhData BvhData[];
};
layout(std430, binding = 4) buffer BvhBuffer
layout(std430, binding = 5) buffer BvhBuffer
{
GPUBvh Bvh[];
};
layout(std430, binding = 5) buffer MaterialBuffer
layout(std430, binding = 6) buffer MaterialBuffer
{
GPUMaterial materials[];
};
layout(std430, binding = 6) buffer LightsBuffer
layout(std430, binding = 7) buffer LightsBuffer
{
int lightsIndex[];
};

View File

@ -192,12 +192,10 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t < hit.t)
{
hit.t = temp_hit.t;
hit.last_t = temp_hit.last_t;
hit.obj_index = bvh_data.triangle_start_index + node.first_primitive + i;
hit.mat_index = obj.mat_index;
hit.position = temp_hit.position;
hit.normal = temp_hit.normal;
hit.obj_index = bvh_data.triangle_start_index + node.first_primitive + i;
}
stats.triangle_count++;
}
}
else
@ -211,8 +209,10 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
left_hit.t = 1e30;
right_hit.t = 1e30;
// stats.box_count += 2;
bool left_bool = intersectRayBVH(ray, left_node.min, left_node.max, left_hit);
bool right_bool = intersectRayBVH(ray, right_node.min, left_node.max, right_hit);
bool right_bool = intersectRayBVH(ray, right_node.min, right_node.max, right_hit);
if (left_hit.t > right_hit.t)
{
@ -230,7 +230,6 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data, inout Stats stats)
return (hit);
}
hitInfo traverseBVHs(Ray ray, GPUBvhData bvh_data, inout Stats stats)
{
hitInfo hit;
@ -246,17 +245,16 @@ hitInfo traverseBVHs(Ray ray, GPUBvhData bvh_data, inout Stats stats)
transformedRay.origin = transformMatrix * (ray.origin - bvh_data.offset);
transformedRay.inv_direction = (1. / transformedRay.direction);
hit = traceBVH(transformedRay, bvh_data, stats);
hitInfo temp_hit = traceBVH(transformedRay, bvh_data, stats);
if (hit.obj_index == -1)
return (hit);
temp_hit.t = temp_hit.t / bvh_data.scale;
hit.t = hit.t / bvh_data.scale;
hit.last_t = hit.last_t / bvh_data.scale;
hit.obj_index = hit.obj_index;
hit.mat_index = hit.mat_index;
hit.position = inverseTransformMatrix * hit.position + bvh_data.offset;
hit.normal = normalize(inverseTransformMatrix * hit.normal);
if (temp_hit.t < hit.t)
{
hit.t = temp_hit.t;
hit.obj_index = temp_hit.obj_index;
hit.normal = normalize(inverseTransformMatrix * temp_hit.normal);
}
return (hit);
}
@ -289,10 +287,7 @@ hitInfo traceTopBVH(Ray ray, inout Stats stats)
if (temp_hit.obj_index != -1 && temp_hit.t < hit.t)
{
hit.t = temp_hit.t;
hit.last_t = temp_hit.last_t;
hit.obj_index = temp_hit.obj_index;
hit.mat_index = temp_hit.mat_index;
hit.position = temp_hit.position;
hit.normal = temp_hit.normal;
}
}
@ -346,6 +341,8 @@ vec3 debugColor(vec2 uv)
Ray ray = initRay(uv);
Stats stats = Stats(0, 0);
// hitInfo hit = traceBVH(ray, BvhData[0], stats);
// hitInfo hit = traverseBVHs(ray, BvhData[0], stats);
hitInfo hit = traceTopBVH(ray, stats);
float box_display = float(stats.box_count) / float(debug.box_treshold);

View File

@ -100,7 +100,7 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
right_hit.t = 1e30;
bool left_bool = intersectRayBVH(ray, left_node.min, left_node.max, left_hit);
bool right_bool = intersectRayBVH(ray, right_node.min, left_node.max, right_hit);
bool right_bool = intersectRayBVH(ray, right_node.min, right_node.max, right_hit);
if (left_hit.t > right_hit.t)
{
@ -118,9 +118,10 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
return (hit);
}
hitInfo traverseBVHs(Ray ray, GPUBvhData bvh_data)
{
hitInfo hit;
hit.t = 1e30;
hit.obj_index = -1;
@ -132,7 +133,7 @@ hitInfo traverseBVHs(Ray ray, GPUBvhData bvh_data)
transformedRay.origin = transformMatrix * (ray.origin - bvh_data.offset);
transformedRay.inv_direction = (1. / transformedRay.direction);
hit = traceBVH(transformedRay, BvhData[i]);
hit = traceBVH(transformedRay, bvh_data);
if (hit.obj_index == -1)
return (hit);
@ -155,6 +156,24 @@ hitInfo traceTopBVH(Ray ray)
hit.t = 1e30;
hit.obj_index = -1;
//this is working
// for (int i = 0; i < u_bvhNum; i++)
// {
// hit_bvh = traverseBVHs(ray, BvhData[i]);
// if (hit_bvh.t < hit.t)
// {
// hit.t = hit_bvh.t;
// hit.last_t = hit_bvh.last_t;
// hit.obj_index = hit_bvh.obj_index;
// hit.mat_index = hit_bvh.mat_index;
// hit.position = hit_bvh.position;
// hit.normal = hit_bvh.normal;
// }
// }
// return (hit);
//
int stack[32];
int stack_ptr = 0;
stack[0] = 0;
@ -164,19 +183,20 @@ hitInfo traceTopBVH(Ray ray)
int current_index = stack[stack_ptr--];
GPUTopBvh node = TopBvh[current_index];
if (node.is_leaf != 0)
{
for (int i = 0; i < node.bvh_count; i++)
{
GPUBvhData bvh_data = bvh_data[node.first_bvh + i];
GPUBvhData bvh_data = BvhData[node.first_bvh + i];
hitInfo temp_hit = traverseBVHs(ray, bvh_data);
if (temp_hit.obj_index != -1 && temp_hit.t < hit.t)
{
hit.t = temp_hit.t;
hit.last_t = temp_hit.last_t;
hit.obj_index = bvh_data.triangle_start_index + node.first_primitive + i;
hit.mat_index = obj.mat_index;
hit.obj_index = temp_hit.obj_index;
hit.mat_index = temp_hit.mat_index;
hit.position = temp_hit.position;
hit.normal = temp_hit.normal;
}
@ -194,7 +214,7 @@ hitInfo traceTopBVH(Ray ray)
right_hit.t = 1e30;
bool left_bool = intersectRayBVH(ray, left_node.min, left_node.max, left_hit);
bool right_bool = intersectRayBVH(ray, right_node.min, left_mode.max, right_hit);
bool right_bool = intersectRayBVH(ray, right_node.min, right_node.max, right_hit);
if (left_hit.t > right_hit.t)
{
@ -224,7 +244,7 @@ hitInfo traceRay(Ray ray)
hitScene = traceScene(ray);
hit = hitBVH.t < hitScene.t ? hitBVH : hitScene;
#if 1
#if 0
if (hit.obj_index == -1 || objects[hit.obj_index].type != 5)
break ;
ray = portalRay(ray, hit);

View File

@ -20,8 +20,8 @@ int main(int argc, char **argv)
return (1);
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/debug.glsl");
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
GLint max_gpu_size;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);

View File

@ -33,8 +33,8 @@ void TopBVH::updateBounds(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh
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;
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);