mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | BVH Working
This commit is contained in:
@ -1,17 +0,0 @@
|
||||
|
||||
|
||||
bool intersectRayBVH(Ray ray, GPUBvh node)
|
||||
{
|
||||
vec3 invDir = 1.0 / ray.direction;
|
||||
|
||||
vec3 t1 = (node.min - ray.origin) * invDir;
|
||||
vec3 t2 = (node.max - ray.origin) * invDir;
|
||||
|
||||
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);
|
||||
|
||||
return tEnter <= tExit && tExit >= 0.0;
|
||||
}
|
@ -66,6 +66,24 @@ layout(std430, binding = 3) buffer LightsBuffer
|
||||
int lightsIndex[];
|
||||
};
|
||||
|
||||
struct GPUBvh
|
||||
{
|
||||
vec3 min;
|
||||
vec3 max;
|
||||
|
||||
int left_index;
|
||||
int right_index;
|
||||
|
||||
int is_leaf;
|
||||
|
||||
int first_primitive;
|
||||
int primitive_count;
|
||||
};
|
||||
layout(std430, binding = 4) buffer BvhBuffer
|
||||
{
|
||||
GPUBvh bvh[];
|
||||
};
|
||||
|
||||
|
||||
layout(std140, binding = 0) uniform CameraData
|
||||
{
|
||||
@ -105,64 +123,8 @@ struct hitInfo
|
||||
#include "shaders/scatter.glsl"
|
||||
#include "shaders/light.glsl"
|
||||
#include "shaders/volumetric.glsl"
|
||||
#include "shaders/trace.glsl"
|
||||
|
||||
Ray portalRay(Ray ray, hitInfo hit)
|
||||
{
|
||||
GPUObject portal_1;
|
||||
GPUObject portal_2;
|
||||
vec3 relative;
|
||||
|
||||
portal_1 = objects[hit.obj_index];
|
||||
portal_2 = objects[int(portal_1.radius)]; // saving memory radius = portal_index
|
||||
|
||||
relative = hit.position - portal_1.position;
|
||||
|
||||
mat3 rotation = mat3(portal_2.rotation) * transpose(mat3(portal_1.rotation));
|
||||
|
||||
if (dot(portal_1.normal, portal_2.normal) > 0.0)
|
||||
{
|
||||
mat3 reflection = mat3(1.0) - 2.0 * outerProduct(portal_2.normal, portal_2.normal);
|
||||
rotation *= reflection;
|
||||
}
|
||||
|
||||
ray.origin = portal_2.position + rotation * relative;
|
||||
ray.direction = normalize(rotation * ray.direction);
|
||||
|
||||
ray.origin += ray.direction * 0.01f;
|
||||
|
||||
return (ray);
|
||||
}
|
||||
|
||||
hitInfo traceRay(Ray ray)
|
||||
{
|
||||
hitInfo hit;
|
||||
|
||||
for (int p = 0; p < 25; p++) //portals
|
||||
{
|
||||
hit.t = 1e30;
|
||||
hit.obj_index = -1;
|
||||
|
||||
for (int i = 0; i < u_objectsNum; i++)
|
||||
{
|
||||
GPUObject obj = objects[i];
|
||||
|
||||
hitInfo temp_hit;
|
||||
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||
{
|
||||
hit.t = temp_hit.t;
|
||||
hit.last_t = temp_hit.last_t;
|
||||
hit.obj_index = i;
|
||||
hit.position = temp_hit.position;
|
||||
hit.normal = temp_hit.normal;
|
||||
}
|
||||
}
|
||||
if (hit.obj_index == -1 || objects[hit.obj_index].type != 5)
|
||||
break ;
|
||||
ray = portalRay(ray, hit);
|
||||
}
|
||||
|
||||
return (hit);
|
||||
}
|
||||
|
||||
vec3 pathtrace(Ray ray, inout uint rng_state)
|
||||
{
|
||||
|
@ -100,33 +100,6 @@ int traceRay(Ray ray)
|
||||
return (num_hit);
|
||||
}
|
||||
|
||||
|
||||
int traceBVHBad(Ray ray)
|
||||
{
|
||||
int num_hit;
|
||||
|
||||
num_hit = 0;
|
||||
for (int i = 0; i < u_bvhNum; i++)
|
||||
{
|
||||
GPUBvh node = bvh[i];
|
||||
|
||||
if (intersectRayBVH(ray, node))
|
||||
{
|
||||
// num_hit++;
|
||||
for (int i = 0; i < node.primitive_count; i++)
|
||||
{
|
||||
GPUObject obj = objects[node.first_primitive + i];
|
||||
|
||||
hitInfo tmp;
|
||||
if (intersect(ray, obj, tmp))
|
||||
num_hit++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (num_hit);
|
||||
}
|
||||
|
||||
int traceBVH(Ray ray)
|
||||
{
|
||||
int num_hit = 0;
|
||||
@ -157,7 +130,7 @@ int traceBVH(Ray ray)
|
||||
|
||||
hitInfo tmp;
|
||||
if (intersect(ray, obj, tmp))
|
||||
num_hit++;
|
||||
num_hit;
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,6 +172,6 @@ void main()
|
||||
Ray ray = initRay(uv);
|
||||
int hits = traceBVH(ray);
|
||||
|
||||
vec3 color = vec3(float(hits) / float(10));
|
||||
vec3 color = vec3(float(hits) / float(u_bvhNum / 4));
|
||||
imageStore(output_image, pixel_coords, vec4(color, 1.));
|
||||
}
|
@ -225,4 +225,23 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
|
||||
if (obj.type == 6)
|
||||
return (intersectCylinder(ray, obj, hit));
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool intersectRayBVH(Ray ray, GPUBvh node)
|
||||
{
|
||||
vec3 invDir = 1.0 / ray.direction;
|
||||
|
||||
vec3 t1 = (node.min - ray.origin) * invDir;
|
||||
vec3 t2 = (node.max - ray.origin) * invDir;
|
||||
|
||||
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);
|
||||
|
||||
return tEnter <= tExit && tExit >= 0.0;
|
||||
}
|
||||
|
||||
|
||||
|
118
shaders/trace.glsl
Normal file
118
shaders/trace.glsl
Normal file
@ -0,0 +1,118 @@
|
||||
|
||||
|
||||
Ray portalRay(Ray ray, hitInfo hit)
|
||||
{
|
||||
GPUObject portal_1;
|
||||
GPUObject portal_2;
|
||||
vec3 relative;
|
||||
|
||||
portal_1 = objects[hit.obj_index];
|
||||
portal_2 = objects[int(portal_1.radius)]; // saving memory radius = portal_index
|
||||
|
||||
relative = hit.position - portal_1.position;
|
||||
|
||||
mat3 rotation = mat3(portal_2.rotation) * transpose(mat3(portal_1.rotation));
|
||||
|
||||
if (dot(portal_1.normal, portal_2.normal) > 0.0)
|
||||
{
|
||||
mat3 reflection = mat3(1.0) - 2.0 * outerProduct(portal_2.normal, portal_2.normal);
|
||||
rotation *= reflection;
|
||||
}
|
||||
|
||||
ray.origin = portal_2.position + rotation * relative;
|
||||
ray.direction = normalize(rotation * ray.direction);
|
||||
|
||||
ray.origin += ray.direction * 0.01f;
|
||||
|
||||
return (ray);
|
||||
}
|
||||
|
||||
hitInfo traceScene(Ray ray)
|
||||
{
|
||||
hitInfo hit;
|
||||
|
||||
for (int p = 0; p < 25; p++) //portals
|
||||
{
|
||||
hit.t = 1e30;
|
||||
hit.obj_index = -1;
|
||||
|
||||
for (int i = 0; i < u_objectsNum; i++)
|
||||
{
|
||||
GPUObject obj = objects[i];
|
||||
|
||||
hitInfo temp_hit;
|
||||
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||
{
|
||||
hit.t = temp_hit.t;
|
||||
hit.last_t = temp_hit.last_t;
|
||||
hit.obj_index = i;
|
||||
hit.position = temp_hit.position;
|
||||
hit.normal = temp_hit.normal;
|
||||
}
|
||||
}
|
||||
if (hit.obj_index == -1 || objects[hit.obj_index].type != 5)
|
||||
break ;
|
||||
ray = portalRay(ray, hit);
|
||||
}
|
||||
|
||||
return (hit);
|
||||
}
|
||||
|
||||
hitInfo traceBVH(Ray ray)
|
||||
{
|
||||
hitInfo hit;
|
||||
|
||||
hit.t = 1e30;
|
||||
hit.obj_index = -1;
|
||||
|
||||
const int MAX_STACK_SIZE = 64;
|
||||
int stack[MAX_STACK_SIZE];
|
||||
int stack_ptr = 0;
|
||||
|
||||
stack[0] = 0;
|
||||
|
||||
while (stack_ptr >= 0)
|
||||
{
|
||||
int current_index = stack[stack_ptr--];
|
||||
|
||||
GPUBvh node = bvh[current_index];
|
||||
|
||||
if (intersectRayBVH(ray, node))
|
||||
{
|
||||
if (node.is_leaf != 0)
|
||||
{
|
||||
for (int i = 0; i < node.primitive_count; i++)
|
||||
{
|
||||
GPUObject obj = objects[node.first_primitive + i];
|
||||
|
||||
hitInfo temp_hit;
|
||||
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||
{
|
||||
hit.t = temp_hit.t;
|
||||
hit.last_t = temp_hit.last_t;
|
||||
hit.obj_index = node.first_primitive + i;
|
||||
hit.position = temp_hit.position;
|
||||
hit.normal = temp_hit.normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (node.is_leaf == 0 && stack_ptr < MAX_STACK_SIZE - 2)
|
||||
{
|
||||
stack_ptr++;
|
||||
stack[stack_ptr] = node.left_index;
|
||||
stack_ptr++;
|
||||
stack[stack_ptr] = node.right_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (hit);
|
||||
}
|
||||
|
||||
hitInfo traceRay(Ray ray)
|
||||
{
|
||||
if (true)
|
||||
return (traceBVH(ray));
|
||||
return (traceScene(ray));
|
||||
}
|
Reference in New Issue
Block a user