Skip to content

Commit

Permalink
Merge pull request #39 from Zielon/release
Browse files Browse the repository at this point in the history
Release 1.0
  • Loading branch information
juanraul8 authored Nov 12, 2020
2 parents 8d3dd0a + 29394a7 commit 7dfffdf
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 88 deletions.
9 changes: 2 additions & 7 deletions PBRVulkan/Assets/Scenes/cornell_box.scene
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ Camera
fov 40
}

material orange
{
color 1 0.5 0
}

material white
{
color 0.725 0.71 0.68
Expand Down Expand Up @@ -61,15 +56,15 @@ mesh
mesh
{
file cornell_box/cbox_smallbox.obj
material orange
material white
position .1855 .0825 .169
scale 0.01 0.01 0.01
}

mesh
{
file cornell_box/cbox_largebox.obj
material orange
material white
position .3685 .165 .35125
scale 0.01 0.01 0.01
}
Expand Down
3 changes: 1 addition & 2 deletions PBRVulkan/RayTracer/src/Assets/Light.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#include <glm/glm.hpp>

namespace Assets
Expand All @@ -18,7 +17,7 @@ namespace Assets
alignas(16) glm::vec3 u{};
alignas(16) glm::vec3 v{};
glm::float32_t area{};
glm::float32_t type{};
glm::int32_t type{};
glm::float32_t radius{};
};
}
1 change: 0 additions & 1 deletion PBRVulkan/RayTracer/src/Assets/Material.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#include <glm/glm.hpp>

namespace Assets
Expand Down
42 changes: 42 additions & 0 deletions PBRVulkan/RayTracer/src/Assets/Shaders/Common/Math.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,46 @@ float powerHeuristic(float a, float b)
float max3(vec3 v)
{
return max (max (v.x, v.y), v.z);
}

vec3 cosineSampleHemisphere()
{
float r1 = rnd(seed);
float r2 = rnd(seed);

vec3 dir;
float r = sqrt(r1);
float phi = 2.0 * PI * r2;

dir.x = r * cos(phi);
dir.y = r * sin(phi);
dir.z = sqrt(max(0.0, 1.0 - dir.x*dir.x - dir.y*dir.y));

return dir;
}

vec2 sampleUnitDisk()
{
while(true)
{
float r1 = rnd(seed);
float r2 = rnd(seed);
vec2 p = 2.0 * vec2(r1, r2) - 1.0;
if (dot(p, p) < 1.0) return p;
}
}

vec3 uniformSampleSphere()
{
float r1 = rnd(seed);
float r2 = rnd(seed);

float z = 1.0 - 2.0 * r1;
float r = sqrt(max(0.f, 1.0 - z * z));
float phi = 2.0 * PI * r2;

float x = r * cos(phi);
float y = r * sin(phi);

return vec3(x, y, z);
}
64 changes: 16 additions & 48 deletions PBRVulkan/RayTracer/src/Assets/Shaders/Common/Sampling.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,6 @@
* Set of different sampling functions
*/

vec3 cosineSampleHemisphere()
{
float r1 = rnd(seed);
float r2 = rnd(seed);

vec3 dir;
float r = sqrt(r1);
float phi = 2.0 * PI * r2;

dir.x = r * cos(phi);
dir.y = r * sin(phi);
dir.z = sqrt(max(0.0, 1.0 - dir.x*dir.x - dir.y*dir.y));

return dir;
}

vec3 uniformSampleSphere()
{
float r1 = rnd(seed);
float r2 = rnd(seed);

float z = 1.0 - 2.0 * r1;
float r = sqrt(max(0.f, 1.0 - z * z));
float phi = 2.0 * PI * r2;

float x = r * cos(phi);
float y = r * sin(phi);

return vec3(x, y, z);
}

