Skip to content

Commit

Permalink
lighting shader refactor, separated direct and indirect terms
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Apr 6, 2019
1 parent 2084e2a commit 2408153
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 289 deletions.
2 changes: 1 addition & 1 deletion WickedEngine/brdf.hlsli
Expand Up @@ -104,7 +104,7 @@ struct Surface

R = -reflect(V, N);
float f90 = saturate(50.0 * dot(f0, 0.33));
F = F_Schlick(f0, f90, NdotV) * occlusion;
F = F_Schlick(f0, f90, NdotV);
}
};
inline Surface CreateSurface(
Expand Down
35 changes: 11 additions & 24 deletions WickedEngine/deferredLightHF.hlsli
Expand Up @@ -24,7 +24,7 @@ struct LightOutputType

#define DEFERREDLIGHT_MAKEPARAMS \
ShaderEntityType light = EntityArray[(uint)g_xColor.x]; \
float3 diffuse, specular; \
Lighting lighting = CreateLighting(0, 0, 0, 0); \
float diffuse_alpha = 1; \
float specular_alpha = 1; \
float2 ScreenCoord = PSIn.pos2D.xy / PSIn.pos2D.w * float2(0.5f, -0.5f) + 0.5f; \
Expand All @@ -41,45 +41,32 @@ struct LightOutputType


#define DEFERREDLIGHT_DIRECTIONAL \
LightingResult result = DirectionalLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
DirectionalLight(light, surface, lighting);

#define DEFERREDLIGHT_SPOT \
LightingResult result = SpotLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
SpotLight(light, surface, lighting);

#define DEFERREDLIGHT_POINT \
LightingResult result = PointLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
PointLight(light, surface, lighting);

#define DEFERREDLIGHT_SPHERE \
LightingResult result = SphereLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
SphereLight(light, surface, lighting);

#define DEFERREDLIGHT_DISC \
LightingResult result = DiscLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
DiscLight(light, surface, lighting);

#define DEFERREDLIGHT_RECTANGLE \
LightingResult result = RectangleLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
RectangleLight(light, surface, lighting);

#define DEFERREDLIGHT_TUBE \
LightingResult result = TubeLight(light, surface); \
diffuse = result.diffuse; \
specular = result.specular;
TubeLight(light, surface, lighting);


#define DEFERREDLIGHT_RETURN \
LightingPart combined_lighting = CombineLighting(surface, lighting); \
LightOutputType Out; \
Out.diffuse = float4(diffuse, diffuse_alpha); \
Out.specular = float4(specular, specular_alpha); \
Out.diffuse = float4(combined_lighting.diffuse, diffuse_alpha); \
Out.specular = float4(combined_lighting.specular, specular_alpha); \
return Out;

#endif // _LIGHTHF_
Expand Down
13 changes: 5 additions & 8 deletions WickedEngine/environmentalLightPS.hlsl
Expand Up @@ -8,18 +8,15 @@ LightOutputType main(VertexToPixel PSIn)
DEFERREDLIGHT_MAKEPARAMS

float envMapMIP = surface.roughness * g_xFrame_EnvProbeMipCount;
specular = max(0, EnvironmentReflection_Global(surface, envMapMIP)) * surface.F;
specular_alpha = 1;
lighting.indirect.specular = max(0, EnvironmentReflection_Global(surface, envMapMIP));

VoxelGIResult vxgiresult = VoxelGI(surface);
diffuse = vxgiresult.diffuse.rgb;
diffuse_alpha = vxgiresult.diffuse.a;
specular = lerp(specular, vxgiresult.specular.rgb, vxgiresult.specular.a);
LightingContribution vxgi_contribution = VoxelGI(surface, lighting);

float4 ssr = xSSR.SampleLevel(sampler_linear_clamp, ReprojectedScreenCoord, 0);
specular = lerp(specular, ssr.rgb * surface.F, ssr.a);
lighting.indirect.specular = lerp(lighting.indirect.specular, ssr.rgb, ssr.a);

specular_alpha = max(vxgiresult.specular.a, ssr.a);
diffuse_alpha = vxgi_contribution.diffuse;
specular_alpha = max(vxgi_contribution.specular, ssr.a);

DEFERREDLIGHT_RETURN
}
5 changes: 2 additions & 3 deletions WickedEngine/hairparticlePS_deferred.hlsl
Expand Up @@ -15,9 +15,8 @@ GBUFFEROutputType main(VertexToPixel input)
ALPHATEST(color.a)
float emissive = 0;
Surface surface = CreateSurface(0, input.nor, 0, color, 1, 1, 0, 0);
float3 diffuse = GetAmbient(surface.N);
float3 specular = 0;
Lighting lighting = CreateLighting(0, 0, GetAmbient(surface.N), 0);
float2 velocity = ((input.pos2DPrev.xy / input.pos2DPrev.w - g_xFrame_TemporalAAJitterPrev) - (input.pos2D.xy / input.pos2D.w - g_xFrame_TemporalAAJitter)) * float2(0.5f, -0.5f);

