defines and alters a series of material features for deferred shading in order to define a fully fleshed out multiple render target gbuffer patterned after the general principles outlined http://www.catalinzima.com/xna/tutorials/deferred-rendering-in-xna/creating-the-g-buffer/ (though I cannot stress enough *not* using the identical layout)

script:
removes dead material features (ie: those that never functioned to begin with)

shader:
bool getFlag(float flags, int num) definition for retreiving data from the 3rd (matinfo) gbuffer slot's red channel (more on that shortly)

purpose:
_A)_ Small primer on how material features function:
When a https://github.com/GarageGames/Torque3D/search?utf8=%E2%9C%93&q=_determineFeatures call is executed, certain conditions trigger a given .addFeature(MFT_SOMEFEATURE) call based upon material definition entries, be it a value, a texture reference, or even the presence or lack thereof for another feature. In general terms, the first to be executed is ProcessedShaderMaterial::_determineFeatures followed by ProcessedPrePassMaterial::_determineFeatures. The next commit will provide the bindings there. For now it's enough to understand that one of those two will trigger the shadergen subsystem, when rendering a material, to check it's associated list of features and spit out a shader if one is not already defined, or reference a pre-existing one that includes codelines determined by that list of features.

Relevant execution of this is as follows:
DeclareFeatureType( MFT_DeferredDiffuseMap ); - Name
ImplementFeatureType( MFT_DeferredDiffuseMap, MFG_Texture, 2.0f, false ); - Codeline Insertion Order
FEATUREMGR->registerFeature( MFT_DeferredDiffuseMap, new DeferredDiffuseMapHLSL ); - Hook to class which actually generates code
alternately    FEATUREMGR->registerFeature( MFT_Imposter, new NamedFeatureHLSL( "Imposter" ) ); - a simple feature that serves no purpose further than as a test of it's existence (to modify other features for instance)

class DeferredDiffuseMapHLSL : public ShaderFeatureHLSL - Class definition
{
getName  -embeded in the proceedural shader as a remline both up top and before actual code insertions
processPix  - pixel shader codeline insertions
getOutputTargets - used to determine which buffer is written to (assumes only one. depending on branched logic, older features that may be run for either forward or deferred rendering depending on circumstance may have a logical switch based on additional feature flags. as an example:  TerrainBaseMapFeatHLSL::getOutputTargets)
getResources - associated with the Resources struct, closely aligned with the hardware regestry
 getBlendOp - used to determine what blend operation to use if a material requires a second pass (defaults to overwriting)
setTexData - ???
processVert - vertex shader codeline insertions
};

_B)_
The resultant Gbuffer layout defined by the previous commit therefore is as follows:
defaultrendertarget (referred to in shaders as out.col or col depending on GFX plugin) contains either lighting and normal data, or color data depending on if it is used in a deferred or forward lit manner (note for forward lit, this data is replaced as a second step with color. why custommaterials have traditionally had problems with lighting)
color1 (referred to in shaders as out.col1 or col1 depending on GFX plugin) RGB color data and an A for blending operations (including transparency)
color2 (referred to in shaders as out.col2 or col2 depending on GFX plugin) contains:
 red channel comprising material flags such as metalness, emissive, ect,
 green channel for translucency (light shining through, as oposed to  see-through transparency), blue for
 blue for specular strength (how much light influences net color)
 alpha for specular power (generally how reflective/glossy an object is)

long term purpose:
further down the line, these will be used to condition data for use with a PBR subsystem, with further corrections to the underlying mathematics, strength being replaced by roughness, and power by metalness
This commit is contained in:
Azaezel 2016-02-16 02:23:23 -06:00
parent 5b5c6b9907
commit 196b214eae
25 changed files with 1257 additions and 486 deletions

View file

