Skip to content

Commit

Permalink
Merge pull request #18 from tippesi/feature/rt-fx
Browse files Browse the repository at this point in the history
Feature/rt fx
  • Loading branch information
tippesi authored Nov 19, 2022
2 parents 6584c8a + 40258be commit 125ec82
Show file tree
Hide file tree
Showing 77 changed files with 2,810 additions and 747 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
[code-quality-image]: https://api.codiga.io/project/10000/score/svg
[code-quality-url]: https://app.codiga.io/public/project/10000/Atlas-Engine/dashboard

![GI scene](wiki/images/gi.gif) <br/>
*Realtime GI demo scene* <br/>
#### The current version is a WIP which means many changes and less stability.
![GI scene](wiki/images/intel_sponza.gif) <br/>*Realtime Sponza scene with software raytraced GI, AO and reflections (model from [Intel Graphics Research Sample Library](https://www.intel.com/content/www/us/en/developer/topic-technology/graphics-research/samples.html))* <br/>
## Introduction
This is a cross platform toy engine that is available on Linux and Windows.
This is a cross platform toy engine developed in my spare time that is available on Linux and Windows.
## Requirements
- OpenGL 4.6
- C++17 compatible compiler
Expand Down Expand Up @@ -72,5 +70,3 @@ For a code example have a look at the [demo application](https://github.com/tipp
*Path traced scene*
![Island scene](wiki/images/island.gif) <br/>
*Island demo scene using the terrain and ocean systems* <br/>
![Gun scene](wiki/images/gun.gif) <br/>
*PBR rendering* <br/>
Binary file added data/noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 57 additions & 57 deletions data/shader/ao/rtao.csh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <../common/random.hsh>
#include <../common/flatten.hsh>
#include <../common/convert.hsh>
#include <../brdf/brdfSample.hsh>

layout (local_size_x = 8, local_size_y = 4) in;

Expand All @@ -14,92 +15,91 @@ layout (binding = 3, r16f) writeonly uniform image2D rtaoImage;
layout(binding = 0) uniform sampler2D normalTexture;
layout(binding = 1) uniform sampler2D shadowMap;
layout(binding = 2) uniform sampler2D randomTexture;
layout(binding = 3) uniform isampler2D offsetTexture;

uniform uint sampleCount;
const ivec2 offsets[4] = ivec2[4](
ivec2(0, 0),
ivec2(1, 0),
ivec2(0, 1),
ivec2(1, 1)
);

uniform int sampleCount;
uniform float radius;

uniform mat4 pMatrix;
uniform mat4 ivMatrix;

uniform ivec2 resolution;
uniform uint frameSeed;

/*
Spherical Fibonacci Mapping
http://lgdv.cs.fau.de/publications/publication/Pub.2015.tech.IMMD.IMMD9.spheri/
Authors: Benjamin Keinert, Matthias Innmann, Michael Sänger, Marc Stamminger
*/
float madfrac(float a, float b) { return a * b - floor(a * b); }

vec3 fibonacciSphere(float i, float n) {
const float PHI = 1.6180339887498948482045868343656;
float phi = 2.0 * PI * madfrac(i, PHI);
float cosTheta = 1.0 - (2.0 * i + 1.0) * (1.0 / n);
float sinTheta = sqrt(saturate(1.0 - sqr(cosTheta)));
return vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
}

ivec2 FindNearest2x2(ivec2 pixel, out float depth) {
float depths[4];
depths[0] = texelFetch(shadowMap, pixel, 0).r;
depths[1] = texelFetch(shadowMap, pixel + ivec2(1, 0), 0).r;
depths[2] = texelFetch(shadowMap, pixel + ivec2(0, 1), 0).r;
depths[3] = texelFetch(shadowMap, pixel + ivec2(1, 1), 0).r;

ivec2 depthOffset = ivec2(0, 0);
depth = depths[0];
for (uint i = 1; i < 4; i++) {
if (depths[i] < depth) {
switch(i) {
case 1: depthOffset = ivec2(1, 0); break;
case 2: depthOffset = ivec2(0, 1); break;
case 3: depthOffset = ivec2(1, 1); break;
}
depth = depths[i];
}
}
void main() {

return depthOffset;
}
int verticalGroupCount = resolution.y / int(gl_WorkGroupSize.y);
verticalGroupCount += ((verticalGroupCount * int(gl_WorkGroupSize.y) == resolution.y) ? 0 : 1);
void main() {
int tileSize = 4;
int groupsPerTile = tileSize * verticalGroupCount;
int tileGroupIdx = int(gl_WorkGroupID.x) % groupsPerTile;
int tileIdx = int(gl_WorkGroupID.x) / groupsPerTile;
ivec2 tileBaseOffset = ivec2(tileIdx * tileSize, 0);
ivec2 tileGroupOffset = ivec2(tileGroupIdx % tileSize, tileGroupIdx / tileSize);
if (int(gl_GlobalInvocationID.x) < resolution.x &&
int(gl_GlobalInvocationID.y) < resolution.y) {
ivec2 groupOffset = (tileBaseOffset + tileGroupOffset) * ivec2(gl_WorkGroupSize);
ivec2 pixel = ivec2(gl_LocalInvocationID.xy) + groupOffset;
if (int(pixel.x) < resolution.x &&
int(pixel.y) < resolution.y) {
ivec2 pixel = ivec2(gl_GlobalInvocationID.xy);
vec2 texCoord = (vec2(pixel) + vec2(0.5)) / vec2(resolution);

float depth;
ivec2 offset = FindNearest2x2(pixel * 2, depth);
float depth = texelFetch(shadowMap, pixel, 0).r;

vec3 worldPos = vec3(ivMatrix * vec4(ConvertDepthToViewSpace(depth, texCoord), 1.0));
vec3 worldNorm = normalize(vec3(ivMatrix * vec4(2.0 * texelFetch(normalTexture, pixel * 2 + offset, 0).rgb - 1.0, 0.0)));
int offsetIdx = texelFetch(offsetTexture, pixel, 0).r;
ivec2 offset = offsets[offsetIdx];

vec2 recontructTexCoord = (2.0 * vec2(pixel) + offset + vec2(0.5)) / (2.0 * vec2(resolution));
vec3 worldPos = vec3(ivMatrix * vec4(ConvertDepthToViewSpace(depth, recontructTexCoord), 1.0));
vec3 worldNorm = normalize(vec3(ivMatrix * vec4(2.0 * textureLod(normalTexture, texCoord, 0).rgb - 1.0, 0.0)));
float seed = texelFetch(randomTexture, pixel % ivec2(4), 0).r;

float ao = 0.0;

vec3 up = abs(worldNorm.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
vec3 tangent = normalize(cross(up, worldNorm));
vec3 bitangent = cross(worldNorm, tangent);

mat3 TBN = mat3(tangent, bitangent, worldNorm);
float raySeed = float(seed);
float curSeed = float(0);

ivec2 noiseOffset = Unflatten2D(int(frameSeed), ivec2(16)) * ivec2(8);
vec2 blueNoiseVec = texelFetch(randomTexture, ((pixel % ivec2(8)) + noiseOffset) % ivec2(128), 0).xy * 256.0;
blueNoiseVec = clamp(blueNoiseVec, 0.0, 255.0);
blueNoiseVec = (blueNoiseVec + 0.5) / 256.0;

const int sampleCount = 32;
const int sampleCount = 1;
for (uint i = 0; i < sampleCount; i++) {
float fibIdx = round(float(sampleCount) * random(vec3(texCoord, float(i))));
Ray ray;
ray.origin = worldPos + worldNorm * EPSILON;
ray.direction = normalize(worldNorm + normalize(TBN * fibonacciSphere(fibIdx, float(sampleCount))));
Surface surface;

float u0 = random(raySeed, curSeed);
float u1 = random(raySeed, curSeed);

surface.N = worldNorm;
surface.P = worldPos;
BRDFSample brdfSample = SampleDiffuseBRDF(surface, blueNoiseVec);

ray.direction = brdfSample.L;
ray.inverseDirection = 1.0 / ray.direction;
ray.origin = worldPos + ray.direction * EPSILON + worldNorm * EPSILON;

ray.hitID = -1;
ray.hitDistance = 0.0;

HitClosest(ray, 0.0, radius);
bool hit = HitAny(ray, 0.0, radius);

ao += ray.hitID > 0 ? saturate(radius - sqr(ray.hitDistance)) : 0.0;
ao += hit ? 1.0 : 0.0;
}

float result = pow(1.0 - (ao / float(sampleCount)), 2.0);
float result = 1.0 - (ao / float(sampleCount));

imageStore(rtaoImage, pixel, vec4(result, 0.0, 0.0, 0.0));
}
Expand Down
6 changes: 3 additions & 3 deletions data/shader/ao/ssao.csh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ layout(binding = 3) uniform sampler2D randomTexture;
uniform vec3 samples[64];
uniform vec2 resolution;

uniform uint sampleCount;
uniform int sampleCount;
uniform float radius;
uniform float strength;
uniform int frameCount;
Expand Down Expand Up @@ -43,7 +43,7 @@ void main() {

//Create TBN matrix
vec3 tang = normalize(randomVec - norm * dot(randomVec, norm));
vec3 bitang = cross(norm, tang);
vec3 bitang = normalize(cross(norm, tang));
mat3 TBN = mat3(tang, bitang, norm);

//Calculate occlusion factor
Expand All @@ -70,7 +70,7 @@ void main() {

}

float result = pow(1.0 - (occlusion / float(sampleCount)), strength);
float result = pow(1.0 - (occlusion / float(sampleCount)), 2.0);
result = depth == 1.0 ? 0.0 : result;
imageStore(textureOut, pixel, vec4(result, 0.0, 0.0, 0.0));

Expand Down
Loading

0 comments on commit 125ec82

Please sign in to comment.