From 2e053e6bbb4d68912ae599f6fe7f627b832b8349 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Sat, 11 Jun 2016 14:07:49 +0200 Subject: [PATCH] gbuffer normal compression --- WickedEngine/DeferredRenderableComponent.cpp | 2 +- WickedEngine/brdf.hlsli | 4 +- WickedEngine/decalPS.hlsl | 3 +- WickedEngine/dirLightHF.hlsli | 2 +- WickedEngine/lightHF.hlsli | 2 +- WickedEngine/objectHF.hlsli | 22 +++++--- WickedEngine/objectPS_forwardSimple.hlsl | 2 - WickedEngine/objectPS_transparent.hlsl | 2 - WickedEngine/packHF.hlsli | 56 ++++++++++++++++++++ WickedEngine/pointLightHF.hlsli | 2 +- WickedEngine/postProcessHF.hlsli | 9 +++- WickedEngine/spotLightHF.hlsli | 2 +- WickedEngine/ssao.hlsl | 2 +- WickedEngine/ssr.hlsl | 2 +- WickedEngine/ssss.hlsl | 2 +- WickedEngine/toneMapPS.hlsl | 1 + WickedEngine/wiVersion.cpp | 2 +- 17 files changed, 92 insertions(+), 25 deletions(-) diff --git a/WickedEngine/DeferredRenderableComponent.cpp b/WickedEngine/DeferredRenderableComponent.cpp index 90aee2c24..f1cd32269 100644 --- a/WickedEngine/DeferredRenderableComponent.cpp +++ b/WickedEngine/DeferredRenderableComponent.cpp @@ -26,7 +26,7 @@ void DeferredRenderableComponent::Initialize() rtGBuffer.Initialize( wiRenderer::GetDevice()->GetScreenWidth(), wiRenderer::GetDevice()->GetScreenHeight() , true, FORMAT_R8G8B8A8_UNORM); - rtGBuffer.Add(FORMAT_R11G11B10_FLOAT); + rtGBuffer.Add(FORMAT_R16G16_FLOAT); rtGBuffer.Add(FORMAT_R8G8B8A8_UNORM); rtGBuffer.Add(FORMAT_R8G8B8A8_UNORM); diff --git a/WickedEngine/brdf.hlsli b/WickedEngine/brdf.hlsli index 86f0e7772..40b51500d 100644 --- a/WickedEngine/brdf.hlsli +++ b/WickedEngine/brdf.hlsli @@ -94,11 +94,11 @@ float GetDiffuse(float NdotV, float NdotL, float LdotH, float linearRoughness) // ROUGHNESS: float surface roughness // F0: float3 surface specular color (fresnel f0) #define BRDF_SPECULAR( ROUGHNESS, F0 ) \ - GetSpecular(NdotV, NdotL, LdotH, NdotH, ROUGHNESS, F0) * LdotN + GetSpecular(NdotV, NdotL, LdotH, NdotH, ROUGHNESS, F0) // ROUGHNESS: float surface roughness #define BRDF_DIFFUSE( ROUGHNESS ) \ - GetDiffuse(NdotV, NdotL, LdotH, ROUGHNESS) * LdotN + GetDiffuse(NdotV, NdotL, LdotH, ROUGHNESS) // BASECOLOR: float4 surface color // REFLECTANCE: float surface reflectance value diff --git a/WickedEngine/decalPS.hlsl b/WickedEngine/decalPS.hlsl index 2cf7def83..e8c4a35be 100644 --- a/WickedEngine/decalPS.hlsl +++ b/WickedEngine/decalPS.hlsl @@ -1,5 +1,6 @@ #include "reconstructPositionHF.hlsli" #include "tangentComputeHF.hlsli" +#include "packHF.hlsli" //Texture2D xSceneDepthMap:register(t1); //Texture2D xTexture:register(t2); @@ -74,7 +75,7 @@ PixelOutputType main(VertexToPixel PSIn) Out.nor.a=Out.col.a; } - Out.nor = Out.nor * 0.5f + 0.5f; + Out.nor = float4(encode(Out.nor.xyz), 0, Out.nor.a); return Out; } \ No newline at end of file diff --git a/WickedEngine/dirLightHF.hlsli b/WickedEngine/dirLightHF.hlsli index 4733a74cd..0cfaf281e 100644 --- a/WickedEngine/dirLightHF.hlsli +++ b/WickedEngine/dirLightHF.hlsli @@ -57,7 +57,7 @@ inline void dirLight(in float3 P, in float3 N, in float3 V, in float roughness, specular += EnvironmentReflection(N, V, P, roughness, f0); - float sh = 1; + float sh = max(NdotL, 0); float4 ShPos[3]; ShPos[0] = mul(float4(P,1),g_xDirLight_ShM[0]); ShPos[1] = mul(float4(P,1),g_xDirLight_ShM[1]); diff --git a/WickedEngine/lightHF.hlsli b/WickedEngine/lightHF.hlsli index 0f24f1522..d6474b841 100644 --- a/WickedEngine/lightHF.hlsli +++ b/WickedEngine/lightHF.hlsli @@ -35,7 +35,7 @@ struct LightOutputType float4 baseColor = texture_gbuffer0.SampleLevel(sampler_point_clamp,screenPos,0); \ float4 g1 = texture_gbuffer1.SampleLevel(sampler_point_clamp,screenPos,0); \ float4 g3 = texture_gbuffer3.SampleLevel(sampler_point_clamp,screenPos,0); \ - float3 N = g1.xyz * 2 - 1; \ + float3 N = decode(g1.xy); \ float roughness = g3.x; \ float reflectance = g3.y; \ float metalness = g3.z; \ diff --git a/WickedEngine/objectHF.hlsli b/WickedEngine/objectHF.hlsli index ee3fccecd..361f2b40b 100644 --- a/WickedEngine/objectHF.hlsli +++ b/WickedEngine/objectHF.hlsli @@ -91,14 +91,24 @@ inline void Refraction(in float2 ScreenCoord, in float2 normal2D, in float3 bump alpha = 1; } -inline void DirectionalLight(in float3 N, in float3 V, in float3 f0, in float3 albedo, in float roughness, +inline void DirectionalLight(in float3 N, in float3 V, in float3 P, in float3 f0, in float3 albedo, in float roughness, inout float3 diffuse, out float3 specular) { float3 L = GetSunDirection(); float3 lightColor = GetSunColor(); BRDF_MAKE(N, L, V); specular = lightColor * BRDF_SPECULAR(roughness, f0); - diffuse = lightColor * BRDF_DIFFUSE(roughness); + diffuse = lightColor * BRDF_DIFFUSE(roughness); + + specular += EnvironmentReflection(N, V, P, roughness, f0); + + float sh = max(NdotL, 0); + + diffuse *= sh; + specular *= sh; + + diffuse = max(diffuse, 0); + specular = max(specular, 0); } @@ -142,7 +152,7 @@ inline void DirectionalLight(in float3 N, in float3 V, in float3 f0, in float3 a BRDF_HELPER_MAKEINPUTS( color, reflectance, metalness ) #define OBJECT_PS_LIGHT_DIRECTIONAL \ - DirectionalLight(N, V, f0, albedo, roughness, diffuse, specular); + DirectionalLight(N, V, P, f0, albedo, roughness, diffuse, specular); #define OBJECT_PS_LIGHT_END \ color.rgb = (GetAmbientColor() * ao + diffuse) * albedo + specular; @@ -153,9 +163,6 @@ inline void DirectionalLight(in float3 N, in float3 V, in float3 f0, in float3 a #define OBJECT_PS_NORMALMAPPING \ NormalMapping(UV, P, N, bumpColor); -#define OBJECT_PS_ENVIRONMENTMAPPING \ - specular += EnvironmentReflection(N, V, P, roughness, f0); - //#define OBJECT_PS_PLANARREFLECTIONS \ // PlanarReflection(input.tex, refUV, N, color); @@ -173,8 +180,9 @@ inline void DirectionalLight(in float3 N, in float3 V, in float3 f0, in float3 a #define OBJECT_PS_OUT_GBUFFER \ GBUFFEROutputType Out = (GBUFFEROutputType)0; \ + /*N = mul(N.xyz, (float3x3)g_xCamera_View);*/ \ Out.g0 = float4(color.rgb, 1); /*FORMAT_R8G8B8A8_UNORM*/ \ - Out.g1 = float4(N.xyz * 0.5f + 0.5f, 0); /*FORMAT_R11G11B10_FLOAT*/ \ + Out.g1 = float4(encode(N), 0, 0); /*FORMAT_R16G16_FLOAT*/ \ Out.g2 = float4(velocity * 0.5f + 0.5f, sss, emissive); /*FORMAT_R8G8B8A8_UNORM*/ \ Out.g3 = float4(roughness, reflectance, metalness, ao); /*FORMAT_R8G8B8A8_UNORM*/ \ return Out; diff --git a/WickedEngine/objectPS_forwardSimple.hlsl b/WickedEngine/objectPS_forwardSimple.hlsl index 6f7a06a98..ccfa78a18 100644 --- a/WickedEngine/objectPS_forwardSimple.hlsl +++ b/WickedEngine/objectPS_forwardSimple.hlsl @@ -15,8 +15,6 @@ float4 main(PixelInputType input) : SV_TARGET OBJECT_PS_LIGHT_DIRECTIONAL - OBJECT_PS_ENVIRONMENTMAPPING - OBJECT_PS_LIGHT_END OBJECT_PS_EMISSIVE diff --git a/WickedEngine/objectPS_transparent.hlsl b/WickedEngine/objectPS_transparent.hlsl index c70fb1675..cdc8b7440 100644 --- a/WickedEngine/objectPS_transparent.hlsl +++ b/WickedEngine/objectPS_transparent.hlsl @@ -16,8 +16,6 @@ float4 main( PixelInputType input) : SV_TARGET OBJECT_PS_LIGHT_DIRECTIONAL - OBJECT_PS_ENVIRONMENTMAPPING - OBJECT_PS_LIGHT_END OBJECT_PS_EMISSIVE diff --git a/WickedEngine/packHF.hlsli b/WickedEngine/packHF.hlsli index 6e4a41c57..efe688d6b 100644 --- a/WickedEngine/packHF.hlsli +++ b/WickedEngine/packHF.hlsli @@ -1,5 +1,6 @@ #ifndef _PACK_HF_ #define _PACK_HF_ +#include "globals.hlsli" // Helper functions to compress and uncompress two floats @@ -22,4 +23,59 @@ float2 Unpack(float input, int precision = 4096) return output / (precision - 1); } + +// Normal vector compressors + +#define NORMALMAPCOMPRESSOR 1 + +#if NORMALMAPCOMPRESSOR == 0 +// Reconstruct Z +float2 encode(float3 n) +{ + return float2(n.xy*0.5 + 0.5); +} +float3 decode(float2 enc) +{ + float3 n; + n.xy = enc * 2 - 1; + n.z = sqrt(1 - dot(n.xy, n.xy)); + return n; +} + +#elif NORMALMAPCOMPRESSOR == 1 + +// Spherical coordinates +float2 encode(float3 n) +{ + return (float2(atan2(n.y, n.x) / PI, n.z) + 1.0)*0.5; +} +float3 decode(float2 enc) +{ + float2 ang = enc * 2 - 1; + float2 scth; + sincos(ang.x * PI, scth.x, scth.y); + float2 scphi = float2(sqrt(1.0 - ang.y*ang.y), ang.y); + return float3(scth.y*scphi.x, scth.x*scphi.x, scphi.y); +} + +#else + +// Spheremap +float2 encode(float3 n) +{ + float2 enc = normalize(n.xy) * (sqrt(-n.z*0.5 + 0.5)); + enc = enc*0.5 + 0.5; + return enc; +} +float3 decode(float2 enc) +{ + float4 nn = float4(enc,0,0)*float4(2, 2, 0, 0) + float4(-1, -1, 1, -1); + float l = dot(nn.xyz, -nn.xyw); + nn.z = l; + nn.xy *= sqrt(l); + return nn.xyz * 2 + float3(0, 0, -1); +} + +#endif + #endif // _PACK_HF_ \ No newline at end of file diff --git a/WickedEngine/pointLightHF.hlsli b/WickedEngine/pointLightHF.hlsli index a99fd7773..74857cb1a 100644 --- a/WickedEngine/pointLightHF.hlsli +++ b/WickedEngine/pointLightHF.hlsli @@ -26,7 +26,7 @@ inline void pointLight(in float3 P, in float3 N, in float3 V, in float roughness diffuse *= attenuation; specular *= attenuation; - float sh = 1; + float sh = max(NdotL, 0); [branch]if(xLightEnerDis.w){ const float3 lv = P - xLightPos.xyz; static const float bias = 0.025; diff --git a/WickedEngine/postProcessHF.hlsli b/WickedEngine/postProcessHF.hlsli index 42e1d587c..408a5a45e 100644 --- a/WickedEngine/postProcessHF.hlsli +++ b/WickedEngine/postProcessHF.hlsli @@ -1,5 +1,8 @@ -//#include "ViewProp.h" +#ifndef _POSTPROCESS_HF_ +#define _POSTPROCESS_HF_ + #include "imageHF.hlsli" +#include "packHF.hlsli" @@ -42,4 +45,6 @@ float4 loadScene(float2 texCoord) float2 dim; texture_0.GetDimensions(dim.x, dim.y); return texture_0.Load(int3(dim*texCoord, 0)); -} \ No newline at end of file +} + +#endif // _POSTPROCESS_HF_ diff --git a/WickedEngine/spotLightHF.hlsli b/WickedEngine/spotLightHF.hlsli index a42301a3e..645db233a 100644 --- a/WickedEngine/spotLightHF.hlsli +++ b/WickedEngine/spotLightHF.hlsli @@ -56,7 +56,7 @@ inline void spotLight(in float3 P, in float3 N, in float3 V, in float roughness, specular *= xLightEnerDisCone.x; //color.rgb = max(dot(normalize(xLightDir.xyz), normalize(N)), 0)*xLightEnerDisCone.x; - float sh = 1; + float sh = max(NdotL, 0); float4 ShPos = mul(float4(P,1),xShMat); float2 ShTex = ShPos.xy / ShPos.w * float2(0.5f,-0.5f) + float2(0.5f,0.5f); [branch]if((saturate(ShTex.x) == ShTex.x) && (saturate(ShTex.y) == ShTex.y)) diff --git a/WickedEngine/ssao.hlsl b/WickedEngine/ssao.hlsl index 50f827e6b..e3acb88c6 100644 --- a/WickedEngine/ssao.hlsl +++ b/WickedEngine/ssao.hlsl @@ -46,7 +46,7 @@ static const float3 AO_SAMPLES[ NUM_SAMPLES ] = float4 main(VertexToPixelPostProcess input ):SV_Target { - float3 normal = loadNormal(input.tex.xy).xyz * 2 - 1; + float3 normal = decode(loadNormal(input.tex.xy).xy); float3 fres = normalize(xMaskTex.Load(int3((64 * input.tex.xy * 400) % 64, 0)).xyz * 2.0 - 1.0); diff --git a/WickedEngine/ssr.hlsl b/WickedEngine/ssr.hlsl index 063209561..b95569ae8 100644 --- a/WickedEngine/ssr.hlsl +++ b/WickedEngine/ssr.hlsl @@ -85,7 +85,7 @@ float4 main(VertexToPixelPostProcess input) : SV_Target float4 o = 1; float4 vNormZ; - vNormZ.rgb = texture_gbuffer1.Load(int3(input.pos.xy, 0)).rgb; + vNormZ.rgb = decode(texture_gbuffer1.Load(int3(input.pos.xy, 0)).rg); vNormZ.a = texture_depth.Load(int3(input.pos.xy, 0)); float2 vRougnessSpec = loadVelocity(input.tex).zw; //specular_power,specular intensity diff --git a/WickedEngine/ssss.hlsl b/WickedEngine/ssss.hlsl index 521eb9468..c64bc4a70 100644 --- a/WickedEngine/ssss.hlsl +++ b/WickedEngine/ssss.hlsl @@ -43,7 +43,7 @@ float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET float2 finalStep = colorM.a * step / depthM; // Accumulate the other samples: - //[unroll] + [unroll] for (int i = 1; i < SSSS_N_SAMPLES; i++) { // Fetch color and depth for current sample: float2 offset = PSIn.tex + kernel[i].a * finalStep; diff --git a/WickedEngine/toneMapPS.hlsl b/WickedEngine/toneMapPS.hlsl index d81d791f6..38459ac75 100644 --- a/WickedEngine/toneMapPS.hlsl +++ b/WickedEngine/toneMapPS.hlsl @@ -4,6 +4,7 @@ static const float strength = 0.85; float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET { + //return texture_gbuffer1.Load(uint3(PSIn.pos.xy,0)).zzzz; float4 hdr = xTexture.Load(uint3(PSIn.pos.xy,0)); float average_luminance = xMaskTex.Load(uint3(0,0,0)).r; //return xMaskTex.SampleLevel(sampler_linear_clamp, PSIn.tex, 0); diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 455470def..6506c7cf2 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -7,7 +7,7 @@ namespace wiVersion // minor features, major bug fixes const int minor = 8; // minor bug fixes, alterations, refactors - const int revision = 1; + const int revision = 2; long GetVersion()