fist attempt at multiple pixel shader paths

PBR
cosmonaut 2020-08-03 17:35:42 -07:00
parent 75114d77ef
commit 964e02c570
3 changed files with 168 additions and 144 deletions

View File

@ -67,6 +67,8 @@ namespace Smuggler
EffectParameter eyePositionParam; EffectParameter eyePositionParam;
EffectParameter shaderIndexParam;
Matrix world = Matrix.Identity; Matrix world = Matrix.Identity;
Matrix view = Matrix.Identity; Matrix view = Matrix.Identity;
Matrix projection = Matrix.Identity; Matrix projection = Matrix.Identity;
@ -77,6 +79,10 @@ namespace Smuggler
float roughness; float roughness;
float ao; float ao;
bool albedoTextureEnabled = false;
bool metallicRoughnessMapEnabled = false;
bool normalMapEnabled = false;
// FIXME: lazily set properties for performance // FIXME: lazily set properties for performance
public Matrix World public Matrix World
@ -175,13 +181,22 @@ namespace Smuggler
public Texture2D AlbedoTexture public Texture2D AlbedoTexture
{ {
get { return albedoTextureParam.GetValueTexture2D(); } get { return albedoTextureParam.GetValueTexture2D(); }
set { albedoTextureParam.SetValue(value); } set
{
albedoTextureParam.SetValue(value);
albedoTextureEnabled = value != null;
System.Console.WriteLine(albedoTextureEnabled);
}
} }
public Texture2D NormalTexture public Texture2D NormalTexture
{ {
get { return normalTextureParam.GetValueTexture2D(); } get { return normalTextureParam.GetValueTexture2D(); }
set { normalTextureParam.SetValue(value); } set
{
normalTextureParam.SetValue(value);
normalMapEnabled = value != null;
}
} }
public Texture2D EmissionTexture public Texture2D EmissionTexture
@ -199,7 +214,11 @@ namespace Smuggler
public Texture2D MetallicRoughnessTexture public Texture2D MetallicRoughnessTexture
{ {
get { return metallicRoughnessTextureParam.GetValueTexture2D(); } get { return metallicRoughnessTextureParam.GetValueTexture2D(); }
set { metallicRoughnessTextureParam.SetValue(value); } set
{
metallicRoughnessTextureParam.SetValue(value);
metallicRoughnessMapEnabled = value != null;
}
} }
public TextureCube EnvDiffuseTexture public TextureCube EnvDiffuseTexture
@ -271,7 +290,40 @@ namespace Smuggler
// FIXME: do param applications here for performance // FIXME: do param applications here for performance
protected override void OnApply() protected override void OnApply()
{ {
base.OnApply(); int shaderIndex = 0;
if (albedoTextureEnabled && metallicRoughnessMapEnabled && normalMapEnabled)
{
System.Console.WriteLine("all enabled");
shaderIndex = 7;
}
else if (metallicRoughnessMapEnabled && normalMapEnabled)
{
shaderIndex = 6;
}
else if (albedoTextureEnabled && normalMapEnabled)
{
shaderIndex = 5;
}
else if (albedoTextureEnabled && metallicRoughnessMapEnabled)
{
shaderIndex = 4;
}
else if (normalMapEnabled)
{
shaderIndex = 3;
}
else if (metallicRoughnessMapEnabled)
{
shaderIndex = 2;
}
else if (albedoTextureEnabled)
{
shaderIndex = 1;
}
System.Console.WriteLine(shaderIndex);
shaderIndexParam.SetValue(shaderIndex);
} }
void CacheEffectParameters(PBREffect cloneSource) void CacheEffectParameters(PBREffect cloneSource)
@ -298,6 +350,8 @@ namespace Smuggler
aoParam = Parameters["AO"]; aoParam = Parameters["AO"];
eyePositionParam = Parameters["EyePosition"]; eyePositionParam = Parameters["EyePosition"];
shaderIndexParam = Parameters["ShaderIndex"];
} }
} }
} }

View File

