+ | BVH opti

This commit is contained in:
TheRedShip
2025-01-18 18:08:01 +01:00
parent 85bab977df
commit 4863a5ef77
8 changed files with 1797 additions and 2879 deletions

4559
fps.txt

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ Pos=1652,11
Size=259,200
[Window][Material]
Pos=1650,214
Pos=1648,207
Size=266,285
[Window][Fog settings]
@ -15,6 +15,6 @@ Pos=1654,501
Size=247,157
[Window][Debug]
Pos=1650,665
Size=260,294
Pos=1651,676
Size=260,143

View File

@ -1,6 +1,6 @@
CAM -1 0 0 0 0 0 1 90 5
CAM -1 40 40 0 -90 0 1 90 5
MAT 255 255 255 0.0 1. 0.5 // white 0
MAT 255 255 255 0.0 1. 1. // white 0
MAT 255 10 10 0.0 0.0 0.0 // red 1
MAT 30 30 30 0.0 0.0 0.0 // gray 2
@ -13,13 +13,13 @@ MAT 255 255 255 5.0 0.0 0.0 // white light 6
MAT 255 255 255 0.0 0.0 0.0 TRN // glass 7
# pl 0 0 -40 0 0 1 1 // back wall
# pl 0 0 40 0 0 -1 5 // front wall
# pl 40 0 0 -1 0 0 3 // right wall
# pl -40 0 0 1 0 0 2 // left wall
# pl 0 60 0 0 -1 0 4 // ceiling
# pl 0 -10 0 0 1 0 2 // floor
pl 0 0 -40 0 0 1 1 // back wall
pl 0 0 40 0 0 -1 5 // front wall
pl 40 0 0 -1 0 0 3 // right wall
pl -40 0 0 1 0 0 2 // left wall
pl 0 60 0 0 -1 0 4 // ceiling
pl 0 -10 0 0 1 0 2 // floor
# qu -20 59 -20 40 0 0 0 0 40 6
qu -20 59 -20 40 0 0 0 0 40 6
OBJ obj/Dragon_80K.obj
OBJ obj/Model.obj

View File

@ -135,28 +135,24 @@ int traceRay(Ray ray)
hitInfo traceBVH(Ray ray, inout Stats stats)
{
hitInfo hit;
hitInfo hit_bvh;
hit.t = 1e30;
hit.obj_index = -1;
const int MAX_STACK_SIZE = 64;
int stack[MAX_STACK_SIZE];
int stack[64];
int stack_ptr = 0;
stack[0] = 0;
vec3 inv_dir = 1.0 / ray.direction;
while (stack_ptr >= 0)
{
int current_index = stack[stack_ptr--];
GPUBvh node = bvh[current_index];
if (intersectRayBVH(ray, node))
{
stats.box_count++;
if (intersectRayBVH(ray, node, hit_bvh) && hit_bvh.t < hit.t)
{
if (node.is_leaf != 0)
{
for (int i = 0; i < node.primitive_count; i++)
@ -173,13 +169,35 @@ hitInfo traceBVH(Ray ray, inout Stats stats)
stats.triangle_count++;
}
}
if (node.is_leaf == 0 && stack_ptr < MAX_STACK_SIZE - 2)
else
{
stack_ptr++;
stack[stack_ptr] = node.left_index;
stack_ptr++;
stack[stack_ptr] = node.right_index;
// GPUBvh left_node = bvh[node.left_index];
// GPUBvh right_node = bvh[node.right_index];
// hitInfo left_hit;
// hitInfo right_hit;
// left_hit.t = 1e30;
// right_hit.t = 1e30;
// stats.box_count += 2;
// intersectRayBVH(ray, left_node, left_hit);
// intersectRayBVH(ray, right_node, right_hit);
// if (left_hit.t > right_hit.t)
// {
// if (left_hit.t < hit.t) stack[++stack_ptr] = node.left_index;
// if (right_hit.t < hit.t) stack[++stack_ptr] = node.right_index;
// }
// else
// {
// if (right_hit.t < hit.t) stack[++stack_ptr] = node.right_index;
// if (left_hit.t < hit.t) stack[++stack_ptr] = node.left_index;
// }
stack[++stack_ptr] = node.left_index;
stack[++stack_ptr] = node.right_index;
}
}
}

View File

@ -225,7 +225,7 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
}
bool intersectRayBVH(Ray ray, GPUBvh node)
bool intersectRayBVH(Ray ray, GPUBvh node, inout hitInfo hit)
{
vec3 invDir = 1.0 / ray.direction;
@ -235,10 +235,10 @@ bool intersectRayBVH(Ray ray, GPUBvh node)
vec3 tMin = min(t1, t2);
vec3 tMax = max(t1, t2);
float tEnter = max(max(tMin.x, tMin.y), tMin.z);
float tExit = min(min(tMax.x, tMax.y), tMax.z);
hit.t = max(max(tMin.x, tMin.y), tMin.z);
hit.last_t = min(min(tMax.x, tMax.y), tMax.z);
return tEnter <= tExit && tExit >= 0.0;
return hit.t <= hit.last_t && hit.last_t >= 0.0;
}

View File

@ -66,10 +66,8 @@ hitInfo traceBVH(Ray ray)
hit.t = 1e30;
hit.obj_index = -1;
const int MAX_STACK_SIZE = 64;
int stack[MAX_STACK_SIZE];
int stack[64];
int stack_ptr = 0;
stack[0] = 0;
while (stack_ptr >= 0)
@ -78,7 +76,8 @@ hitInfo traceBVH(Ray ray)
GPUBvh node = bvh[current_index];
if (intersectRayBVH(ray, node))
hitInfo temp_hit;
if (intersectRayBVH(ray, node, temp_hit) && temp_hit.t < hit.t)
{
if (node.is_leaf != 0)
{
@ -98,13 +97,10 @@ hitInfo traceBVH(Ray ray)
}
}
}
if (node.is_leaf == 0 && stack_ptr < MAX_STACK_SIZE - 2)
else
{
stack_ptr++;
stack[stack_ptr] = node.left_index;
stack_ptr++;
stack[stack_ptr] = node.right_index;
stack[++stack_ptr] = node.left_index;
stack[++stack_ptr] = node.right_index;
}
}
}

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);
@ -122,13 +122,22 @@ int main(int argc, char **argv)
Camera *camera = scene.getCamera();
// performance profiling
if (true && window.getFps() < 2000)
if (false)
{
float time = (float)(glfwGetTime()) ;
recorded_fps.push_back((int)window.getFps());
camera->setPosition(glm::vec3(cos((time + 6.28) * 0.5), 0., sin((time + 6.28) * 0.5)));
float y_offset = 30;
float dist_to_obj = 60;
float speed = 0.5;
camera->setPosition(glm::vec3(
cos((time + 6.28) * speed) * dist_to_obj,
y_offset,
sin((time + 6.28) * speed) * dist_to_obj
));
glm::vec3 direction = glm::normalize(camera->getPosition());
float yaw = glm::degrees(atan2(direction.z, direction.x));

View File

@ -67,7 +67,7 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com
const char *fragmentCode = loadFileWithIncludes(fragmentPath);
const char *computeCode = loadFileWithIncludes(computePath);
// printWithLineNumbers(computeCode);
printWithLineNumbers(computeCode);
_vertex = glCreateShader(GL_VERTEX_SHADER);