Core implementation of Physical Based Rendering.

This commit is contained in:
Areloch 2018-09-15 20:19:57 -05:00
parent 54f1d8c18e
commit b4a1d18f42
148 changed files with 4464 additions and 1016 deletions

View file

@ -140,8 +140,20 @@ void SpecularMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const
LangElement *texOp = new GenOp( "texture(@, @)", specularMap, texCoord );
Var *specularColor = new Var( "specularColor", "vec4" );
Var *metalness = (Var*)LangElement::find("metalness");
if (!metalness) metalness = new Var("metalness", "float");
Var *smoothness = (Var*)LangElement::find("smoothness");
if (!smoothness) smoothness = new Var("smoothness", "float");
MultiLine * meta = new MultiLine;
output = new GenOp( " @ = @;\r\n", new DecOp( specularColor ), texOp );
meta->addStatement(new GenOp(" @ = @.r;\r\n", new DecOp(smoothness), texOp));
meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(metalness), texOp));
if (fd.features[MFT_InvertSmoothness])
meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
meta->addStatement(new GenOp(" @ = @.ggga;\r\n", new DecOp(specularColor), texOp));
output = meta;
}
ShaderFeature::Resources SpecularMapGLSL::getResources( const MaterialFeatureData &fd )

View file

@ -1794,12 +1794,10 @@ void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
}
}
else
if (!glossColor)
{
if (fd.features[MFT_isDeferred])
glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
if (!glossColor)
glossColor = (Var*)LangElement::find("specularColor");
if (!glossColor)
glossColor = (Var*)LangElement::find("diffuseColor");
if (!glossColor)
@ -1821,6 +1819,12 @@ void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
cubeMap->sampler = true;
cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var *cubeMips = new Var;
cubeMips->setType("float");
cubeMips->setName("cubeMips");
cubeMips->uniform = true;
cubeMips->constSortPos = cspPotentialPrimitive;
// TODO: Restore the lighting attenuation here!
Var *attn = NULL;
//if ( fd.materialFeatures[MFT_DynamicLight] )
@ -1831,18 +1835,12 @@ void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
LangElement *texCube = NULL;
Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
//first try and grab the gbuffer
if (fd.features[MFT_isDeferred] && matinfo)
{
if (fd.features[MFT_DeferredSpecMap])
texCube = new GenOp("textureLod( @, @, (@.a*5) )", cubeMap, reflectVec, matinfo);
else
texCube = new GenOp("textureLod( @, @, ((1.0-@.a)*6) )", cubeMap, reflectVec, matinfo);
}
else if(glossColor) //failing that, rtry and find color data
texCube = new GenOp("textureLod( @, @, @.a*5)", cubeMap, reflectVec, glossColor);
else
Var *smoothness = (Var*)LangElement::find("smoothness");
if (smoothness) //try to grab smoothness directly
texCube = new GenOp("textureLod( @, @, min((1.0 - @)*@ + 1.0, @))", cubeMap, reflectVec, smoothness, cubeMips, cubeMips);
else if (glossColor) //failing that, try and find color data
texCube = new GenOp("textureLod( @, @, min((1.0 - @.b)*@ + 1.0, @))", cubeMap, reflectVec, glossColor, cubeMips, cubeMips);
else //failing *that*, just draw the cubemap
texCube = new GenOp("texture( @, @)", cubeMap, reflectVec);
LangElement *lerpVal = NULL;
@ -1872,13 +1870,34 @@ void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
else
blendOp = Material::Mul;
}
Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
if (fd.features[MFT_isDeferred])
{
Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
//metalness: black(0) = color, white(1) = reflection
if (fd.features[MFT_ToneMap])
meta->addStatement(new GenOp(" @ *= @;\r\n", targ, texCube));
else
meta->addStatement(new GenOp(" @ = @;\r\n", targ, texCube));
}
else
meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
{
meta->addStatement(new GenOp(" //forward lit cubemapping\r\n"));
targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
Var *metalness = (Var*)LangElement::find("metalness");
if (metalness)
{
Var *dColor = new Var("dColor", "vec3");
Var *envColor = new Var("envColor", "vec3");
meta->addStatement(new GenOp(" @ = @.rgb - (@.rgb * @);\r\n", new DecOp(dColor), targ, targ, metalness));
meta->addStatement(new GenOp(" @ = @.rgb*(@).rgb;\r\n", new DecOp(envColor), targ, texCube));
}
else if (lerpVal)
meta->addStatement(new GenOp(" @ *= vec4(@.rgb*@.a, @.a);\r\n", targ, texCube, lerpVal, targ));
else
meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", targ, texCube));
}
output = meta;
}
@ -2109,28 +2128,40 @@ void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
lightSpotFalloff->uniform = true;
lightSpotFalloff->constSortPos = cspPotentialPrimitive;
Var *specularPower = new Var( "specularPower", "float" );
specularPower->uniform = true;
specularPower->constSortPos = cspPotentialPrimitive;
Var *specularColor = (Var*)LangElement::find( "specularColor" );
if ( !specularColor )
Var *smoothness = (Var*)LangElement::find("smoothness");
if (!fd.features[MFT_SpecularMap])
{
specularColor = new Var( "specularColor", "vec4" );
specularColor->uniform = true;
specularColor->constSortPos = cspPotentialPrimitive;
if (!smoothness)
{
smoothness = new Var("smoothness", "float");
smoothness->uniform = true;
smoothness->constSortPos = cspPotentialPrimitive;
}
}
Var *metalness = (Var*)LangElement::find("metalness");
if (!fd.features[MFT_SpecularMap])
{
if (!metalness)
{
metalness = new Var("metalness", "float");
metalness->uniform = true;
metalness->constSortPos = cspPotentialPrimitive;
}
}
Var *albedo = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
Var *ambient = new Var( "ambient", "vec4" );
ambient->uniform = true;
ambient->constSortPos = cspPass;
// Calculate the diffuse shading and specular powers.
meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
" @, @, @, @, @, @, @, @,\r\n"
" @, @, @, @, @, @, @, @, @,\r\n"
" @, @ );\r\n",
wsView, wsPosition, wsNormal, lightMask,
inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, smoothness, metalness, albedo,
rtShading, specular ) );
// Apply the lighting to the diffuse color.

