mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | Small opti
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
#version 430 core
|
||||
#extension GL_NV_gpu_shader5 : enable
|
||||
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
layout(binding = 0, rgba32f) uniform image2D output_image;
|
||||
@ -80,10 +81,9 @@ struct GPUBvh
|
||||
int left_index;
|
||||
int right_index;
|
||||
|
||||
int is_leaf;
|
||||
|
||||
int first_primitive;
|
||||
int primitive_count;
|
||||
|
||||
};
|
||||
|
||||
layout(std430, binding = 1) buffer ObjectBuffer
|
||||
@ -162,53 +162,41 @@ struct hitInfo
|
||||
#include "shaders/volumetric.glsl"
|
||||
#include "shaders/trace.glsl"
|
||||
|
||||
|
||||
vec3 pathtrace(Ray ray, inout uint rng_state)
|
||||
vec3 pathtrace(Ray ray, inout uint rng_state)
|
||||
{
|
||||
vec3 color = vec3(1.0);
|
||||
vec3 light = vec3(0.0);
|
||||
|
||||
vec3 transmittance = vec3(1.0);
|
||||
vec3 color = vec3(1.0);
|
||||
vec3 light = vec3(0.0);
|
||||
vec3 transmittance = vec3(1.0);
|
||||
|
||||
for (int i = 0; i < camera.bounce; i++)
|
||||
{
|
||||
hitInfo hit = traceRay(ray);
|
||||
|
||||
float t_scatter = 0.0;
|
||||
float scatter_valid = float(volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state));
|
||||
|
||||
float miss_condition = float(hit.obj_index == -1);
|
||||
light += miss_condition * transmittance * GetEnvironmentLight(ray);
|
||||
|
||||
float p = max(color.r, max(color.g, color.b));
|
||||
float rr_continue = float(randomValue(rng_state) <= p);
|
||||
color /= max(p, 0.001);
|
||||
|
||||
GPUMaterial mat = materials[hit.mat_index];
|
||||
|
||||
for (int i = 0; i < camera.bounce; i++)
|
||||
{
|
||||
hitInfo hit = traceRay(ray);
|
||||
|
||||
float t_scatter = 0.0;
|
||||
if (volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state))
|
||||
{
|
||||
calculateVolumetricLight(t_scatter, ray, color, light, transmittance, rng_state);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hit.obj_index == -1)
|
||||
{
|
||||
light += transmittance * GetEnvironmentLight(ray);
|
||||
break;
|
||||
}
|
||||
|
||||
if (volume.enabled != 0)
|
||||
transmittance *= exp(-volume.sigma_t * hit.t);
|
||||
|
||||
GPUMaterial mat = materials[hit.mat_index];
|
||||
|
||||
// RR
|
||||
float p = max(color.r, max(color.g, color.b));
|
||||
if (randomValue(rng_state) > p)
|
||||
break;
|
||||
color /= p;
|
||||
//
|
||||
|
||||
calculateLightColor(mat, hit, color, light, rng_state);
|
||||
|
||||
float break_condition = miss_condition + (1.0 - rr_continue);
|
||||
if (break_condition > 0.0) break;
|
||||
|
||||
calculateLightColor(mat, hit, color, light, rng_state);
|
||||
|
||||
if (mat.emission > 0.0)
|
||||
break;
|
||||
|
||||
ray = newRay(hit, ray, rng_state);
|
||||
ray.inv_direction = 1.0 / ray.direction;
|
||||
}
|
||||
|
||||
return (color * light);
|
||||
ray = newRay(hit, ray, rng_state);
|
||||
ray.inv_direction = 1.0 / ray.direction;
|
||||
}
|
||||
|
||||
return color * light;
|
||||
}
|
||||
|
||||
Ray initRay(vec2 uv, inout uint rng_state)
|
||||
|
@ -70,6 +70,7 @@ layout(std430, binding = 2) buffer TriangleBuffer
|
||||
struct GPUBvhData
|
||||
{
|
||||
mat4 transform;
|
||||
mat4 inv_transform;
|
||||
vec3 offset;
|
||||
float scale;
|
||||
|
||||
|
@ -93,26 +93,31 @@ bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
||||
return (valid);
|
||||
}
|
||||
|
||||
bool intersectTriangle(Ray ray, GPUTriangle tri, out hitInfo hit)
|
||||
bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit)
|
||||
{
|
||||
vec3 edgeAB = tri.vertex1 - tri.position;
|
||||
vec3 edgeAC = tri.vertex2 - tri.position;
|
||||
vec3 normalVector = tri.normal;
|
||||
vec3 ao = ray.origin - tri.position;
|
||||
vec3 dao = cross(ao, ray.direction);
|
||||
|
||||
float determinant = -dot(ray.direction, normalVector);
|
||||
float invDet = 1 / determinant;
|
||||
|
||||
float dst = dot(ao, normalVector) * invDet;
|
||||
float u = dot(edgeAC, dao) * invDet;
|
||||
float v = -dot(edgeAB, dao) * invDet;
|
||||
float w = 1 - u - v;
|
||||
|
||||
hit.position = ray.origin + ray.direction * dst;
|
||||
hit.normal = normalVector;
|
||||
hit.t = dst;
|
||||
return (determinant >= 1E-8 && dst >= 0 && u >= 0 && v >= 0 && w >= 0);
|
||||
vec3 vertex1 = obj.vertex1 - obj.position;
|
||||
vec3 vertex2 = obj.vertex2 - obj.position;
|
||||
|
||||
vec3 pvec = cross(ray.direction, vertex2);
|
||||
float det = dot(vertex1, pvec);
|
||||
vec3 tvec = ray.origin - obj.position;
|
||||
|
||||
float invDet = 1.0 / det;
|
||||
float u = dot(tvec, pvec) * invDet;
|
||||
vec3 qvec = cross(tvec, vertex1);
|
||||
float v = dot(ray.direction, qvec) * invDet;
|
||||
float t = dot(vertex2, qvec) * invDet;
|
||||
|
||||
bool valid = abs(det) > 1e-8 &&
|
||||
u >= 0.0 && u <= 1.0 &&
|
||||
v >= 0.0 && (u + v) <= 1.0 &&
|
||||
t > 0.0;
|
||||
|
||||
hit.t = t;
|
||||
hit.position = ray.origin + ray.direction * t;
|
||||
hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
|
||||
|
||||
return (valid);
|
||||
}
|
||||
|
||||
bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)
|
||||
|
@ -71,7 +71,7 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
|
||||
int current_index = stack[stack_ptr--];
|
||||
GPUBvh node = Bvh[bvh_data.bvh_start_index + current_index];
|
||||
|
||||
if (node.is_leaf != 0)
|
||||
if (node.primitive_count != 0)
|
||||
{
|
||||
for (int i = 0; i < node.primitive_count; i++)
|
||||
{
|
||||
|
Reference in New Issue
Block a user