+ | Accumulation texture

This commit is contained in:
TheRedShip
2024-12-27 17:10:35 +01:00
parent c25c337d1f
commit f64d6d0042
9 changed files with 135 additions and 72 deletions

View File

@ -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);
}

28
shaders/random.glsl Normal file
View File

@ -0,0 +1,28 @@
float seed = 1.0;
float getRandom(vec2 uv, int frameCount)
{
float seed = dot(uv, vec2(12.9898, 78.233)) + float(frameCount);
return fract(sin(seed) * 43758.5453);
}
vec3 randomVec3(vec2 uv, int frameCount)
{
return vec3(
getRandom(uv + vec2(0.1, 0.1), frameCount),
getRandom(uv + vec2(0.2, 0.2), frameCount),
getRandom(uv + vec2(0.3, 0.3), frameCount)
);
}
vec3 randomVec3Mixed(vec2 uv, int frameCount, float min_val, float max_val)
{
float randomX = getRandom(uv + vec2(0.1, 0.1), frameCount);
float randomY = getRandom(uv + vec2(0.2, 0.2), frameCount);
float randomZ = getRandom(uv + vec2(0.3, 0.3), frameCount);
return vec3(
mix(min_val, max_val, randomX),
mix(min_val, max_val, randomY),
mix(min_val, max_val, randomZ)
);
}

View File

@ -1,15 +0,0 @@
float seed = 1.0;
float getRandom(vec2 uv, int frameCount)
{
float seed = dot(uv, vec2(12.9898, 78.233)) + float(frameCount);
return fract(sin(seed) * 43758.5453);
}
vec3 randomVec3(vec2 uv, int frameCount)
{
return vec3(
getRandom(uv + vec2(0.1, 0.1), frameCount),
getRandom(uv + vec2(0.2, 0.2), frameCount),
getRandom(uv + vec2(0.3, 0.3), frameCount)
);
}