View file

@ -63,8 +63,10 @@ void _initShaderGenGLSL( ShaderGen *shaderGen )
FEATUREMGR->registerFeature( MFT_NormalMap, new BumpFeatGLSL );
FEATUREMGR->registerFeature( MFT_DetailNormalMap, new NamedFeatureGLSL( "Detail Normal Map" ) );
FEATUREMGR->registerFeature( MFT_DetailMap, new DetailFeatGLSL );
FEATUREMGR->registerFeature( MFT_StaticCubemap, new NamedFeatureGLSL( "Static Cubemap" ) );
FEATUREMGR->registerFeature( MFT_CubeMap, new ReflectCubeFeatGLSL );
FEATUREMGR->registerFeature( MFT_PixSpecular, new PixelSpecularGLSL );
FEATUREMGR->registerFeature( MFT_InvertSmoothness, new NamedFeatureGLSL("Roughest = 1.0"));
FEATUREMGR->registerFeature( MFT_SpecularMap, new SpecularMapGLSL );
FEATUREMGR->registerFeature( MFT_AccuMap, new AccuTexFeatGLSL );
FEATUREMGR->registerFeature( MFT_GlossMap, new NamedFeatureGLSL( "Gloss Map" ) );
@ -104,7 +106,6 @@ void _initShaderGenGLSL( ShaderGen *shaderGen )
FEATUREMGR->registerFeature( MFT_DeferredSpecMap, new DeferredSpecMapGLSL );
FEATUREMGR->registerFeature( MFT_DeferredSpecVars, new DeferredSpecVarsGLSL );
FEATUREMGR->registerFeature( MFT_DeferredMatInfoFlags, new DeferredMatInfoFlagsGLSL );
FEATUREMGR->registerFeature( MFT_DeferredEmptySpec, new DeferredEmptySpecGLSL );
FEATUREMGR->registerFeature( MFT_SkyBox, new NamedFeatureGLSL( "skybox" ) );
FEATUREMGR->registerFeature( MFT_HardwareSkinning, new HardwareSkinningFeatureGLSL );
}

View file

@ -148,17 +148,23 @@ void SpecularMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const
specularMapTex->uniform = true;
specularMapTex->texture = true;
specularMapTex->constNum = specularMap->constNum;
LangElement *texOp = new GenOp("@.Sample(@, @)", specularMapTex, specularMap, texCoord);
LangElement *texOp = NULL;
if (specularMapTex)
texOp = new GenOp("@.Sample(@, @)", specularMapTex, specularMap, texCoord);
else
texOp = new GenOp("tex2D(@, @)", specularMap, texCoord);
Var *specularColor = new Var( "specularColor", "float4" );
Var *metalness = (Var*)LangElement::find("metalness");
if (!metalness) metalness = new Var("metalness", "float");
Var *smoothness = (Var*)LangElement::find("smoothness");
if (!smoothness) smoothness = new Var("smoothness", "float");
MultiLine * meta = new MultiLine;
Var *specularColor = new Var("specularColor", "float4");
meta->addStatement(new GenOp(" @ = @.r;\r\n", new DecOp(smoothness), texOp));
meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(metalness), texOp));
output = new GenOp(" @ = @;\r\n", new DecOp(specularColor), texOp);
if (fd.features[MFT_InvertSmoothness])
meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", smoothness, smoothness));
meta->addStatement(new GenOp(" @ = @.ggga;\r\n", new DecOp(specularColor), texOp));
output = meta;
}
ShaderFeature::Resources SpecularMapHLSL::getResources( const MaterialFeatureData &fd )