@ -121,15 +121,15 @@ float3 GetNormalFromMap(float3 worldPos, float2 texCoords, float3 normal)
return normalize(mul(tangentNormal, TBN)); return normalize(mul(tangentNormal, TBN));
} }
// The case where we have no texture maps for any PBR data float4 ComputeColor(
float4 None(PixelShaderInput input) : SV_TARGET0 float3 worldPosition,
{ float3 worldNormal,
float3 albedo = AlbedoValue; float3 albedo,
float metallic = MetallicValue; float metallic,
float roughness = RoughnessValue; float roughness
) {
float3 N = normalize(input.NormalWS); float3 V = normalize(EyePosition - worldPosition);
float3 V = normalize(EyePosition - input.PositionWS); float3 N = worldNormal;
float3 F0 = float3(0.04, 0.04, 0.04); float3 F0 = float3(0.04, 0.04, 0.04);
F0 = lerp(F0, albedo, metallic); F0 = lerp(F0, albedo, metallic);
@ -138,7 +138,7 @@ float4 None(PixelShaderInput input) : SV_TARGET0
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
float3 lightDir = LightPositions[i] - input.PositionWS; float3 lightDir = LightPositions[i] - worldPosition;
float3 L = normalize(lightDir); float3 L = normalize(lightDir);
float3 H = normalize(V + L); float3 H = normalize(V + L);
@ -173,166 +173,136 @@ float4 None(PixelShaderInput input) : SV_TARGET0
return float4(color, 1.0); return float4(color, 1.0);
} }
// The case where we have no texture maps for any PBR data
float4 None(PixelShaderInput input) : SV_TARGET0
{
return ComputeColor(
input.PositionWS,
input.NormalWS,
AlbedoValue,
MetallicValue,
RoughnessValue
);
}
float4 AlbedoMapPS(PixelShaderInput input) : SV_TARGET float4 AlbedoMapPS(PixelShaderInput input) : SV_TARGET
{ {
float3 albedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord).rgb; float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float metallic = MetallicValue;
float roughness = RoughnessValue;
float3 N = normalize(input.NormalWS); return ComputeColor(
float3 V = normalize(EyePosition - input.PositionWS); input.PositionWS,
input.NormalWS,
float3 F0 = float3(0.04, 0.04, 0.04); albedo,
F0 = lerp(F0, albedo, metallic); MetallicValue,
RoughnessValue
float3 Lo = float3(0.0, 0.0, 0.0); );
for (int i = 0; i < 4; i++)
{
float3 lightDir = LightPositions[i] - input.PositionWS;
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = LightColors[i] * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
float3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
} }
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO; float4 MetallicRoughnessPS(PixelShaderInput input) : SV_TARGET
float3 color = ambient + Lo; {
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
color = color / (color + float3(1.0, 1.0, 1.0)); return ComputeColor(
float exposureConstant = 1.0 / 2.2; input.PositionWS,
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant)); input.NormalWS,
AlbedoValue,
metallicRoughness.r,
metallicRoughness.g
);
}
return float4(color, 1.0); float4 NormalPS(PixelShaderInput input) : SV_TARGET
{
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
return ComputeColor(
input.PositionWS,
normal,
AlbedoValue,
MetallicValue,
RoughnessValue
);
} }
float4 AlbedoMetallicRoughnessMapPS(PixelShaderInput input) : SV_TARGET float4 AlbedoMetallicRoughnessMapPS(PixelShaderInput input) : SV_TARGET
{ {
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb; float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg; float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
float metallic = metallicRoughness.r;
float roughness = metallicRoughness.g;
float3 N = normalize(input.NormalWS); return ComputeColor(
float3 V = normalize(EyePosition - input.PositionWS); input.PositionWS,
input.NormalWS,
float3 F0 = float3(0.04, 0.04, 0.04); albedo,
F0 = lerp(F0, albedo, metallic); metallicRoughness.r,
metallicRoughness.g
float3 Lo = float3(0.0, 0.0, 0.0); );
for (int i = 0; i < 4; i++)
{
float3 lightDir = LightPositions[i] - input.PositionWS;
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = LightColors[i] * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
float3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
} }
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO; float4 AlbedoNormalPS(PixelShaderInput input) : SV_TARGET
float3 color = ambient + Lo; {
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
color = color / (color + float3(1.0, 1.0, 1.0)); return ComputeColor(
float exposureConstant = 1.0 / 2.2; input.PositionWS,
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant)); normal,
albedo,
MetallicValue,
RoughnessValue
);
}
return float4(color, 1.0); float4 MetallicRoughnessNormalPS(PixelShaderInput input) : SV_TARGET
{
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
return ComputeColor(
input.PositionWS,
normal,
AlbedoValue,
metallicRoughness.r,
metallicRoughness.g
);
} }
float4 AlbedoMetallicRoughnessNormalMapPS(PixelShaderInput input) : SV_TARGET float4 AlbedoMetallicRoughnessNormalMapPS(PixelShaderInput input) : SV_TARGET
{ {
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb; float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg; float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
float metallic = metallicRoughness.r; float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
float roughness = metallicRoughness.g;
float3 N = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS); return ComputeColor(
float3 V = normalize(EyePosition - input.PositionWS); input.PositionWS,
normal,
albedo,
metallicRoughness.r,
metallicRoughness.g
);
}
float3 F0 = float3(0.04, 0.04, 0.04); PixelShader PSArray[8] =
F0 = lerp(F0, albedo, metallic);
float3 Lo = float3(0.0, 0.0, 0.0);
for (int i = 0; i < 4; i++)
{ {
float3 lightDir = LightPositions[i] - input.PositionWS; compile ps_3_0 None(),
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float distance = length(lightDir); compile ps_3_0 AlbedoMapPS(),
float attenuation = 1.0 / (distance * distance); compile ps_3_0 MetallicRoughnessPS(),
float3 radiance = LightColors[i] * attenuation; compile ps_3_0 NormalPS(),
float NDF = DistributionGGX(N, H, roughness); compile ps_3_0 AlbedoMetallicRoughnessMapPS(),
float G = GeometrySmith(N, V, L, roughness); compile ps_3_0 AlbedoNormalPS(),
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0); compile ps_3_0 MetallicRoughnessNormalPS(),
float3 numerator = NDF * G * F; compile ps_3_0 AlbedoMetallicRoughnessNormalMapPS()
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0); };
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F; int ShaderIndex = 0;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO;
float3 color = ambient + Lo;
color = color / (color + float3(1.0, 1.0, 1.0));
float exposureConstant = 1.0 / 2.2;
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant));
return float4(color, 1.0);
}
Technique PBR Technique PBR
{ {
Pass Pass1 Pass
{ {
VertexShader = compile vs_3_0 main_vs(); VertexShader = compile vs_3_0 main_vs();
PixelShader = compile ps_3_0 AlbedoMetallicRoughnessNormalMapPS(); PixelShader = (PSArray[ShaderIndex]);
} }
} }

Binary file not shown.