Getting the GL side up-to-date to match.

This commit is contained in:
Areloch 2018-10-06 17:50:15 -05:00
parent 24007432e8
commit 4efcb567b0
21 changed files with 694 additions and 286 deletions

View file

@ -714,12 +714,11 @@ void ReflectionProbe::_onRenderViz(ObjectRenderInst *ri,
}
else
{
const MatrixF worldToObjectXfm = getTransform();
Box3F cube(-Point3F(mRadius, mRadius, mRadius),Point3F(mRadius, mRadius, mRadius));
cube.setCenter(getPosition()+mProbePosOffset);
draw->drawCube(desc, cube, color);
cube = getWorldBox();
draw->drawCube(desc, cube, color);
draw->drawCube(desc, cube, color, &worldToObjectXfm);
}
}

View file

@ -617,7 +617,7 @@ void RenderProbeMgr::ReflectProbeMaterialInfo::setProbeParameters(const ProbeRen
matParams->setSafe(bbMax, probeInfo->mBounds.maxExtents);
matParams->setSafe(useSphereMode, probeInfo->mProbeShapeType == ProbeInfo::Sphere ? 1.0f : 0.0f);
//SH Terms
//static AlignedArray<Point3F> shTermsArray(9, sizeof(Point3F));
//dMemset(shTermsArray.getBuffer(), 0, shTermsArray.getBufferSize());

View file

@ -285,6 +285,9 @@ new ShaderData( ReflectionProbeShader )
samplerNames[0] = "$deferredBuffer";
samplerNames[1] = "$matInfoBuffer";
samplerNames[2] = "$cubeMap";
samplerNames[3] = "$irradianceCubemap";
samplerNames[4] = "$BRDFTexture";
pixVersion = 3.0;
};
@ -307,7 +310,11 @@ new GFXStateBlockData( AL_ProbeState )
samplerStates[0] = SamplerClampPoint; // G-buffer
mSamplerNames[0] = "deferredBuffer";
samplerStates[1] = SamplerClampLinear; // Shadow Map (Do not use linear, these are perspective projections)
mSamplerNames[1] = "matInfoBuffer";
mSamplerNames[1] = "matInfoBuffer";
mSamplerNames[2] = "cubeMap";
mSamplerNames[3] = "irradianceCubemap";
mSamplerNames[4] = "BRDFTexture";
cullDefined = true;
cullMode = GFXCullCW;
@ -341,6 +348,8 @@ new ShaderData( IrradianceShader )
OGLVertexShaderFile = "shaders/common/lighting/advanced/gl/cubemapV.glsl";
OGLPixelShaderFile = "shaders/common/lighting/advanced/gl/irradianceP.glsl";
samplerNames[0] = "$environmentMap";
pixVersion = 3.0;
};
@ -352,6 +361,8 @@ new ShaderData( PrefiterCubemapShader )
OGLVertexShaderFile = "shaders/common/lighting/advanced/gl/cubemapV.glsl";
OGLPixelShaderFile = "shaders/common/lighting/advanced/gl/prefilterP.glsl";
samplerNames[0] = "$environmentMap";
pixVersion = 3.0;
};
@ -376,6 +387,9 @@ new ShaderData( SklyightShader )
samplerNames[0] = "$deferredBuffer";
samplerNames[1] = "$matInfoBuffer";
samplerNames[2] = "$cubeMap";
samplerNames[3] = "$irradianceCubemap";
samplerNames[4] = "$BRDFTexture";
pixVersion = 3.0;
};

View file

@ -269,4 +269,41 @@ void compute4Lights( vec3 wsView,
outDiffuse = vec4(albedo.rgb*(1.0-metalness),albedo.a);
outSpecular = EvalBDRF( vec3( 1.0, 1.0, 1.0 ), lightColor.rgb, toLight, wsPosition, wsNormal, smoothness, metalness );
}
float G1V(float dotNV, float k)
{
return 1.0f/(dotNV*(1.0f-k)+k);
}
vec3 directSpecular(vec3 N, vec3 V, vec3 L, float roughness, float F0)
{
float alpha = roughness*roughness;
//TODO don't need to calculate all this again timmy!!!!!!
vec3 H = normalize(V + L);
float dotNL = clamp(dot(N,L), 0.0, 1.0);
float dotNV = clamp(dot(N,V), 0.0, 1.0);
float dotNH = clamp(dot(N,H), 0.0, 1.0);
float dotHV = clamp(dot(H,V), 0.0, 1.0);
float dotLH = clamp(dot(L,H), 0.0, 1.0);
float F, D, vis;
// D
float alphaSqr = alpha*alpha;
float pi = 3.14159f;
float denom = dotNH * dotNH *(alphaSqr-1.0) + 1.0f;
D = alphaSqr/(pi * denom * denom);
// F
float dotLH5 = pow(1.0f-dotLH,5);
F = F0 + (1.0-F0)*(dotLH5);
// V
float k = alpha/2.0f;
vis = G1V(dotNL,k)*G1V(dotNV,k);
float specular = dotNL * D * F * vis;
return vec3(specular,specular,specular);
}

View file

