Update ray tracing samples to use VK_KHR_ray_tracing (#753)

* Started updating ray tracing samples to KHR extension

* Updated GLSL shaders to use GL_EXT_ray_tracing

* Code cleanup, naming

* Fix include directories to use Vulkan headers from repository instead of NDK for the Android build

* Added new Android function pointers

* Renamed basic ray tracing sample

Added android build files

* Remove unused batch file

* Replaced remaining NV identifiers

* Updating ray tracing shadow sample to KHR extension

* Updated shaders to use KHR instead of NV extension

Fixed shader bindings

* Updating ray tracing reflections sample to KHR extension

* Renamed ray tracing reflections sample

* Renamed ray tracing shadows sample

Added android build files

* Removed no-longer used batch files for shader generation

* Proper alignment for the shader binding table

* Updated readme

* Reworked shader group setup

* Cleanup

* Reworked shader group setup

* Reworked shader group setup

* Code cleanup
This commit is contained in:
Sascha Willems 2020-08-15 17:59:02 +02:00 committed by GitHub
parent e6956acfbd
commit ee946e2abf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
98 changed files with 3291 additions and 2457 deletions

View file

@ -0,0 +1,75 @@
// Copyright 2020 Google LLC
struct RayPayload
{
float3 color;
float distance;
float3 normal;
float reflector;
};
RaytracingAccelerationStructure topLevelAS : register(t0);
struct UBO
{
float4x4 viewInverse;
float4x4 projInverse;
float4 lightPos;
int vertexSize;
};
cbuffer ubo : register(b2) { UBO ubo; };
StructuredBuffer<float4> vertices : register(t3);
StructuredBuffer<uint> indices : register(t4);
struct Vertex
{
float3 pos;
float3 normal;
float2 uv;
float4 color;
float4 _pad0;
float4 _pad1;
};
Vertex unpack(uint index)
{
// Unpack the vertices from the SSBO using the glTF vertex structure
// The multiplier is the size of the vertex divided by four float components (=16 bytes)
const int m = ubo.vertexSize / 16;
float4 d0 = vertices[m * index + 0];
float4 d1 = vertices[m * index + 1];
float4 d2 = vertices[m * index + 2];
Vertex v;
v.pos = d0.xyz;
v.normal = float3(d0.w, d1.x, d1.y);
v.color = float4(d2.x, d2.y, d2.z, 1.0);
return v;
}
[shader("closesthit")]
void main(inout RayPayload rayPayload, in float3 attribs)
{
uint PrimitiveID = PrimitiveIndex();
int3 index = int3(indices[3 * PrimitiveID], indices[3 * PrimitiveID + 1], indices[3 * PrimitiveID + 2]);
Vertex v0 = unpack(index.x);
Vertex v1 = unpack(index.y);
Vertex v2 = unpack(index.z);
// Interpolate normal
const float3 barycentricCoords = float3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
float3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);
// Basic lighting
float3 lightVector = normalize(ubo.lightPos.xyz);
float dot_product = max(dot(lightVector, normal), 0.6);
rayPayload.color.rgb = v0.color * dot_product;
rayPayload.distance = RayTCurrent();
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 @@
// Copyright 2020 Google LLC
struct RayPayload {
float3 color;
float distance;
float3 normal;
float reflector;
};
[shader("miss")]
void main(inout RayPayload rayPayload)
{
float3 worldRayDirection = WorldRayDirection();
// View-independent background gradient to simulate a basic sky background
const float3 gradientStart = float3(0.5, 0.6, 1.0);
const float3 gradientEnd = float3(1.0, 1.0, 1.0);
float3 unitDir = normalize(worldRayDirection);
float t = 0.5 * (unitDir.y + 1.0);
rayPayload.color = (1.0-t) * gradientStart + t * gradientEnd;
rayPayload.distance = -1.0f;
rayPayload.normal = float3(0, 0, 0);
rayPayload.reflector = 0.0f;
}

Binary file not shown.

View file

@ -0,0 +1,64 @@
// Copyright 2020 Google LLC
RaytracingAccelerationStructure rs : register(t0);
RWTexture2D<float4> image : register(u1);
struct CameraProperties
{
float4x4 viewInverse;
float4x4 projInverse;
float4 lightPos;
};
cbuffer cam : register(b2) { CameraProperties cam; };
struct RayPayload {
float3 color;
float distance;
float3 normal;
float reflector;
};
// Max. number of recursion is passed via a specialization constant
[[vk::constant_id(0)]] const int MAX_RECURSION = 0;
[shader("raygeneration")]
void main()
{
uint3 LaunchID = DispatchRaysIndex();
uint3 LaunchSize = DispatchRaysDimensions();
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
const float2 inUV = pixelCenter/float2(LaunchSize.xy);
float2 d = inUV * 2.0 - 1.0;
float4 target = mul(cam.projInverse, float4(d.x, d.y, 1, 1));
RayDesc rayDesc;
rayDesc.Origin = mul(cam.viewInverse, float4(0,0,0,1)).xyz;
rayDesc.Direction = mul(cam.viewInverse, float4(normalize(target.xyz), 0)).xyz;
rayDesc.TMin = 0.001;
rayDesc.TMax = 10000.0;
float3 color = float3(0.0, 0.0, 0.0);
for (int i = 0; i < MAX_RECURSION; i++) {
RayPayload rayPayload;
TraceRay(rs, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, rayPayload);
float3 hitColor = rayPayload.color;
if (rayPayload.distance < 0.0f) {
color += hitColor;
break;
} else if (rayPayload.reflector == 1.0f) {
const float3 hitPos = rayDesc.Origin + rayDesc.Direction * rayPayload.distance;
rayDesc.Origin = hitPos + rayPayload.normal * 0.001f;
rayDesc.Direction = reflect(rayDesc.Direction, rayPayload.normal);
} else {
color += hitColor;
break;
}
}
image[int2(LaunchID.xy)] = float4(color, 0.0);
}