View file

@ -1842,12 +1842,12 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
meta->addStatement(new GenOp(" @ = @.Sample( @, @ );\r\n", colorDecl, glowMapTex, newMap, inTex));
}
}
else
if (!glossColor)
{
if (fd.features[MFT_isDeferred])
glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
if (!glossColor)
glossColor = (Var*)LangElement::find("specularColor");
//if (!glossColor)
//glossColor = (Var*)LangElement::find("specularColor");
if (!glossColor)
glossColor = (Var*)LangElement::find("diffuseColor");
if (!glossColor)
@ -1876,6 +1876,12 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
cubeMapTex->texture = true;
cubeMapTex->constNum = cubeMap->constNum;
Var *cubeMips = new Var;
cubeMips->setType("float");
cubeMips->setName("cubeMips");
cubeMips->uniform = true;
cubeMips->constSortPos = cspPotentialPrimitive;
// TODO: Restore the lighting attenuation here!
Var *attn = NULL;
//if ( fd.materialFeatures[MFT_DynamicLight] )
@ -1886,32 +1892,19 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
LangElement *texCube = NULL;
Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
//first try and grab the gbuffer
if (fd.features[MFT_isDeferred] && matinfo)
{
// Cube LOD level = (1.0 - Roughness) * 8
// mip_levle = min((1.0 - u_glossiness)*11.0 + 1.0, 8.0)
//LangElement *texCube = new GenOp( "texCUBElod( @, float4(@, min((1.0 - (@ / 128.0)) * 11.0 + 1.0, 8.0)) )", cubeMap, reflectVec, specPower );
Var *smoothness = (Var*)LangElement::find("smoothness");
if (fd.features[MFT_DeferredSpecMap])
{
texCube = new GenOp("@.SampleLevel( @, @, @.a*5)", cubeMapTex, cubeMap, reflectVec, matinfo);
}
else
{
texCube = new GenOp("@.SampleLevel( @, @, (1.0-@.a)*6 )", cubeMapTex, cubeMap, reflectVec, matinfo);
}
}
else
if (smoothness) //try to grab smoothness directly
{
if (glossColor) //failing that, rtry and find color data
{
texCube = new GenOp("@.SampleLevel( @, @, @.a*5)", cubeMapTex, cubeMap, reflectVec, glossColor);
}
else //failing *that*, just draw the cubemap
{
texCube = new GenOp("@.Sample( @, @ )", cubeMapTex, cubeMap, reflectVec);
}
texCube = new GenOp("@.SampleLevel( @, float3(@).rgb, min((1.0 - @)*@ + 1.0, @))", cubeMapTex, cubeMap, reflectVec, smoothness, cubeMips, cubeMips);
}
else if (glossColor)//failing that, try and find color data
{
texCube = new GenOp("@.SampleLevel( @, float3(@).rgb, min((1.0 - @.b)*@ + 1.0, @))", cubeMapTex, cubeMap, reflectVec, glossColor, cubeMips, cubeMips);
}
else //failing *that*, just draw the cubemap
{
texCube = new GenOp("@.Sample( @, @ )", cubeMapTex, cubeMap, reflectVec);
}
LangElement *lerpVal = NULL;
@ -1941,13 +1934,35 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
else
blendOp = Material::Mul;
}
Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
if (fd.features[MFT_isDeferred])
{
Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
//metalness: black(0) = color, white(1) = reflection
if (fd.features[MFT_ToneMap])
meta->addStatement(new GenOp(" @ *= @;\r\n", targ, texCube));
else
meta->addStatement(new GenOp(" @ = @;\r\n", targ, texCube));
}
else
meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
{
meta->addStatement(new GenOp(" //forward lit cubemapping\r\n"));
targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
Var *metalness = (Var*)LangElement::find("metalness");
if (metalness)
{
Var *dColor = new Var("dColor", "float3");
Var *reflectColor = new Var("reflectColor", "float3");
meta->addStatement(new GenOp(" @ = @.rgb - (@.rgb * @);\r\n", new DecOp(dColor), targ, targ, metalness));
meta->addStatement(new GenOp(" @ = @.rgb*(@).rgb*@;\r\n", new DecOp(reflectColor), targ, texCube, metalness));
meta->addStatement(new GenOp(" @.rgb = @+@;\r\n", targ, dColor, reflectColor));
}
else if (lerpVal)
meta->addStatement(new GenOp(" @ *= float4(@.rgb*@.a, @.a);\r\n", targ, texCube, lerpVal, targ));
else
meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", targ, texCube));
}
output = meta;
}
@ -2177,29 +2192,41 @@ void RTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "float4" );
lightSpotFalloff->uniform = true;
lightSpotFalloff->constSortPos = cspPotentialPrimitive;
Var *specularPower = new Var( "specularPower", "float" );
specularPower->uniform = true;
specularPower->constSortPos = cspPotentialPrimitive;
Var *specularColor = (Var*)LangElement::find( "specularColor" );
if ( !specularColor )
Var *smoothness = (Var*)LangElement::find("smoothness");
if (!fd.features[MFT_SpecularMap])
{
specularColor = new Var( "specularColor", "float4" );
specularColor->uniform = true;
specularColor->constSortPos = cspPotentialPrimitive;
if (!smoothness)
{
smoothness = new Var("smoothness", "float");
smoothness->uniform = true;
smoothness->constSortPos = cspPotentialPrimitive;
}
}
Var *metalness = (Var*)LangElement::find("metalness");
if (!fd.features[MFT_SpecularMap])
{
if (!metalness)
{
metalness = new Var("metalness", "float");
metalness->uniform = true;
metalness->constSortPos = cspPotentialPrimitive;
}
}
Var *albedo = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
Var *ambient = new Var( "ambient", "float4" );
ambient->uniform = true;
ambient->constSortPos = cspPass;
// Calculate the diffuse shading and specular powers.
meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
" @, @, @, @, @, @, @, @,\r\n"
" @, @, @, @, @, @, @, @, @,\r\n"
" @, @ );\r\n",
wsView, wsPosition, wsNormal, lightMask,
inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, smoothness, metalness, albedo,
rtShading, specular ) );
// Apply the lighting to the diffuse color.

