From fcf693329dc8e18f40bb6d7150f62e231f754aeb Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Mon, 20 Jan 2025 10:28:24 +0100 Subject: [PATCH] + | Multiple obj implenting on compute --- imgui.ini | 2 +- scenes/dragon.rt | 8 ++++---- shaders/compute.glsl | 22 +++++++++++++++++---- shaders/trace.glsl | 46 ++++++++++++++++++++++++++++++++++---------- srcs/RT.cpp | 4 ++-- 5 files changed, 61 insertions(+), 21 deletions(-) diff --git a/imgui.ini b/imgui.ini index 9929b65..95b4e37 100644 --- a/imgui.ini +++ b/imgui.ini @@ -3,7 +3,7 @@ Pos=60,60 Size=400,400 [Window][Camera] -Pos=1646,5 +Pos=1643,7 Size=259,200 [Window][Material] diff --git a/scenes/dragon.rt b/scenes/dragon.rt index 56eddec..307edae 100644 --- a/scenes/dragon.rt +++ b/scenes/dragon.rt @@ -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 -OBJ obj/Dragon_800K.obj -OBJ obj/Lowpoly_tree_sample.obj +OBJ obj/Dragon_80K.obj +# OBJ obj/Lowpoly_tree_sample.obj -po -1.99 -0.5 -0.5 0 1 0 0 0 1 1 4 -po -0.5 -0.5 -1.99 0 1 0 1 0 0 0 4 +# po -1.99 -0.5 -0.5 0 1 0 0 0 1 1 4 +# po -0.5 -0.5 -1.99 0 1 0 1 0 0 0 4 diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 894dbfd..de1636e 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -61,6 +61,14 @@ struct GPUVolume int enabled; }; +struct GPUBvhData +{ + vec3 offset; + + int bvh_start_index; + int triangle_start_index; +}; + struct GPUBvh { vec3 min; @@ -85,17 +93,22 @@ layout(std430, binding = 2) buffer TriangleBuffer GPUTriangle triangles[]; }; -layout(std430, binding = 3) buffer BvhBuffer +layout(std430, binding = 3) buffer BvhDataBuffer { - GPUBvh bvh[]; + GPUBvhData BvhData[]; }; -layout(std430, binding = 4) buffer MaterialBuffer +layout(std430, binding = 4) buffer BvhBuffer +{ + GPUBvh Bvh[]; +}; + +layout(std430, binding = 5) buffer MaterialBuffer { GPUMaterial materials[]; }; -layout(std430, binding = 5) buffer LightsBuffer +layout(std430, binding = 6) buffer LightsBuffer { int lightsIndex[]; }; @@ -115,6 +128,7 @@ layout(std140, binding = 1) uniform VolumeData uniform int u_objectsNum; +uniform int u_bvhNum; uniform int u_lightsNum; uniform vec2 u_resolution; uniform int u_pixelisation; diff --git a/shaders/trace.glsl b/shaders/trace.glsl index 26ac5ed..9f2d0a0 100644 --- a/shaders/trace.glsl +++ b/shaders/trace.glsl @@ -53,10 +53,10 @@ hitInfo traceScene(Ray ray) return (hit); } -hitInfo traceBVH(Ray ray) +hitInfo traceBVH(Ray ray, GPUBvhData bvh_data) { - hitInfo temp_hit; hitInfo hit; + hitInfo hit_bvh; hit.t = 1e30; hit.obj_index = -1; @@ -64,24 +64,24 @@ hitInfo traceBVH(Ray ray) int stack[32]; int stack_ptr = 0; stack[0] = 0; - + while (stack_ptr >= 0) { int current_index = stack[stack_ptr--]; + GPUBvh node = Bvh[bvh_data.bvh_start_index + current_index]; - GPUBvh node = bvh[current_index]; - if (node.is_leaf != 0) { for (int i = 0; i < node.primitive_count; i++) { - GPUTriangle obj = triangles[node.first_primitive + i]; + GPUTriangle obj = triangles[bvh_data.triangle_start_index + node.first_primitive + i]; + hitInfo temp_hit; 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 = node.first_primitive + i; + 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; @@ -90,8 +90,8 @@ hitInfo traceBVH(Ray ray) } else { - GPUBvh left_node = bvh[node.left_index]; - GPUBvh right_node = bvh[node.right_index]; + GPUBvh left_node = Bvh[bvh_data.bvh_start_index + node.left_index]; + GPUBvh right_node = Bvh[bvh_data.bvh_start_index + node.right_index]; hitInfo left_hit; hitInfo right_hit; @@ -118,6 +118,32 @@ hitInfo traceBVH(Ray ray) return (hit); } +hitInfo traverseBVHs(Ray ray) +{ + hitInfo hit; + + hit.t = 1e30; + hit.obj_index = -1; + + for (int i = 0; i < u_bvhNum; i++) + { + ray.origin = i == 0 ? ray.origin : ray.origin + vec3(2., 0., 0.); + hitInfo temp_hit = traceBVH(ray, BvhData[i]); + + if (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; + } + } + + return (hit); +} + hitInfo traceRay(Ray ray) { hitInfo hitBVH; @@ -126,7 +152,7 @@ hitInfo traceRay(Ray ray) for (int i = 0; i < 10; i++) // portal ray { - hitBVH = traceBVH(ray); + hitBVH = traverseBVHs(ray); hitScene = traceScene(ray); hit = hitBVH.t < hitScene.t ? hitBVH : hitScene; diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 5667ac1..e7d6445 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -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);