2024-07-08 13:46:49 +02:00
|
|
|
import Common;
|
|
|
|
|
import MaterialParameter;
|
|
|
|
|
import LightEnv;
|
2025-04-11 09:54:09 +02:00
|
|
|
import VertexData;
|
2024-07-15 17:55:22 +02:00
|
|
|
import Material;
|
2025-06-30 21:15:14 +02:00
|
|
|
import StaticMeshVertexData;
|
2024-07-15 17:55:22 +02:00
|
|
|
import MATERIAL_FILE_NAME;
|
2026-04-12 20:49:02 +02:00
|
|
|
import RayTracingData;
|
|
|
|
|
import Scene;
|
2024-07-08 13:46:49 +02:00
|
|
|
|
|
|
|
|
// simplification: all BLAS only have 1 geometry
|
|
|
|
|
|
2025-01-29 16:15:48 +01:00
|
|
|
/*vec3 nextEventEstimation(vec3 accmat, vec3 w, vec3 x, vec3 nl, float kt, bool useAtt, vec3 rnd) {
|
|
|
|
|
uint triId = 0;
|
|
|
|
|
uint sphereId = 0;
|
|
|
|
|
ShadingParams paramsls;
|
|
|
|
|
Material matls;
|
|
|
|
|
vec3 result = vec3(0);
|
|
|
|
|
// Direct Illumination: Next Event Estimation over any present lights
|
|
|
|
|
for (int i = spheres.length(); i-->0;) {
|
|
|
|
|
Sphere ls = spheres[i];
|
2026-03-15 22:37:08 +01:00
|
|
|
if (all(equal(ls.e, vec3(0)))) continue; // skip non-emissive spheres
|
2025-01-29 16:15:48 +01:00
|
|
|
vec3 xc = ls.geo.xyz - x;
|
|
|
|
|
vec3 sw = normalize(xc), su = normalize(cross((abs(sw.x)>.1 ? vec3(0,1,0) : vec3(1,0,0)), sw)), sv = cross(sw,su);
|
|
|
|
|
float cos_a_max = sqrt(float(1 - ls.geo.w*ls.geo.w / dot(xc,xc)));
|
|
|
|
|
float cos_a = 1 - rnd.x + rnd.x*cos_a_max, sin_a = sqrt(1 - cos_a*cos_a);
|
|
|
|
|
float phi = 2 * pi * rnd.y;
|
|
|
|
|
vec3 l = normalize(su*cos(phi)*sin_a + sv*sin(phi)*sin_a + sw*cos_a); // sampled direction towards light
|
2026-03-15 22:37:08 +01:00
|
|
|
if (intersect(Ray(x,l), matls, paramsls, sphereId, triId) && sphereId == i) { // test if shadow ray hits this light
|
|
|
|
|
source float omega = 2 * pi * (1-cos_a_max); if(useAtt) { float tau = exp(-kt * length(x - paramsls.x)); result += max(dot(l,nl),0) *
|
|
|
|
|
ls.e * omega * tau; } else { result += accmat / pi * max(dot(l,nl),0) * ls.e * omega; // brdf term obj.c.xyz already in accmat, 1/pi
|
|
|
|
|
for brdf
|
2025-01-29 16:15:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i = meshLights.length(); i-->0;) {
|
|
|
|
|
MeshDescriptor mesh = meshes[meshLights[i]];
|
|
|
|
|
for(int j = 0; j < mesh.numIndices; j+=3) {
|
|
|
|
|
vec3 A = vec3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 0]]);
|
|
|
|
|
vec3 B = vec3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 1]]);
|
|
|
|
|
vec3 C = vec3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 2]]);
|
|
|
|
|
float b1 = 1 - sqrt(rnd.x);
|
|
|
|
|
float b2 = (1 - rnd.y) * sqrt(rnd.x);
|
|
|
|
|
float b3 = rnd.y * sqrt(rnd.x);
|
|
|
|
|
vec3 P = A * b1 + B * b2 + C * b3;
|
|
|
|
|
vec3 omega = P - x;
|
|
|
|
|
vec3 l = normalize(omega);
|
|
|
|
|
float v = 0.f;
|
|
|
|
|
if(intersect(Ray(x,l), matls, paramsls, sphereId, triId) && triId == j) {
|
|
|
|
|
v = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
vec3 e1 = C - A;
|
|
|
|
|
vec3 e2 = B - A;
|
|
|
|
|
float area = length(cross(e1, e2)) / 2;
|
|
|
|
|
float rayLen = length(omega);
|
|
|
|
|
float cosTheta = dot(nl, l);
|
|
|
|
|
float cosThetaDash = dot(paramsls.n, -l);
|
|
|
|
|
float factor = area * (cosThetaDash / (rayLen * rayLen));
|
|
|
|
|
if(useAtt) {
|
|
|
|
|
float tau = phase(w, l) * exp(-kt * length(x - paramsls.x));
|
|
|
|
|
result += tau * matls.e * max(cosTheta, 0) * factor;
|
|
|
|
|
} else {
|
|
|
|
|
result += accmat * (matls.e * max(cosTheta, 0) * factor) / pi;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2025-01-29 21:55:11 +01:00
|
|
|
const static float ka = 0;
|
|
|
|
|
const static float ks = 0;
|
|
|
|
|
const static float3 fogEmm = float3(0, 0.01, 0.01);
|
|
|
|
|
|
|
|
|
|
const static float eps = 1e-5;
|
2024-07-08 13:46:49 +02:00
|
|
|
[shader("closesthit")]
|
2026-03-15 22:37:08 +01:00
|
|
|
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr) {
|
2024-12-31 10:40:03 +01:00
|
|
|
hitValue.hit = true;
|
|
|
|
|
// todo: replace with anyhit shader
|
2026-03-15 22:37:08 +01:00
|
|
|
if (hitValue.anyHit)
|
2024-12-31 10:40:03 +01:00
|
|
|
return;
|
2025-01-30 23:25:41 +01:00
|
|
|
|
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
|
2025-04-15 00:33:49 +02:00
|
|
|
uint indexOffset = m.indicesRange.offset;
|
2024-07-15 17:55:22 +02:00
|
|
|
// added to indices to reference correct part of global mesh pool
|
2025-04-15 00:33:49 +02:00
|
|
|
uint vertexOffset = pScene.meshletInfos[m.meshletRange.offset].indicesOffset;
|
2024-07-15 17:55:22 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2025-01-30 23:25:41 +01:00
|
|
|
hitValue.params = params.getLightingParameter();
|
|
|
|
|
hitValue.materialParams = params.getMaterialParameter();
|
2026-03-15 22:37:08 +01:00
|
|
|
|
2025-01-30 23:25:41 +01:00
|
|
|
LightingParameter lightingParams = hitValue.params;
|
2024-12-31 10:40:03 +01:00
|
|
|
lightingParams.viewDir_WS = -WorldRayDirection();
|
2025-01-30 23:25:41 +01:00
|
|
|
let brdf = Material.prepare(hitValue.materialParams);
|
2024-07-16 16:44:56 +02:00
|
|
|
|
2025-01-30 23:25:41 +01:00
|
|
|
float3 normal_WS = brdf.getNormal();
|
2026-03-15 22:37:08 +01:00
|
|
|
float3 normalLight_WS = dot(normal_WS, WorldRayDirection()) < 0 ? normal_WS : -normal_WS;
|
2025-01-30 23:25:41 +01:00
|
|
|
float3 intersection_WS = lightingParams.position_WS;
|
2026-03-15 22:37:08 +01:00
|
|
|
|
2024-12-31 10:40:03 +01:00
|
|
|
hitValue.depth++;
|
2025-01-29 16:15:48 +01:00
|
|
|
float3 rnd = rand01(hitValue.rndSeed);
|
2026-03-15 22:37:08 +01:00
|
|
|
// float kt = ka + ks;
|
|
|
|
|
// float s = -log(rnd.z) / kt;
|
|
|
|
|
// if (s < RayTCurrent()) {
|
2025-01-29 21:55:11 +01:00
|
|
|
// float3 xs = WorldRayOrigin() + s * WorldRayDirection();
|
2026-03-15 22:37:08 +01:00
|
|
|
// float p = kt * rnd.z;
|
|
|
|
|
// if (hitValue.depth > 5) {
|
|
|
|
|
// if (rnd.z >= p) return;
|
|
|
|
|
// else brdf.baseColor /= p;
|
|
|
|
|
// }
|
|
|
|
|
// float3 ldirect = nextEventEstimation(brdf.baseColor, WorldRayDirection(), xs, -WorldRayDirection(), kt, true, rnd);
|
|
|
|
|
// hitValue.light += (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
|
|
|
|
|
// );
|
|
|
|
|
// TraceRay(scene, 0, 0xff, 0, 0, rayDesc);
|
|
|
|
|
// }
|
2024-12-31 10:40:03 +01:00
|
|
|
|
2025-01-30 23:25:41 +01:00
|
|
|
float p = max(max(brdf.getBaseColor().x, brdf.getBaseColor().y), brdf.getBaseColor().z);
|
2026-03-15 22:37:08 +01:00
|
|
|
if (hitValue.depth > 5) {
|
|
|
|
|
if (rnd.z >= p)
|
|
|
|
|
return;
|
2025-01-29 21:55:11 +01:00
|
|
|
}
|
2024-12-31 10:40:03 +01:00
|
|
|
|
2025-04-06 09:57:47 +02:00
|
|
|
hitValue.light += brdf.getEmissive() * hitValue.emissive + brdf.evaluateAmbient(lightingParams.viewDir_WS);
|
2024-12-31 10:40:03 +01:00
|
|
|
//-- Ideal DIFFUSE reflection
|
2026-03-15 22:37:08 +01:00
|
|
|
// if(bool(useNEE)) {
|
2024-12-31 10:40:03 +01:00
|
|
|
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
|
|
|
|
|
//}
|
2026-03-15 22:37:08 +01:00
|
|
|
// Only trace shadow rays for direct lighting at shallow depths
|
|
|
|
|
if (hitValue.depth <= 3) {
|
|
|
|
|
for (uint i = 0; i < pLightEnv.numDirectionalLights; ++i) {
|
|
|
|
|
float3 x = intersection_WS;
|
|
|
|
|
float3 l = -pLightEnv.directionalLights[i].direction.xyz;
|
|
|
|
|
RayDesc rayDesc;
|
|
|
|
|
rayDesc.TMax = 10000.0f;
|
|
|
|
|
rayDesc.TMin = eps;
|
|
|
|
|
rayDesc.Origin = x;
|
|
|
|
|
rayDesc.Direction = l;
|
|
|
|
|
RayPayload payload;
|
|
|
|
|
payload.depth = hitValue.depth;
|
|
|
|
|
payload.emissive = 1;
|
|
|
|
|
payload.anyHit = true;
|
|
|
|
|
payload.hit = false;
|
|
|
|
|
payload.rndSeed = hitValue.rndSeed;
|
|
|
|
|
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xff, 0, 0, 0,
|
|
|
|
|
rayDesc, payload);
|
2024-12-31 10:40:03 +01:00
|
|
|
|
2026-03-15 22:37:08 +01:00
|
|
|
// we have missed all geometry, so directional light is affecting us
|
|
|
|
|
if (!payload.hit) {
|
|
|
|
|
hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
|
|
|
|
}
|
2025-01-29 21:55:11 +01:00
|
|
|
}
|
2026-03-15 22:37:08 +01:00
|
|
|
for (uint i = 0; i < pLightEnv.numPointLights; ++i) {
|
|
|
|
|
RayPayload payload;
|
|
|
|
|
payload.rndSeed = hitValue.rndSeed;
|
|
|
|
|
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 = eps;
|
|
|
|
|
rayDesc.Origin = x;
|
|
|
|
|
rayDesc.Direction = l;
|
|
|
|
|
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xff, 0, 0, 0,
|
|
|
|
|
rayDesc, payload);
|
|
|
|
|
|
|
|
|
|
// hitting only after the light
|
|
|
|
|
if (!payload.hit) {
|
|
|
|
|
hitValue.light += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
|
|
|
|
|
}
|
2025-01-02 14:28:31 +01:00
|
|
|
}
|
2026-03-15 22:37:08 +01:00
|
|
|
} // end depth <= 2 direct lighting guard
|
|
|
|
|
// Indirect Illumination: probabilistic diffuse/specular importance sampling
|
|
|
|
|
if (hitValue.depth < 3) {
|
|
|
|
|
float3 V = -WorldRayDirection();
|
|
|
|
|
float3 N = normalLight_WS;
|
|
|
|
|
float rough = brdf.getRoughness();
|
|
|
|
|
float met = brdf.getMetallic();
|
|
|
|
|
float3 F0 = lerp(float3(0.04), brdf.getBaseColor(), met);
|
|
|
|
|
float NdotV = max(dot(N, V), 0.001);
|
|
|
|
|
float3 F = F0 + (1 - F0) * pow(1 - NdotV, 5.0);
|
|
|
|
|
float specProb = clamp((F.x + F.y + F.z) / 3.0, 0.1, 0.9);
|
|
|
|
|
|
2025-01-02 14:28:31 +01:00
|
|
|
RayDesc rayDesc;
|
|
|
|
|
rayDesc.TMax = 10000.0f;
|
2025-01-29 16:15:48 +01:00
|
|
|
rayDesc.TMin = eps;
|
2025-01-02 14:28:31 +01:00
|
|
|
rayDesc.Origin = intersection_WS;
|
2025-01-30 23:25:41 +01:00
|
|
|
|
2026-03-15 22:37:08 +01:00
|
|
|
float3 bounceWeight = float3(0);
|
|
|
|
|
bool validBounce = true;
|
|
|
|
|
|
|
|
|
|
if (rnd.z < specProb) {
|
|
|
|
|
// GGX importance sampling for specular reflection
|
|
|
|
|
float a = max(rough * rough, 0.001);
|
|
|
|
|
float a2 = a * a;
|
|
|
|
|
float phi = 2.0 * PI * rnd.x;
|
|
|
|
|
float cosTheta = sqrt((1.0 - rnd.y) / (1.0 + (a2 - 1.0) * rnd.y));
|
|
|
|
|
float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
|
|
|
|
|
|
|
|
|
|
float3 u = normalize(cross(abs(N.x) > 0.1 ? float3(0, 1, 0) : float3(1, 0, 0), N));
|
|
|
|
|
float3 v = cross(N, u);
|
|
|
|
|
float3 H = normalize(u * cos(phi) * sinTheta + v * sin(phi) * sinTheta + N * cosTheta);
|
|
|
|
|
float3 L = reflect(-V, H);
|
|
|
|
|
rayDesc.Direction = L;
|
|
|
|
|
|
|
|
|
|
float NdotL = dot(N, L);
|
|
|
|
|
float NdotH = max(dot(N, H), 0.0);
|
|
|
|
|
float VdotH = max(dot(V, H), 0.0);
|
|
|
|
|
|
|
|
|
|
if (NdotL <= 0) {
|
|
|
|
|
validBounce = false;
|
|
|
|
|
} else {
|
|
|
|
|
// Smith G term
|
|
|
|
|
float k = (rough + 1);
|
|
|
|
|
k = (k * k) / 8;
|
|
|
|
|
float G = (NdotV / (NdotV * (1 - k) + k)) * (NdotL / (NdotL * (1 - k) + k));
|
|
|
|
|
// Fresnel at half-vector
|
|
|
|
|
float3 Fh = F0 + (max(float3(1.0 - rough), F0) - F0) * pow(clamp(1 - VdotH, 0, 1), 5.0);
|
|
|
|
|
// Monte Carlo weight: F * G * VdotH / (NdotH * NdotV * specProb)
|
|
|
|
|
bounceWeight = Fh * G * VdotH / (NdotH * NdotV * specProb + 0.0001);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Cosine-weighted importance sampling for diffuse
|
|
|
|
|
float r1 = 2 * PI * rnd.x, r2 = rnd.y, r2s = sqrt(r2);
|
|
|
|
|
float3 w = N;
|
|
|
|
|
float3 u = normalize(cross(abs(w.x) > 0.1 ? float3(0, 1, 0) : float3(1, 0, 0), w));
|
|
|
|
|
float3 v = cross(w, u);
|
|
|
|
|
rayDesc.Direction = normalize(u * cos(r1) * r2s + v * sin(r1) * r2s + w * sqrt(1 - r2));
|
|
|
|
|
// Diffuse weight: kd * baseColor / (1 - specProb)
|
|
|
|
|
float3 kd = (1 - F) * (1 - met);
|
|
|
|
|
bounceWeight = kd * brdf.getBaseColor() / (1 - specProb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validBounce) {
|
|
|
|
|
RayPayload payload;
|
|
|
|
|
payload.light = float3(0);
|
|
|
|
|
payload.hit = false;
|
|
|
|
|
payload.emissive = 0;
|
|
|
|
|
payload.depth = hitValue.depth;
|
|
|
|
|
payload.anyHit = false;
|
|
|
|
|
payload.rndSeed = hitValue.rndSeed + 1;
|
|
|
|
|
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
|
|
|
|
|
hitValue.light += bounceWeight * payload.light;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// hitValue.light /= p;
|
|
|
|
|
}
|