Ray Tracing doesnt look like shit anymore
This commit is contained in:
@@ -31,7 +31,7 @@ struct FragmentParameter
|
||||
float4 qTangent : QTANGENT;
|
||||
float3 position_WS : POSITIONWS;
|
||||
float3 position_TS : POSITIONTS;
|
||||
float3 viewDir_TS : VIEWDIRTS;
|
||||
float3 cameraPos_TS : CAMPOSTS;
|
||||
float3 vertexColor : COLOR;
|
||||
float4 texCoords0 : TEXCOORDS0;
|
||||
float4 texCoords1 : TEXCOORDS1;
|
||||
@@ -58,10 +58,15 @@ struct FragmentParameter
|
||||
float3x3 worldToTangent = transpose(constructTBNFromQTangent(qTangent));
|
||||
LightingParameter result;
|
||||
result.worldToTangent = worldToTangent;
|
||||
result.viewDir_TS = viewDir_TS;
|
||||
result.viewDir_TS = cameraPos_TS - position_TS;
|
||||
result.position_TS = position_TS;
|
||||
return result;
|
||||
}
|
||||
|
||||
float3x3 getTangentToWorld()
|
||||
{
|
||||
return constructTBNFromQTangent(qTangent);
|
||||
}
|
||||
#endif
|
||||
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
|
||||
{
|
||||
@@ -71,10 +76,11 @@ struct FragmentParameter
|
||||
result.qTangent = f0.qTangent * barycentricCoords.x + f1.qTangent * barycentricCoords.y + f2.qTangent * barycentricCoords.z;
|
||||
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
|
||||
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
|
||||
//for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
//{
|
||||
// result.texCoords[i] = f0.texCoords[i] * barycentricCoords.x + f1.texCoords[i] * barycentricCoords.y + f2.texCoords[i] * barycentricCoords.z;
|
||||
//}
|
||||
result.texCoords0 = f0.texCoords0 * barycentricCoords.x + f1.texCoords0 * barycentricCoords.y + f2.texCoords0 * barycentricCoords.z;
|
||||
result.texCoords1 = f0.texCoords1 * barycentricCoords.x + f1.texCoords1 * barycentricCoords.y + f2.texCoords1 * barycentricCoords.z;
|
||||
result.texCoords2 = f0.texCoords2 * barycentricCoords.x + f1.texCoords2 * barycentricCoords.y + f2.texCoords2 * barycentricCoords.z;
|
||||
result.texCoords3 = f0.texCoords3 * barycentricCoords.x + f1.texCoords3 * barycentricCoords.y + f2.texCoords3 * barycentricCoords.z;
|
||||
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
@@ -99,11 +105,11 @@ struct VertexAttributes
|
||||
result.position_CS = clipPos;
|
||||
#ifndef POS_ONLY
|
||||
result.qTangent = qTangent;
|
||||
float3x3 tbn = transpose(constructTBNFromQTangent(qTangent));
|
||||
float3x3 tbn = constructTBNFromQTangent(qTangent);
|
||||
result.position_TS = mul(tbn, worldPos.xyz);
|
||||
result.vertexColor = vertexColor;
|
||||
result.position_WS = worldPos.xyz;
|
||||
result.viewDir_TS = mul(tbn, pViewParams.cameraPosition_WS.xyz - worldPos.xyz);
|
||||
result.cameraPos_TS = mul(tbn, pViewParams.cameraPosition_WS.xyz);
|
||||
result.texCoords0 = float4(texCoords[0], texCoords[1]);
|
||||
result.texCoords1 = float4(texCoords[2], texCoords[3]);
|
||||
result.texCoords2 = float4(texCoords[4], texCoords[5]);
|
||||
|
||||
@@ -36,23 +36,13 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
|
||||
|
||||
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
||||
|
||||
LightingParameter lightingParams = params.getLightingParameter();
|
||||
MaterialParameter materialParams = params.getMaterialParameter();
|
||||
let brdf = Material.prepare(materialParams);
|
||||
let brdf = Material.prepare(materialParams);
|
||||
|
||||
|
||||
float3 result = float3(0, 0, 0);
|
||||
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
|
||||
{
|
||||
result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
for(uint i = 0; i < pLightEnv.numPointLights; ++i)
|
||||
{
|
||||
result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
result += brdf.evaluateAmbient();
|
||||
|
||||
hitValue.color = result;
|
||||
hitValue.alpha = brdf.getAlpha();
|
||||
hitValue.intersection_WS = WorldRayOrigin() + RayTCurrent() * WorldRayDirection();
|
||||
hitValue.normal_WS = mul(params.getTangentToWorld(), brdf.getTangentNormal());
|
||||
hitValue.material.color = float4(brdf.baseColor, 1);
|
||||
hitValue.material.emissive = float3(0, 0, 0);
|
||||
hitValue.shading.position = WorldRayOrigin() + RayTCurrent() * WorldRayDirection();
|
||||
hitValue.shading.normal = cross(f2.position_WS - f0.position_WS, f1.position_WS - f0.position_WS);
|
||||
hitValue.shading.normalLight = dot(hitValue.shading.normal, WorldRayDirection()) < 0 ? hitValue.shading.normal : -hitValue.shading.normal;
|
||||
}
|
||||
@@ -3,8 +3,9 @@ import RayTracingData;
|
||||
[shader("miss")]
|
||||
void miss(inout RayPayload p)
|
||||
{
|
||||
p.color = float3(0, 1, 0);
|
||||
p.alpha = 1;
|
||||
p.intersection_WS = float3(0, 0, 0);
|
||||
p.normal_WS = float3(0, 0, 0);
|
||||
p.shading.position = WorldRayDirection() * 1000.0f;
|
||||
p.shading.normal = -WorldRayDirection();
|
||||
p.shading.normalLight = -WorldRayDirection();
|
||||
p.material.color = float4(0, 0, 0, 0);
|
||||
p.material.emissive = float3(pRayTracingParams.skyBox.Sample(pRayTracingParams.skyBoxSampler, WorldRayDirection()).xyz);
|
||||
}
|
||||
@@ -1,36 +1,213 @@
|
||||
import Common;
|
||||
import LightEnv;
|
||||
import RayTracingData;
|
||||
|
||||
struct Ray
|
||||
{
|
||||
float3 o;
|
||||
float3 d;
|
||||
}
|
||||
|
||||
const static float S_O = 6.9;
|
||||
const static float f = 0.035;
|
||||
const static float A = 0.4;
|
||||
const static float ka = 0;
|
||||
const static float ks = 0;
|
||||
const static float3 fogEmm = float3(0, 0.01, 0.01);
|
||||
|
||||
struct SampleParams
|
||||
{
|
||||
uint pass;
|
||||
uint samplesPerPixel;
|
||||
};
|
||||
layout(push_constant)
|
||||
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 result = float3(0);
|
||||
// Direct Illumination: Next Event Estimation over any present lights
|
||||
/*for(int i = meshLights.length(); i-->0;) {
|
||||
MeshDescriptor mesh = meshes[meshLights[i]];
|
||||
for(int j = 0; j < mesh.numIndices; j+=3) {
|
||||
float3 A = float3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 0]]);
|
||||
float3 B = float3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 1]]);
|
||||
float3 C = float3(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);
|
||||
float3 P = A * b1 + B * b2 + C * b3;
|
||||
float3 omega = P - x;
|
||||
float3 l = normalize(omega);
|
||||
float v = 0.f;
|
||||
if(intersect(Ray(x,l), matls, paramsls, sphereId, triId) && triId == j) {
|
||||
v = 1.0f;
|
||||
}
|
||||
float3 e1 = C - A;
|
||||
float3 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;
|
||||
}
|
||||
[shader("raygeneration")]
|
||||
void raygen()
|
||||
{
|
||||
uint3 LaunchID = DispatchRaysIndex();
|
||||
uint3 LaunchSize = DispatchRaysDimensions();
|
||||
uint2 pix = DispatchRaysIndex().xy;
|
||||
uint2 imgdim = DispatchRaysDimensions().xy;
|
||||
|
||||
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
|
||||
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
|
||||
float2 d = float2(inUV.x, 1 - inUV.y) * 2.0 - 1.0;
|
||||
float4 target = mul(pViewParams.inverseProjection, float4(d.x, d.y, 1, 1));
|
||||
//-- define cam
|
||||
Ray cam = Ray(pViewParams.cameraPosition_WS.xyz, pViewParams.cameraForward_WS.xyz);
|
||||
float3 cx = -normalize(cross(cam.d, abs(cam.d.y) < 0.9 ? float3(0, 1, 0) : float3(0, 0, 1))), cy = cross(cam.d, cx);
|
||||
const float2 sdim = float2(0.036, 0.024);
|
||||
|
||||
float S_I = (S_O * f) / (S_O - f);
|
||||
|
||||
//-- sample sensor
|
||||
float2 rnd2 = 2*rand01(uint3(pix, pSamps.pass)).xy; // vvv tent filter sample
|
||||
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;
|
||||
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
|
||||
|
||||
|
||||
//-- setup lens
|
||||
float3 lensP = lc;
|
||||
float3 lensN = -cam.d;
|
||||
float3 lensX = cross(lensN, float3(0, 1, 0)); // the exact vector doesnt matter
|
||||
float3 lensY = cross(lensN, lensX);
|
||||
uint3 rndSeed = uint3(pix, pSamps.pass);
|
||||
float2 rnd01 = rand01(rndSeed).xy;
|
||||
|
||||
float3 lensSample = lensP + rnd01.x * A * lensX + rnd01.y * A * lensY;
|
||||
|
||||
float3 focalPoint = cam.o + (S_O + S_I) * cam.d;
|
||||
float t = dot(focalPoint - r.o, lensN) / dot(r.d, lensN);
|
||||
float3 focus = r.o + t * r.d;
|
||||
|
||||
RayDesc rayDesc;
|
||||
rayDesc.Origin = mul(pViewParams.inverseViewMatrix, float4(0, 0, 0, 1)).xyz;
|
||||
rayDesc.Direction = mul(pViewParams.inverseViewMatrix, float4(normalize(target.xyz), 0)).xyz;
|
||||
rayDesc.Origin = lensSample;
|
||||
rayDesc.Direction = normalize(focus - lensSample);
|
||||
rayDesc.TMin = 0.001;
|
||||
rayDesc.TMax = 10000.0;
|
||||
|
||||
const uint maxRays = 12;
|
||||
float3 color = float3(0, 0, 0);
|
||||
float alpha;
|
||||
for(uint i = 0; i < maxRays; ++i) {
|
||||
RayPayload payload;
|
||||
const uint maxDepth = 12;
|
||||
float emissive = 1;
|
||||
RayPayload payload;
|
||||
for(uint depth = 0; depth < maxDepth; ++depth) {
|
||||
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
|
||||
color = color.rgb * alpha + payload.color * payload.alpha;
|
||||
alpha = alpha + payload.alpha;
|
||||
rayDesc.Origin = payload.intersection_WS;
|
||||
if(alpha > 0.9){
|
||||
float3 rnd = rand01(uint3(pix, pSamps.pass*maxDepth + depth));
|
||||
//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(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);
|
||||
|
||||
pRayTracingParams.image[int2(LaunchID.xy)] = float4(color, alpha);
|
||||
// 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);
|
||||
pRayTracingParams.radianceAccumulator[pix] += float4(accrad / pSamps.samplesPerPixel, 0);
|
||||
pRayTracingParams.image[pix] = float4(clamp(pRayTracingParams.radianceAccumulator[pix].xyz, 0, 1), 1);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@ import MaterialParameter;
|
||||
struct RayTracingParams
|
||||
{
|
||||
RaytracingAccelerationStructure scene;
|
||||
RWTexture2D<float4> radianceAccumulator;
|
||||
RWTexture2D<float4> image;
|
||||
StructuredBuffer<uint> indexBuffer;
|
||||
TextureCube<float4> skyBox;
|
||||
SamplerState skyBoxSampler;
|
||||
};
|
||||
layout(set=5)
|
||||
ParameterBlock<RayTracingParams> pRayTracingParams;
|
||||
@@ -15,10 +18,21 @@ struct CallablePayload
|
||||
float3 color;
|
||||
};
|
||||
|
||||
struct ShadingParams
|
||||
{
|
||||
float3 position;
|
||||
float3 normal;
|
||||
float3 normalLight;
|
||||
};
|
||||
|
||||
struct MaterialParams
|
||||
{
|
||||
float4 color;
|
||||
float3 emissive;
|
||||
}
|
||||
|
||||
struct RayPayload
|
||||
{
|
||||
float3 color;
|
||||
float alpha;
|
||||
float3 intersection_WS;
|
||||
float3 normal_WS;
|
||||
ShadingParams shading;
|
||||
MaterialParams material;
|
||||
};
|
||||
Reference in New Issue
Block a user