// Own shaders. The normal encoding matches the installed renderer's documented
// spheremap G-buffer; no game shader source is bundled or overwritten.
cbuffer Parameters : register(b0) {
row_major float4x4 inverseViewProjection;
row_major float4x4 inverseView;
float4 originDelta;
float4 sunDirection;
float4 settings; // AO radius, strength, shadow strength, diagnostic view
uint4 sizes; // full width/height, trace width/height
float4 sunColor;
float4 effects;
row_major float4x4 previousViewProjection;
float4 previousCamera, originChange, history, transport;
row_major float4x4 viewProjection;
float4 pathSettings, environmentSettings;
};
Texture2D<float> depthInput : register(t0);
Texture2D<float4> normalInput : register(t1);
Texture2D<float4> lightInput : register(t2);
Texture2D<float4> materialInput : register(t3);
Texture2D<float4> positions : register(t4);
Texture2D<float4> normals : register(t5);
Texture2D<float4> traceResult : register(t6);
Texture2D<float4> reflectionResult : register(t7);
Texture2D<float4> indirectResult : register(t8);
Texture2D<float4> albedoInput : register(t9);
Texture2D<float4> glassReflectionResult : register(t10);
Texture2D<float4> glassDataResult : register(t11);
Texture2D<float4> glassFactorResult : register(t15);
Texture2D<float4> glassRefractionResult : register(t12);
Texture2D<float4> glassSharpResult : register(t13);
Texture2D<float4> glassGuideResult : register(t14);
RWTexture2D<float4> positionOutput : register(u0);
RWTexture2D<float4> normalOutput : register(u1);
RWTexture2D<float4> surfaceOutput : register(u2);
float3 DecodeNormal(float2 packed) {
float2 q = packed * 4 - 2;
float d = dot(q, q);
return float3(q * sqrt(saturate(1 - d * .25)), 1 - d * .5);
}
[numthreads(8, 8, 1)]
void Pack(uint3 id : SV_DispatchThreadID) {
if (any(id.xy >= sizes.zw)) return;
uint2 p = min(uint2((float2(id.xy)+.5)*float2(sizes.xy)/sizes.zw),sizes.xy-1);
float depth = depthInput[p];
float2 uv = (float2(p) + .5) / sizes.xy;
float4 world = mul(float4(uv.x * 2 - 1, 1 - uv.y * 2, depth, 1), inverseViewProjection);
float3 local = world.xyz / world.w;
bool valid = depth > 0 && abs(world.w) > 1e-8 && all(isfinite(local)) && length(local) < 450;
// Far planets exceed FP16 range. Zero invalid positions before conversion;
// multiplying an infinite position by zero in the bilateral filter is NaN.
positionOutput[id.xy] = valid ? float4(local + originDelta.xyz, 1+materialInput[p].b) : 0;
surfaceOutput[id.xy]=float4(albedoInput[p].rgb,materialInput[p].r);
normalOutput[id.xy] = float4(normalize(mul(float4(DecodeNormal(normalInput[p].xy), 0), inverseView).xyz), materialInput[p].g);
}
float4 Fullscreen(uint id : SV_VertexID) : SV_Position {
return float4(id == 2 ? 3 : -1, id == 1 ? 3 : -1, 0, 1);
}
float4 GlassComposite(uint2 pixel,float4 color) {
if(pathSettings.z==1)return color;
int2 center=int2(min(pixel*sizes.zw/sizes.xy,sizes.zw-1));float4 data=glassDataResult[center];
if(settings.w==8)return float4(glassSharpResult[center].rgb,1);
if(settings.w==9)return float4(glassReflectionResult[center].rgb,1);
if(settings.w==10)return data.w<=0?float4(0,0,0,1):(glassFactorResult[center].w>0?float4(0,1,0,1):float4(1,0,0,1));
if(data.w<=0) return color;
float4 refraction=glassRefractionResult[center];
if(refraction.w>0) {
float2 shifted=clamp(float2(pixel)+refraction.xy*sizes.xy,0,float2(sizes.xy)-2);
int2 q=int2(floor(shifted));float2 f=frac(shifted);
float4 depths=float4(depthInput[q],depthInput[q+int2(1,0)],depthInput[q+int2(0,1)],depthInput[q+1]);
if(all(abs(depths-refraction.z)<.0002)) {
color=lerp(lerp(lightInput[q],lightInput[q+int2(1,0)],f.x),lerp(lightInput[q+int2(0,1)],lightInput[q+1],f.x),f.y);
}
}
float4 reflected=glassReflectionResult[center];
reflected.rgb*=glassFactorResult[center].rgb;
// Texture/emissive lettering and deterministic sun detail are current-frame
// values. Only indirect illumination crosses this spatial filter.
reflected.rgb+=glassSharpResult[center].rgb;
reflected.a=glassReflectionResult[center].a;
// Native transparency already contains tint/dirt and background radiance.
// Do not multiply its transmission a second time. Replace only the local
// reflected Fresnel fraction, retaining native environment on ray misses.
color.rgb=lerp(color.rgb,reflected.rgb,saturate(reflected.a*effects.x));
return color;
}
float4 Composite(float4 screen : SV_Position) : SV_Target {
uint2 pixel = uint2(screen.xy);
float4 color = lightInput[pixel];
if(pathSettings.z==2)return GlassComposite(pixel,color);
if(settings.w==5) {
float4 g=glassDataResult[min(pixel*sizes.zw/sizes.xy,sizes.zw-1)];return g.w>0?float4(g.rgb,1):float4(0,0,0,1);
}
if(settings.w==6) return float4(traceResult[min(pixel*sizes.zw/sizes.xy,sizes.zw-1)].zzz,1);
float depth = depthInput[pixel];
if (depth <= 0) return GlassComposite(pixel,color);
float2 uv = (float2(pixel) + .5) / sizes.xy;
float4 world = mul(float4(uv.x*2-1, 1-uv.y*2, depth, 1), inverseViewProjection);
float3 local = world.xyz / world.w;
if (abs(world.w) <= 1e-8 || !all(isfinite(local)) || length(local) >= 450) return GlassComposite(pixel,color);
float3 pos = local + originDelta.xyz;
float3 n = normalize(mul(float4(DecodeNormal(normalInput[pixel].xy), 0), inverseView).xyz);
float2 sum = 0; float4 reflected = 0; float3 indirect = 0; float weight = 0;
int2 center = int2(min(pixel*sizes.zw/sizes.xy,sizes.zw-1));
if(pathSettings.x>0 && traceResult[center].w<=0)return GlassComposite(pixel,color);
if(pathSettings.x>0 && settings.w==7)return float4(indirectResult[center].aaa/32,1);
if(all(sizes.xy==sizes.zw)) {
// Native pixels already match the G-buffer and have been denoised.
// An additional 25-tap upsample here blurs detail and wastes bandwidth.
sum=traceResult[center].xy;reflected=reflectionResult[center];indirect=indirectResult[center].rgb;weight=1;
} else {
// Joint bilateral upsample: respect geometry edges, do not darken sky/HUD.
[unroll] for (int y = -2; y <= 2; ++y) [unroll] for (int x = -2; x <= 2; ++x) {
int2 q = clamp(center + int2(x, y), 0, int2(sizes.zw) - 1);
float4 p = positions[q];
if (p.w == 0) continue;
if(pathSettings.x>0 && traceResult[q].w==0)continue;
float3 nn = normals[q].xyz;
float w = exp(-abs(dot(p.xyz - pos, n))*12 - (x*x+y*y)*.25)
* pow(saturate(dot(n,nn)), 24);
sum += traceResult[q].xy * w; reflected += reflectionResult[q] * w; indirect += indirectResult[q].rgb * w; weight += w;
}
}
float2 visibility = weight > 1e-5 ? sum / weight : 1;
reflected /= max(weight,1e-5); indirect /= max(weight,1e-5);
if(pathSettings.x>0 && settings.w==0) {
float4 material=materialInput[pixel];float3 base=albedoInput[pixel].rgb;
if(environmentSettings.w>0)indirect=indirect*max(base*(1-material.r)+lerp(.04,base,material.r),.08)+base*(1-material.r)*material.b*environmentSettings.z;
indirect+=reflectionResult[center].rgb;
return GlassComposite(pixel,float4(weight>1e-5?indirect:color.rgb,color.a));
}
float4 mat = materialInput[pixel];
float diffuse = (1 - mat.x) * (1 - mat.z);
float ao = lerp(1, visibility.x, settings.y * (1 - mat.z));
float shadow = lerp(1, visibility.y, settings.z * diffuse * saturate(dot(n,sunDirection.xyz)));
if (settings.w == 1) return float4(visibility.xxx, 1);
if (settings.w == 2) return float4(visibility.yyy, 1);
if (settings.w == 3) return float4(reflected.rgb, 1);
if (settings.w == 4) return float4(indirect, 1);
float3 albedo=albedoInput[pixel].rgb;
float3 view=normalize(originDelta.xyz-pos);
float3 f0=lerp(.04,albedo,mat.x);
float3 fresnel=f0+(1-f0)*pow(1-saturate(dot(n,view)),5);
float3 shaded=color.rgb*ao*shadow;
shaded += indirect*albedo*(1-mat.x)*(1-mat.z)*effects.y;
// Preserve the native environment reflection on ray misses. A local hit
// contributes textured radiance, weighted by the receiver's Fresnel term.
shaded = lerp(shaded,reflected.rgb/max(reflected.a,1e-3), saturate(fresnel*effects.x*reflected.a*(1-mat.z)));
return GlassComposite(pixel,float4(shaded,color.a));
}
// Own shaders. The normal encoding matches the installed renderer's documented
// spheremap G-buffer; no game shader source is bundled or overwritten.
cbuffer Parameters : register(b0) {
row_major float4x4 inverseViewProjection;
row_major float4x4 inverseView;
float4 originDelta;
float4 sunDirection;
float4 settings; // AO radius, strength, shadow strength, diagnostic view
uint4 sizes; // full width/height, trace width/height
float4 sunColor;
float4 effects;
row_major float4x4 previousViewProjection;
float4 previousCamera, originChange, history, transport;
row_major float4x4 viewProjection;
float4 pathSettings, environmentSettings;
};
Texture2D<float> depthInput : register(t0);
Texture2D<float4> normalInput : register(t1);
Texture2D<float4> lightInput : register(t2);
Texture2D<float4> materialInput : register(t3);
Texture2D<float4> positions : register(t4);
Texture2D<float4> normals : register(t5);
Texture2D<float4> traceResult : register(t6);
Texture2D<float4> reflectionResult : register(t7);
Texture2D<float4> indirectResult : register(t8);
Texture2D<float4> albedoInput : register(t9);
Texture2D<float4> glassReflectionResult : register(t10);
Texture2D<float4> glassDataResult : register(t11);
Texture2D<float4> glassFactorResult : register(t15);
Texture2D<float4> glassRefractionResult : register(t12);
Texture2D<float4> glassSharpResult : register(t13);
Texture2D<float4> glassGuideResult : register(t14);
RWTexture2D<float4> positionOutput : register(u0);
RWTexture2D<float4> normalOutput : register(u1);
RWTexture2D<float4> surfaceOutput : register(u2);
float3 DecodeNormal(float2 packed) {
float2 q = packed * 4 - 2;
float d = dot(q, q);
return float3(q * sqrt(saturate(1 - d * .25)), 1 - d * .5);
}
[numthreads(8, 8, 1)]
void Pack(uint3 id : SV_DispatchThreadID) {
if (any(id.xy >= sizes.zw)) return;
uint2 p = min(uint2((float2(id.xy)+.5)*float2(sizes.xy)/sizes.zw),sizes.xy-1);
float depth = depthInput[p];
float2 uv = (float2(p) + .5) / sizes.xy;
float4 world = mul(float4(uv.x * 2 - 1, 1 - uv.y * 2, depth, 1), inverseViewProjection);
float3 local = world.xyz / world.w;
bool valid = depth > 0 && abs(world.w) > 1e-8 && all(isfinite(local)) && length(local) < 450;
// Far planets exceed FP16 range. Zero invalid positions before conversion;
// multiplying an infinite position by zero in the bilateral filter is NaN.
positionOutput[id.xy] = valid ? float4(local + originDelta.xyz, 1+materialInput[p].b) : 0;
surfaceOutput[id.xy]=float4(albedoInput[p].rgb,materialInput[p].r);
normalOutput[id.xy] = float4(normalize(mul(float4(DecodeNormal(normalInput[p].xy), 0), inverseView).xyz), materialInput[p].g);
}
float4 Fullscreen(uint id : SV_VertexID) : SV_Position {
return float4(id == 2 ? 3 : -1, id == 1 ? 3 : -1, 0, 1);
}
float4 GlassComposite(uint2 pixel,float4 color) {
if(pathSettings.z==1)return color;
int2 center=int2(min(pixel*sizes.zw/sizes.xy,sizes.zw-1));float4 data=glassDataResult[center];
if(settings.w==8)return float4(glassSharpResult[center].rgb,1);
if(settings.w==9)return float4(glassReflectionResult[center].rgb,1);
if(settings.w==10)return data.w<=0?float4(0,0,0,1):(glassFactorResult[center].w>0?float4(0,1,0,1):float4(1,0,0,1));
if(data.w<=0) return color;
float4 refraction=glassRefractionResult[center];
if(refraction.w>0) {
float2 shifted=clamp(float2(pixel)+refraction.xy*sizes.xy,0,float2(sizes.xy)-2);
int2 q=int2(floor(shifted));float2 f=frac(shifted);
float4 depths=float4(depthInput[q],depthInput[q+int2(1,0)],depthInput[q+int2(0,1)],depthInput[q+1]);
if(all(abs(depths-refraction.z)<.0002)) {
color=lerp(lerp(lightInput[q],lightInput[q+int2(1,0)],f.x),lerp(lightInput[q+int2(0,1)],lightInput[q+1],f.x),f.y);
}
}
float4 reflected=glassReflectionResult[center];
reflected.rgb*=glassFactorResult[center].rgb;
// Texture/emissive lettering and deterministic sun detail are current-frame
// values. Only indirect illumination crosses this spatial filter.
reflected.rgb+=glassSharpResult[center].rgb;
reflected.a=glassReflectionResult[center].a;
// Native transparency already contains tint/dirt and background radiance.
// Do not multiply its transmission a second time. Replace only the local
// reflected Fresnel fraction, retaining native environment on ray misses.
color.rgb=lerp(color.rgb,reflected.rgb,saturate(reflected.a*effects.x));
return color;
}
float4 Composite(float4 screen : SV_Position) : SV_Target {
uint2 pixel = uint2(screen.xy);
float4 color = lightInput[pixel];
if(pathSettings.z==2)return GlassComposite(pixel,color);
if(settings.w==5) {
float4 g=glassDataResult[min(pixel*sizes.zw/sizes.xy,sizes.zw-1)];return g.w>0?float4(g.rgb,1):float4(0,0,0,1);
}
if(settings.w==6) return float4(traceResult[min(pixel*sizes.zw/sizes.xy,sizes.zw-1)].zzz,1);
float depth = depthInput[pixel];
if (depth <= 0) return GlassComposite(pixel,color);
float2 uv = (float2(pixel) + .5) / sizes.xy;
float4 world = mul(float4(uv.x*2-1, 1-uv.y*2, depth, 1), inverseViewProjection);
float3 local = world.xyz / world.w;
if (abs(world.w) <= 1e-8 || !all(isfinite(local)) || length(local) >= 450) return GlassComposite(pixel,color);
float3 pos = local + originDelta.xyz;
float3 n = normalize(mul(float4(DecodeNormal(normalInput[pixel].xy), 0), inverseView).xyz);
float2 sum = 0; float4 reflected = 0; float3 indirect = 0; float weight = 0;
int2 center = int2(min(pixel*sizes.zw/sizes.xy,sizes.zw-1));
if(pathSettings.x>0 && traceResult[center].w<=0)return GlassComposite(pixel,color);
if(pathSettings.x>0 && settings.w==7)return float4(indirectResult[center].aaa/32,1);
if(all(sizes.xy==sizes.zw)) {
// Native pixels already match the G-buffer and have been denoised.
// An additional 25-tap upsample here blurs detail and wastes bandwidth.
sum=traceResult[center].xy;reflected=reflectionResult[center];indirect=indirectResult[center].rgb;weight=1;
} else {
// Joint bilateral upsample: respect geometry edges, do not darken sky/HUD.
[unroll] for (int y = -2; y <= 2; ++y) [unroll] for (int x = -2; x <= 2; ++x) {
int2 q = clamp(center + int2(x, y), 0, int2(sizes.zw) - 1);
float4 p = positions[q];
if (p.w == 0) continue;
if(pathSettings.x>0 && traceResult[q].w==0)continue;
float3 nn = normals[q].xyz;
float w = exp(-abs(dot(p.xyz - pos, n))*12 - (x*x+y*y)*.25)
* pow(saturate(dot(n,nn)), 24);
sum += traceResult[q].xy * w; reflected += reflectionResult[q] * w; indirect += indirectResult[q].rgb * w; weight += w;
}
}
float2 visibility = weight > 1e-5 ? sum / weight : 1;
reflected /= max(weight,1e-5); indirect /= max(weight,1e-5);
if(pathSettings.x>0 && settings.w==0) {
float4 material=materialInput[pixel];float3 base=albedoInput[pixel].rgb;
if(environmentSettings.w>0)indirect=indirect*max(base*(1-material.r)+lerp(.04,base,material.r),.08)+base*(1-material.r)*material.b*environmentSettings.z;
indirect+=reflectionResult[center].rgb;
return GlassComposite(pixel,float4(weight>1e-5?indirect:color.rgb,color.a));
}
float4 mat = materialInput[pixel];
float diffuse = (1 - mat.x) * (1 - mat.z);
float ao = lerp(1, visibility.x, settings.y * (1 - mat.z));
float shadow = lerp(1, visibility.y, settings.z * diffuse * saturate(dot(n,sunDirection.xyz)));
if (settings.w == 1) return float4(visibility.xxx, 1);
if (settings.w == 2) return float4(visibility.yyy, 1);
if (settings.w == 3) return float4(reflected.rgb, 1);
if (settings.w == 4) return float4(indirect, 1);
float3 albedo=albedoInput[pixel].rgb;
float3 view=normalize(originDelta.xyz-pos);
float3 f0=lerp(.04,albedo,mat.x);
float3 fresnel=f0+(1-f0)*pow(1-saturate(dot(n,view)),5);
float3 shaded=color.rgb*ao*shadow;
shaded += indirect*albedo*(1-mat.x)*(1-mat.z)*effects.y;
// Preserve the native environment reflection on ray misses. A local hit
// contributes textured radiance, weighted by the receiver's Fresnel term.
shaded = lerp(shaded,reflected.rgb/max(reflected.a,1e-3), saturate(fresnel*effects.x*reflected.a*(1-mat.z)));
return GlassComposite(pixel,float4(shaded,color.a));
}