return CreateGbuffer(color, surface, velocity, diffuse, 0);
return CreateGbuffer(color, surface, velocity, lighting);
}
7 changes: 3 additions & 4 deletions WickedEngine/hairparticlePS_forward.hlsl
Expand Up @@ -19,15 +19,14 @@ GBUFFEROutputType_Thin main(VertexToPixel input)
V /= dist;
float emissive = 0;
Surface surface = CreateSurface(input.pos3D, input.nor, V, color, 1, 1, 0, 0);
Lighting lighting = CreateLighting(0, 0, GetAmbient(surface.N), 0);
float2 pixel = input.pos.xy;
float depth = input.pos.z;
float3 diffuse = GetAmbient(surface.N);
float3 specular = 0;
float2 velocity = ((input.pos2DPrev.xy / input.pos2DPrev.w - g_xFrame_TemporalAAJitterPrev) - (input.pos2D.xy / input.pos2D.w - g_xFrame_TemporalAAJitter)) * float2(0.5f, -0.5f);

ForwardLighting(surface, diffuse, specular);
ForwardLighting(surface, lighting);

ApplyLighting(surface, diffuse, specular, color);
ApplyLighting(surface, lighting, color);

ApplyFog(dist, color);

Expand Down
7 changes: 3 additions & 4 deletions WickedEngine/hairparticlePS_tiledforward.hlsl
Expand Up @@ -19,16 +19,15 @@ GBUFFEROutputType_Thin main(VertexToPixel input)
V /= dist;
float emissive = 0;
Surface surface = CreateSurface(input.pos3D, input.nor, V, color, 1, 1, 0, 0);
Lighting lighting = CreateLighting(0, 0, GetAmbient(surface.N), 0);
float2 pixel = input.pos.xy;
float depth = input.pos.z;
float3 diffuse = GetAmbient(surface.N);
float3 specular = 0;
float3 reflection = 0;
float2 velocity = ((input.pos2DPrev.xy / input.pos2DPrev.w - g_xFrame_TemporalAAJitterPrev) - (input.pos2D.xy / input.pos2D.w - g_xFrame_TemporalAAJitter)) * float2(0.5f, -0.5f);

TiledLighting(pixel, surface, diffuse, specular);
TiledLighting(pixel, surface, lighting);

ApplyLighting(surface, diffuse, specular, color);
ApplyLighting(surface, lighting, color);

ApplyFog(dist, color);

Expand Down
3 changes: 2 additions & 1 deletion WickedEngine/impostorPS_deferred.hlsl
Expand Up @@ -25,8 +25,9 @@ GBUFFEROutputType main(VSOut input)
float reflectance = surfaceparams.a;

Surface surface = CreateSurface(0, N, 0, color, ao, roughness, metalness, reflectance);
Lighting lighting = CreateLighting(0, 0, GetAmbient(surface.N), 0);

float2 velocity = ((input.pos2DPrev.xy / input.pos2DPrev.w - g_xFrame_TemporalAAJitterPrev) - (input.pos2D.xy / input.pos2D.w - g_xFrame_TemporalAAJitter)) * float2(0.5f, -0.5f);

return CreateGbuffer(color, surface, velocity, 0, 0);
return CreateGbuffer(color, surface, velocity, lighting);
}
7 changes: 3 additions & 4 deletions WickedEngine/impostorPS_forward.hlsl
Expand Up @@ -27,15 +27,14 @@ GBUFFEROutputType_Thin main(VSOut input)
float dist = length(V);
V /= dist;
Surface surface = CreateSurface(input.pos3D, N, V, color, ao, roughness, reflectance, metalness);
Lighting lighting = CreateLighting(0, 0, GetAmbient(surface.N), 0);
float2 pixel = input.pos.xy;
float depth = input.pos.z;
float3 diffuse = 0;
float3 specular = 0;
float2 velocity = ((input.pos2DPrev.xy / input.pos2DPrev.w - g_xFrame_TemporalAAJitterPrev) - (input.pos2D.xy / input.pos2D.w - g_xFrame_TemporalAAJitter)) * float2(0.5f, -0.5f);

ForwardLighting(surface, diffuse, specular);
ForwardLighting(surface, lighting);

ApplyLighting(surface, diffuse, specular, color);
ApplyLighting(surface, lighting, color);

ApplyFog(dist, color);

Expand Down
7 changes: 3 additions & 4 deletions WickedEngine/impostorPS_tiledforward.hlsl
Expand Up @@ -24,15 +24,14 @@ GBUFFEROutputType_Thin main(VSOut input)
float dist = length(V);
V /= dist;
Surface surface = CreateSurface(input.pos3D, N, V, color, ao, roughness, metalness, reflectance);
Lighting lighting = CreateLighting(0, 0, GetAmbient(surface.N), 0);
float2 pixel = input.pos.xy;
float depth = input.pos.z;
float3 diffuse = 0;
float3 specular = 0;
float2 velocity = ((input.pos2DPrev.xy / input.pos2DPrev.w - g_xFrame_TemporalAAJitterPrev) - (input.pos2D.xy / input.pos2D.w - g_xFrame_TemporalAAJitter)) * float2(0.5f, -0.5f);

