shader adjustments

This commit is contained in:
2026-03-15 22:37:08 +01:00
parent cd0f6d038f
commit 9109baad18
10 changed files with 172 additions and 105 deletions
+1 -1
+8 -8
View File
@@ -65,12 +65,12 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
float shadowFactor = 0.0f; float shadowFactor = 0.0f;
int count = 0; int count = 0;
int range = 1; int range = 1;
//for (int x = -range; x <= range; ++x) { for (int x = -range; x <= range; ++x) {
// for (int y = -range; y <= range; ++y) { for (int y = -range; y <= range; ++y) {
float shadow = 1.0f; float shadow = 1.0f;
if (currentDepth > 0.0 && currentDepth < 1.0) if (currentDepth > 0.0 && currentDepth < 1.0)
{ {
float dist = pShadowMapping.shadowMaps[cascadeIndex].Sample(pShadowMapping.shadowSampler, float3(shadowCoord.xy, i)).r; float dist = pShadowMapping.shadowMaps[cascadeIndex].Sample(pShadowMapping.shadowSampler, float3(shadowCoord.xy + float2(dx * x, dy * y), i)).r;
if (shadowCoord.w > 0 && currentDepth < dist) if (shadowCoord.w > 0 && currentDepth < dist)
{ {
shadow = 0; shadow = 0;
@@ -78,16 +78,16 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
} }
shadowFactor += shadow; shadowFactor += shadow;
count++; count++;
// } }
//} }
result = (shadowFactor / count) * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); result = (shadowFactor / count) * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
} }
for (uint i = 0; i < pLightEnv.numPointLights; ++i) for (uint i = 0; i < pLightEnv.numPointLights; ++i)
{ {
//uint lightIndex = pLightCullingData.lightIndexList[startOffset + i]; uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
//result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf); result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf);
} }
//result += brdf.evaluateAmbient(lightingParams.viewDir_WS); result += brdf.evaluateAmbient(lightingParams.viewDir_WS);
return float4(result, brdf.getAlpha()); return float4(result, brdf.getAlpha());
} }
+1
View File
@@ -1,4 +1,5 @@
import Common; import Common;
import Frustum;
import DispatchParams; import DispatchParams;
struct ComputeShaderInput struct ComputeShaderInput
+1
View File
@@ -2,6 +2,7 @@ import Common;
import Scene; import Scene;
import Bounding; import Bounding;
import DepthCommon; import DepthCommon;
import Frustum;
groupshared MeshPayload p; groupshared MeshPayload p;
groupshared uint head; groupshared uint head;
+1
View File
@@ -1,6 +1,7 @@
import Common; import Common;
import DispatchParams; import DispatchParams;
import LightEnv; import LightEnv;
import Frustum;
struct ComputeShaderInput struct ComputeShaderInput
{ {
+1
View File
@@ -1,4 +1,5 @@
import Common; import Common;
import Frustum;
struct BoundingSphere struct BoundingSphere
{ {
+1
View File
@@ -1,4 +1,5 @@
import Common; import Common;
import Frustum;
struct DispatchParams struct DispatchParams
{ {
+11
View File
@@ -1,5 +1,6 @@
import Common; import Common;
import MaterialParameter; import MaterialParameter;
import Frustum;
interface ILightEnv interface ILightEnv
{ {
@@ -81,6 +82,8 @@ interface IBRDF
float3 evaluateAmbient(float3 viewDir_WS); float3 evaluateAmbient(float3 viewDir_WS);
float getAlpha(); float getAlpha();
float3 getEmissive(); float3 getEmissive();
float getRoughness();
float getMetallic();
[mutating] void setNormal(float3 n); [mutating] void setNormal(float3 n);
}; };
@@ -134,6 +137,8 @@ struct Phong : IBRDF
{ {
return emissive; return emissive;
} }
float getRoughness() { return pow(2.0 / (shininess + 2.0), 0.25); }
float getMetallic() { return 0; }
[mutating] void setNormal(float3 n) { normal = n; } [mutating] void setNormal(float3 n) { normal = n; }
}; };
@@ -187,6 +192,8 @@ struct BlinnPhong : IBRDF
{ {
return emissive; return emissive;
} }
float getRoughness() { return pow(2.0 / (shininess + 2.0), 0.25); }
float getMetallic() { return 0; }
[mutating] void setNormal(float3 n) { normal = n; } [mutating] void setNormal(float3 n) { normal = n; }
}; };
@@ -240,6 +247,8 @@ struct CelShading : IBRDF
{ {
return emissive; return emissive;
} }
float getRoughness() { return 1.0; }
float getMetallic() { return 0; }
[mutating] void setNormal(float3 n) { normal = n; } [mutating] void setNormal(float3 n) { normal = n; }
}; };
@@ -364,5 +373,7 @@ struct CookTorrance : IBRDF
{ {
return emissive; return emissive;
} }
float getRoughness() { return roughness; }
float getMetallic() { return metallic; }
[mutating] void setNormal(float3 n) { normal = n; } [mutating] void setNormal(float3 n) { normal = n; }
}; };
+141 -90
View File
@@ -26,13 +26,10 @@ import MATERIAL_FILE_NAME;
float cos_a = 1 - rnd.x + rnd.x*cos_a_max, sin_a = sqrt(1 - cos_a*cos_a); 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; 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 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 if (intersect(Ray(x,l), matls, paramsls, sphereId, triId) && sphereId == i) { // test if shadow ray hits this light
float omega = 2 * pi * (1-cos_a_max); 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) *
if(useAtt) { 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
float tau = exp(-kt * length(x - paramsls.x)); for brdf
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
} }
} }
} }
@@ -78,11 +75,10 @@ const static float3 fogEmm = float3(0, 0.01, 0.01);
const static float eps = 1e-5; const static float eps = 1e-5;
[shader("closesthit")] [shader("closesthit")]
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr) void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr) {
{
hitValue.hit = true; hitValue.hit = true;
// todo: replace with anyhit shader // todo: replace with anyhit shader
if(hitValue.anyHit) if (hitValue.anyHit)
return; 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);
@@ -117,106 +113,161 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
let brdf = Material.prepare(hitValue.materialParams); let brdf = Material.prepare(hitValue.materialParams);
float3 normal_WS = brdf.getNormal(); float3 normal_WS = brdf.getNormal();
float3 normalLight_WS = dot(normal_WS,WorldRayDirection())<0 ? normal_WS : -normal_WS; float3 normalLight_WS = dot(normal_WS, WorldRayDirection()) < 0 ? normal_WS : -normal_WS;
float3 intersection_WS = lightingParams.position_WS; float3 intersection_WS = lightingParams.position_WS;
hitValue.depth++; hitValue.depth++;
float3 rnd = rand01(hitValue.rndSeed); float3 rnd = rand01(hitValue.rndSeed);
//float kt = ka + ks; // float kt = ka + ks;
//float s = -log(rnd.z) / kt; // float s = -log(rnd.z) / kt;
//if (s < RayTCurrent()) { // if (s < RayTCurrent()) {
// float3 xs = WorldRayOrigin() + s * WorldRayDirection(); // float3 xs = WorldRayOrigin() + s * WorldRayDirection();
// float p = kt * rnd.z; // float p = kt * rnd.z;
// if (hitValue.depth > 5) { // if (hitValue.depth > 5) {
// if (rnd.z >= p) return; // if (rnd.z >= p) return;
// else brdf.baseColor /= p; // else brdf.baseColor /= p;
// } // }
// float3 ldirect = nextEventEstimation(brdf.baseColor, WorldRayDirection(), xs, -WorldRayDirection(), kt, true, rnd); // float3 ldirect = nextEventEstimation(brdf.baseColor, WorldRayDirection(), xs, -WorldRayDirection(), kt, true, rnd);
// hitValue.light += (fogEmm + ks * ldirect) / kt; // hitValue.light += (fogEmm + ks * ldirect) / kt;
// accmat *= ks / kt; // accmat *= ks / kt;
// rayDesc.Origin = xs; // rayDesc.Origin = xs;
// rayDesc.Direction = float3( // rayDesc.Direction = float3(
// cos(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y), // cos(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
// sin(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y), // sin(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
// rnd.y // rnd.y
// ); // );
// TraceRay(scene, 0, 0xff, 0, 0, rayDesc); // TraceRay(scene, 0, 0xff, 0, 0, rayDesc);
//} // }
float p = max(max(brdf.getBaseColor().x, brdf.getBaseColor().y), brdf.getBaseColor().z); float p = max(max(brdf.getBaseColor().x, brdf.getBaseColor().y), brdf.getBaseColor().z);
if(hitValue.depth > 5) { if (hitValue.depth > 5) {
if (rnd.z >= p) return; if (rnd.z >= p)
return;
} }
hitValue.light += brdf.getEmissive() * hitValue.emissive + brdf.evaluateAmbient(lightingParams.viewDir_WS); hitValue.light += brdf.getEmissive() * hitValue.emissive + brdf.evaluateAmbient(lightingParams.viewDir_WS);
//-- Ideal DIFFUSE reflection //-- Ideal DIFFUSE reflection
//if(bool(useNEE)) { // if(bool(useNEE)) {
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd); // accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
//} //}
for(uint i = 0; i < pLightEnv.numDirectionalLights; ++i) { // Only trace shadow rays for direct lighting at shallow depths
float3 x = intersection_WS; if (hitValue.depth <= 3) {
float3 l = -pLightEnv.directionalLights[i].direction.xyz; for (uint i = 0; i < pLightEnv.numDirectionalLights; ++i) {
RayDesc rayDesc; float3 x = intersection_WS;
rayDesc.TMax = 10000.0f; float3 l = -pLightEnv.directionalLights[i].direction.xyz;
rayDesc.TMin = eps; RayDesc rayDesc;
rayDesc.Origin = x; rayDesc.TMax = 10000.0f;
rayDesc.Direction = l; rayDesc.TMin = eps;
RayPayload payload; rayDesc.Origin = x;
payload.depth = hitValue.depth; rayDesc.Direction = l;
payload.emissive = 1; RayPayload payload;
payload.anyHit = true; payload.depth = hitValue.depth;
payload.hit = false; payload.emissive = 1;
payload.rndSeed = hitValue.rndSeed; payload.anyHit = true;
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xff, 0, 0, 0, rayDesc, payload); 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);
// we have missed all geometry, so directional light is affecting us // we have missed all geometry, so directional light is affecting us
if(!payload.hit) { if (!payload.hit) {
hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
}
} }
} for (uint i = 0; i < pLightEnv.numPointLights; ++i) {
for(uint i = 0; i < pLightEnv.numPointLights; ++i) { RayPayload payload;
RayPayload payload; payload.rndSeed = hitValue.rndSeed;
payload.rndSeed = hitValue.rndSeed; float3 x = intersection_WS;
float3 x = intersection_WS; float3 l = pLightEnv.pointLights[i].position_WS.xyz - intersection_WS;
float3 l = pLightEnv.pointLights[i].position_WS.xyz - intersection_WS; if (length(l) > pLightEnv.pointLights[i].colorRange.w) {
if(length(l) > pLightEnv.pointLights[i].colorRange.w) { continue;
continue; }
} RayDesc rayDesc;
RayDesc rayDesc; rayDesc.TMax = 1.0f;
rayDesc.TMax = 1.0f; rayDesc.TMin = eps;
rayDesc.TMin = eps; rayDesc.Origin = x;
rayDesc.Origin = x; rayDesc.Direction = l;
rayDesc.Direction = l; TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xff, 0, 0, 0,
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xff, 0, 0, 0, rayDesc, payload); rayDesc, payload);
// hitting only after the light // hitting only after the light
if(!payload.hit) { if (!payload.hit) {
hitValue.light += pLightEnv.pointLights[i].illuminate(lightingParams, brdf); hitValue.light += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
}
} }
} } // end depth <= 2 direct lighting guard
// Indirect Illumination: cosine-weighted importance sampling // Indirect Illumination: probabilistic diffuse/specular importance sampling
if(hitValue.depth < 1) { if (hitValue.depth < 3) {
float r1 = 2 * PI * rnd.x, r2 = rnd.y, r2s = sqrt(r2); float3 V = -WorldRayDirection();
float3 w = normalLight_WS; float3 N = normalLight_WS;
float3 u = normalize(cross(abs(w.x)>0.1 ? float3(0,1,0) : float3(1,0,0), w)); float rough = brdf.getRoughness();
float3 v = cross(w,u); 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);
RayDesc rayDesc; RayDesc rayDesc;
rayDesc.TMax = 10000.0f; rayDesc.TMax = 10000.0f;
rayDesc.TMin = eps; rayDesc.TMin = eps;
rayDesc.Origin = intersection_WS; 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 = 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, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
float bias = dot(normalLight_WS, rayDesc.Direction);
hitValue.light += brdf.evaluate(-WorldRayDirection(), rayDesc.Direction, payload.light / bias);
}
//hitValue.light /= p;
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;
} }
+1 -1
View File
@@ -819,7 +819,7 @@ void Graphics::pickPhysicalDevice() {
shaderFloatControls = true; shaderFloatControls = true;
} }
} }
rayTracingEnabled = false && rayTracingPipelineSupport && accelerationStructureSupport && hostOperationsSupport && deviceAddressSupport && rayTracingEnabled = rayTracingPipelineSupport && accelerationStructureSupport && hostOperationsSupport && deviceAddressSupport &&
descriptorIndexingSupport && spirv14Support && shaderFloatControls; descriptorIndexingSupport && spirv14Support && shaderFloatControls;
} }