@ -352,4 +352,35 @@ vec3 simpleFresnel(vec3 diffuseColor, vec3 reflectColor, float metalness, float
return mix(diffuseColor, reflectColor, fresnelTerm);
}
//get direction for a cube face
vec3 getCubeDir(int face, vec2 uv)
{
vec2 debiased = uv * 2.0f - 1.0f;
vec3 dir = vec3(0);
switch (face)
{
case 0: dir = vec3(1, -debiased.y, -debiased.x);
break;
case 1: dir = vec3(-1, -debiased.y, debiased.x);
break;
case 2: dir = vec3(debiased.x, 1, debiased.y);
break;
case 3: dir = vec3(debiased.x, -1, -debiased.y);
break;
case 4: dir = vec3(debiased.x, -debiased.y, 1);
break;
case 5: dir = vec3(-debiased.x, -debiased.y, -1);
break;
};
return normalize(dir);
}
#endif // _TORQUE_GLSL_

View file

@ -55,9 +55,6 @@ float4 main( PFXVertToPix IN ) : TORQUE_TARGET0
float3 specularColor = lerp(float3(0.04,0.04,0.04), albedo, metalness);
float3 light = (diffuseColor * diffuse.rgb) + (specularColor * specular.rgb);
//albedo = diffuseColor+lerp(reflectColor,indiffuseLighting,frez);
//albedo *= max(diffuseLighting.rgb,float3(0,0,0));
return float4(light.rgb, 1.0);
}

View file

@ -0,0 +1,136 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../../../torque.glsl"
in vec4 hpos;
in vec2 uv0;
// ----------------------------------------------------------------------------
// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
// efficient VanDerCorpus calculation.
float RadicalInverse_VdC(uint bits)
{
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
// ----------------------------------------------------------------------------
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i)/float(N), RadicalInverse_VdC(i));
}
// ----------------------------------------------------------------------------
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
{
float a = roughness*roughness;
float phi = 2.0 * M_PI_F * Xi.x;
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y));
float sinTheta = sqrt(1.0 - cosTheta*cosTheta);
// from spherical coordinates to cartesian coordinates - halfway vector
vec3 H;
H.x = cos(phi) * sinTheta;
H.y = sin(phi) * sinTheta;
H.z = cosTheta;
// from tangent-space H vector to world-space sample vector
vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
vec3 tangent = normalize(cross(up, N));
vec3 bitangent = cross(N, tangent);
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
return normalize(sampleVec);
}
// ----------------------------------------------------------------------------
float GeometrySchlickGGX(float NdotV, float roughness)
{
// note that we use a different k for IBL
float a = roughness;
float k = (a * a) / 2.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
// ----------------------------------------------------------------------------
float GeometrySmith(vec3 N, vec3 V, vec3 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;
}
// ----------------------------------------------------------------------------
vec2 IntegrateBRDF(float NdotV, float roughness)
{
vec3 V;
V.x = sqrt(1.0 - NdotV*NdotV);
V.y = 0.0;
V.z = NdotV;
float A = 0.0;
float B = 0.0;
vec3 N = vec3(0.0, 0.0, 1.0);
const uint SAMPLE_COUNT = 1024u;
for(uint i = 0u; i < SAMPLE_COUNT; ++i)
{
// generates a sample vector that's biased towards the
// preferred alignment direction (importance sampling).
vec2 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, N, roughness);
vec3 L = normalize(2.0 * dot(V, H) * H - V);
float NdotL = max(L.z, 0.0);
float NdotH = max(H.z, 0.0);
float VdotH = max(dot(V, H), 0.0);
if(NdotL > 0.0)
{
float G = GeometrySmith(N, V, L, roughness);
float G_Vis = (G * VdotH) / (NdotH * NdotV);
float Fc = pow(1.0 - VdotH, 5.0);
A += (1.0 - Fc) * G_Vis;
B += Fc * G_Vis;
}
}
A /= float(SAMPLE_COUNT);
B /= float(SAMPLE_COUNT);
return vec2(A, B);
}
out vec4 OUT_col;
void main()
{
OUT_col = vec4(IntegrateBRDF(uv0.x, uv0.y).rg,0,1);
}

View file

@ -21,11 +21,13 @@
//-----------------------------------------------------------------------------
#include "../../../gl/hlslCompat.glsl"
out vec2 uv;
out vec2 uv0;
void main()
{
uv = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
gl_Position = vec4(uv * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
correctSSP(gl_Position);
float x = float((gl_VertexID & 1) << 2)-1.0;
float y = float((gl_VertexID & 2) << 1)-1.0;
gl_Position = vec4(x, y, 0, 1);
uv0.x = (x+1.0)*0.5;
uv0.y = (y+1.0)*0.5;
}

View file

@ -62,9 +62,5 @@ void main()
vec3 light = (diffuseColor * diffuse.rgb) + (specularColor * specular.rgb);
//albedo = diffuseColor+lerp(reflectColor,indiffuseLighting,frez);
//albedo *= max(diffuseLighting.rgb,vec3(0,0,0));
//OUT_col = hdrEncode(vec4(colorBuffer,1.0));
OUT_col = hdrEncode(vec4(light, 1.0));
}

View file

@ -22,16 +22,16 @@
#include "../../../gl/torque.glsl"
in vec2 uv;
in vec2 uv0;
uniform int face;
uniform samplerCube environmentMap;
out vec4 outColor;
out vec4 OUT_col;
void main()
{
vec3 N = getCubeDir(face, uv);
vec3 N = getCubeDir(face, uv0);
vec3 irradiance = vec3(0.0);
// tangent space calculation from origin point
@ -56,5 +56,5 @@ void main()
}
irradiance = M_PI_F * irradiance * (1.0 / float(nrSamples));
outColor = vec4(irradiance, 1.0);
OUT_col = vec4(irradiance, 1.0);
}

