2020-08-05 03:50:44 +00:00
|
|
|
#include "Macros.fxh" //from FNA
|
|
|
|
|
|
|
|
static const float PI = 3.141592653589793;
|
|
|
|
|
|
|
|
// Samplers
|
|
|
|
|
|
|
|
DECLARE_TEXTURE(AlbedoTexture, 0);
|
|
|
|
DECLARE_TEXTURE(NormalTexture, 1);
|
|
|
|
DECLARE_TEXTURE(EmissionTexture, 2);
|
|
|
|
DECLARE_TEXTURE(OcclusionTexture, 3);
|
|
|
|
DECLARE_TEXTURE(MetallicRoughnessTexture, 4);
|
|
|
|
DECLARE_CUBEMAP(EnvDiffuseTexture, 8);
|
|
|
|
DECLARE_TEXTURE(BrdfLutTexture, 9);
|
|
|
|
DECLARE_CUBEMAP(EnvSpecularTexture, 10);
|
|
|
|
|
|
|
|
BEGIN_CONSTANTS
|
|
|
|
|
|
|
|
// PBR Values
|
|
|
|
float3 AlbedoValue _ps(c0) _cb(c0);
|
|
|
|
float MetallicValue _ps(c1) _cb(c1);
|
|
|
|
float RoughnessValue _ps(c2) _cb(c2);
|
|
|
|
float AO _ps(c3) _cb(c3);
|
|
|
|
|
|
|
|
// Light Info
|
|
|
|
float3 LightPositions[4] _ps(c4) _cb(c4);
|
2020-08-07 08:12:46 +00:00
|
|
|
float3 PositionLightColors[4] _ps(c8) _cb(c8);
|
2020-08-05 03:50:44 +00:00
|
|
|
|
2020-08-16 18:48:51 +00:00
|
|
|
float3 LightDirections[4] _ps(c12) _cb(c12);
|
2020-08-07 08:12:46 +00:00
|
|
|
float3 DirectionLightColors[4] _ps(c16) _cb(c16);
|
2020-08-05 03:50:44 +00:00
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
float3 EyePosition _ps(c20) _cb(c20);
|
|
|
|
|
|
|
|
float4x4 World _vs(c0) _cb(c21);
|
|
|
|
float4x4 WorldInverseTranspose _vs(c4) _cb(c25);
|
2020-08-05 03:50:44 +00:00
|
|
|
|
|
|
|
MATRIX_CONSTANTS
|
|
|
|
|
|
|
|
float4x4 WorldViewProjection _vs(c8) _cb(c0);
|
|
|
|
|
|
|
|
END_CONSTANTS
|
|
|
|
|
|
|
|
struct VertexShaderInput
|
|
|
|
{
|
|
|
|
float4 Position : POSITION;
|
|
|
|
float3 Normal : NORMAL;
|
|
|
|
float2 TexCoord : TEXCOORD0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PixelShaderInput
|
|
|
|
{
|
|
|
|
float4 Position : SV_Position;
|
|
|
|
float2 TexCoord : TEXCOORD0;
|
|
|
|
float3 PositionWS : TEXCOORD1;
|
|
|
|
float3 NormalWS : TEXCOORD2;
|
|
|
|
};
|
|
|
|
|
|
|
|
PixelShaderInput main_vs(VertexShaderInput input)
|
|
|
|
{
|
|
|
|
PixelShaderInput output;
|
2020-08-16 18:48:51 +00:00
|
|
|
|
2020-08-05 03:50:44 +00:00
|
|
|
output.TexCoord = input.TexCoord;
|
|
|
|
output.PositionWS = mul(input.Position, World).xyz;
|
|
|
|
output.NormalWS = mul(input.Normal, (float3x3)WorldInverseTranspose).xyz;
|
|
|
|
output.Position = mul(input.Position, WorldViewProjection);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float3 FresnelSchlick(float cosTheta, float3 F0)
|
|
|
|
{
|
|
|
|
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
float DistributionGGX(float3 N, float3 H, float roughness)
|
|
|
|
{
|
|
|
|
float a = roughness * roughness;
|
|
|
|
float a2 = a * a;
|
|
|
|
float NdotH = max(dot(N, H), 0.0);
|
|
|
|
float NdotH2 = NdotH * NdotH;
|
|
|
|
|
|
|
|
float num = a2;
|
|
|
|
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
|
|
|
denom = PI * denom * denom;
|
|
|
|
|
|
|
|
return num / denom;
|
|
|
|
}
|
|
|
|
|
|
|
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
|
|
|
{
|
|
|
|
float r = (roughness + 1.0);
|
|
|
|
float k = (r * r) / 8.0;
|
|
|
|
|
|
|
|
float num = NdotV;
|
|
|
|
float denom = NdotV * (1.0 - k) + k;
|
|
|
|
|
|
|
|
return num / denom;
|
|
|
|
}
|
|
|
|
|
|
|
|
float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
|
|
|
|
{
|
|
|
|
float NdotV = max(dot(N, V), 0.0);
|
|
|
|
float NdotL = max(dot(N, L), 0.0);
|
|
|
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
|
|
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
|
|
|
|
|
|
|
return ggx1 * ggx2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Easy trick to get tangent-normals to world-space to keep PBR code simplified.
|
|
|
|
float3 GetNormalFromMap(float3 worldPos, float2 texCoords, float3 normal)
|
|
|
|
{
|
|
|
|
float3 tangentNormal = SAMPLE_TEXTURE(NormalTexture, texCoords).xyz * 2.0 - 1.0;
|
|
|
|
|
|
|
|
float3 Q1 = ddx(worldPos);
|
|
|
|
float3 Q2 = ddy(worldPos);
|
|
|
|
float2 st1 = ddx(texCoords);
|
|
|
|
float2 st2 = ddy(texCoords);
|
|
|
|
|
|
|
|
float3 N = normalize(normal);
|
|
|
|
float3 T = normalize(Q1*st2.y - Q2*st1.y);
|
|
|
|
float3 B = -normalize(cross(N, T));
|
|
|
|
float3x3 TBN = float3x3(T, B, N);
|
|
|
|
|
|
|
|
return normalize(mul(tangentNormal, TBN));
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
float3 ComputeLight(
|
|
|
|
float3 lightDir,
|
|
|
|
float3 radiance,
|
|
|
|
float3 F0,
|
|
|
|
float3 V,
|
|
|
|
float3 N,
|
|
|
|
float3 albedo,
|
|
|
|
float metallic,
|
|
|
|
float roughness
|
|
|
|
) {
|
|
|
|
float3 L = normalize(lightDir);
|
|
|
|
float3 H = normalize(V + L);
|
|
|
|
|
|
|
|
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);
|
|
|
|
return (kD * albedo / PI + specular) * radiance * NdotL;
|
|
|
|
}
|
|
|
|
|
2020-08-05 03:50:44 +00:00
|
|
|
float4 ComputeColor(
|
2020-08-16 18:48:51 +00:00
|
|
|
float3 worldPosition,
|
|
|
|
float3 worldNormal,
|
|
|
|
float3 albedo,
|
|
|
|
float metallic,
|
2020-08-05 03:50:44 +00:00
|
|
|
float roughness
|
|
|
|
) {
|
|
|
|
float3 V = normalize(EyePosition - worldPosition);
|
2020-08-16 18:48:51 +00:00
|
|
|
float3 N = normalize(worldNormal);
|
2020-08-05 03:50:44 +00:00
|
|
|
|
|
|
|
float3 F0 = float3(0.04, 0.04, 0.04);
|
|
|
|
F0 = lerp(F0, albedo, metallic);
|
|
|
|
|
|
|
|
float3 Lo = float3(0.0, 0.0, 0.0);
|
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
// point light
|
2020-08-05 03:50:44 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
float3 lightDir = LightPositions[i] - worldPosition;
|
|
|
|
float distance = length(lightDir);
|
|
|
|
float attenuation = 1.0 / (distance * distance);
|
2020-08-07 08:12:46 +00:00
|
|
|
float3 radiance = PositionLightColors[i] * attenuation;
|
2020-08-05 03:50:44 +00:00
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness);
|
|
|
|
}
|
2020-08-05 03:50:44 +00:00
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
// directional light
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
float3 lightDir = LightDirections[i];
|
|
|
|
float3 radiance = DirectionLightColors[i];
|
2020-08-05 03:50:44 +00:00
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness);
|
2020-08-05 03:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The case where we have no texture maps for any PBR data
|
|
|
|
float4 None(PixelShaderInput input) : SV_TARGET0
|
|
|
|
{
|
|
|
|
return ComputeColor(
|
|
|
|
input.PositionWS,
|
2020-08-16 18:48:51 +00:00
|
|
|
input.NormalWS,
|
2020-08-05 03:50:44 +00:00
|
|
|
AlbedoValue,
|
|
|
|
MetallicValue,
|
|
|
|
RoughnessValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 AlbedoMapPS(PixelShaderInput input) : SV_TARGET
|
|
|
|
{
|
|
|
|
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
|
|
|
|
|
|
|
|
return ComputeColor(
|
|
|
|
input.PositionWS,
|
|
|
|
input.NormalWS,
|
|
|
|
albedo,
|
|
|
|
MetallicValue,
|
|
|
|
RoughnessValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 MetallicRoughnessPS(PixelShaderInput input) : SV_TARGET
|
|
|
|
{
|
|
|
|
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
|
|
|
|
|
|
|
|
return ComputeColor(
|
|
|
|
input.PositionWS,
|
|
|
|
input.NormalWS,
|
|
|
|
AlbedoValue,
|
|
|
|
metallicRoughness.r,
|
|
|
|
metallicRoughness.g
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
|
|
|
|
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
|
|
|
|
|
|
|
|
return ComputeColor(
|
|
|
|
input.PositionWS,
|
|
|
|
input.NormalWS,
|
|
|
|
albedo,
|
|
|
|
metallicRoughness.r,
|
|
|
|
metallicRoughness.g
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 AlbedoNormalPS(PixelShaderInput input) : SV_TARGET
|
|
|
|
{
|
|
|
|
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
|
|
|
|
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
|
|
|
|
|
|
|
|
return ComputeColor(
|
|
|
|
input.PositionWS,
|
|
|
|
normal,
|
|
|
|
albedo,
|
|
|
|
MetallicValue,
|
|
|
|
RoughnessValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
|
|
|
|
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
|
|
|
|
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
|
|
|
|
|
|
|
|
return ComputeColor(
|
|
|
|
input.PositionWS,
|
|
|
|
normal,
|
|
|
|
albedo,
|
|
|
|
metallicRoughness.r,
|
|
|
|
metallicRoughness.g
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelShader PSArray[8] =
|
|
|
|
{
|
|
|
|
compile ps_3_0 None(),
|
|
|
|
|
|
|
|
compile ps_3_0 AlbedoMapPS(),
|
|
|
|
compile ps_3_0 MetallicRoughnessPS(),
|
|
|
|
compile ps_3_0 NormalPS(),
|
|
|
|
|
|
|
|
compile ps_3_0 AlbedoMetallicRoughnessMapPS(),
|
|
|
|
compile ps_3_0 AlbedoNormalPS(),
|
|
|
|
compile ps_3_0 MetallicRoughnessNormalPS(),
|
|
|
|
|
|
|
|
compile ps_3_0 AlbedoMetallicRoughnessNormalMapPS()
|
|
|
|
};
|
|
|
|
|
|
|
|
int PSIndices[8] =
|
|
|
|
{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7
|
|
|
|
};
|
|
|
|
|
|
|
|
int ShaderIndex = 0;
|
|
|
|
|
|
|
|
Technique PBR
|
|
|
|
{
|
|
|
|
Pass
|
|
|
|
{
|
|
|
|
VertexShader = compile vs_3_0 main_vs();
|
|
|
|
PixelShader = (PSArray[PSIndices[ShaderIndex]]);
|
|
|
|
}
|
|
|
|
}
|