refactor ray tracing
This commit is contained in:
@@ -12,6 +12,10 @@ import MATERIAL_FILE_NAME;
|
|||||||
[shader("closesthit")]
|
[shader("closesthit")]
|
||||||
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
|
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
|
||||||
{
|
{
|
||||||
|
hitValue.hit = true;
|
||||||
|
// todo: replace with anyhit shader
|
||||||
|
if(hitValue.anyHit)
|
||||||
|
return;
|
||||||
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
|
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
|
||||||
|
|
||||||
InstanceData inst = pScene.instances[InstanceID()];
|
InstanceData inst = pScene.instances[InstanceID()];
|
||||||
@@ -37,12 +41,103 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
|
|||||||
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
||||||
|
|
||||||
MaterialParameter materialParams = params.getMaterialParameter();
|
MaterialParameter materialParams = params.getMaterialParameter();
|
||||||
|
LightingParameter lightingParams = params.getLightingParameter();
|
||||||
|
lightingParams.viewDir_WS = -WorldRayDirection();
|
||||||
let brdf = Material.prepare(materialParams);
|
let brdf = Material.prepare(materialParams);
|
||||||
|
|
||||||
|
|
||||||
hitValue.material.color = float4(brdf.baseColor, 1);
|
float3 normal_WS = mul(params.getTangentToWorld(), brdf.normal);
|
||||||
hitValue.material.emissive = float3(0, 0, 0);
|
float3 normalLight_WS = dot(normal_WS,WorldRayOrigin())<0 ? normal_WS : -normal_WS;
|
||||||
hitValue.shading.position = WorldRayOrigin() + RayTCurrent() * WorldRayDirection();
|
|
||||||
hitValue.shading.normal = mul(params.getTangentToWorld(), brdf.normal);
|
hitValue.depth++;
|
||||||
hitValue.shading.normalLight = dot(hitValue.shading.normal, WorldRayDirection()) < 0 ? hitValue.shading.normal : -hitValue.shading.normal;
|
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;
|
||||||
|
//}
|
||||||
|
|
||||||
|
RayDesc rayDesc;
|
||||||
|
rayDesc.TMax = 10000.0f;
|
||||||
|
rayDesc.TMin = 0.001f;
|
||||||
|
|
||||||
|
//-- 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) {
|
||||||
|
float3 x = params.position_WS;
|
||||||
|
float3 l = -pLightEnv.directionalLights[i].direction.xyz;
|
||||||
|
rayDesc.Origin = x;
|
||||||
|
rayDesc.Direction = l;
|
||||||
|
RayPayload payload;
|
||||||
|
payload.depth = hitValue.depth;
|
||||||
|
payload.emissive = 1;
|
||||||
|
payload.anyHit = true;
|
||||||
|
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
||||||
|
|
||||||
|
// we have missed all geometry, so directional light is affecting us
|
||||||
|
if(!payload.hit) {
|
||||||
|
localAccRad += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//for(uint i = 0; i < pLightEnv.numPointLights; ++i) {
|
||||||
|
// float3 x = payload.shading.position;
|
||||||
|
// float3 l = pLightEnv.pointLights[i].position_WS.xyz - payload.shading.position;
|
||||||
|
// float3 nl = -payload.shading.normal;
|
||||||
|
// rayDesc.Origin = x;
|
||||||
|
// rayDesc.Direction = l;
|
||||||
|
// TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
||||||
|
//
|
||||||
|
// // hitting only after the light
|
||||||
|
// if(length(payload.shading.position - x) > length(pLightEnv.pointLights[i].position_WS.xyz - x))
|
||||||
|
// {
|
||||||
|
// float omega = 2 * PI;
|
||||||
|
// accrad += accmat / PI * max(dot(l,nl),0) * pLightEnv.pointLights[i].colorRange.xyz * omega;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
// 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);
|
||||||
|
rayDesc.Origin = params.position_WS;
|
||||||
|
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;
|
||||||
|
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,10 @@ import RayTracingData;
|
|||||||
[shader("miss")]
|
[shader("miss")]
|
||||||
void miss(inout RayPayload p)
|
void miss(inout RayPayload p)
|
||||||
{
|
{
|
||||||
p.shading.position = WorldRayDirection() * 1000.0f;
|
// p.shading.position = WorldRayDirection() * 1000.0f;
|
||||||
p.shading.normal = -WorldRayDirection();
|
// p.shading.normal = -WorldRayDirection();
|
||||||
p.shading.normalLight = -WorldRayDirection();
|
// p.shading.normalLight = -WorldRayDirection();
|
||||||
p.material.color = float4(0, 0, 0, 0);
|
// p.material.color = float4(0, 0, 0, 0);
|
||||||
p.material.emissive = float3(pRayTracingParams.skyBox.Sample(pRayTracingParams.skyBoxSampler, WorldRayDirection()).xyz);
|
p.light = float3(pRayTracingParams.skyBox.Sample(pRayTracingParams.skyBoxSampler, WorldRayDirection()).xyz);
|
||||||
|
p.hit = false;
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@ struct Ray
|
|||||||
|
|
||||||
const static float S_O = 6.9;
|
const static float S_O = 6.9;
|
||||||
const static float f = 0.035;
|
const static float f = 0.035;
|
||||||
const static float A = 0.04;
|
const static float A = 0.0;
|
||||||
const static float ka = 0;
|
const static float ka = 0;
|
||||||
const static float ks = 0;
|
const static float ks = 0;
|
||||||
const static float3 fogEmm = float3(0, 0.01, 0.01);
|
const static float3 fogEmm = float3(0, 0.01, 0.01);
|
||||||
@@ -23,10 +23,6 @@ struct SampleParams
|
|||||||
layout(push_constant)
|
layout(push_constant)
|
||||||
ConstantBuffer<SampleParams> pSamps;
|
ConstantBuffer<SampleParams> pSamps;
|
||||||
|
|
||||||
float3 rand01(uint3 x){ // pseudo-random number generator
|
|
||||||
for (int i=3; i-->0;) x = ((x>>8U)^x.yzx)*1103515245U;
|
|
||||||
return float3(x)*(1.0/float(0xffffffffU));
|
|
||||||
}
|
|
||||||
|
|
||||||
float3 nextEventEstimation(float3 accmat, float3 w, float3 x, float3 nl, float kt, bool useAtt, float3 rnd) {
|
float3 nextEventEstimation(float3 accmat, float3 w, float3 x, float3 nl, float kt, bool useAtt, float3 rnd) {
|
||||||
float3 result = float3(0);
|
float3 result = float3(0);
|
||||||
@@ -67,6 +63,7 @@ float3 nextEventEstimation(float3 accmat, float3 w, float3 x, float3 nl, float k
|
|||||||
[shader("raygeneration")]
|
[shader("raygeneration")]
|
||||||
void raygen()
|
void raygen()
|
||||||
{
|
{
|
||||||
|
if(pSamps.pass == pSamps.samplesPerPixel) return;
|
||||||
uint2 pix = DispatchRaysIndex().xy;
|
uint2 pix = DispatchRaysIndex().xy;
|
||||||
uint2 imgdim = DispatchRaysDimensions().xy;
|
uint2 imgdim = DispatchRaysDimensions().xy;
|
||||||
|
|
||||||
@@ -82,7 +79,6 @@ void raygen()
|
|||||||
float2 tent = float2(rnd2.x<1 ? sqrt(rnd2.x)-1 : 1-sqrt(2-rnd2.x), rnd2.y<1 ? sqrt(rnd2.y)-1 : 1-sqrt(2-rnd2.y));
|
float2 tent = float2(rnd2.x<1 ? sqrt(rnd2.x)-1 : 1-sqrt(2-rnd2.x), rnd2.y<1 ? sqrt(rnd2.y)-1 : 1-sqrt(2-rnd2.y));
|
||||||
float2 s = ((pix + 0.5 * (0.5 + float2((pSamps.pass/2)%2, pSamps.pass%2) + tent)) / float2(imgdim) - 0.5) * sdim;
|
float2 s = ((pix + 0.5 * (0.5 + float2((pSamps.pass/2)%2, pSamps.pass%2) + tent)) / float2(imgdim) - 0.5) * sdim;
|
||||||
float3 spos = cam.o + cx*s.x + cy*s.y, lc = cam.o + cam.d * 0.035; // sample on 3d sensor plane
|
float3 spos = cam.o + cx*s.x + cy*s.y, lc = cam.o + cam.d * 0.035; // sample on 3d sensor plane
|
||||||
float3 accrad=float3(0), accmat=float3(1); // initialize accumulated radiance and bxdf
|
|
||||||
Ray r = Ray(lc, normalize(lc - spos)); // construct ray
|
Ray r = Ray(lc, normalize(lc - spos)); // construct ray
|
||||||
|
|
||||||
|
|
||||||
@@ -107,107 +103,15 @@ void raygen()
|
|||||||
rayDesc.TMax = 10000.0;
|
rayDesc.TMax = 10000.0;
|
||||||
|
|
||||||
const uint maxDepth = 12;
|
const uint maxDepth = 12;
|
||||||
float emissive = 1;
|
|
||||||
RayPayload payload;
|
RayPayload payload;
|
||||||
for(uint depth = 0; depth < maxDepth; ++depth) {
|
// initialize accumulated radiance and bxdf
|
||||||
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
payload.light=float3(0);
|
||||||
float3 rnd = rand01(uint3(pix, pSamps.pass*maxDepth + depth));
|
payload.emissive = 1;
|
||||||
//float kt = ka + ks;
|
payload.depth = 1;
|
||||||
//float s = -log(rnd.z) / kt;
|
payload.anyHit = false;
|
||||||
//float3 xs = r.o + s * r.d;
|
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
||||||
//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(payload.material.color.x, payload.material.color.y), payload.material.color.z);
|
|
||||||
if(depth > 5) {
|
|
||||||
if (rnd.z >= p) break;
|
|
||||||
else accmat /= p;
|
|
||||||
}
|
|
||||||
accrad += accmat * payload.material.emissive * emissive;
|
|
||||||
accmat *= payload.material.color.xyz;
|
|
||||||
if (payload.material.color.w == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
//-- Ideal DIFFUSE reflection
|
|
||||||
else if (payload.material.color.w == 1) {
|
|
||||||
//if(bool(useNEE)) {
|
|
||||||
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
|
|
||||||
//}
|
|
||||||
for(uint i = 0; i < pLightEnv.numDirectionalLights; ++i) {
|
|
||||||
float3 x = payload.shading.position;
|
|
||||||
float3 l = -pLightEnv.directionalLights[i].direction.xyz;
|
|
||||||
float3 nl = l;
|
|
||||||
rayDesc.Origin = x;
|
|
||||||
rayDesc.Direction = l;
|
|
||||||
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
|
||||||
|
|
||||||
// we have missed all geometry, so directional light is affecting us
|
|
||||||
if(payload.material.color.w == 0) {
|
|
||||||
float omega = 2 * PI;
|
|
||||||
accrad += accmat / PI * max(dot(l, nl), 0) * pLightEnv.directionalLights[i].color.xyz * omega;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//for(uint i = 0; i < pLightEnv.numPointLights; ++i) {
|
|
||||||
// float3 x = payload.shading.position;
|
|
||||||
// float3 l = pLightEnv.pointLights[i].position_WS.xyz - payload.shading.position;
|
|
||||||
// float3 nl = -payload.shading.normal;
|
|
||||||
// rayDesc.Origin = x;
|
|
||||||
// rayDesc.Direction = l;
|
|
||||||
// TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
|
||||||
//
|
|
||||||
// // hitting only after the light
|
|
||||||
// if(length(payload.shading.position - x) > length(pLightEnv.pointLights[i].position_WS.xyz - x))
|
|
||||||
// {
|
|
||||||
// float omega = 2 * PI;
|
|
||||||
// accrad += accmat / PI * max(dot(l,nl),0) * pLightEnv.pointLights[i].colorRange.xyz * omega;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
// Indirect Illumination: cosine-weighted importance sampling
|
|
||||||
float r1 = 2 * PI * rnd.x, r2 = rnd.y, r2s = sqrt(r2);
|
|
||||||
float3 w = payload.shading.normalLight, u = normalize((cross(abs(w.x)>0.1 ? float3(0,1,0) : float3(1,0,0), w))), v = cross(w,u);
|
|
||||||
rayDesc.Origin = payload.shading.position;
|
|
||||||
rayDesc.Direction = normalize(u*cos(r1)*r2s + v * sin(r1)*r2s + w * sqrt(1 - r2));
|
|
||||||
emissive = 0; // in the next bounce, consider reflective part only!
|
|
||||||
}
|
|
||||||
//-- Ideal SPECULAR reflection
|
|
||||||
else if (payload.material.color.w == 2) {
|
|
||||||
rayDesc.Origin = payload.shading.position;
|
|
||||||
rayDesc.Direction = reflect(r.d,payload.shading.normal);
|
|
||||||
emissive = 1;
|
|
||||||
}
|
|
||||||
//-- Ideal dielectric REFRACTION
|
|
||||||
else if (payload.material.color.w == 3) {
|
|
||||||
bool into = all(payload.shading.normal==payload.shading.normalLight);
|
|
||||||
float cos2t, nc=1, nt=1.5, nnt = into ? nc/nt : nt/nc, ddn = dot(r.d,payload.shading.normalLight);
|
|
||||||
if ((cos2t = 1 - nnt * nnt*(1 - ddn * ddn)) >= 0) { // Fresnel reflection/refraction
|
|
||||||
float3 tdir = normalize(r.d*nnt - payload.shading.normal * ((into ? 1 : -1)*(ddn*nnt + sqrt(cos2t))));
|
|
||||||
float a = nt - nc, b = nt + nc, R0 = a*a/(b*b), c = 1 - (into ? -ddn : dot(tdir,payload.shading.normal));
|
|
||||||
float Re = R0 + (1 - R0)*c*c*c*c*c, Tr = 1 - Re, P = 0.25 + 0.5*Re, RP = Re/P, TP = Tr/(1-P);
|
|
||||||
rayDesc.Origin = payload.shading.position;
|
|
||||||
rayDesc.Direction = rnd.x < P ? reflect(r.d,payload.shading.normal) : tdir; // pick reflection with probability P
|
|
||||||
accmat *= rnd.x < P ? RP : TP; // energy compensation
|
|
||||||
} else {
|
|
||||||
rayDesc.Origin = payload.shading.position;
|
|
||||||
rayDesc.Direction = reflect(r.d,payload.shading.normal); // Total internal reflection
|
|
||||||
}
|
|
||||||
emissive = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(pSamps.pass == 0) pRayTracingParams.radianceAccumulator[pix] = float4(0);
|
if(pSamps.pass == 0) pRayTracingParams.radianceAccumulator[pix] = float4(0);
|
||||||
pRayTracingParams.radianceAccumulator[pix] += float4(accrad / pSamps.samplesPerPixel, 0);
|
pRayTracingParams.radianceAccumulator[pix] += float4(payload.light / pSamps.samplesPerPixel, 0);
|
||||||
pRayTracingParams.image[pix] = float4(clamp(pRayTracingParams.radianceAccumulator[pix].xyz, 0, 1), 1);
|
pRayTracingParams.image[pix] = float4(clamp(pRayTracingParams.radianceAccumulator[pix].xyz, 0, 1), 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,21 +18,16 @@ struct CallablePayload
|
|||||||
float3 color;
|
float3 color;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ShadingParams
|
|
||||||
{
|
|
||||||
float3 position;
|
|
||||||
float3 normal;
|
|
||||||
float3 normalLight;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MaterialParams
|
|
||||||
{
|
|
||||||
float4 color;
|
|
||||||
float3 emissive;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct RayPayload
|
struct RayPayload
|
||||||
{
|
{
|
||||||
ShadingParams shading;
|
float3 light;
|
||||||
MaterialParams material;
|
float emissive;
|
||||||
};
|
uint depth;
|
||||||
|
bool hit;
|
||||||
|
bool anyHit;
|
||||||
|
};
|
||||||
|
|
||||||
|
float3 rand01(uint3 x){ // pseudo-random number generator
|
||||||
|
for (int i=3; i-->0;) x = ((x>>8U)^x.yzx)*1103515245U;
|
||||||
|
return float3(x)*(1.0/float(0xffffffffU));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user