TiledLighting(pixel, surface, diffuse, specular);
TiledLighting(pixel, surface, lighting);

ApplyLighting(surface, diffuse, specular, color);
ApplyLighting(surface, lighting, color);

ApplyFog(dist, color);

Expand Down
39 changes: 17 additions & 22 deletions WickedEngine/lightCullingCS.hlsl
Expand Up @@ -331,15 +331,16 @@ void main(ComputeShaderInput IN)
if (pixel.x >= (uint)GetInternalResolution().x || pixel.y >= (uint)GetInternalResolution().y)
continue;

float3 diffuse = deferred_Diffuse[pixel].rgb;
float3 specular = deferred_Specular[pixel].rgb;
float4 g0 = texture_gbuffer0[pixel];
float4 g1 = texture_gbuffer1[pixel];
float4 g2 = texture_gbuffer2[pixel];
float3 ld = deferred_Diffuse[pixel].rgb;
float3 ls = deferred_Specular[pixel].rgb;
float3 N = decode(g1.xy);
float3 P = getPosition((float2)pixel * g_xFrame_InternalResolution_Inverse, depth[granularity]);
float3 V = normalize(g_xFrame_MainCamera_CamPos - P);
Surface surface = CreateSurface(P, N, V, float4(g0.rgb, 1), g2.r, g2.g, g2.b, g2.a);
Lighting lighting = CreateLighting(0, ls, ld, 0);

#ifndef DISABLE_ENVMAPS
// Apply environment maps:
Expand Down Expand Up @@ -411,13 +412,11 @@ void main(ComputeShaderInput IN)
{
envmapAccumulation.rgb = lerp(EnvironmentReflection_Global(surface, envMapMIP), envmapAccumulation.rgb, envmapAccumulation.a);
}
specular += max(0, envmapAccumulation.rgb) * surface.F;
lighting.indirect.specular += max(0, envmapAccumulation.rgb);
#endif // DISABLE_ENVMAPS

#ifndef DISABLE_VOXELGI
VoxelGIResult vxgiresult = VoxelGI(surface);
diffuse = lerp(diffuse, vxgiresult.diffuse.rgb, vxgiresult.diffuse.a);
specular = lerp(specular, vxgiresult.specular.rgb, vxgiresult.specular.a);
VoxelGI(surface, lighting);
#endif //DISABLE_VOXELGI

[branch]
Expand Down Expand Up @@ -446,49 +445,44 @@ void main(ComputeShaderInput IN)
{
ShaderEntityType light = EntityArray[entity_index];

LightingResult result = (LightingResult)0;

switch (light.GetType())
{
case ENTITY_TYPE_DIRECTIONALLIGHT:
{
result = DirectionalLight(light, surface);
DirectionalLight(light, surface, lighting);
}
break;
case ENTITY_TYPE_POINTLIGHT:
{
result = PointLight(light, surface);
PointLight(light, surface, lighting);
}
break;
case ENTITY_TYPE_SPOTLIGHT:
{
result = SpotLight(light, surface);
SpotLight(light, surface, lighting);
}
break;
case ENTITY_TYPE_SPHERELIGHT:
{
result = SphereLight(light, surface);
SphereLight(light, surface, lighting);
}
break;
case ENTITY_TYPE_DISCLIGHT:
{
result = DiscLight(light, surface);
DiscLight(light, surface, lighting);
}
break;
case ENTITY_TYPE_RECTANGLELIGHT:
{
result = RectangleLight(light, surface);
RectangleLight(light, surface, lighting);
}
break;
case ENTITY_TYPE_TUBELIGHT:
{
result = TubeLight(light, surface);
TubeLight(light, surface, lighting);
}
break;
}

diffuse += max(0.0f, result.diffuse);
specular += max(0.0f, result.specular);
}
else if (entity_index > last_item)
{
Expand All @@ -501,14 +495,15 @@ void main(ComputeShaderInput IN)
}
}

float2 ScreenCoord = (float2)pixel * g_xFrame_ScreenWidthHeight_Inverse;
float2 ScreenCoord = (float2)pixel * g_xFrame_InternalResolution_Inverse;
float2 velocity = g1.zw;
float2 ReprojectedScreenCoord = ScreenCoord + velocity;
float4 ssr = xSSR.SampleLevel(sampler_linear_clamp, ReprojectedScreenCoord, 0);
specular = lerp(specular, ssr.rgb * surface.F, ssr.a);
lighting.indirect.specular = lerp(lighting.indirect.specular, ssr.rgb, ssr.a);

deferred_Diffuse[pixel] = float4(diffuse, 1);
deferred_Specular[pixel] = float4(specular, 1);
LightingPart combined_lighting = CombineLighting(surface, lighting);
deferred_Diffuse[pixel] = float4(combined_lighting.diffuse, 1);
deferred_Specular[pixel] = float4(combined_lighting.specular, 1);
}
#endif // DEFERRED

Expand Down

0 comments on commit 2408153

Please sign in to comment.