void sampleAreaLight(in Light light, out LightSample lightSample)
{
float r1 = rnd(seed);
Expand All @@ -49,19 +18,19 @@ void sampleAreaLight(in Light light, out LightSample lightSample)
void sampleSphereLight(in Light light, out LightSample lightSample)
{
vec3 position = light.position + uniformSampleSphere() * light.radius;
lightSample.position = position;

lightSample.normal = normalize(position - light.position);
lightSample.emission = light.emission * float(ubo.lights);
lightSample.position = position;
}

LightSample sampleLight(in Light light)
{
LightSample lightSample;

if (int(light.type) == AREA_LIGHT)
sampleAreaLight(light, lightSample);

if (int(light.type) == SPHERE_LIGHT)
if (light.type == AREA_LIGHT)
sampleAreaLight(light, lightSample);
else
sampleSphereLight(light, lightSample);

return lightSample;
Expand Down Expand Up @@ -119,17 +88,17 @@ float planeIntersect(in Light light)

void checkAreaLightIntersection(inout float closest, float hit, in Light light, inout LightSample lightSample)
{
float distance = planeIntersect(light);
float dist = planeIntersect(light);

if (distance < 0.) distance = INFINITY;
if (dist < 0.) dist = INFINITY;

if (distance < closest && distance < hit)
if (dist < closest && dist < hit)
{
closest = distance;
closest = dist;

vec3 normal = normalize(cross(light.u, light.v));
float cosTheta = abs(dot(-gl_WorldRayDirectionNV, normal));
float pdf = (distance * distance) / (light.area * cosTheta);
float pdf = (dist * dist) / (light.area * cosTheta);

lightSample.emission = light.emission;
lightSample.pdf = pdf;
Expand All @@ -140,17 +109,17 @@ void checkAreaLightIntersection(inout float closest, float hit, in Light light,

void checkSphereLightIntersection(inout float closest, float hit, in Light light, inout LightSample lightSample)
{
float distance = sphereIntersect(light);
float dist = sphereIntersect(light);

if (distance < 0.) distance = INFINITY;
if (dist < 0.) dist = INFINITY;

if (distance < closest && distance < hit)
if (dist < closest && dist < hit)
{
closest = distance;
closest = dist;

vec3 surfacePos = gl_WorldRayOriginNV + gl_WorldRayDirectionNV * hit;
vec3 normal = normalize(surfacePos - light.position);
float pdf = (distance * distance) / light.area;
float pdf = (dist * dist) / light.area;

lightSample.emission = light.emission;
lightSample.pdf = pdf;
Expand All @@ -168,8 +137,7 @@ bool interesetsEmitter(inout LightSample lightSample, float hit)

if (light.type == AREA_LIGHT)
checkAreaLightIntersection(closest, hit, light, lightSample);

if (light.type == SPHERE_LIGHT)
else
checkSphereLightIntersection(closest, hit, light, lightSample);
}

Expand Down
4 changes: 2 additions & 2 deletions PBRVulkan/RayTracer/src/Assets/Shaders/Common/Structs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct Material
int heightmapTexID;
};

struct Ray
struct Ray
{
vec3 origin;
vec3 direction;
Expand All @@ -56,7 +56,7 @@ struct Light
vec3 u;
vec3 v;
float area;
float type;
int type;
float radius;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ layout(binding = 1) readonly buffer MaterialArray { Material[] materials; };
layout(binding = 2) uniform sampler2D[] textureSamplers;
layout(binding = 3) readonly buffer LightArray { Light[] Lights; };

#include "../Common/Random.glsl"
#include "../Common/Math.glsl"

layout(location = 0) in vec2 inTexCoord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ layout(location = 1) rayPayloadNV bool isShadowed;
hitAttributeNV vec2 hit;

#include "../Common/Vertex.glsl"
#include "../Common/Math.glsl"
#include "../Common/Random.glsl"
#include "../Common/Math.glsl"

#ifdef USE_HDR
#include "../Common/HDR.glsl"
Expand Down
27 changes: 14 additions & 13 deletions PBRVulkan/RayTracer/src/Assets/Shaders/Raytracer/Raytracing.rgen
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main()
float tMax = INFINITY;

uint SPP = ubo.spp; // Samples per pixel
uint seed = tea(gl_LaunchIDNV.y * gl_LaunchSizeNV.x + gl_LaunchIDNV.x, ubo.frame);
seed = tea(gl_LaunchIDNV.y * gl_LaunchSizeNV.x + gl_LaunchIDNV.x, ubo.frame);

vec3 radiance = vec3(0);

Expand All @@ -42,31 +42,31 @@ void main()
float r1 = rnd(seed);
float r2 = rnd(seed);

vec2 jitter = vec2(r1, r2);
vec2 jitter = vec2(r1, r2);
vec3 lensOffset = vec3(ubo.aperture / 2.0 * sampleUnitDisk(), 0.0);

vec2 pixel = vec2(gl_LaunchIDNV.x, gl_LaunchIDNV.y) + jitter;
vec2 uv = (pixel / gl_LaunchSizeNV.xy) * 2.0 - 1.0;
vec4 origin = viewInv * vec4(0, 0, 0, 1);
vec4 target = projInv * vec4(uv.x, uv.y, 1, 1);
vec4 direction = viewInv * vec4(normalize(target.xyz), 0);

// TODO Simulate a lens
vec4 origin = viewInv * vec4(lensOffset, 1.0);
vec4 target = projInv * vec4(uv.x, uv.y, 1.0, 1.0);
vec4 direction = viewInv * vec4(normalize(target.xyz * ubo.focalDistance - lensOffset), 0.0);

Ray ray = Ray(origin.xyz, direction.xyz);
vec3 pathRadiance = vec3(0);

vec3 beta = vec3(1);
vec3 pathRadiance = vec3(0);
bool specularBounce = false;
BsdfSample bsdf;

for (uint j = 0; j < ubo.maxDepth; ++j)
{
payload.depth = j;
payload.specularBounce = specularBounce;
payload.stop = false;
payload.radiance = pathRadiance;
payload.beta = beta;
payload.ray = ray;
payload.bsdf = bsdf;
payload.specularBounce = specularBounce;

traceNV(
TLAS, // acceleration structure
Expand All @@ -92,7 +92,7 @@ void main()
if (j == 0)
{
imageStore(NormalsImage, ivec2(gl_LaunchIDNV.xy), vec4(payload.ffnormal, 0.0));
//imageStore(DepthImage, ivec2(gl_LaunchIDNV.xy), vec4(payload.worldPos, 0.0));
//imageStore(PositionImage, ivec2(gl_LaunchIDNV.xy), vec4(payload.worldPos, 0.0));
}

if (payload.stop) break;
Expand All @@ -104,11 +104,12 @@ void main()
radiance /= float(SPP);

// HDR scale
vec3 accumulatedRadiance = ubo.frame > 1 ? imageLoad(AccumulationImage, ivec2(gl_LaunchIDNV.xy)).rbg : vec3(0);
accumulatedRadiance += radiance;
vec4 accumulated = ubo.frame > 1 ? imageLoad(AccumulationImage, ivec2(gl_LaunchIDNV.xy)) : vec4(0.0);
vec3 accumulatedRadiance = accumulated.xyz + radiance;

imageStore(AccumulationImage, ivec2(gl_LaunchIDNV.xy), vec4(accumulatedRadiance, 1.0));

float inv = 1.f / ubo.frame;
float inv = 1.f / float(ubo.frame);
radiance = accumulatedRadiance * inv;

// LDR scale
Expand Down
1 change: 0 additions & 1 deletion PBRVulkan/RayTracer/src/Geometry/Global.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#include <glm/glm.hpp>

namespace Uniforms
Expand Down
7 changes: 5 additions & 2 deletions PBRVulkan/RayTracer/src/Loader/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ namespace Loader

sscanf(line, " name %s", name);
sscanf(line, " color %f %f %f", &material.albedo.x, &material.albedo.y, &material.albedo.z);
sscanf(line, " emission %f %f %f", &material.emission.x, &material.emission.y, &material.emission.z);
sscanf(line, " emission %f %f %f", &material.emission.x, &material.emission.y,
&material.emission.z);
sscanf(line, " materialType %f", &material.albedo.w);
sscanf(line, " metallic %f", &material.metallic);
sscanf(line, " roughness %f", &material.roughness);
Expand Down Expand Up @@ -228,7 +229,7 @@ namespace Loader
sscanf(line, " fov %f", &fov);
}

float aspect = float(renderOptions.resolution.x) / renderOptions.resolution.y;
float aspect = static_cast<float>(renderOptions.resolution.x) / renderOptions.resolution.y;
scene.AddCamera(position, lookAt, fov, aspect);
cameraAdded = true;
}
Expand Down Expand Up @@ -291,5 +292,7 @@ namespace Loader
// Add default camera if none was specified
if (!cameraAdded)
scene.AddCamera(glm::vec3(0.0f, 0.0f, 10.0f), glm::vec3(0.0f, 0.0f, -10.0f), 35.0f, 1.f);

return true;
}
}
2 changes: 1 addition & 1 deletion PBRVulkan/RayTracer/src/Loader/Loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

#define GLM_SWIZZLE
#define GLM_FORCE_SWIZZLE
#include <glm/glm.hpp>

namespace Assets
Expand Down
Loading

0 comments on commit 7dfffdf

Please sign in to comment.