lot more code

This commit is contained in:
Dynamitos
2025-01-28 00:08:52 +01:00
parent 8bcf38834e
commit 65c165f407
14 changed files with 705 additions and 27 deletions
+125
View File
@@ -0,0 +1,125 @@
import Common;
[shader("closesthit")]
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);
ModelReference m = pParams.modelData[InstanceID()];
// offset into the index buffer
uint indexOffset = m.indicesOffset;
// added to indices to reference correct part of global mesh pool
uint vertexOffset = m.positionOffset;
uint vertexIndex0 = vertexOffset + pParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 0];
uint vertexIndex1 = vertexOffset + pParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 1];
uint vertexIndex2 = vertexOffset + pParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 2];
Vertex attr0 = loadVertex(vertexIndex0);
Vertex attr1 = loadVertex(vertexIndex1);
Vertex attr2 = loadVertex(vertexIndex2);
Vertex vert = Vertex.interpolate(attr0, attr1, attr2, barycentricCoords);
float3 normalLight = dot(vert.normal, WorldRayDirection()) < 0 ? vert.normal : -vert.normal;
MaterialParameter mat; // TOOD:
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(mat.albedo.x, mat.albedo.y), mat.albedo.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 < pSamps.numDirectionalLights; ++i) {
float3 x = vert.position;
float3 l = -pParams.directionalLights[i].direction.xyz;
RayDesc rayDesc;
rayDesc.TMax = 10000.0f;
rayDesc.TMin = 0.001f;
rayDesc.Origin = x;
rayDesc.Direction = l;
RayPayload payload;
payload.depth = hitValue.depth;
payload.emissive = 1;
payload.anyHit = true;
TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
// we have missed all geometry, so directional light is affecting us
if(!payload.hit) {
localAccRad += mat.shade(vert.normal, -WorldRayDirection(), -pParams.directionalLights[i].direction, pParams.directionalLights[i].color);
}
}
for(uint i = 0; i < pSamps.numPointLights; ++i) {
RayPayload payload;
float3 x = vert.position;
float3 l = pParams.pointLights[i].position - vert.position;
// todo: cancel if light too far away to affect
RayDesc rayDesc;
rayDesc.TMax = 1.0f;
rayDesc.TMin = 0.001f;
rayDesc.Origin = x;
rayDesc.Direction = l;
TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
// hitting only after the light
if(!payload.hit) {
localAccRad += mat.shade(vert.normal, -WorldRayDirection(), normalize(l), pParams.pointLights[i].color);
}
}
hitValue.light += localAccRad;
// 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;
float3 u = normalize((cross(abs(w.x)>0.1 ? float3(0,1,0) : float3(1,0,0), w)));
float3 v = cross(w,u);
RayDesc rayDesc;
rayDesc.TMax = 10000.0f;
rayDesc.TMin = 0.001f;
rayDesc.Origin = vert.position;
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(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
}
}
+121
View File
@@ -0,0 +1,121 @@
const static float PI = 3.1415926535897932f;
struct Camera
{
float3 cameraPosition;
float f;
float3 cameraForward;
float S_O;
float3 fogEmm;
float ks;
float A;
float ka;
};
struct MaterialParameter
{
float3 albedo = float3(1, 1, 1);
float alpha = 1;
float3 specularColor = float3(1, 1, 1);
float shininess = 0.04;
float3 emissive = float3(0, 0, 0);
float3 shade(float3 normal, float3 viewDir, float3 lightDir, float3 lightColor)
{
float diffuse = max(dot(normal, lightDir), 0);
float3 h = normalize(lightDir + viewDir);
float specular = pow(clamp(dot(normal, h), 0, 1), shininess);
return (albedo * diffuse * lightColor);
}
};
struct ModelReference
{
uint32_t positionOffset = 0;
uint32_t indicesOffset = 0;
uint32_t numIndices = 0;
};
struct PointLight
{
float3 position = float3(0, 0, 0);
float3 color = float3(1, 1, 1);
float attenuation = 1;
};
struct DirectionalLight
{
float3 direction = float3(0, 1, 0);
float3 color = float3(1, 1, 1);
};
struct RaytracingParams
{
Camera cam;
RaytracingAccelerationStructure scene;
RWTexture2D<float4> radianceAccumulator;
RWTexture2D<float4> image;
StructuredBuffer<ModelReference> modelData;
StructuredBuffer<MaterialParameter> materialData;
StructuredBuffer<float> positions;
StructuredBuffer<float> texCoords;
StructuredBuffer<float> normals;
StructuredBuffer<DirectionalLight> directionalLights;
StructuredBuffer<PointLight> pointLights;
StructuredBuffer<uint32_t> indexBuffer;
};
ParameterBlock<RaytracingParams> pParams;
struct Vertex
{
float3 position;
float2 texCoords;
float3 normal;
static Vertex interpolate(Vertex f0, Vertex f1, Vertex f2, float3 barycentricCoords)
{
Vertex vert;
vert.position = f0.position * barycentricCoords.x + f1.position * barycentricCoords.y + f2.position * barycentricCoords.z;
vert.texCoords = f0.texCoords * barycentricCoords.x + f1.texCoords * barycentricCoords.y + f2.texCoords * barycentricCoords.z;
vert.normal = f0.normal * barycentricCoords.x + f1.normal * barycentricCoords.y + f2.normal * barycentricCoords.z;
return vert;
}
};
Vertex loadVertex(uint32_t vertexIndex)
{
Vertex vert;
vert.position = float3(pParams.positions[vertexIndex * 3 + 0], pParams.positions[vertexIndex * 3 + 1], pParams.positions[vertexIndex * 3 + 2]);
vert.texCoords = float2(pParams.texCoords[vertexIndex * 2 + 0], pParams.texCoords[vertexIndex * 2 + 1]);
vert.normal = float3(pParams.normals[vertexIndex * 3 + 0], pParams.normals[vertexIndex * 3 + 1], pParams.normals[vertexIndex * 3 + 2]);
return vert;
}
struct SampleParams
{
uint pass;
uint samplesPerPixel;
uint numDirectionalLights;
uint numPointLights;
};
layout(push_constant)
ConstantBuffer<SampleParams> pSamps;
struct Ray
{
float3 o;
float3 d;
};
struct RayPayload
{
float3 light;
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));
}
+57
View File
@@ -0,0 +1,57 @@
import Common;
[shader("raygeneration")]
void raygen()
{
if(pSamps.pass == pSamps.samplesPerPixel) return;
uint2 pix = DispatchRaysIndex().xy;
uint2 imgdim = DispatchRaysDimensions().xy;
//-- define cam
Ray cam = Ray(pParams.cam.cameraPosition, pParams.cam.cameraForward);
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 = (pParams.cam.S_O * pParams.cam.f) / (pParams.cam.S_O - pParams.cam.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
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 * pParams.cam.A * lensX + rnd01.y * pParams.cam.A * lensY;
float3 focalPoint = cam.o + (pParams.cam.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 = lensSample;
rayDesc.Direction = normalize(focus - lensSample);
rayDesc.TMin = 0.001;
rayDesc.TMax = 10000.0;
const uint maxDepth = 12;
RayPayload payload;
// initialize accumulated radiance and bxdf
payload.light=float3(0);
payload.emissive = 1;
payload.depth = 1;
payload.anyHit = false;
TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
if(pSamps.pass == 0) pParams.radianceAccumulator[pix] = float4(0);
pParams.radianceAccumulator[pix] += float4(payload.light / pSamps.samplesPerPixel, 0);
pParams.image[pix] = float4(clamp(pParams.radianceAccumulator[pix].xyz, 0, 1), 1);
}