mirror of
https://mirror.ghproxy.com/https://github.com/dexyfex/CodeWalker
synced 2024-11-21 22:42:54 +08:00
Refactored deferred lights code, and skipping SSAA when sample count is 1
This commit is contained in:
parent
1843d70652
commit
152d439f89
@ -382,6 +382,26 @@
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||
</FxCompile>
|
||||
<FxCompile Include="DirLightPS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
|
||||
</FxCompile>
|
||||
<FxCompile Include="DirLightVS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
|
||||
</FxCompile>
|
||||
<FxCompile Include="DistantLightsPS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
@ -414,6 +434,26 @@
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LodLightsPS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LodLightsVS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
|
||||
</FxCompile>
|
||||
<FxCompile Include="MarkerPS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
@ -504,7 +544,7 @@
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Compute</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
|
||||
</FxCompile>
|
||||
<FxCompile Include="PPMSAAPS.hlsl">
|
||||
<FxCompile Include="PPSSAAPS.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
|
||||
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
@ -731,6 +771,7 @@
|
||||
<None Include="CablePS.hlsli" />
|
||||
<None Include="Clouds.hlsli" />
|
||||
<None Include="Common.hlsli" />
|
||||
<None Include="LightPS.hlsli" />
|
||||
<None Include="Quaternion.hlsli" />
|
||||
<None Include="Shadowmap.hlsli" />
|
||||
<None Include="Skydome.hlsli" />
|
||||
|
@ -247,9 +247,21 @@
|
||||
<FxCompile Include="DistantLightsVS.hlsl">
|
||||
<Filter>Lights</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="PPMSAAPS.hlsl">
|
||||
<FxCompile Include="PPSSAAPS.hlsl">
|
||||
<Filter>PostProcessor</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="DirLightVS.hlsl">
|
||||
<Filter>Lights</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="DirLightPS.hlsl">
|
||||
<Filter>Lights</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LodLightsVS.hlsl">
|
||||
<Filter>Lights</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LodLightsPS.hlsl">
|
||||
<Filter>Lights</Filter>
|
||||
</FxCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="BasicPS.hlsli">
|
||||
@ -291,6 +303,9 @@
|
||||
<None Include="WaterVS.hlsli">
|
||||
<Filter>Water</Filter>
|
||||
</None>
|
||||
<None Include="LightPS.hlsli">
|
||||
<Filter>Lights</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="Basic">
|
||||
|
48
CodeWalker.Shaders/DirLightPS.hlsl
Normal file
48
CodeWalker.Shaders/DirLightPS.hlsl
Normal file
@ -0,0 +1,48 @@
|
||||
#include "LightPS.hlsli"
|
||||
|
||||
|
||||
Texture2D DepthTex : register(t0);
|
||||
Texture2D DiffuseTex : register(t2);
|
||||
Texture2D NormalTex : register(t3);
|
||||
Texture2D SpecularTex : register(t4);
|
||||
Texture2D IrradianceTex : register(t5);
|
||||
|
||||
struct VS_Output
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Screen : TEXCOORD0;
|
||||
};
|
||||
|
||||
PS_OUTPUT main(VS_Output input)
|
||||
{
|
||||
uint3 ssloc = uint3(input.Pos.xy, 0); //pixel location
|
||||
float depth = DepthTex.Load(ssloc).r;
|
||||
if (depth == 0) discard; //no existing pixel rendered here
|
||||
|
||||
float4 diffuse = DiffuseTex.Load(ssloc);
|
||||
float4 normal = NormalTex.Load(ssloc);
|
||||
float4 specular = SpecularTex.Load(ssloc);
|
||||
float4 irradiance = IrradianceTex.Load(ssloc);
|
||||
|
||||
PS_OUTPUT output;
|
||||
output.Depth = input.Pos.z;
|
||||
|
||||
switch (RenderMode)
|
||||
{
|
||||
case 5: output.Colour = float4(diffuse.rgb, 1); return output;
|
||||
case 6: output.Colour = float4(normal.rgb, 1); return output;
|
||||
case 7: output.Colour = float4(specular.rgb, 1); return output;
|
||||
}
|
||||
|
||||
float4 spos = float4(input.Screen.xy/input.Screen.w, depth, 1);
|
||||
float4 cpos = mul(spos, ViewProjInv);
|
||||
float3 camRel = cpos.xyz * (1/cpos.w);
|
||||
float3 norm = normal.xyz * 2 - 1;
|
||||
|
||||
float3 c = DeferredDirectionalLight(camRel, norm, diffuse, specular, irradiance);
|
||||
|
||||
output.Colour = float4(c, 1);
|
||||
output.Depth = depth;
|
||||
return output;
|
||||
}
|
||||
|
30
CodeWalker.Shaders/DirLightVS.hlsl
Normal file
30
CodeWalker.Shaders/DirLightVS.hlsl
Normal file
@ -0,0 +1,30 @@
|
||||
#include "Common.hlsli"
|
||||
|
||||
|
||||
struct VS_Output
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Screen : TEXCOORD0;
|
||||
};
|
||||
|
||||
cbuffer VSLightVars : register(b0)
|
||||
{
|
||||
float4x4 ViewProj;
|
||||
float4 CameraPos;
|
||||
uint LightType; //0=directional, 1=Point, 2=Spot, 4=Capsule
|
||||
uint IsLOD; //useful or not?
|
||||
uint Pad0;
|
||||
uint Pad1;
|
||||
}
|
||||
|
||||
|
||||
VS_Output main(float4 ipos : POSITION)
|
||||
{
|
||||
float3 opos = ipos.xyz;
|
||||
float4 spos = mul(float4(opos, 1), ViewProj);
|
||||
VS_Output output;
|
||||
output.Pos = spos;
|
||||
output.Screen = spos;
|
||||
return output;
|
||||
}
|
||||
|
@ -1,32 +1,7 @@
|
||||
#include "Shadowmap.hlsli"
|
||||
#include "LightPS.hlsli"
|
||||
|
||||
|
||||
struct LODLight
|
||||
{
|
||||
float3 Position;
|
||||
uint Colour;
|
||||
float3 Direction;
|
||||
uint TimeAndStateFlags;
|
||||
float4 TangentX;
|
||||
float4 TangentY;
|
||||
float Falloff;
|
||||
float FalloffExponent;
|
||||
float InnerAngle; //for cone
|
||||
float OuterAngleOrCapExt; //outer angle for cone, cap extent for capsule
|
||||
};
|
||||
|
||||
struct VS_Output
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Screen : TEXCOORD0;
|
||||
uint IID : SV_INSTANCEID;
|
||||
};
|
||||
|
||||
struct PS_OUTPUT
|
||||
{
|
||||
float4 Colour : SV_TARGET;
|
||||
float Depth : SV_DEPTH;
|
||||
};
|
||||
//currently unused - TODO: implement individual HD lights here
|
||||
|
||||
|
||||
Texture2D DepthTex : register(t0);
|
||||
@ -35,41 +10,28 @@ Texture2D NormalTex : register(t3);
|
||||
Texture2D SpecularTex : register(t4);
|
||||
Texture2D IrradianceTex : register(t5);
|
||||
|
||||
|
||||
|
||||
cbuffer PSLightVars : register(b0)
|
||||
struct VS_Output
|
||||
{
|
||||
ShaderGlobalLightParams GlobalLights;
|
||||
float4x4 ViewProjInv;
|
||||
float4 CameraPos;
|
||||
uint EnableShadows;
|
||||
uint RenderMode; //0=default, 1=normals, 2=tangents, 3=colours, 4=texcoords, 5=diffuse, 6=normalmap, 7=spec, 8=direct
|
||||
uint RenderModeIndex;
|
||||
uint RenderSamplerCoord;
|
||||
uint LightType; //0=directional, 1=Point, 2=Spot, 4=Capsule
|
||||
uint IsLOD; //useful or not?
|
||||
uint Pad0;
|
||||
uint Pad1;
|
||||
}
|
||||
|
||||
StructuredBuffer<LODLight> LODLights : register(t6);
|
||||
|
||||
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Screen : TEXCOORD0;
|
||||
uint IID : SV_INSTANCEID;
|
||||
};
|
||||
|
||||
|
||||
PS_OUTPUT main(VS_Output input)
|
||||
{
|
||||
PS_OUTPUT output;
|
||||
output.Depth = input.Pos.z;
|
||||
|
||||
uint3 ssloc = uint3(input.Pos.xy, 0); //pixel location
|
||||
float depth = DepthTex.Load(ssloc).r;
|
||||
if (depth == 0) discard; //no existing pixel rendered here
|
||||
|
||||
float4 diffuse = DiffuseTex.Load(ssloc);
|
||||
float4 normal = NormalTex.Load(ssloc);
|
||||
float4 specular = SpecularTex.Load(ssloc);
|
||||
float4 irradiance = IrradianceTex.Load(ssloc);
|
||||
|
||||
if (depth == 0) discard; //no existing pixel rendered here
|
||||
|
||||
PS_OUTPUT output;
|
||||
output.Depth = input.Pos.z;
|
||||
|
||||
switch (RenderMode)
|
||||
{
|
||||
@ -82,22 +44,12 @@ PS_OUTPUT main(VS_Output input)
|
||||
float4 spos = float4(input.Screen.xy/input.Screen.w, depth, 1);
|
||||
float4 cpos = mul(spos, ViewProjInv);
|
||||
float3 camRel = cpos.xyz * (1/cpos.w);
|
||||
|
||||
float3 norm = normal.xyz * 2 - 1;
|
||||
|
||||
float3 incident = normalize(camRel);
|
||||
float3 refl = normalize(reflect(incident, norm));
|
||||
|
||||
if (LightType == 0) //directional light
|
||||
{
|
||||
float specb = saturate(dot(refl, GlobalLights.LightDir));
|
||||
float specp = max(exp(specb * 10) - 1, 0);
|
||||
float3 spec = GlobalLights.LightDirColour.rgb * 0.00006 * specp * specular.r;
|
||||
float4 lightspacepos;
|
||||
float shadowdepth = ShadowmapSceneDepth(camRel, lightspacepos);
|
||||
float3 c = FullLighting(diffuse.rgb, spec, norm, irradiance, GlobalLights, EnableShadows, shadowdepth, lightspacepos);
|
||||
|
||||
c += diffuse.rgb * irradiance.b;//emissive multiplier
|
||||
float3 c = DeferredDirectionalLight(camRel, norm, diffuse, specular, irradiance);
|
||||
|
||||
PS_OUTPUT output;
|
||||
output.Colour = float4(c, 1);
|
||||
@ -105,57 +57,11 @@ PS_OUTPUT main(VS_Output input)
|
||||
return output;
|
||||
}
|
||||
|
||||
float3 wpos = camRel + CameraPos.xyz;
|
||||
|
||||
LODLight lodlight = LODLights[input.IID];
|
||||
float3 srpos = lodlight.Position - wpos; //light position relative to surface position
|
||||
float ldist = length(srpos);
|
||||
if (ldist > lodlight.Falloff) discard; //out of range of the light... TODO: capsules!
|
||||
if (ldist <= 0) discard;
|
||||
float4 lcol = DeferredLODLight(camRel, norm, diffuse, specular, irradiance, input.IID);
|
||||
if (lcol.a <= 0) discard;
|
||||
|
||||
float4 rgbi = Unpack4x8UNF(lodlight.Colour).gbar;
|
||||
float3 lcol = rgbi.rgb * rgbi.a * 10.0f;
|
||||
|
||||
float3 ldir = srpos / ldist;
|
||||
float pclit = saturate(dot(ldir, norm));
|
||||
float lamt = 1;
|
||||
|
||||
if (LightType == 1)//point (sphere)
|
||||
{
|
||||
lamt *= pow(saturate(1 - (ldist / lodlight.Falloff)), lodlight.FalloffExponent);
|
||||
}
|
||||
else if (LightType == 2)//spot (cone)
|
||||
{
|
||||
float ang = acos(-dot(ldir, lodlight.Direction));
|
||||
float iang = lodlight.InnerAngle * 0.01745329;
|
||||
float oang = lodlight.OuterAngleOrCapExt * 0.01745329 * 0.5;
|
||||
if (ang > oang) discard;
|
||||
lamt *= saturate(1 - ((ang - iang) / (oang - iang)));
|
||||
lamt *= pow(saturate(1 - (ldist / lodlight.Falloff)), lodlight.FalloffExponent);
|
||||
}
|
||||
else if (LightType == 4)//capsule
|
||||
{
|
||||
lamt *= pow(saturate(1 - (ldist / lodlight.Falloff)), lodlight.FalloffExponent); //TODO! proper capsule lighting... (use point-line dist!)
|
||||
}
|
||||
|
||||
pclit *= lamt;
|
||||
|
||||
if (pclit <= 0) discard;
|
||||
|
||||
float specb = saturate(dot(refl, ldir));
|
||||
float specp = max(exp(specb * 10) - 1, 0);
|
||||
float3 spec = lcol * (0.00006 * specp * specular.r * lamt);
|
||||
|
||||
|
||||
lcol = lcol * diffuse.rgb * pclit + spec;
|
||||
|
||||
|
||||
output.Colour = float4(lcol, 0.5);
|
||||
output.Colour = lcol;
|
||||
return output;
|
||||
|
||||
//return float4(diffuse.rgb, 1);
|
||||
//return float4(normal.rgb, 1);
|
||||
//return float4(specular.rgb, 1);
|
||||
//return float4(irradiance.rgb, 1);
|
||||
}
|
||||
|
||||
|
117
CodeWalker.Shaders/LightPS.hlsli
Normal file
117
CodeWalker.Shaders/LightPS.hlsli
Normal file
@ -0,0 +1,117 @@
|
||||
#include "Shadowmap.hlsli"
|
||||
|
||||
|
||||
struct PS_OUTPUT
|
||||
{
|
||||
float4 Colour : SV_TARGET;
|
||||
float Depth : SV_DEPTH;
|
||||
};
|
||||
|
||||
cbuffer PSLightVars : register(b0)
|
||||
{
|
||||
ShaderGlobalLightParams GlobalLights;
|
||||
float4x4 ViewProjInv;
|
||||
float4 CameraPos;
|
||||
uint EnableShadows;
|
||||
uint RenderMode; //0=default, 1=normals, 2=tangents, 3=colours, 4=texcoords, 5=diffuse, 6=normalmap, 7=spec, 8=direct
|
||||
uint RenderModeIndex;
|
||||
uint RenderSamplerCoord;
|
||||
uint LightType; //0=directional, 1=Point, 2=Spot, 4=Capsule
|
||||
uint IsLOD; //useful or not?
|
||||
uint Pad0;
|
||||
uint Pad1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct LODLight
|
||||
{
|
||||
float3 Position;
|
||||
uint Colour;
|
||||
float3 Direction;
|
||||
uint TimeAndStateFlags;
|
||||
float4 TangentX;
|
||||
float4 TangentY;
|
||||
float Falloff;
|
||||
float FalloffExponent;
|
||||
float InnerAngle; //for cone
|
||||
float OuterAngleOrCapExt; //outer angle for cone, cap extent for capsule
|
||||
};
|
||||
|
||||
StructuredBuffer<LODLight> LODLights : register(t6);
|
||||
|
||||
|
||||
|
||||
|
||||
float3 GetReflectedDir(float3 camRel, float3 norm)
|
||||
{
|
||||
float3 incident = normalize(camRel);
|
||||
float3 refl = normalize(reflect(incident, norm));
|
||||
return refl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
float3 DeferredDirectionalLight(float3 camRel, float3 norm, float4 diffuse, float4 specular, float4 irradiance)
|
||||
{
|
||||
float3 refl = GetReflectedDir(camRel, norm);
|
||||
float specb = saturate(dot(refl, GlobalLights.LightDir));
|
||||
float specp = max(exp(specb * 10) - 1, 0);
|
||||
float3 spec = GlobalLights.LightDirColour.rgb * 0.00006 * specp * specular.r;
|
||||
float4 lightspacepos;
|
||||
float shadowdepth = ShadowmapSceneDepth(camRel, lightspacepos);
|
||||
float3 c = FullLighting(diffuse.rgb, spec, norm, irradiance, GlobalLights, EnableShadows, shadowdepth, lightspacepos);
|
||||
c += diffuse.rgb * irradiance.b; //emissive multiplier
|
||||
return c;
|
||||
}
|
||||
|
||||
float4 DeferredLODLight(float3 camRel, float3 norm, float4 diffuse, float4 specular, float4 irradiance, uint iid)
|
||||
{
|
||||
LODLight lodlight = LODLights[iid];
|
||||
float3 srpos = lodlight.Position - (camRel + CameraPos.xyz); //light position relative to surface position
|
||||
float ldist = length(srpos);
|
||||
if (ldist > lodlight.Falloff) return 0; //out of range of the light... TODO: capsules!
|
||||
if (ldist <= 0) return 0;
|
||||
|
||||
float4 rgbi = Unpack4x8UNF(lodlight.Colour).gbar;
|
||||
float3 lcol = rgbi.rgb * rgbi.a * 5.0f;
|
||||
float3 ldir = srpos / ldist;
|
||||
float pclit = saturate(dot(ldir, norm));
|
||||
float lamt = 1;
|
||||
|
||||
if (LightType == 1)//point (sphere)
|
||||
{
|
||||
lamt *= pow(saturate(1 - (ldist / lodlight.Falloff)), lodlight.FalloffExponent);
|
||||
}
|
||||
else if (LightType == 2)//spot (cone)
|
||||
{
|
||||
float ang = acos(-dot(ldir, lodlight.Direction));
|
||||
float iang = lodlight.InnerAngle * 0.01745329;
|
||||
float oang = lodlight.OuterAngleOrCapExt * 0.01745329 * 0.5;
|
||||
if (ang > oang) return 0;
|
||||
lamt *= saturate(1 - ((ang - iang) / (oang - iang)));
|
||||
lamt *= pow(saturate(1 - (ldist / lodlight.Falloff)), lodlight.FalloffExponent);
|
||||
}
|
||||
else if (LightType == 4)//capsule
|
||||
{
|
||||
lamt *= pow(saturate(1 - (ldist / lodlight.Falloff)), lodlight.FalloffExponent); //TODO! proper capsule lighting... (use point-line dist!)
|
||||
}
|
||||
|
||||
pclit *= lamt;
|
||||
|
||||
if (pclit <= 0) return 0;
|
||||
|
||||
float3 refl = GetReflectedDir(camRel, norm);
|
||||
float specb = saturate(dot(refl, ldir));
|
||||
float specp = max(exp(specb * 10) - 1, 0);
|
||||
float3 spec = lcol * (0.00006 * specp * specular.r * lamt);
|
||||
|
||||
lcol = lcol * diffuse.rgb * pclit + spec;
|
||||
|
||||
return float4(lcol, 1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
#include "Common.hlsli"
|
||||
|
||||
//currently unused - TODO: implement individual HD lights here
|
||||
|
||||
|
||||
|
||||
struct LODLight
|
||||
{
|
||||
|
38
CodeWalker.Shaders/LodLightsPS.hlsl
Normal file
38
CodeWalker.Shaders/LodLightsPS.hlsl
Normal file
@ -0,0 +1,38 @@
|
||||
#include "LightPS.hlsli"
|
||||
|
||||
|
||||
Texture2D DepthTex : register(t0);
|
||||
Texture2D DiffuseTex : register(t2);
|
||||
Texture2D NormalTex : register(t3);
|
||||
Texture2D SpecularTex : register(t4);
|
||||
Texture2D IrradianceTex : register(t5);
|
||||
|
||||
struct VS_Output
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Screen : TEXCOORD0;
|
||||
uint IID : SV_INSTANCEID;
|
||||
};
|
||||
|
||||
float4 main(VS_Output input) : SV_TARGET
|
||||
{
|
||||
uint3 ssloc = uint3(input.Pos.xy, 0); //pixel location
|
||||
float depth = DepthTex.Load(ssloc).r;
|
||||
if (depth == 0) discard; //no existing pixel rendered here
|
||||
|
||||
float4 diffuse = DiffuseTex.Load(ssloc);
|
||||
float4 normal = NormalTex.Load(ssloc);
|
||||
float4 specular = SpecularTex.Load(ssloc);
|
||||
float4 irradiance = IrradianceTex.Load(ssloc);
|
||||
|
||||
float4 spos = float4(input.Screen.xy / input.Screen.w, depth, 1);
|
||||
float4 cpos = mul(spos, ViewProjInv);
|
||||
float3 camRel = cpos.xyz * (1 / cpos.w);
|
||||
float3 norm = normal.xyz * 2 - 1;
|
||||
|
||||
float4 lcol = DeferredLODLight(camRel, norm, diffuse, specular, irradiance, input.IID);
|
||||
if (lcol.a <= 0) discard;
|
||||
|
||||
return lcol;
|
||||
}
|
||||
|
67
CodeWalker.Shaders/LodLightsVS.hlsl
Normal file
67
CodeWalker.Shaders/LodLightsVS.hlsl
Normal file
@ -0,0 +1,67 @@
|
||||
#include "Common.hlsli"
|
||||
|
||||
struct LODLight
|
||||
{
|
||||
float3 Position;
|
||||
uint Colour;
|
||||
float3 Direction;
|
||||
uint TimeAndStateFlags;
|
||||
float4 TangentX;
|
||||
float4 TangentY;
|
||||
float Falloff;
|
||||
float FalloffExponent;
|
||||
float InnerAngle; //for cone
|
||||
float OuterAngleOrCapExt; //outer angle for cone, cap extent for capsule
|
||||
};
|
||||
|
||||
struct VS_Output
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Screen : TEXCOORD0;
|
||||
uint IID : SV_INSTANCEID;
|
||||
};
|
||||
|
||||
|
||||
cbuffer VSLightVars : register(b0)
|
||||
{
|
||||
float4x4 ViewProj;
|
||||
float4 CameraPos;
|
||||
uint LightType; //0=directional, 1=Point, 2=Spot, 4=Capsule
|
||||
uint IsLOD; //useful or not?
|
||||
uint Pad0;
|
||||
uint Pad1;
|
||||
}
|
||||
|
||||
StructuredBuffer<LODLight> LODLights : register(t0);
|
||||
|
||||
|
||||
VS_Output main(float4 ipos : POSITION, uint iid : SV_InstanceID)
|
||||
{
|
||||
LODLight lodlight = LODLights[iid];
|
||||
float extent = lodlight.Falloff;
|
||||
float3 opos = 0;
|
||||
if (LightType == 1)//point (sphere)
|
||||
{
|
||||
opos = ipos.xyz * extent;
|
||||
}
|
||||
else if (LightType == 2)//spot (cone)
|
||||
{
|
||||
float arads = lodlight.OuterAngleOrCapExt * 0.01745329 * 0.5; // deg -> rad
|
||||
float3 cpos = ipos.xyz * (tan(arads) * extent);
|
||||
cpos.y += ipos.w * extent;
|
||||
opos = (cpos.x * lodlight.TangentX.xyz) + (cpos.y * lodlight.Direction.xyz) + (cpos.z * lodlight.TangentY.xyz);
|
||||
}
|
||||
else if (LightType == 4)//capsule
|
||||
{
|
||||
float3 cpos = ipos.xyz * extent;
|
||||
cpos.y += (ipos.w * 2 - 1) * lodlight.OuterAngleOrCapExt * 0.1;
|
||||
opos = (cpos.x * lodlight.TangentX.xyz) + (cpos.y * lodlight.Direction.xyz) + (cpos.z * lodlight.TangentY.xyz);
|
||||
}
|
||||
opos += (lodlight.Position - CameraPos.xyz);
|
||||
float4 spos = mul(float4(opos, 1), ViewProj);
|
||||
VS_Output output;
|
||||
output.Pos = spos;
|
||||
output.Screen = spos;
|
||||
output.IID = iid;
|
||||
return output;
|
||||
}
|
@ -61,7 +61,7 @@ namespace CodeWalker.Rendering
|
||||
|
||||
public bool deferred = Settings.Default.Deferred;
|
||||
public bool hdr = Settings.Default.HDR;
|
||||
public float hdrLumBlendSpeed = 1.0f;
|
||||
public float hdrLumBlendSpeed = 2.0f;
|
||||
int Width;
|
||||
int Height;
|
||||
|
||||
@ -645,22 +645,20 @@ namespace CodeWalker.Rendering
|
||||
context.OutputMerger.BlendState = bsDefault;
|
||||
context.OutputMerger.DepthStencilState = dsDisableAll;
|
||||
|
||||
if (DefScene != null)
|
||||
{
|
||||
if (HDR != null)
|
||||
{
|
||||
HDR.SetPrimary(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
DXMan.SetDefaultRenderTarget(context);
|
||||
}
|
||||
|
||||
DefScene.FinalPass(context);
|
||||
}
|
||||
if (HDR != null)
|
||||
{
|
||||
HDR.Render(DXMan, CurrentElapsedTime);
|
||||
if ((DefScene?.SSAASampleCount ?? 1) > 1)
|
||||
{
|
||||
HDR.SetPrimary(context);
|
||||
DefScene.SSAAPass(context);
|
||||
}
|
||||
|
||||
HDR.Render(DXMan, CurrentElapsedTime, DefScene);
|
||||
}
|
||||
else if (DefScene != null)
|
||||
{
|
||||
DXMan.SetDefaultRenderTarget(context);
|
||||
DefScene.SSAAPass(context);
|
||||
}
|
||||
|
||||
Basic.Deferred = deferred;
|
||||
|
@ -41,7 +41,7 @@ namespace CodeWalker.Rendering
|
||||
public uint Pad1;
|
||||
}
|
||||
|
||||
public struct DeferredMSAAPSVars
|
||||
public struct DeferredSSAAPSVars
|
||||
{
|
||||
public uint SampleCount;
|
||||
public float SampleMult;
|
||||
@ -64,8 +64,10 @@ namespace CodeWalker.Rendering
|
||||
int Height = 0;
|
||||
ViewportF Viewport;
|
||||
|
||||
VertexShader LightVS;
|
||||
PixelShader LightPS;
|
||||
VertexShader DirLightVS;
|
||||
PixelShader DirLightPS;
|
||||
VertexShader LodLightVS;
|
||||
PixelShader LodLightPS;
|
||||
UnitCone LightCone;
|
||||
UnitSphere LightSphere;
|
||||
UnitCapsule LightCapsule;
|
||||
@ -77,12 +79,11 @@ namespace CodeWalker.Rendering
|
||||
GpuVarsBuffer<DeferredLightPSVars> LightPSVars;
|
||||
|
||||
|
||||
int MSAASampleCount = 1;
|
||||
|
||||
VertexShader FinalVS;
|
||||
PixelShader MSAAPS;
|
||||
|
||||
GpuVarsBuffer<DeferredMSAAPSVars> MSAAPSVars;
|
||||
PixelShader SSAAPS;
|
||||
GpuVarsBuffer<DeferredSSAAPSVars> SSAAPSVars;
|
||||
public int SSAASampleCount = 1;
|
||||
|
||||
|
||||
public long VramUsage
|
||||
@ -99,18 +100,22 @@ namespace CodeWalker.Rendering
|
||||
{
|
||||
var device = dxman.device;
|
||||
|
||||
byte[] bLightVS = File.ReadAllBytes("Shaders\\LightVS.cso");
|
||||
byte[] bLightPS = File.ReadAllBytes("Shaders\\LightPS.cso");
|
||||
byte[] bDirLightVS = File.ReadAllBytes("Shaders\\DirLightVS.cso");
|
||||
byte[] bDirLightPS = File.ReadAllBytes("Shaders\\DirLightPS.cso");
|
||||
byte[] bLodLightVS = File.ReadAllBytes("Shaders\\LodLightsVS.cso");
|
||||
byte[] bLodLightPS = File.ReadAllBytes("Shaders\\LodLightsPS.cso");
|
||||
byte[] bFinalVS = File.ReadAllBytes("Shaders\\PPFinalPassVS.cso");
|
||||
byte[] bMSAAPS = File.ReadAllBytes("Shaders\\PPMSAAPS.cso");
|
||||
byte[] bSSAAPS = File.ReadAllBytes("Shaders\\PPSSAAPS.cso");
|
||||
|
||||
LightVS = new VertexShader(device, bLightVS);
|
||||
LightPS = new PixelShader(device, bLightPS);
|
||||
LightCone = new UnitCone(device, bLightVS, 4, false);
|
||||
LightSphere = new UnitSphere(device, bLightVS, 4, true);
|
||||
LightCapsule = new UnitCapsule(device, bLightVS, 4, false);
|
||||
DirLightVS = new VertexShader(device, bDirLightVS);
|
||||
DirLightPS = new PixelShader(device, bDirLightPS);
|
||||
LodLightVS = new VertexShader(device, bLodLightVS);
|
||||
LodLightPS = new PixelShader(device, bLodLightPS);
|
||||
LightCone = new UnitCone(device, bLodLightVS, 4, false);
|
||||
LightSphere = new UnitSphere(device, bLodLightVS, 4, true);
|
||||
LightCapsule = new UnitCapsule(device, bLodLightVS, 4, false);
|
||||
LightQuad = new UnitQuad(device, true);
|
||||
LightQuadLayout = new InputLayout(device, bLightVS, new[]
|
||||
LightQuadLayout = new InputLayout(device, bDirLightVS, new[]
|
||||
{
|
||||
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
|
||||
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0),
|
||||
@ -121,9 +126,9 @@ namespace CodeWalker.Rendering
|
||||
|
||||
|
||||
FinalVS = new VertexShader(device, bFinalVS);
|
||||
MSAAPS = new PixelShader(device, bMSAAPS);
|
||||
SSAAPS = new PixelShader(device, bSSAAPS);
|
||||
|
||||
MSAAPSVars = new GpuVarsBuffer<DeferredMSAAPSVars>(device);
|
||||
SSAAPSVars = new GpuVarsBuffer<DeferredSSAAPSVars>(device);
|
||||
|
||||
TextureAddressMode a = TextureAddressMode.Clamp;
|
||||
Color4 b = new Color4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
@ -188,25 +193,35 @@ namespace CodeWalker.Rendering
|
||||
LightCapsule.Dispose();
|
||||
LightCapsule = null;
|
||||
}
|
||||
if (LightPS != null)
|
||||
if (DirLightPS != null)
|
||||
{
|
||||
LightPS.Dispose();
|
||||
LightPS = null;
|
||||
DirLightPS.Dispose();
|
||||
DirLightPS = null;
|
||||
}
|
||||
if (LightVS != null)
|
||||
if (DirLightVS != null)
|
||||
{
|
||||
LightVS.Dispose();
|
||||
LightVS = null;
|
||||
DirLightVS.Dispose();
|
||||
DirLightVS = null;
|
||||
}
|
||||
if (MSAAPSVars != null)
|
||||
if (LodLightPS != null)
|
||||
{
|
||||
MSAAPSVars.Dispose();
|
||||
MSAAPSVars = null;
|
||||
LodLightPS.Dispose();
|
||||
LodLightPS = null;
|
||||
}
|
||||
if (MSAAPS != null)
|
||||
if (LodLightVS != null)
|
||||
{
|
||||
MSAAPS.Dispose();
|
||||
MSAAPS = null;
|
||||
LodLightVS.Dispose();
|
||||
LodLightVS = null;
|
||||
}
|
||||
if (SSAAPSVars != null)
|
||||
{
|
||||
SSAAPSVars.Dispose();
|
||||
SSAAPSVars = null;
|
||||
}
|
||||
if (SSAAPS != null)
|
||||
{
|
||||
SSAAPS.Dispose();
|
||||
SSAAPS = null;
|
||||
}
|
||||
if (FinalVS != null)
|
||||
{
|
||||
@ -223,8 +238,8 @@ namespace CodeWalker.Rendering
|
||||
|
||||
|
||||
|
||||
int uw = Width = dxman.backbuffer.Description.Width * MSAASampleCount;
|
||||
int uh = Height = dxman.backbuffer.Description.Height * MSAASampleCount;
|
||||
int uw = Width = dxman.backbuffer.Description.Width * SSAASampleCount;
|
||||
int uh = Height = dxman.backbuffer.Description.Height * SSAASampleCount;
|
||||
Viewport = new ViewportF();
|
||||
Viewport.Width = (float)uw;
|
||||
Viewport.Height = (float)uh;
|
||||
@ -285,8 +300,8 @@ namespace CodeWalker.Rendering
|
||||
//discard pixels where scene depth is 0, since nothing was rendered there
|
||||
//blend mode: overwrite
|
||||
|
||||
context.VertexShader.Set(LightVS);
|
||||
context.PixelShader.Set(LightPS);
|
||||
context.VertexShader.Set(DirLightVS);
|
||||
context.PixelShader.Set(DirLightPS);
|
||||
|
||||
LightVSVars.Vars.ViewProj = Matrix.Identity; //Matrix.Transpose(camera.ViewProjMatrix);
|
||||
LightVSVars.Vars.CameraPos = Vector4.Zero; //new Vector4(camera.Position, 0.0f);
|
||||
@ -336,8 +351,8 @@ namespace CodeWalker.Rendering
|
||||
//blend mode: additive
|
||||
|
||||
|
||||
context.VertexShader.Set(LightVS);
|
||||
context.PixelShader.Set(LightPS);
|
||||
context.VertexShader.Set(LodLightVS);
|
||||
context.PixelShader.Set(LodLightPS);
|
||||
|
||||
LightVSVars.Vars.ViewProj = Matrix.Transpose(camera.ViewProjMatrix);
|
||||
LightVSVars.Vars.CameraPos = new Vector4(camera.Position, 0.0f);
|
||||
@ -421,22 +436,22 @@ namespace CodeWalker.Rendering
|
||||
|
||||
|
||||
|
||||
public void FinalPass(DeviceContext context)
|
||||
public void SSAAPass(DeviceContext context)
|
||||
{
|
||||
//do antialiasing from SceneColour into HDR primary
|
||||
//do antialiasing from SceneColour
|
||||
|
||||
context.VertexShader.Set(FinalVS);
|
||||
context.PixelShader.Set(MSAAPS);
|
||||
context.PixelShader.Set(SSAAPS);
|
||||
|
||||
context.PixelShader.SetShaderResources(0, SceneColour.SRV);
|
||||
context.PixelShader.SetSamplers(0, SampleStatePoint);
|
||||
|
||||
MSAAPSVars.Vars.SampleCount = (uint)MSAASampleCount;
|
||||
MSAAPSVars.Vars.SampleMult = 1.0f / (MSAASampleCount * MSAASampleCount);
|
||||
MSAAPSVars.Vars.TexelSizeX = 1.0f / Width;
|
||||
MSAAPSVars.Vars.TexelSizeY = 1.0f / Height;
|
||||
MSAAPSVars.Update(context);
|
||||
MSAAPSVars.SetPSCBuffer(context, 0);
|
||||
SSAAPSVars.Vars.SampleCount = (uint)SSAASampleCount;
|
||||
SSAAPSVars.Vars.SampleMult = 1.0f / (SSAASampleCount * SSAASampleCount);
|
||||
SSAAPSVars.Vars.TexelSizeX = 1.0f / Width;
|
||||
SSAAPSVars.Vars.TexelSizeY = 1.0f / Height;
|
||||
SSAAPSVars.Update(context);
|
||||
SSAAPSVars.SetPSCBuffer(context, 0);
|
||||
|
||||
context.InputAssembler.InputLayout = LightQuadLayout;
|
||||
LightQuad.Draw(context);
|
||||
|
@ -165,6 +165,23 @@ namespace CodeWalker.Rendering
|
||||
|
||||
RawViewportF[] vpOld = new RawViewportF[15];
|
||||
|
||||
DeferredScene DefScene;
|
||||
bool UsePrimary = true;
|
||||
|
||||
ShaderResourceView SceneColourSRV
|
||||
{
|
||||
get
|
||||
{
|
||||
var srv = DefScene?.SceneColour?.SRV;
|
||||
if (UsePrimary || (srv == null))
|
||||
{
|
||||
srv = Primary.SRV;
|
||||
}
|
||||
return srv;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public PostProcessor(DXManager dxman)
|
||||
{
|
||||
var device = dxman.device;
|
||||
@ -417,13 +434,15 @@ namespace CodeWalker.Rendering
|
||||
context.Rasterizer.SetViewport(Viewport);
|
||||
}
|
||||
|
||||
public void Render(DXManager dxman, float elapsed)
|
||||
public void Render(DXManager dxman, float elapsed, DeferredScene defScene)
|
||||
{
|
||||
ElapsedTime = elapsed;
|
||||
DefScene = defScene;
|
||||
UsePrimary = ((defScene?.SSAASampleCount ?? 2) > 1) || (defScene?.SceneColour == null);
|
||||
|
||||
var context = dxman.context;
|
||||
|
||||
if (Multisampled)
|
||||
if (Multisampled && UsePrimary)
|
||||
{
|
||||
int sr = 0;// D3D11CalcSubresource(0, 0, 1);
|
||||
context.ResolveSubresource(Primary.TextureMS, sr, Primary.Texture, sr, Format.R32G32B32A32_Float);
|
||||
@ -442,6 +461,8 @@ namespace CodeWalker.Rendering
|
||||
|
||||
private void ProcessLuminance(DeviceContext context)
|
||||
{
|
||||
var srv = SceneColourSRV;
|
||||
|
||||
uint dimx, dimy;
|
||||
if (CS_FULL_PIXEL_REDUCTION)
|
||||
{
|
||||
@ -462,7 +483,8 @@ namespace CodeWalker.Rendering
|
||||
ReduceCSVars.Vars.Height = (uint)Height;
|
||||
ReduceCSVars.Update(context);
|
||||
|
||||
Compute(context, ReduceTo1DCS, ReduceCSVars.Buffer, Reduction0.UAV, (int)dimx, (int)dimy, 1, Primary.SRV);
|
||||
|
||||
Compute(context, ReduceTo1DCS, ReduceCSVars.Buffer, Reduction0.UAV, (int)dimx, (int)dimy, 1, srv);
|
||||
|
||||
uint dim = dimx * dimy;
|
||||
uint nNumToReduce = dim;
|
||||
@ -506,13 +528,10 @@ namespace CodeWalker.Rendering
|
||||
{
|
||||
if (EnableBloom)
|
||||
{
|
||||
var srv = SceneColourSRV;
|
||||
|
||||
// Bright pass and horizontal blur
|
||||
|
||||
ShaderResourceView view = Primary.SRV;
|
||||
|
||||
ShaderResourceView[] aRViews = { view, LumBlendResult.SRV };
|
||||
//BloomFilterShaderVars cbFilter;
|
||||
//GetSampleWeights(cbFilter.avSampleWeights, 3.0f, 1.25f);
|
||||
FilterBPHCSVars.Vars.outputwidth = (uint)(Width / 8);
|
||||
if (CS_FULL_PIXEL_REDUCTION)
|
||||
@ -529,7 +548,7 @@ namespace CodeWalker.Rendering
|
||||
|
||||
int x = (int)(Math.Ceiling((float)FilterBPHCSVars.Vars.outputwidth / (128 - 7 * 2)));
|
||||
int y = (Height / 8);
|
||||
Compute(context, BloomFilterBPHCS, FilterBPHCSVars.Buffer, Bloom1.UAV, x, y, 1, view, LumBlendResult.SRV);
|
||||
Compute(context, BloomFilterBPHCS, FilterBPHCSVars.Buffer, Bloom1.UAV, x, y, 1, srv, LumBlendResult.SRV);
|
||||
|
||||
// Vertical blur
|
||||
FilterVCSVars.Vars.outputsize0 = (int)(Width / 8);
|
||||
@ -552,16 +571,16 @@ namespace CodeWalker.Rendering
|
||||
context.VertexShader.Set(FinalPassVS);
|
||||
context.PixelShader.Set(FinalPassPS);
|
||||
|
||||
context.PixelShader.SetShaderResources(0, Primary.SRV, LumBlendResult.SRV, EnableBloom ? Bloom.SRV : null);
|
||||
var srv = SceneColourSRV;
|
||||
|
||||
context.PixelShader.SetShaderResources(0, srv, LumBlendResult.SRV, EnableBloom ? Bloom.SRV : null);
|
||||
|
||||
if (CS_FULL_PIXEL_REDUCTION)
|
||||
{
|
||||
//pcbCS[0] = 1.0f / (Width * Height);
|
||||
FinalPSVars.Vars.invPixelCount = new Vector4(1.0f / (Width * Height));
|
||||
}
|
||||
else
|
||||
{
|
||||
//pcbCS[0] = 1.0f / (81 * 81); //ToneMappingTexSize*ToneMappingTexSize
|
||||
FinalPSVars.Vars.invPixelCount = new Vector4(1.0f / (81 * 81));
|
||||
}
|
||||
FinalPSVars.Update(context);
|
||||
|
BIN
Shaders/DirLightPS.cso
Normal file
BIN
Shaders/DirLightPS.cso
Normal file
Binary file not shown.
BIN
Shaders/DirLightVS.cso
Normal file
BIN
Shaders/DirLightVS.cso
Normal file
Binary file not shown.
Binary file not shown.
BIN
Shaders/LodLightsPS.cso
Normal file
BIN
Shaders/LodLightsPS.cso
Normal file
Binary file not shown.
BIN
Shaders/LodLightsVS.cso
Normal file
BIN
Shaders/LodLightsVS.cso
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user