2020-05-21 10:20:19 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
|
|
|
|
|
|
struct RayPayload
|
|
|
|
|
{
|
|
|
|
|
float3 color;
|
|
|
|
|
float distance;
|
|
|
|
|
float3 normal;
|
|
|
|
|
float reflector;
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-13 17:26:51 +02:00
|
|
|
struct Attributes
|
|
|
|
|
{
|
|
|
|
|
float2 bary;
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-21 10:20:19 +00:00
|
|
|
RaytracingAccelerationStructure topLevelAS : register(t0);
|
2020-07-28 20:20:38 +02:00
|
|
|
struct UBO
|
2020-05-21 10:20:19 +00:00
|
|
|
{
|
|
|
|
|
float4x4 viewInverse;
|
|
|
|
|
float4x4 projInverse;
|
|
|
|
|
float4 lightPos;
|
2020-07-28 20:20:38 +02:00
|
|
|
int vertexSize;
|
2020-05-21 10:20:19 +00:00
|
|
|
};
|
2020-07-28 20:20:38 +02:00
|
|
|
cbuffer ubo : register(b2) { UBO ubo; };
|
2020-05-21 10:20:19 +00:00
|
|
|
|
|
|
|
|
StructuredBuffer<float4> vertices : register(t3);
|
|
|
|
|
StructuredBuffer<uint> indices : register(t4);
|
|
|
|
|
|
|
|
|
|
struct Vertex
|
|
|
|
|
{
|
|
|
|
|
float3 pos;
|
|
|
|
|
float3 normal;
|
|
|
|
|
float2 uv;
|
2020-07-28 20:20:38 +02:00
|
|
|
float4 color;
|
|
|
|
|
float4 _pad0;
|
|
|
|
|
float4 _pad1;
|
2020-05-21 10:20:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vertex unpack(uint index)
|
|
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
// 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];
|
2020-05-21 10:20:19 +00:00
|
|
|
|
|
|
|
|
Vertex v;
|
|
|
|
|
v.pos = d0.xyz;
|
|
|
|
|
v.normal = float3(d0.w, d1.x, d1.y);
|
2020-07-28 20:20:38 +02:00
|
|
|
v.color = float4(d2.x, d2.y, d2.z, 1.0);
|
|
|
|
|
|
2020-05-21 10:20:19 +00:00
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[shader("closesthit")]
|
2023-10-13 17:26:51 +02:00
|
|
|
void main(inout RayPayload rayPayload, in Attributes attribs)
|
2020-05-21 10:20:19 +00:00
|
|
|
{
|
|
|
|
|
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
|
2023-10-13 17:26:51 +02:00
|
|
|
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
|
2020-05-21 10:20:19 +00:00
|
|
|
float3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);
|
|
|
|
|
|
|
|
|
|
// Basic lighting
|
2020-07-28 20:20:38 +02:00
|
|
|
float3 lightVector = normalize(ubo.lightPos.xyz);
|
2020-05-21 10:20:19 +00:00
|
|
|
float dot_product = max(dot(lightVector, normal), 0.6);
|
2020-07-28 20:20:38 +02:00
|
|
|
rayPayload.color.rgb = v0.color * dot_product;
|
2020-05-21 10:20:19 +00:00
|
|
|
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;
|
|
|
|
|
}
|