Kav/Effects/HLSL/PBREffect.fx

314 lines
8.1 KiB
HLSL

#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);
float3 LightColors[4] _ps(c8) _cb(c8);
float3 EyePosition _ps(c12) _cb(c12);
float4x4 World _vs(c0) _cb(c16);
float4x4 WorldInverseTranspose _vs(c4) _cb(c20);
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;
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));
}
float4 ComputeColor(
float3 worldPosition,
float3 worldNormal,
float3 albedo,
float metallic,
float roughness
) {
float3 V = normalize(EyePosition - worldPosition);
float3 N = worldNormal;
float3 F0 = float3(0.04, 0.04, 0.04);
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] - worldPosition;
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;
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,
input.NormalWS,
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]]);
}
}