View file

@ -62,8 +62,10 @@ void _initShaderGenHLSL( ShaderGen *shaderGen )
FEATUREMGR->registerFeature( MFT_NormalMap, new BumpFeatHLSL );
FEATUREMGR->registerFeature( MFT_DetailNormalMap, new NamedFeatureHLSL( "Detail Normal Map" ) );
FEATUREMGR->registerFeature( MFT_DetailMap, new DetailFeatHLSL );
FEATUREMGR->registerFeature( MFT_StaticCubemap, new NamedFeatureHLSL( "Static Cubemap" ) );
FEATUREMGR->registerFeature( MFT_CubeMap, new ReflectCubeFeatHLSL );
FEATUREMGR->registerFeature( MFT_PixSpecular, new PixelSpecularHLSL );
FEATUREMGR->registerFeature( MFT_InvertSmoothness, new NamedFeatureHLSL( "Roughest = 1.0" ) );
FEATUREMGR->registerFeature( MFT_IsTranslucent, new NamedFeatureHLSL( "Translucent" ) );
FEATUREMGR->registerFeature( MFT_IsTranslucentZWrite, new NamedFeatureHLSL( "Translucent ZWrite" ) );
FEATUREMGR->registerFeature( MFT_Visibility, new VisibilityFeatHLSL );
@ -105,7 +107,6 @@ void _initShaderGenHLSL( ShaderGen *shaderGen )
FEATUREMGR->registerFeature( MFT_DeferredSpecMap, new DeferredSpecMapHLSL );
FEATUREMGR->registerFeature( MFT_DeferredSpecVars, new DeferredSpecVarsHLSL );
FEATUREMGR->registerFeature( MFT_DeferredMatInfoFlags, new DeferredMatInfoFlagsHLSL );
FEATUREMGR->registerFeature( MFT_DeferredEmptySpec, new DeferredEmptySpecHLSL );
FEATUREMGR->registerFeature( MFT_SkyBox, new NamedFeatureHLSL( "skybox" ) );
FEATUREMGR->registerFeature( MFT_HardwareSkinning, new HardwareSkinningFeatureHLSL );
}

View file

@ -65,8 +65,8 @@ const String ShaderGenVars::lightSpotDir("$inLightSpotDir");
const String ShaderGenVars::lightSpotAngle("$inLightSpotAngle");
const String ShaderGenVars::lightSpotFalloff("$inLightSpotFalloff");
const String ShaderGenVars::specularColor("$specularColor");
const String ShaderGenVars::specularPower("$specularPower");
const String ShaderGenVars::specularStrength("$specularStrength");
const String ShaderGenVars::smoothness("$smoothness");
const String ShaderGenVars::metalness("$metalness");
// These are ignored by the D3D layers.
const String ShaderGenVars::fogMap("$fogMap");

View file

@ -78,8 +78,8 @@ struct ShaderGenVars
const static String lightSpotAngle;
const static String lightSpotFalloff;
const static String specularColor;
const static String specularPower;
const static String specularStrength;
const static String smoothness;
const static String metalness;
// Textures
const static String fogMap;