Files
Seele/res/shaders/raytracing/ClosestHit.slang
T

151 lines
5.9 KiB
Plaintext
Raw Normal View History

2024-07-08 13:46:49 +02:00
import Common;
import MaterialParameter;
import LightEnv;
2024-07-10 21:07:10 +02:00
import Scene;
import RayTracingData;
2024-09-27 15:57:37 +02:00
import StaticMeshVertexData;
2024-07-15 17:55:22 +02:00
import Material;
import MATERIAL_FILE_NAME;
2024-07-08 13:46:49 +02:00
// simplification: all BLAS only have 1 geometry
[shader("closesthit")]
2024-07-12 13:33:52 +02:00
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
2024-07-08 13:46:49 +02:00
{
2024-12-31 10:40:03 +01:00
hitValue.hit = true;
// todo: replace with anyhit shader
if(hitValue.anyHit)
return;
2024-07-15 17:55:22 +02:00
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
2024-07-08 13:46:49 +02:00
2024-07-15 17:55:22 +02:00
InstanceData inst = pScene.instances[InstanceID()];
MeshData m = pScene.meshData[InstanceID()];
2024-07-12 13:33:52 +02:00
2024-07-15 17:55:22 +02:00
// offset into the index buffer
uint indexOffset = m.firstIndex;
// added to indices to reference correct part of global mesh pool
uint vertexOffset = pScene.meshletInfos[m.meshletOffset].indicesOffset;
uint vertexIndex0 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 0];
uint vertexIndex1 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 1];
uint vertexIndex2 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 2];
VertexAttributes attr0 = pVertexData.getAttributes(vertexIndex0);
VertexAttributes attr1 = pVertexData.getAttributes(vertexIndex1);
VertexAttributes attr2 = pVertexData.getAttributes(vertexIndex2);
2024-12-27 17:06:43 +01:00
FragmentParameter f0 = attr0.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
FragmentParameter f1 = attr1.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
FragmentParameter f2 = attr2.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
2024-07-15 17:55:22 +02:00
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
MaterialParameter materialParams = params.getMaterialParameter();
2024-12-31 10:40:03 +01:00
LightingParameter lightingParams = params.getLightingParameter();
lightingParams.viewDir_WS = -WorldRayDirection();
2024-12-25 14:59:08 +01:00
let brdf = Material.prepare(materialParams);
2024-07-16 16:44:56 +02:00
2025-01-02 14:28:31 +01:00
float3 normal_WS = normalize(mul(params.getTangentToWorld(), brdf.normal));
2024-12-31 10:40:03 +01:00
float3 normalLight_WS = dot(normal_WS,WorldRayOrigin())<0 ? normal_WS : -normal_WS;
2025-01-02 14:28:31 +01:00
float3 intersection_WS = params.position_WS + normal_WS * 0.001f;
2024-12-31 10:40:03 +01:00
hitValue.depth++;
float3 localAccRad = float3(0);
float3 rnd = rand01(uint3(vertexIndex0, vertexIndex1, vertexIndex2));
//float kt = ka + ks;
//float s = -log(rnd.z) / kt;
//float3 xs = r.o + s * r.d;
//if (s < t) {
// float p = kt * rnd.z;
// if (depth > 5) {
// if (rnd.z >= p) break;
// else accmat /= p;
// }
// float3 ldirect = nextEventEstimation(accmat, r.d, xs, -r.d, kt, true, rnd);
// accrad += (fogEmm + ks * ldirect) / kt;
// accmat *= ks / kt;
// rayDesc.Origin = xs;
// rayDesc.Direction = float3(
// cos(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
// sin(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
// rnd.y
// );
// continue;
//}
//float p = max(max(brdf.baseColor.x, brdf.baseColor.color.y), brdf.baseColor.color.z);
//if(hitValue.depth > 5) {
// if (rnd.z >= p) return;
// else hitValue.accmat /= p;
//}
//-- Ideal DIFFUSE reflection
//if(bool(useNEE)) {
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
//}
for(uint i = 0; i < pLightEnv.numDirectionalLights; ++i) {
2025-01-02 14:28:31 +01:00
float3 x = intersection_WS;
2024-12-31 10:40:03 +01:00
float3 l = -pLightEnv.directionalLights[i].direction.xyz;
2025-01-02 14:28:31 +01:00
RayDesc rayDesc;
rayDesc.TMax = 10000.0f;
rayDesc.TMin = 0.001f;
2024-12-31 10:40:03 +01:00
rayDesc.Origin = x;
rayDesc.Direction = l;
RayPayload payload;
payload.depth = hitValue.depth;
payload.emissive = 1;
payload.anyHit = true;
2025-01-02 14:28:31 +01:00
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
2024-12-31 10:40:03 +01:00
// we have missed all geometry, so directional light is affecting us
if(!payload.hit) {
localAccRad += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
}
}
2025-01-02 14:28:31 +01:00
for(uint i = 0; i < pLightEnv.numPointLights; ++i) {
RayPayload payload;
float3 x = intersection_WS;
float3 l = pLightEnv.pointLights[i].position_WS.xyz - intersection_WS;
if(length(l) > pLightEnv.pointLights[i].colorRange.w) {
continue;
}
RayDesc rayDesc;
rayDesc.TMax = 1.0f;
rayDesc.TMin = 0.001f;
rayDesc.Origin = x;
rayDesc.Direction = l;
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
// hitting only after the light
if(!payload.hit) {
localAccRad += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
}
}
2024-12-31 10:40:03 +01:00
// Indirect Illumination: cosine-weighted importance sampling
if(hitValue.depth < 12) {
float r1 = 2 * PI * rnd.x, r2 = rnd.y, r2s = sqrt(r2);
float3 w = normalLight_WS;
float3 u = normalize((cross(abs(w.x)>0.1 ? float3(0,1,0) : float3(1,0,0), w)));
float3 v = cross(w,u);
2025-01-02 14:28:31 +01:00
RayDesc rayDesc;
rayDesc.TMax = 10000.0f;
rayDesc.TMin = 0.001f;
rayDesc.Origin = intersection_WS;
2024-12-31 10:40:03 +01:00
rayDesc.Direction = normalize(u*cos(r1)*r2s + v * sin(r1)*r2s + w * sqrt(1 - r2));
RayPayload payload;
payload.light = float3(0);
payload.emissive = 0; // in the next bounce, consider reflective part only!
payload.depth = hitValue.depth+1;
payload.anyHit = false;
2025-01-02 14:28:31 +01:00
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
2024-12-31 10:40:03 +01:00
if(payload.hit) {
DirectionalLight dir;
dir.color = float4(payload.light, 0);
dir.direction = float4(-rayDesc.Direction, 0);
localAccRad += dir.illuminate(lightingParams, brdf);
}
}
hitValue.light += localAccRad;
2024-07-08 13:46:49 +02:00
}