@ -845,12 +845,22 @@ void DiffuseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
output = meta;
}
U32 DiffuseMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
{
return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}
void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
// grab connector texcoord register
Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
//determine output target
ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
if (fd.features[MFT_isDeferred])
targ = ShaderFeature::RenderTarget1;
// create texture var
Var *diffuseMap = new Var;
diffuseMap->setType( "sampler2D" );
@ -877,7 +887,7 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
if (!fd.features[MFT_Imposter])
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul ) ) );
meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
}
else if(fd.features[MFT_DiffuseMapAtlas])
{
@ -944,7 +954,7 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
if(!fd.features[MFT_PrePassConditioner])
{
meta->addStatement(new GenOp(" @ = float4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul)));
meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
return;
}
#endif
@ -962,15 +972,15 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
if (!fd.features[MFT_Imposter])
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul)));
meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
}
else
{
meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
if (!fd.features[MFT_Imposter])
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul)));
}
meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
}
}
ShaderFeature::Resources DiffuseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
@ -1087,6 +1097,11 @@ void OverlayTexFeatHLSL::setTexData( Material::StageData &stageDat,
// Diffuse color
//****************************************************************************
U32 DiffuseFeatureHLSL::getOutputTargets(const MaterialFeatureData &fd) const
{
return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}
void DiffuseFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
@ -1096,8 +1111,34 @@ void DiffuseFeatureHLSL::processPix( Vector<ShaderComponent*> &componentList,
diffuseMaterialColor->uniform = true;
diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
MultiLine * meta = new MultiLine;
meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, Material::Mul ) ) );
MultiLine* meta = new MultiLine;
Var *col = (Var*)LangElement::find("col");
ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
if (fd.features[MFT_isDeferred])
{
targ = ShaderFeature::RenderTarget1;
col = (Var*)LangElement::find("col1");
MultiLine * meta = new MultiLine;
if (!col)
{
// create color var
col = new Var;
col->setType("fragout");
col->setName(getOutputTargetVarName(targ));
col->setStructName("OUT");
meta->addStatement(new GenOp(" @ = float4(1.0);\r\n", col));
}
}
Material::BlendOp op;
if (fd.features[MFT_DiffuseMap])
op = Material::Mul;
else
op = Material::None;
meta->addStatement( new GenOp( " @;\r\n", assignColor( diffuseMaterialColor, op, NULL, targ ) ) );
output = meta;
}
@ -1243,8 +1284,8 @@ void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
MultiLine *meta = new MultiLine;
if( fd.features[MFT_LightbufferMRT] )
{
meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
}
else
meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
@ -1276,7 +1317,7 @@ void LightmapFeatHLSL::setTexData( Material::StageData &stageDat,
U32 LightmapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
{
return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
}
//****************************************************************************
@ -1370,8 +1411,8 @@ void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// Assign to proper render target
if( fd.features[MFT_LightbufferMRT] )
{
meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
}
else
meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
@ -1404,7 +1445,7 @@ void TonemapFeatHLSL::setTexData( Material::StageData &stageDat,
U32 TonemapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
{
return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
}
//****************************************************************************
@ -1519,8 +1560,8 @@ void VertLitHLSL::processPix( Vector<ShaderComponent*> &componentList,
// Output the color
if ( fd.features[MFT_LightbufferMRT] )
{
meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget1 ) ) );
meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ) ) );
meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
}
else
meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
@ -1530,7 +1571,7 @@ void VertLitHLSL::processPix( Vector<ShaderComponent*> &componentList,
U32 VertLitHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
{
return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
}
//****************************************************************************
@ -1569,7 +1610,10 @@ void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// and a simple multiplication with the detail map.
LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
if ( fd.features[MFT_isDeferred])
output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
else
output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
}
ShaderFeature::Resources DetailFeatHLSL::getResources( const MaterialFeatureData &fd )
@ -1694,7 +1738,7 @@ void ReflectCubeFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
cubeNormal->setType( "float3" );
LangElement *cubeNormDecl = new DecOp( cubeNormal );
meta->addStatement( new GenOp( " @ = normalize( mul(@, float4(normalize(@),0.0)).xyz );\r\n",
meta->addStatement( new GenOp( " @ = normalize( mul(@, float4(normalize(@),0.0)).xyz );\r\n",
cubeNormDecl, cubeTrans, inNormal ) );
// grab the eye position
@ -1767,9 +1811,14 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
}
else
{
glossColor = (Var*) LangElement::find( "diffuseColor" );
if( !glossColor )
glossColor = (Var*) LangElement::find( "bumpNormal" );
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)
glossColor = (Var*)LangElement::find("bumpNormal");
}
// grab connector texcoord register
@ -1795,15 +1844,41 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
//else
if ( fd.materialFeatures[MFT_RTLighting] )
attn =(Var*)LangElement::find("d_NL_Att");
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 );
if (fd.features[MFT_DeferredSpecMap])
texCube = new GenOp("texCUBElod( @, float4(@, (@.a*5)) )", cubeMap, reflectVec, matinfo);
else
texCube = new GenOp("texCUBElod( @, float4(@, (@.a/4)) )", cubeMap, reflectVec, matinfo);
}
else
if (glossColor) //failing that, rtry and find color data
texCube = new GenOp("texCUBElod( @, float4(@, @.a*5))", cubeMap, reflectVec, glossColor);
else //failing *that*, just draw the cubemap
texCube = new GenOp("texCUBE( @, @)", cubeMap, reflectVec);
LangElement *texCube = new GenOp( "texCUBE( @, @ )", cubeMap, reflectVec );
LangElement *lerpVal = NULL;
Material::BlendOp blendOp = Material::LerpAlpha;
// Note that the lerpVal needs to be a float4 so that
// it will work with the LerpAlpha blend.
if ( glossColor )
if (matinfo)
{
if (attn)
lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
else
lerpVal = new GenOp("@", matinfo);
}
else if ( glossColor )
{
if ( attn )
lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
@ -1817,8 +1892,16 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
else
blendOp = Material::Mul;
}
meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
if (fd.features[MFT_isDeferred])
{
Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
if (fd.features[MFT_DeferredSpecMap])
meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
else
meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b*128/5));\r\n", targ, targ, texCube, lerpVal));
}
else
meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
output = meta;
}
@ -1865,9 +1948,9 @@ void ReflectCubeFeatHLSL::setTexData( Material::StageData &stageDat,
{
passData.mSamplerNames[ texIndex ] = "bumpMap";
passData.mTexSlot[ texIndex++ ].texObject = tex;
}
}
}
}
if( stageDat.getCubemap() )
{
@ -2347,7 +2430,9 @@ void AlphaTestHLSL::processPix( Vector<ShaderComponent*> &componentList,
}
// If we don't have a color var then we cannot do an alpha test.
Var *color = (Var*)LangElement::find( "col" );
Var *color = (Var*)LangElement::find( "col1" );
if (!color)
color = (Var*)LangElement::find("col");
if ( !color )
{
output = NULL;
@ -2714,3 +2799,16 @@ void ImposterVertFeatureHLSL::determineFeature( Material *material,
outFeatureData->features.addFeature( MFT_ImposterVert );
}
//****************************************************************************
// Vertex position
//****************************************************************************
void DeferredSkyHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
Var *outPosition = (Var*)LangElement::find( "hpos" );
MultiLine *meta = new MultiLine;
meta->addStatement( new GenOp( " @.w = @.z;\r\n", outPosition, outPosition ) );
output = meta;
}