various UI and rt changes

This commit is contained in:
Dynamitos
2025-01-29 16:15:48 +01:00
parent 4e6eb02e74
commit e5ac354527
29 changed files with 314 additions and 223 deletions
+1 -1
View File
@@ -59,5 +59,5 @@ float4 fragmentMain(
if(style.textureIndex == -1) {
return float4(style.color, style.opacity);
}
return float4(pParams.textures[style.textureIndex].Sample(pParams.sampler, texCoords.xy).xyz, style.opacity);
return pParams.textures[style.textureIndex].Sample(pParams.sampler, texCoords.xy);
}
+10 -2
View File
@@ -16,6 +16,7 @@ struct Phong : IBRDF
float3 normal;
float3 ambient;
float shininess;
float3 emissive;
__init()
{
@@ -25,6 +26,7 @@ struct Phong : IBRDF
normal = float3(0, 0, 1);
ambient = float3(0, 0, 0);
shininess = 0;
emissive = float3(0, 0, 0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
@@ -59,6 +61,7 @@ struct BlinnPhong : IBRDF
float3 normal;
float shininess;
float3 ambient;
float3 emissive;
__init()
{
@@ -66,8 +69,9 @@ struct BlinnPhong : IBRDF
alpha = 1;
specularColor = float3(0, 0, 0);
normal = float3(0, 0, 1);
shininess = 0;
shininess = 4;
ambient = float3(0, 0, 0);
emissive = float3(0, 0, 0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
@@ -77,7 +81,7 @@ struct BlinnPhong : IBRDF
float3 h = normalize(lightDir_WS + viewDir_WS);
float specular = pow(saturate(dot(normal_WS, h)), shininess);
return (baseColor * diffuse * lightColor);// + (specularColor * specular);
return (baseColor * diffuse * lightColor) + (specularColor * specular);
}
float3 evaluateAmbient()
@@ -99,12 +103,14 @@ struct CelShading : IBRDF
float3 baseColor;
float alpha;
float3 normal;
float3 emissive;
__init()
{
baseColor = float3(0, 0, 0);
alpha = 1;
normal = float3(0, 0, 1);
emissive = float3(0, 0, 0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
@@ -146,6 +152,7 @@ struct CookTorrance : IBRDF
float roughness;
float metallic;
float ambientOcclusion;
float3 emissive;
__init()
{
@@ -155,6 +162,7 @@ struct CookTorrance : IBRDF
roughness = 0;
metallic = 0;
ambientOcclusion = 1;
emissive = float3(0, 0, 0);
}
float TrowbridgeReitzGGX(float3 normal, float3 halfway)
+8 -8
View File
@@ -106,15 +106,15 @@ struct FragmentParameter
FragmentParameter result;
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
#ifndef POS_ONLY
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * 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;
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;
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;
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;
}
+86 -24
View File
@@ -9,6 +9,69 @@ import MATERIAL_FILE_NAME;
// simplification: all BLAS only have 1 geometry
/*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];
if (all(equal(ls.e, vec3(0)))) continue; // skip non-emissive spheres
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
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
}
}
}
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;
}
*/
const float eps = 1e-5;
[shader("closesthit")]
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
{
@@ -46,12 +109,11 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
let brdf = Material.prepare(materialParams);
float3 normal_WS = normalize(mul(params.getTangentToWorld(), brdf.normal));
float3 normalLight_WS = dot(normal_WS,WorldRayOrigin())<0 ? normal_WS : -normal_WS;
float3 intersection_WS = params.position_WS + normal_WS * 0.001f;
float3 normalLight_WS = dot(normal_WS,WorldRayDirection())<0 ? normal_WS : -normal_WS;
float3 intersection_WS = params.position_WS;
hitValue.depth++;
float3 localAccRad = float3(0);
float3 rnd = rand01(uint3(vertexIndex0, vertexIndex1, vertexIndex2));
float3 rnd = rand01(hitValue.rndSeed);
//float kt = ka + ks;
//float s = -log(rnd.z) / kt;
//float3 xs = r.o + s * r.d;
@@ -73,13 +135,13 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
// continue;
//}
//float p = max(max(brdf.baseColor.x, brdf.baseColor.color.y), brdf.baseColor.color.z);
//float p = max(max(brdf.baseColor.x, brdf.baseColor.y), brdf.baseColor.z);
//if(hitValue.depth > 5) {
// if (rnd.z >= p) return;
// else hitValue.accmat /= p;
//}
hitValue.light += brdf.emissive * hitValue.emissive + brdf.evaluateAmbient();
//-- Ideal DIFFUSE reflection
//if(bool(useNEE)) {
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
@@ -89,22 +151,25 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
float3 l = -pLightEnv.directionalLights[i].direction.xyz;
RayDesc rayDesc;
rayDesc.TMax = 10000.0f;
rayDesc.TMin = 0.001f;
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, 0, 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);
}
//if(!payload.hit) {
//hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
//}
}
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) {
@@ -112,40 +177,37 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
}
RayDesc rayDesc;
rayDesc.TMax = 1.0f;
rayDesc.TMin = 0.001f;
rayDesc.TMin = eps;
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);
hitValue.light += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
}
}
// 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 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.TMin = eps;
rayDesc.Origin = intersection_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.emissive = 1;
payload.hit = false;
//payload.emissive = 0; // in the next bounce, consider reflective part only!
payload.depth = hitValue.depth;
payload.anyHit = false;
payload.rndSeed = hitValue.rndSeed + 1;
TraceRay(pRayTracingParams.scene, 0, 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);
}
float bias = dot(normalLight_WS, rayDesc.Direction);
hitValue.light += brdf.evaluate(params.getTangentToWorld(), -WorldRayDirection(), rayDesc.Direction, payload.light / bias);
}
hitValue.light += localAccRad;
}
+5 -2
View File
@@ -108,10 +108,13 @@ void raygen()
payload.light=float3(0);
payload.emissive = 1;
payload.depth = 1;
payload.rndSeed = rndSeed + 1;
payload.anyHit = false;
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
if(pSamps.pass == 0) pRayTracingParams.radianceAccumulator[pix] = float4(0);
pRayTracingParams.radianceAccumulator[pix] += float4(payload.light / pSamps.samplesPerPixel, 0);
pRayTracingParams.image[pix] = float4(clamp(pRayTracingParams.radianceAccumulator[pix].xyz, 0, 1), 1);
float3 accumulatedRadiance = payload.light / pSamps.samplesPerPixel;
pRayTracingParams.radianceAccumulator[pix] += float4(accumulatedRadiance, 0);
float3 compensatedRadiance = pRayTracingParams.radianceAccumulator[pix].xyz * pSamps.samplesPerPixel / (pSamps.pass + 1);
pRayTracingParams.image[pix] = float4(clamp(compensatedRadiance, 0, 1), 1);
}
@@ -25,6 +25,7 @@ struct RayPayload
uint depth;
bool hit;
bool anyHit;
uint3 rndSeed;
};
float3 rand01(uint3 x){ // pseudo-random number generator