mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Accumulation texture
This commit is contained in:
@ -1,11 +1,10 @@
|
||||
#version 430 core
|
||||
#include "shaders/utils.glsl"
|
||||
|
||||
// Work group dimensions
|
||||
#include "shaders/random.glsl"
|
||||
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
|
||||
// Output image
|
||||
layout(binding = 0, rgba32f) uniform image2D outputImage;
|
||||
layout(binding = 1, rgba32f) uniform image2D accumulationImage;
|
||||
|
||||
struct GPUObject {
|
||||
vec3 position; // 12 + 4
|
||||
@ -31,53 +30,100 @@ uniform int u_frameCount;
|
||||
vec3 lightPos = vec3(5.0, 5.0, 5.0);
|
||||
vec3 lightColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
struct Ray {
|
||||
struct Ray
|
||||
{
|
||||
vec3 origin;
|
||||
vec3 direction;
|
||||
};
|
||||
|
||||
bool intersectSphere(Ray ray, vec3 center, float radius, out float t)
|
||||
struct hitInfo
|
||||
{
|
||||
vec3 oc = ray.origin - center;
|
||||
float t;
|
||||
vec3 normal;
|
||||
vec3 position;
|
||||
int obj_index;
|
||||
};
|
||||
|
||||
bool intersectSphere(Ray ray, GPUObject obj, out hitInfo hit)
|
||||
{
|
||||
vec3 oc = ray.origin - obj.position;
|
||||
float a = dot(ray.direction, ray.direction);
|
||||
float b = 2.0 * dot(oc, ray.direction);
|
||||
float c = dot(oc, oc) - radius * radius;
|
||||
float c = dot(oc, oc) - obj.radius * obj.radius;
|
||||
float discriminant = b * b - 4.0 * a * c;
|
||||
|
||||
if (discriminant < 0.0) {
|
||||
if (discriminant < 0.0)
|
||||
return false;
|
||||
}
|
||||
float t1 = (-b - sqrt(discriminant)) / (2.0 * a);
|
||||
if (t1 > 0.001) {
|
||||
t = t1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
float t = (-b - sqrt(discriminant)) / (2.0 * a);
|
||||
if (t < 0.0)
|
||||
t = (-b + sqrt(discriminant)) / (2.0 * a);
|
||||
|
||||
hit.t = t;
|
||||
hit.position = ray.origin + ray.direction * t;
|
||||
hit.normal = normalize(hit.position - obj.position);
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
vec3 pathtrace(Ray ray)
|
||||
hitInfo trace_ray(Ray ray)
|
||||
{
|
||||
vec3 color = vec3(0.0);
|
||||
vec3 light = vec3(0.0);
|
||||
hitInfo hit;
|
||||
hit.t = 1e30;
|
||||
hit.obj_index = -1;
|
||||
|
||||
float closest_t = 1e30;
|
||||
for (int i = 0; i < u_objectsNum; i++)
|
||||
{
|
||||
float t;
|
||||
if (intersectSphere(ray, objects[i].position, objects[i].radius, t))
|
||||
{
|
||||
if (t < closest_t)
|
||||
{
|
||||
closest_t = t;
|
||||
GPUObject obj = objects[i];
|
||||
|
||||
vec3 hitPoint = ray.origin + t * ray.direction;
|
||||
vec3 normal = normalize(hitPoint - objects[i].position);
|
||||
|
||||
color = objects[i].color * normal.y;
|
||||
hitInfo tempHit;
|
||||
if (intersectSphere(ray, obj, tempHit))
|
||||
{
|
||||
if (tempHit.t > 0.0f && tempHit.t < hit.t)
|
||||
{
|
||||
hit.t = tempHit.t;
|
||||
hit.obj_index = i;
|
||||
hit.position = tempHit.position;
|
||||
hit.normal = tempHit.normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (color);
|
||||
|
||||
return (hit);
|
||||
}
|
||||
|
||||
vec3 pathtrace(Ray ray, vec2 random)
|
||||
{
|
||||
vec3 color = vec3(1.0);
|
||||
vec3 light = vec3(0.0);
|
||||
|
||||
float closest_t = 1e30;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
hitInfo hit = trace_ray(ray);
|
||||
if (hit.obj_index == -1)
|
||||
{
|
||||
light += vec3(0); //ambient color
|
||||
break;
|
||||
}
|
||||
|
||||
GPUObject obj = objects[hit.obj_index];
|
||||
|
||||
color *= obj.color;
|
||||
if (obj.emission > 0.0)
|
||||
{
|
||||
light += obj.emission * obj.color;
|
||||
break;
|
||||
}
|
||||
|
||||
ray.origin = hit.position + hit.normal * 0.001;
|
||||
//cosine weighted importance sampling
|
||||
vec3 unit_sphere = normalize(randomVec3Mixed(random, u_frameCount, -1.0, 1.0));
|
||||
if (dot(unit_sphere, hit.normal) < 0.0)
|
||||
unit_sphere = -unit_sphere;
|
||||
ray.direction = normalize(hit.normal + unit_sphere);
|
||||
}
|
||||
return (color * light);
|
||||
}
|
||||
|
||||
void main() {
|
||||
@ -97,8 +143,13 @@ void main() {
|
||||
rayDirection = normalize(rayDirection);
|
||||
Ray ray = Ray(u_cameraPosition, rayDirection);
|
||||
|
||||
vec3 color = pathtrace(ray);
|
||||
vec3 color = pathtrace(ray, uv);
|
||||
|
||||
imageStore(outputImage, pixelCoords, vec4(randomVec3(uv, u_frameCount), 1.0));
|
||||
vec4 accum = imageLoad(accumulationImage, pixelCoords);
|
||||
accum.rgb = accum.rgb * float(u_frameCount) / float(u_frameCount + 1) + color / float(u_frameCount + 1);
|
||||
accum.a = 1.0;
|
||||
|
||||
imageStore(accumulationImage, pixelCoords, accum);
|
||||
imageStore(outputImage, pixelCoords, accum);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user