View file

@ -131,6 +131,7 @@ uniform vec4 lightParams;
uniform float shadowSoftness;
out vec4 OUT_col;
out vec4 OUT_col1;
void main()
{
@ -145,6 +146,7 @@ void main()
if ( emissive )
{
OUT_col = vec4(0.0, 0.0, 0.0, 0.0);
OUT_col1 = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
@ -241,39 +243,32 @@ void main()
atten *= max( cookie.r, max( cookie.g, cookie.b ) );
#endif
// NOTE: Do not clip on fully shadowed pixels as it would
// cause the hardware occlusion query to disable the shadow.
// Specular term
float specular = 0;
vec4 real_specular = EvalBDRF( colorSample.rgb,
lightcol,
lightVec,
viewSpacePos,
normal,
1.05-matInfo.b*0.9, //slightly compress roughness to allow for non-baked lighting
matInfo.a );
vec3 lightColorOut = real_specular.rgb * lightBrightness * shadowed* atten;
vec3 l = lightVec;// normalize(-lightDirection);
vec3 v = eyeRay;// normalize(eyePosWorld - worldPos.xyz);
float Sat_NL_Att = saturate( nDotL * atten * shadowed ) * lightBrightness;
vec4 addToResult = vec4(0.0);
// TODO: This needs to be removed when lightmapping is disabled
// as its extra work per-pixel on dynamic lit scenes.
//
// Special lightmapping pass.
if ( lightMapParams.a < 0.0 )
{
// This disables shadows on the backsides of objects.
shadowed = nDotL < 0.0f ? 1.0f : shadowed;
vec3 h = normalize(v + l);
float dotNLa = clamp(dot(normal, l), 0.0, 1.0);
float dotNVa = clamp(dot(normal, v), 0.0, 1.0);
float dotNHa = clamp(dot(normal, h), 0.0, 1.0);
float dotHVa = clamp(dot(normal, v), 0.0, 1.0);
float dotLHa = clamp(dot(l, h), 0.0, 1.0);
Sat_NL_Att = 1.0f;
shadowed = mix( 1.0f, shadowed, atten );
lightColorOut = vec3(shadowed);
specular *= lightBrightness;
addToResult = ( 1.0 - shadowed ) * abs(lightMapParams);
}
float roughness = matInfo.g;
float metalness = matInfo.b;
OUT_col = vec4((lightColorOut*Sat_NL_Att+subsurface*(1.0-Sat_NL_Att)+addToResult.rgb),real_specular.a);
//diffuse
float disDiff = Fr_DisneyDiffuse(dotNVa, dotNLa, dotLHa, roughness);
vec3 diffuse = vec3(disDiff, disDiff, disDiff) / M_PI_F;
//specular
vec3 specular = directSpecular(normal, v, l, roughness, 1.0) * lightColor.rgb;
if (nDotL<0) shadowed = 0;
float Sat_NL_Att = saturate( nDotL * shadowed ) * lightBrightness;
//output
OUT_col = float4(diffuse * lightBrightness, Sat_NL_Att*shadowed);
OUT_col1 = float4(specular * lightBrightness, Sat_NL_Att*shadowed);
}

View file

@ -0,0 +1,130 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../../../gl/torque.glsl"
in vec4 hpos;
in vec2 uv0;
uniform samplerCube environmentMap;
uniform float roughness;
uniform int face;
uniform int mipSize;
uniform int resolution;
float RadicalInverse_VdC(uint bits)
{
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i) / float(N), RadicalInverse_VdC(i));
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH * NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = M_PI_F * denom * denom;
return nom / denom;
}
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N)
{
float a = roughness * roughness;
float phi = 2.0 * M_PI_F * Xi.x;
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y));
float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
// from spherical coordinates to cartesian coordinates
vec3 H;
H.x = cos(phi) * sinTheta;
H.y = sin(phi) * sinTheta;
H.z = cosTheta;
// from tangent-space vector to world-space sample vector
vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
vec3 tangent = normalize(cross(up, N));
vec3 bitangent = cross(N, tangent);
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
return normalize(sampleVec);
}
vec4 prefilterEnvMap(vec3 R)
{
int sampleCount = resolution*2;
vec3 N = R;
vec3 V = R;
float totalWeight = 0.0;
vec4 prefilteredColor = vec4(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i < sampleCount; ++i)
{
vec2 Xi = Hammersley(i, sampleCount);
vec3 H = ImportanceSampleGGX(Xi, N);
vec3 L = normalize(2.0 * dot(V, H) * H - V);
float NdotL = max(dot(N, L), 0.0);
if (NdotL > 0.0)
{
// sample from the environment's mip level based on roughness/pdf
float D = DistributionGGX(N, H, roughness);
float NdotH = max(dot(N, H), 0.0);
float HdotV = max(dot(H, V), 0.0);
float pdf = D * NdotH / (4.0 * HdotV) + 0.0001;
float saTexel = 4.0 * M_PI_F / float(6.0 * sampleCount * sampleCount);
float saSample = 1.0 / (float(sampleCount) * pdf + 0.0001);
float mipLevel = roughness == 0.0 ? 0.0 : 0.5 * log2(saSample / saTexel);
prefilteredColor += texture(environmentMap, L, mipLevel) * NdotL;
totalWeight += NdotL;
}
}
return (prefilteredColor / totalWeight);
}
out vec4 OUT_col;
void main()
{
vec3 N = getCubeDir(face, uv0);
OUT_col = prefilterEnvMap(N);
}

