#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.PositionWS = mul(input.Position, World).xyz; output.TexCoord = input.TexCoord; output.NormalWS = normalize(mul(input.Normal, WorldInverseTranspose)); 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; } // The case where we have no texture maps for any PBR data float4 None(PixelShaderInput input) : SV_TARGET { float3 albedo = AlbedoValue; float metallic = MetallicValue; float roughness = RoughnessValue; float3 N = normalize(input.NormalWS); float3 V = normalize(EyePosition - input.PositionWS); 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] - 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; 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); } float4 AlbedoMapPS(PixelShaderInput input) : SV_TARGET { float3 albedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord).rgb; float metallic = MetallicValue; float roughness = RoughnessValue; float3 N = normalize(input.NormalWS); float3 V = normalize(EyePosition - input.PositionWS); 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] - 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; 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); } float4 AlbedoMetallicRoughnessMapPS(PixelShaderInput input) : SV_TARGET { float3 albedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord).rgb; float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg; float metallic = metallicRoughness.r; float roughness = metallicRoughness.g; float3 N = normalize(input.NormalWS); float3 V = normalize(EyePosition - input.PositionWS); 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] - 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; 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 { Pass Pass1 { VertexShader = compile vs_3_0 main_vs(); PixelShader = compile ps_3_0 None(); } }