Added VK_NV_ray_tracing reflections example

This commit is contained in:
Sascha Willems 2019-04-27 11:05:29 +02:00
parent e140095002
commit 8fdabb0019
7 changed files with 918 additions and 0 deletions

View file

@ -0,0 +1,70 @@
#version 460
#extension GL_NV_ray_tracing : require
#extension GL_EXT_nonuniform_qualifier : enable
struct RayPayload {
vec3 color;
float distance;
vec3 normal;
float reflector;
};
layout(location = 0) rayPayloadInNV RayPayload rayPayload;
layout(location = 2) rayPayloadNV bool shadowed;
hitAttributeNV vec3 attribs;
layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS;
layout(binding = 2, set = 0) uniform CameraProperties
{
mat4 viewInverse;
mat4 projInverse;
vec4 lightPos;
} cam;
layout(binding = 3, set = 0) buffer Vertices { vec4 v[]; } vertices;
layout(binding = 4, set = 0) buffer Indices { uint i[]; } indices;
struct Vertex
{
vec3 pos;
vec3 normal;
vec3 color;
vec2 uv;
float _pad0;
};
Vertex unpack(uint index)
{
vec4 d0 = vertices.v[3 * index + 0];
vec4 d1 = vertices.v[3 * index + 1];
vec4 d2 = vertices.v[3 * index + 2];
Vertex v;
v.pos = d0.xyz;
v.normal = vec3(d0.w, d1.x, d1.y);
v.color = vec3(d1.z, d1.w, d2.x);
return v;
}
void main()
{
ivec3 index = ivec3(indices.i[3 * gl_PrimitiveID], indices.i[3 * gl_PrimitiveID + 1], indices.i[3 * gl_PrimitiveID + 2]);
Vertex v0 = unpack(index.x);
Vertex v1 = unpack(index.y);
Vertex v2 = unpack(index.z);
// Interpolate normal
const vec3 barycentricCoords = vec3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
vec3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);
// Basic lighting
vec3 lightVector = normalize(cam.lightPos.xyz);
float dot_product = max(dot(lightVector, normal), 0.6);
rayPayload.color = v0.color * vec3(dot_product);
rayPayload.distance = gl_RayTmaxNV;
rayPayload.normal = normal;
// Objects with full white vertex color are treated as reflectors
rayPayload.reflector = ((v0.color.r == 1.0f) && (v0.color.g == 1.0f) && (v0.color.b == 1.0f)) ? 1.0f : 0.0f;
}

View file

@ -0,0 +1,25 @@
#version 460
#extension GL_NV_ray_tracing : require
struct RayPayload {
vec3 color;
float distance;
vec3 normal;
float reflector;
};
layout(location = 0) rayPayloadInNV RayPayload rayPayload;
void main()
{
// View-independent background gradient to simulate a basic sky background
const vec3 gradientStart = vec3(0.5, 0.6, 1.0);
const vec3 gradientEnd = vec3(1.0);
vec3 unitDir = normalize(gl_WorldRayDirectionNV);
float t = 0.5 * (unitDir.y + 1.0);
rayPayload.color = (1.0-t) * gradientStart + t * gradientEnd;
rayPayload.distance = -1.0f;
rayPayload.normal = vec3(0.0f);
rayPayload.reflector = 0.0f;
}

Binary file not shown.

View file

@ -0,0 +1,62 @@
#version 460
#extension GL_NV_ray_tracing : require
layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS;
layout(binding = 1, set = 0, rgba8) uniform image2D image;
layout(binding = 2, set = 0) uniform CameraProperties
{
mat4 viewInverse;
mat4 projInverse;
vec4 lightPos;
} cam;
struct RayPayload {
vec3 color;
float distance;
vec3 normal;
float reflector;
};
layout(location = 0) rayPayloadNV RayPayload rayPayload;
// Max. number of recursion is passed via a specialization constant
layout (constant_id = 0) const int MAX_RECURSION = 0;
void main()
{
const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5);
const vec2 inUV = pixelCenter/vec2(gl_LaunchSizeNV.xy);
vec2 d = inUV * 2.0 - 1.0;
vec4 origin = cam.viewInverse * vec4(0,0,0,1);
vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1) ;
vec4 direction = cam.viewInverse*vec4(normalize(target.xyz), 0);
uint rayFlags = gl_RayFlagsOpaqueNV;
uint cullMask = 0xff;
float tmin = 0.001;
float tmax = 10000.0;
vec3 color = vec3(0.0);
for (int i = 0; i < MAX_RECURSION; i++) {
traceNV(topLevelAS, rayFlags, cullMask, 0, 0, 0, origin.xyz, tmin, direction.xyz, tmax, 0);
vec3 hitColor = rayPayload.color;
if (rayPayload.distance < 0.0f) {
color += hitColor;
break;
} else if (rayPayload.reflector == 1.0f) {
const vec4 hitPos = origin + direction * rayPayload.distance;
origin.xyz = hitPos.xyz + rayPayload.normal * 0.001f;
direction.xyz = reflect(direction.xyz, rayPayload.normal);
} else {
color += hitColor;
break;
}
}
imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(color, 0.0));
}