View file

@ -31,6 +31,10 @@ uniform sampler2D matInfoTex;
uniform sampler2D specularLightingBuffer;
uniform sampler2D deferredTex;
uniform float radius;
uniform vec2 targetSize;
uniform int captureRez;
out vec4 OUT_col;
void main()
@ -38,22 +42,27 @@ void main()
float depth = deferredUncondition( deferredTex, uv0 ).w;
if (depth>0.9999)
{
OUT_col = vec4(0.0);
discard;
return;
}
vec3 colorBuffer = texture( colorBufferTex, uv0 ).rgb; //albedo
vec4 matInfo = texture( matInfoTex, uv0 ); //flags|smoothness|ao|metallic
vec4 matInfo = texture(matInfoTex, uv0); //flags|smoothness|ao|metallic
bool emissive = getFlag(matInfo.r, 0);
if (emissive)
{
OUT_col = float4(colorBuffer, 1.0);
return;
OUT_col = vec4(colorBuffer, 1.0);
return;
}
vec4 diffuseLighting = texture( diffuseLightingBuffer, uv0 ); //shadowmap*specular
colorBuffer *= diffuseLighting.rgb;
vec2 relUV = uv0*targetSize/captureRez;
colorBuffer *= max(diffuseLighting.rgb,vec3(0,0,0));
//we use a 1k depth range in the capture frustum.
//reduce that a bit to get something resembling depth fidelity out of 8 bits
depth*=2000/radius;
OUT_col = hdrEncode(vec4(colorBuffer,1.0));
float rLen = length(vec3(relUV,depth)-vec3(0.5,0.5,0));
OUT_col = hdrEncode( vec4(colorBuffer,rLen));
}

View file

@ -15,6 +15,9 @@ in vec4 vsEyeDir;
uniform sampler2D deferredBuffer;
uniform sampler2D matInfoBuffer;
uniform samplerCube cubeMap;
uniform samplerCube irradianceCubemap;
uniform sampler2D BRDFTexture;
uniform float cubeMips;
uniform vec4 rtParams0;
@ -22,78 +25,135 @@ uniform vec3 probeWSPos;
uniform vec3 probeLSPos;
uniform vec4 vsFarPlane;
uniform float lightRange;
uniform vec2 lightAttenuation;
uniform float radius;
uniform vec2 attenuation;
uniform mat4 invViewMat;
uniform mat4x4 invViewMat;
uniform vec3 eyePosWorld;
uniform vec3 bbMin;
uniform vec3 bbMax;
uniform float Intensity;
//SHTerms
uniform vec4 SHTerms0;
uniform vec4 SHTerms1;
uniform vec4 SHTerms2;
uniform vec4 SHTerms3;
uniform vec4 SHTerms4;
uniform vec4 SHTerms5;
uniform vec4 SHTerms6;
uniform vec4 SHTerms7;
uniform vec4 SHTerms8;
uniform float SHConsts0;
uniform float SHConsts1;
uniform float SHConsts2;
uniform float SHConsts3;
uniform float SHConsts4;
uniform float useSphereMode;
vec4 decodeSH(vec3 normal)
// Box Projected IBL Lighting
// Based on: http://www.gamedev.net/topic/568829-box-projected-cubemap-environment-mapping/
// and https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
vec3 boxProject(vec3 wsPosition, vec3 reflectDir, vec3 boxWSPos, vec3 boxMin, vec3 boxMax)
{
vec3 nrdir = reflectDir;
vec3 offset = wsPosition;
vec3 plane1vec = (boxMax - offset) / nrdir;
vec3 plane2vec = (boxMin - offset) / nrdir;
vec3 furthestPlane = max(plane1vec, plane2vec);
float dist = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
vec3 posonbox = offset + nrdir * dist;
return posonbox - boxWSPos;
}
vec3 iblBoxDiffuse(vec3 normal,
vec3 wsPos,
samplerCube irradianceCube,
vec3 boxPos,
vec3 boxMin,
vec3 boxMax)
{
float x = normal.x;
float y = normal.y;
float z = normal.z;
// Irradiance (Diffuse)
vec3 cubeN = normalize(normal);
vec3 irradiance = texture(irradianceCube, cubeN).xyz;
vec3 l00 = SHTerms0.rgb;
return irradiance;
}
vec3 l10 = SHTerms1.rgb;
vec3 l11 = SHTerms2.rgb;
vec3 l12 = SHTerms3.rgb;
vec3 iblBoxSpecular(vec3 normal,
vec3 wsPos,
float roughness,
vec3 surfToEye,
sampler2D brdfTexture,
samplerCube radianceCube,
vec3 boxPos,
vec3 boxMin,
vec3 boxMax)
{
float ndotv = clamp(dot(normal, surfToEye), 0.0, 1.0);
vec3 l20 = SHTerms4.rgb;
vec3 l21 = SHTerms5.rgb;
vec3 l22 = SHTerms6.rgb;
vec3 l23 = SHTerms7.rgb;
vec3 l24 = SHTerms8.rgb;
// BRDF
vec2 brdf = texture(brdfTexture, vec2(roughness, ndotv)).xy;
vec3 result = (
l00 * SHConsts0 +
// Radiance (Specular)
float maxmip = pow(cubeMips+1,2);
float lod = roughness*maxmip;
vec3 r = reflect(surfToEye, normal);
vec3 cubeR = normalize(r);
cubeR = boxProject(wsPos, cubeR, boxPos, boxMin, boxMax);
vec3 radiance = textureLod(radianceCube, cubeR, lod).xyz * (brdf.x + brdf.y);
return radiance;
}
l12 * SHConsts1 * x +
l10 * SHConsts1 * y +
l11 * SHConsts1 * z +
float defineSphereSpaceInfluence(vec3 centroidPosVS, float rad, vec2 atten, vec3 surfPosVS, vec3 norm)
{
// Build light vec, get length, clip pixel if needed
vec3 lightVec = centroidPosVS - surfPosVS;
float lenLightV = length( lightVec );
if (( rad - lenLightV )<0)
return -1;
l20 * SHConsts2 * x*y +
l21 * SHConsts2 * y*z +
l22 * SHConsts3 * (3.0*z*z - 1.0) +
l23 * SHConsts2 * x*z +
l24 * SHConsts4 * (x*x - y*y)
);
// Get the attenuated falloff.
float attn = attenuate( vec4(1,1,1,1), atten, lenLightV );
if ((attn - 1e-6)<0)
return -1;
return vec4(result,1);
// Normalize lightVec
lightVec = lightVec /= lenLightV;
// If we can do dynamic branching then avoid wasting
// fillrate on pixels that are backfacing to the light.
float nDotL = abs(dot( lightVec, norm ));
return saturate( nDotL * attn );
}
float defineBoxSpaceInfluence(vec3 surfPosWS, vec3 probePos, float rad, vec2 atten) //atten currently unused
{
vec3 boxMin = probePos-(vec3(0.5,0.5,0.5)*rad);
vec3 boxMax = probePos+(vec3(0.5,0.5,0.5)*rad);
//Try to clip anything that falls outside our box as well
//TODO: Make it support rotated boxes as well
if(surfPosWS.x > boxMax.x || surfPosWS.y > boxMax.y || surfPosWS.z > boxMax.z ||
surfPosWS.x < boxMin.x || surfPosWS.y < boxMin.y || surfPosWS.z < boxMin.z)
return -1;
float blendVal = 1;
//vec3 atten = min(boxMax-surfPosWS,surfPosWS-boxMin);
//blendVal = min(min(atten.x,atten.y),atten.z);
return blendVal;
}
float defineDepthInfluence(vec3 probePosWS, vec3 surfPosWS, samplerCube radianceCube)
{
//TODO properly: filter out pixels projected uppon by probes behind walls by looking up the depth stored in the probes cubemap alpha
//and comparing legths
vec3 probeToSurf = probePosWS-surfPosWS;
float depthRef = texture(cubeMap, -probeToSurf,0).a*radius;
float dist = length( probeToSurf );
return depthRef-dist;
}
out vec4 OUT_col;
out vec4 OUT_col1;
void main()
{
{
// Compute scene UV
vec3 ssPos = ssPos.xyz / ssPos.w;
//vec4 hardCodedRTParams0 = vec4(0,0.0277777780,1,0.972222209);
vec2 uvScene = getUVFromSSPos( ssPos, rtParams0 );
// Matinfo flags
@ -104,15 +164,15 @@ void main()
vec3 normal = deferredSample.rgb;
float depth = deferredSample.a;
if (depth>0.9999)
OUT_col = vec4(0,0,0,0);
{
OUT_col = vec4(0.0);
OUT_col1 = vec4(0.0);
return;
}
// Need world-space normal.
vec3 wsNormal = tMul(vec4(normal, 1), invViewMat).rgb;
vec4 color = vec4(1, 1, 1, 1);
vec4 ref = vec4(0,0,0,0);
float alpha = 0;
vec3 eyeRay = getDistanceVectorToPlane( -vsFarPlane.w, vsEyeDir.xyz, vsFarPlane );
vec3 viewSpacePos = eyeRay * depth;
@ -120,110 +180,31 @@ void main()
// Use eye ray to get ws pos
vec3 worldPos = vec3(eyePosWorld + wsEyeRay * depth);
float smoothness = min((1.0 - matInfo.b)*11.0 + 1.0, 8.0);//bump up to 8 for finalization
if(useSphereMode>0.0)
{
// Eye ray - Eye -> Pixel
// Build light vec, get length, clip pixel if needed
vec3 lightVec = probeLSPos - viewSpacePos;
float lenLightV = length( lightVec );
clip( lightRange - lenLightV );
// Get the attenuated falloff.
float atten = attenuate( vec4(1,1,1,1), lightAttenuation, lenLightV );
clip( atten - 1e-6 );
// Normalize lightVec
lightVec /= lenLightV;
// If we can do dynamic branching then avoid wasting
// fillrate on pixels that are backfacing to the light.
float nDotL = abs(dot( lightVec, normal ));
float Sat_NL_Att = saturate( nDotL * atten );
vec3 reflectionVec = reflect(wsEyeDir, vec4(wsNormal,nDotL)).xyz;
vec3 nrdir = normalize(reflectionVec);
vec3 rbmax = (bbMax - worldPos.xyz) / nrdir;
vec3 rbmin = (bbMin - worldPos.xyz) / nrdir;
vec3 rbminmax = rbmin;
if (nrdir.x > 0.0)
rbminmax.x = rbmax.x;
if (nrdir.y > 0.0)
rbminmax.y = rbmax.y;
if (nrdir.z > 0.0)
rbminmax.z = rbmax.z;
float fa = min(min(rbminmax.x,rbminmax.y),rbminmax.z);
if (dot( lightVec, normal )<0.0f)
clip(fa);
vec3 posOnBox = worldPos.xyz + nrdir * fa;
reflectionVec = posOnBox - probeWSPos;
//reflectionVec = tMul(probeWSPos,reflectionVec);
ref = vec4(reflectionVec, smoothness);
alpha = Sat_NL_Att;
float blendVal = 1.0;
//clip bounds and (TODO properly: set falloff)
if(useSphereMode>0)
{
blendVal = defineSphereSpaceInfluence(probeLSPos, radius, attenuation, viewSpacePos, normal);
}
else
{
// Build light vec, get length, clip pixel if needed
vec3 lightVec = probeLSPos - viewSpacePos;
float lenLightV = length(lightVec);
//clip(lightRange - lenLightV);
// Normalize lightVec
lightVec /= lenLightV;
// If we can do dynamic branching then avoid wasting
// fillrate on pixels that are backfacing to the light.
float nDotL = abs(dot(lightVec, normal));
vec3 reflectionVec = reflect(wsEyeDir, vec4(wsNormal, nDotL)).xyz;
vec3 nrdir = normalize(reflectionVec);
vec3 rbmax = (bbMax - worldPos.xyz) / nrdir;
vec3 rbmin = (bbMin - worldPos.xyz) / nrdir;
vec3 rbminmax = rbmin;
if (nrdir.x > 0.0)
rbminmax.x = rbmax.x;
if (nrdir.y > 0.0)
rbminmax.y = rbmax.y;
if (nrdir.z > 0.0)
rbminmax.z = rbmax.z;
float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
if (dot(lightVec, normal)<0.0f)
clip(fa);
//Try to clip anything that falls outside our box as well
//TODO: Make it support rotated boxes as well
if(worldPos.x > bbMax.x || worldPos.y > bbMax.y || worldPos.z > bbMax.z ||
worldPos.x < bbMin.x || worldPos.y < bbMin.y || worldPos.z < bbMin.z)
clip(-1);
vec3 posOnBox = worldPos.xyz + nrdir * fa;
reflectionVec = posOnBox - probeWSPos;
ref = vec4(reflectionVec, smoothness);
alpha = 1;
blendVal = defineBoxSpaceInfluence(worldPos, probeWSPos, radius*2, attenuation);
}
color = textureLod(cubeMap, vec3(ref.xyz), float(ref.w));
vec4 specularColor = (color);
vec4 indirectColor = (decodeSH(wsNormal));
color.rgb = lerp(indirectColor.rgb * 1.5, specularColor.rgb * 1.5, matInfo.b);
OUT_col = vec4(color.rgb, alpha);
if (blendVal<0)
{
OUT_col = vec4(0.0);
OUT_col1 = vec4(0.0);
return;
}
//flip me on to have probes filter by depth
//clip(defineDepthInfluence(probeWSPos, worldPos, cubeMap));
//render into the bound space defined above
vec3 surfToEye = normalize(worldPos.xyz-eyePosWorld.xyz);
OUT_col = vec4(iblBoxDiffuse(wsNormal, worldPos, irradianceCubemap, probeWSPos, bbMin, bbMax), blendVal);
OUT_col1 = vec4(iblBoxSpecular(wsNormal, worldPos, 1.0 - matInfo.b, surfToEye, BRDFTexture, cubeMap, probeWSPos, bbMin, bbMax), blendVal);
}

View file

@ -0,0 +1,85 @@
#include "../../../gl/hlslCompat.glsl"
#include "shadergen:/autogenConditioners.h"
#include "farFrustumQuad.glsl"
#include "lightingUtils.glsl"
#include "../../../gl/lighting.glsl"
#include "../../../gl/torque.glsl"
#line 8
in vec4 pos;
in vec4 wsEyeDir;
in vec4 ssPos;
in vec4 vsEyeDir;
uniform sampler2D deferredBuffer;
uniform sampler2D matInfoBuffer;
uniform samplerCube cubeMap;
uniform samplerCube irradianceCubemap;
uniform sampler2D BRDFTexture;
uniform float cubeMips;
uniform vec4 rtParams0;
uniform vec4 vsFarPlane;
uniform float4x4 invViewMat;
uniform vec3 eyePosWorld;
vec3 iblSpecular(vec3 v, vec3 n, float roughness)
{
vec3 R = reflect(v, n);
const float MAX_REFLECTION_LOD = 4.0;
vec3 prefilteredColor = TORQUE_TEXCUBELOD(cubeMap, vec4(R, roughness * MAX_REFLECTION_LOD)).rgb;
vec2 envBRDF = TORQUE_TEX2D(BRDFTexture, vec2(max(dot(n, v), 0.0), roughness)).rg;
return prefilteredColor * (envBRDF.x + envBRDF.y);
//return prefilteredColor;
}
out vec4 OUT_col;
out vec4 OUT_col1;
void main()
{
// Compute scene UV
vec3 ssPos = IN.ssPos.xyz / IN.ssPos.w;
//vec4 hardCodedRTParams0 = vec4(0,0.0277777780,1,0.972222209);
vec2 uvScene = getUVFromSSPos( ssPos, rtParams0 );
// Matinfo flags
vec4 matInfo = TORQUE_TEX2D( matInfoBuffer, uvScene );
// Sample/unpack the normal/z data
vec4 deferredSample = TORQUE_DEFERRED_UNCONDITION( deferredBuffer, uvScene );
vec3 normal = deferredSample.rgb;
float depth = deferredSample.a;
if (depth>0.9999)
return Output;
// Need world-space normal.
vec3 wsNormal = mul(vec4(normal, 1), invViewMat).rgb;
vec3 eyeRay = getDistanceVectorToPlane( -vsFarPlane.w, IN.vsEyeDir.xyz, vsFarPlane );
vec3 wsEyeRay = mul(vec4(eyeRay, 1), invViewMat).rgb;
// Use eye ray to get ws pos
vec3 worldPos = vec3(eyePosWorld + wsEyeRay * depth);
vec3 reflectionVec = reflect(IN.wsEyeDir, vec4(wsNormal,1)).xyz;
float roughness = 1 - matInfo.b;
vec3 v = normalize(eyePosWorld - worldPos);
vec3 irradiance = TORQUE_TEXCUBE(irradianceCubemap, wsNormal).rgb;
vec3 specular = iblSpecular(wsEyeRay, wsNormal, roughness);
OUT_col = vec4(irradiance.rgb, 1);
OUT_col1 = vec4(specular.rgb, 1);
}

View file

@ -73,6 +73,7 @@ uniform vec4 lightParams;
uniform float shadowSoftness;
out vec4 OUT_col;
out vec4 OUT_col1;
void main()
{
@ -87,6 +88,7 @@ void main()
if ( emissive )
{
OUT_col = vec4(0.0, 0.0, 0.0, 0.0);
OUT_col1 = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
@ -182,37 +184,29 @@ void main()
// NOTE: Do not clip on fully shadowed pixels as it would
// cause the hardware occlusion query to disable the shadow.
// Specular term
float specular = 0;
vec3 l = normalize(-lightDirection);
vec3 v = eyeRay;// normalize(eyePosWorld - worldPos.xyz);
vec3 h = normalize(v + l);
float dotNLa = clamp(dot(normal, l), 0.0, 1.0);
float dotNVa = clamp(dot(normal, v), 0.0, 1.0);
float dotNHa = clamp(dot(normal, h), 0.0, 1.0);
float dotHVa = clamp(dot(normal, v), 0.0, 1.0);
float dotLHa = clamp(dot(l, h), 0.0, 1.0);
float roughness = matInfo.g;
float metalness = matInfo.b;
//diffuse
float disDiff = Fr_DisneyDiffuse(dotNVa, dotNLa, dotLHa, roughness);
vec3 diffuse = vec3(disDiff, disDiff, disDiff) / M_PI_F;
//specular
vec3 specular = directSpecular(normal, v, l, roughness, 1.0) * lightColor.rgb;
vec3 lightVec = lightPosition - viewSpacePos;
vec4 real_specular = EvalBDRF( colorSample.rgb,
lightcol,
lightVec,
viewSpacePos,
normal,
1.05-matInfo.b*0.9, //slightly compress roughness to allow for non-baked lighting
matInfo.a );
vec3 lightColorOut = real_specular.rgb * lightBrightness * shadowed* atten;
float Sat_NL_Att = saturate( nDotL * atten * shadowed ) * lightBrightness;
vec4 addToResult = vec4(0.0);
// TODO: This needs to be removed when lightmapping is disabled
// as its extra work per-pixel on dynamic lit scenes.
//
// Special lightmapping pass.
if ( lightMapParams.a < 0.0 )
{
// This disables shadows on the backsides of objects.
shadowed = nDotL < 0.0f ? 1.0f : shadowed;
Sat_NL_Att = 1.0f;
shadowed = mix( 1.0f, shadowed, atten );
lightColorOut = vec3(shadowed);
specular *= lightBrightness;
addToResult = ( 1.0 - shadowed ) * abs(lightMapParams);
}
OUT_col = vec4((lightColorOut*Sat_NL_Att+subsurface*(1.0-Sat_NL_Att)+addToResult.rgb),real_specular.a);
if (nDotL<0) shadowed = 0;
float Sat_NL_Att = saturate( nDotL * shadowed ) * lightBrightness;
//output
OUT_col = float4(diffuse * lightBrightness, Sat_NL_Att*shadowed);
OUT_col1 = float4(specular * lightBrightness, Sat_NL_Att*shadowed);
}

View file

@ -191,17 +191,11 @@ vec4 AL_VectorLightShadowCast( sampler2D _sourceshadowMap,
}
out vec4 OUT_col;
out vec4 OUT_col1;
void main()
{
// Matinfo flags
float4 matInfo = texture( matInfoBuffer, uv0 );
//early out if emissive
bool emissive = getFlag( matInfo.r, 0 );
if ( emissive )
{
OUT_col = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
float4 matInfo = texture( matInfoBuffer, uv0 );
vec4 colorSample = texture( colorBuffer, uv0 );
vec3 subsurface = vec3(0.0,0.0,0.0);
@ -290,30 +284,29 @@ void main()
#endif // !NO_SHADOW
// Specular term
vec3 viewSpacePos = vsEyeRay * depth;
vec4 real_specular = EvalBDRF( colorSample.rgb,
lightColor.rgb,
normalize( -lightDirection ),
viewSpacePos,
normal,
1.0-matInfo.b,
matInfo.a );
vec3 lightColorOut = real_specular.rgb * lightBrightness * shadowed;
vec3 l = normalize(-lightDirection);
vec3 v = normalize(eyePosWorld - worldPos.xyz);
vec3 h = normalize(v + l);
float dotNLa = clamp(dot(normal, l), 0.0, 1.0);
float dotNVa = clamp(dot(normal, v), 0.0, 1.0);
float dotNHa = clamp(dot(normal, h), 0.0, 1.0);
float dotHVa = clamp(dot(normal, v), 0.0, 1.0);
float dotLHa = clamp(dot(l, h), 0.0, 1.0);
float roughness = matInfo.g;
float metalness = matInfo.b;
//diffuse
//float dotNL = clamp(dot(normal,l), 0.0, 1.0);
float disDiff = Fr_DisneyDiffuse(dotNVa, dotNLa, dotLHa, roughness);
vec3 diffuse = vec3(disDiff, disDiff, disDiff) / M_PI_F;// alternative: (lightColor * dotNL) / Pi;
//specular
vec3 specular = directSpecular(normal, v, l, roughness, 1.0) * lightColor.rgb;
float Sat_NL_Att = saturate( dotNL * shadowed ) * lightBrightness;
float Sat_NdotV = saturate(dot(normalize(-vsEyeRay), normal));
vec4 addToResult = ( lightAmbient * (1 - ambientCameraFactor)) + ( lightAmbient * ambientCameraFactor * Sat_NdotV );
float finalShadowed = shadowed;
// Sample the AO texture.
#ifdef USE_SSAO_MASK
float ao = 1.0 - texture( ssaoMask, viewportCoordToRenderTarget( uv0.xy, rtParams3 ) ).r;
addToResult *= ao;
#endif
#ifdef PSSM_DEBUG_RENDER
lightColorOut = debugColor;
#endif
OUT_col = vec4(matInfo.g*(lightColorOut*Sat_NL_Att+subsurface*(1.0-Sat_NL_Att)+addToResult.rgb),real_specular.a);
//output
OUT_col = float4(diffuse * (lightBrightness), dotNLa*shadowed);
OUT_col1 = float4(specular * (lightBrightness), dotNLa*shadowed);
}

View file

@ -30,6 +30,7 @@ uniform float radius;
uniform float2 attenuation;
uniform float4x4 invViewMat;
uniform float4x4 worldToObj;
uniform float3 eyePosWorld;
uniform float3 bbMin;
@ -128,10 +129,14 @@ float defineBoxSpaceInfluence(float3 surfPosWS, float3 probePos, float rad, floa
{
float3 boxMin = probePos-(float3(0.5,0.5,0.5)*rad);
float3 boxMax = probePos+(float3(0.5,0.5,0.5)*rad);
//rotated boxes
float3 surfPosLS = mul( worldToObj, float4(surfPosWS,1.0)).xyz;
//Try to clip anything that falls outside our box as well
//TODO: Make it support rotated boxes as well
if(surfPosWS.x > boxMax.x || surfPosWS.y > boxMax.y || surfPosWS.z > boxMax.z ||
surfPosWS.x < boxMin.x || surfPosWS.y < boxMin.y || surfPosWS.z < boxMin.z)
//was surfPosWS
if(surfPosLS.x > boxMax.x || surfPosLS.y > boxMax.y || surfPosLS.z > boxMax.z ||
surfPosLS.x < boxMin.x || surfPosLS.y < boxMin.y || surfPosLS.z < boxMin.z)
return -1;
float blendVal = 1;

View file

@ -79,7 +79,7 @@ struct PS_OUTPUT
float4 spec: TORQUE_TARGET1;
};
PS_OUTPUT main( ConvexConnectP IN ) : TORQUE_TARGET0
PS_OUTPUT main( ConvexConnectP IN )
{
PS_OUTPUT Output = (PS_OUTPUT)0;
// Compute scene UV

View file

@ -203,7 +203,11 @@ PS_OUTPUT main(FarFrustumQuadConnectP IN)
PS_OUTPUT Output = (PS_OUTPUT)0;
// Matinfo flags
float4 matInfo = TORQUE_TEX2D(matInfoBuffer, IN.uv0);
bool emissive = getFlag( matInfo.r, 0 );
if (emissive)
{
return Output;
}
float4 colorSample = TORQUE_TEX2D(colorBuffer, IN.uv0);
float3 subsurface = float3(0.0, 0.0, 0.0);
if (getFlag(matInfo.r, 1))

View file

@ -24,16 +24,16 @@
#define pkDepthBitShft 65536.0
#define pkDepthChanMax 256.0
#define bias -0.5/255.0
#define coeff 0.9999991
//#define coeff 1.0
#define gbias -0.5/255.0
#define gcoeff 0.9999991
//#define gcoeff 1.0
vec4 encodeShadowMap( float depth )
{
#if defined(SM_Fmt_R8G8B8A8)
return frac( vec4(1.0, 255.0, 65025.0, 160581375.0) * depth ) + vec4(bias);
return frac( vec4(1.0, 255.0, 65025.0, 160581375.0) * depth ) + vec4(gbias);
//float4 packedValue = frac((depth / coeff) * float4(16777216.0, 65536.0, 256.0, 1.0));
//float4 packedValue = frac((depth / gcoeff) * float4(16777216.0, 65536.0, 256.0, 1.0));
//return (packedValue - packedValue.xxyz * float4(0, 1.0 / 256, 1.0 / 256, 1.0 / 256));
#else
return vec4(depth);