mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-03-09 15:30:41 +00:00
engine:
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:
parent
5b5c6b9907
commit
196b214eae
25 changed files with 1257 additions and 486 deletions
|
|
@ -26,6 +26,7 @@
|
|||
#include "terrain/terrFeatureTypes.h"
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
#include "materials/materialFeatureData.h"
|
||||
#include "materials/processedMaterial.h"
|
||||
#include "gfx/gfxDevice.h"
|
||||
#include "shaderGen/langElement.h"
|
||||
#include "shaderGen/shaderOp.h"
|
||||
|
|
@ -48,6 +49,10 @@ namespace
|
|||
FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureGLSL( "Terrain Side Projection" ) );
|
||||
FEATUREMGR->registerFeature( MFT_TerrainAdditive, new TerrainAdditiveFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_DeferredTerrainBaseMap, new TerrainBaseMapFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_DeferredTerrainMacroMap, new TerrainMacroMapFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_DeferredTerrainDetailMap, new TerrainDetailMapFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_DeferredTerrainBlankInfoMap, new TerrainBlankInfoMapFeatGLSL );
|
||||
}
|
||||
|
||||
};
|
||||
|
|
@ -250,10 +255,6 @@ void TerrainBaseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentLis
|
|||
// grab connector texcoord register
|
||||
Var *texCoord = getInTexCoord( "texCoord", "vec3", true, componentList );
|
||||
|
||||
// We do nothing more if this is a prepass.
|
||||
if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
return;
|
||||
|
||||
// create texture var
|
||||
Var *diffuseMap = new Var;
|
||||
diffuseMap->setType( "sampler2D" );
|
||||
|
|
@ -269,7 +270,14 @@ void TerrainBaseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentLis
|
|||
baseColor->setName( "baseColor" );
|
||||
meta->addStatement( new GenOp( " @ = tex2D( @, @.xy );\r\n", new DecOp( baseColor ), diffuseMap, texCoord ) );
|
||||
meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", baseColor, baseColor));
|
||||
meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul ) ) );
|
||||
|
||||
ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
|
||||
|
||||
if(fd.features.hasFeature(MFT_isDeferred))
|
||||
{
|
||||
target= ShaderFeature::RenderTarget1;
|
||||
}
|
||||
meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) );
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
|
@ -278,14 +286,16 @@ ShaderFeature::Resources TerrainBaseMapFeatGLSL::getResources( const MaterialFea
|
|||
{
|
||||
Resources res;
|
||||
res.numTexReg = 1;
|
||||
|
||||
// We only sample from the base map during a diffuse pass.
|
||||
if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
res.numTex = 1;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
U32 TerrainBaseMapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
|
||||
{
|
||||
return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
|
||||
}
|
||||
|
||||
TerrainDetailMapFeatGLSL::TerrainDetailMapFeatGLSL()
|
||||
: mTorqueDep( "shaders/common/gl/torque.glsl" ),
|
||||
mTerrainDep( "shaders/common/terrain/terrain.glsl" )
|
||||
|
|
@ -386,6 +396,9 @@ void TerrainDetailMapFeatGLSL::processPix( Vector<ShaderComponent*> &component
|
|||
const U32 detailIndex = getProcessIndex();
|
||||
Var *inTex = getVertTexCoord( "texCoord" );
|
||||
|
||||
// new terrain
|
||||
bool hasNormal = fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex);
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
// We need the negative tangent space view vector
|
||||
|
|
@ -454,6 +467,100 @@ void TerrainDetailMapFeatGLSL::processPix( Vector<ShaderComponent*> &component
|
|||
meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
|
||||
new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
|
||||
|
||||
// New terrain
|
||||
|
||||
Var *lerpBlend = (Var*)LangElement::find("lerpBlend");
|
||||
if (!lerpBlend)
|
||||
{
|
||||
lerpBlend = new Var;
|
||||
lerpBlend->setType("float");
|
||||
lerpBlend->setName("lerpBlend");
|
||||
lerpBlend->uniform = true;
|
||||
lerpBlend->constSortPos = cspPrimitive;
|
||||
}
|
||||
|
||||
|
||||
Var *blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", detailIndex));
|
||||
if (!blendDepth)
|
||||
{
|
||||
blendDepth = new Var;
|
||||
blendDepth->setType("float");
|
||||
blendDepth->setName(String::ToString("blendDepth%d", detailIndex));
|
||||
blendDepth->uniform = true;
|
||||
blendDepth->constSortPos = cspPrimitive;
|
||||
}
|
||||
|
||||
Var *baseColor = (Var*)LangElement::find("baseColor");
|
||||
ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
|
||||
|
||||
if(fd.features.hasFeature( MFT_DeferredTerrainDetailMap ))
|
||||
target= ShaderFeature::RenderTarget1;
|
||||
|
||||
Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
|
||||
|
||||
if (!outColor)
|
||||
{
|
||||
// create color var
|
||||
outColor = new Var;
|
||||
outColor->setType("float4");
|
||||
outColor->setName("col");
|
||||
outColor->setStructName("OUT");
|
||||
meta->addStatement(new GenOp(" @;\r\n", outColor));
|
||||
}
|
||||
|
||||
Var *detailColor = (Var*)LangElement::find("detailColor");
|
||||
if (!detailColor)
|
||||
{
|
||||
detailColor = new Var;
|
||||
detailColor->setType("float4");
|
||||
detailColor->setName("detailColor");
|
||||
meta->addStatement(new GenOp(" @;\r\n", new DecOp(detailColor)));
|
||||
}
|
||||
|
||||
// Get the detail texture.
|
||||
Var *detailMap = new Var;
|
||||
detailMap->setType("sampler2D");
|
||||
detailMap->setName(String::ToString("detailMap%d", detailIndex));
|
||||
detailMap->uniform = true;
|
||||
detailMap->sampler = true;
|
||||
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
|
||||
|
||||
// Get the normal map texture.
|
||||
Var *normalMap = _getNormalMapTex();
|
||||
|
||||
// Issue happens somewhere here -----
|
||||
|
||||
// Sample the normal map.
|
||||
//
|
||||
// We take two normal samples and lerp between them for
|
||||
// side projection layers... else a single sample.
|
||||
LangElement *texOp;
|
||||
|
||||
// Note that we're doing the standard greyscale detail
|
||||
// map technique here which can darken and lighten the
|
||||
// diffuse texture.
|
||||
//
|
||||
// We take two color samples and lerp between them for
|
||||
// side projection layers... else a single sample.
|
||||
//
|
||||
if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
|
||||
{
|
||||
meta->addStatement(new GenOp(" @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
|
||||
detailColor, detailMap, inDet, detailMap, inDet, inTex));
|
||||
|
||||
texOp = new GenOp("lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z )",
|
||||
normalMap, inDet, normalMap, inDet, inTex);
|
||||
}
|
||||
else
|
||||
{
|
||||
meta->addStatement(new GenOp(" @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n",
|
||||
detailColor, detailMap, inDet));
|
||||
|
||||
texOp = new GenOp("tex2D(@, @.xy)", normalMap, inDet);
|
||||
}
|
||||
|
||||
// New terrain
|
||||
|
||||
// Get a var and accumulate the blend amount.
|
||||
Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
|
||||
if ( !blendTotal )
|
||||
|
|
@ -487,46 +594,6 @@ void TerrainDetailMapFeatGLSL::processPix( Vector<ShaderComponent*> &component
|
|||
}
|
||||
}
|
||||
|
||||
// If this is a prepass then we skip color.
|
||||
if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
{
|
||||
// Check to see if we have a gbuffer normal.
|
||||
Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
|
||||
|
||||
// If we have a gbuffer normal and we don't have a
|
||||
// normal map feature then we need to lerp in a
|
||||
// default normal else the normals below this layer
|
||||
// will show thru.
|
||||
if ( gbNormal &&
|
||||
!fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
|
||||
{
|
||||
Var *viewToTangent = getInViewToTangent( componentList );
|
||||
|
||||
meta->addStatement( new GenOp( " @ = lerp( @, tGetMatrix3Row(@, 2), min( @, @.w ) );\r\n",
|
||||
gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) );
|
||||
}
|
||||
|
||||
output = meta;
|
||||
return;
|
||||
}
|
||||
|
||||
Var *detailColor = (Var*)LangElement::find( "detailColor" );
|
||||
if ( !detailColor )
|
||||
{
|
||||
detailColor = new Var;
|
||||
detailColor->setType( "vec4" );
|
||||
detailColor->setName( "detailColor" );
|
||||
meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
|
||||
}
|
||||
|
||||
// Get the detail texture.
|
||||
Var *detailMap = new Var;
|
||||
detailMap->setType( "sampler2D" );
|
||||
detailMap->setName( String::ToString( "detailMap%d", detailIndex ) );
|
||||
detailMap->uniform = true;
|
||||
detailMap->sampler = true;
|
||||
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
|
||||
|
||||
// If we're using SM 3.0 then take advantage of
|
||||
// dynamic branching to skip layers per-pixel.
|
||||
|
||||
|
|
@ -557,9 +624,6 @@ void TerrainDetailMapFeatGLSL::processPix( Vector<ShaderComponent*> &component
|
|||
meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
|
||||
detailColor, detailInfo, inDet ) );
|
||||
|
||||
Var *baseColor = (Var*)LangElement::find( "baseColor" );
|
||||
Var *outColor = (Var*)LangElement::find( "col" );
|
||||
|
||||
meta->addStatement( new GenOp( " @ = lerp( @, @ + @, @ );\r\n",
|
||||
outColor, outColor, baseColor, detailColor, detailBlend ) );
|
||||
|
||||
|
|
@ -585,9 +649,7 @@ ShaderFeature::Resources TerrainDetailMapFeatGLSL::getResources( const MaterialF
|
|||
res.numTexReg += 4;
|
||||
}
|
||||
|
||||
// If this isn't the prepass then we sample
|
||||
// from the detail texture for diffuse coloring.
|
||||
if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
// sample from the detail texture for diffuse coloring.
|
||||
res.numTex += 1;
|
||||
|
||||
// If we have parallax for this layer then we'll also
|
||||
|
|
@ -602,6 +664,11 @@ ShaderFeature::Resources TerrainDetailMapFeatGLSL::getResources( const MaterialF
|
|||
return res;
|
||||
}
|
||||
|
||||
U32 TerrainDetailMapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
|
||||
{
|
||||
return fd.features[MFT_DeferredTerrainDetailMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
|
||||
}
|
||||
|
||||
|
||||
TerrainMacroMapFeatGLSL::TerrainMacroMapFeatGLSL()
|
||||
: mTorqueDep( "shaders/common/gl/torque.glsl" ),
|
||||
|
|
@ -759,29 +826,6 @@ void TerrainMacroMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentL
|
|||
// Add to the blend total.
|
||||
meta->addStatement( new GenOp( " @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend ) );
|
||||
|
||||
// If this is a prepass then we skip color.
|
||||
if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
{
|
||||
// Check to see if we have a gbuffer normal.
|
||||
Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
|
||||
|
||||
// If we have a gbuffer normal and we don't have a
|
||||
// normal map feature then we need to lerp in a
|
||||
// default normal else the normals below this layer
|
||||
// will show thru.
|
||||
if ( gbNormal &&
|
||||
!fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
|
||||
{
|
||||
Var *viewToTangent = getInViewToTangent( componentList );
|
||||
|
||||
meta->addStatement( new GenOp( " @ = lerp( @, tGetMatrix3Row(@, 2), min( @, @.w ) );\r\n",
|
||||
gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) );
|
||||
}
|
||||
|
||||
output = meta;
|
||||
return;
|
||||
}
|
||||
|
||||
Var *detailColor = (Var*)LangElement::find( "macroColor" );
|
||||
if ( !detailColor )
|
||||
{
|
||||
|
|
@ -826,8 +870,12 @@ void TerrainMacroMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentL
|
|||
|
||||
meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
|
||||
detailColor, detailInfo, inDet ) );
|
||||
ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
|
||||
|
||||
Var *outColor = (Var*)LangElement::find( "col" );
|
||||
if(fd.features.hasFeature(MFT_DeferredTerrainMacroMap))
|
||||
target= ShaderFeature::RenderTarget1;
|
||||
|
||||
Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
|
||||
|
||||
meta->addStatement( new GenOp( " @ = lerp( @, @ + @, @ );\r\n",
|
||||
outColor, outColor, outColor, detailColor, detailBlend ) );
|
||||
|
|
@ -850,9 +898,6 @@ ShaderFeature::Resources TerrainMacroMapFeatGLSL::getResources( const MaterialFe
|
|||
res.numTex += 1;
|
||||
}
|
||||
|
||||
// If this isn't the prepass then we sample
|
||||
// from the detail texture for diffuse coloring.
|
||||
if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
res.numTex += 1;
|
||||
|
||||
// Finally we always send the detail texture
|
||||
|
|
@ -862,6 +907,11 @@ ShaderFeature::Resources TerrainMacroMapFeatGLSL::getResources( const MaterialFe
|
|||
return res;
|
||||
}
|
||||
|
||||
U32 TerrainMacroMapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
|
||||
{
|
||||
return fd.features[MFT_DeferredTerrainMacroMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
|
||||
}
|
||||
|
||||
void TerrainNormalMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
|
|
@ -881,9 +931,6 @@ void TerrainNormalMapFeatGLSL::processVert( Vector<ShaderComponent*> &component
|
|||
void TerrainNormalMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// We only need to process normals during the prepass.
|
||||
if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
|
||||
return;
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
|
|
@ -1023,7 +1070,12 @@ ShaderFeature::Resources TerrainLightMapFeatGLSL::getResources( const MaterialFe
|
|||
void TerrainAdditiveFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
Var *color = (Var*) LangElement::find( "col" );
|
||||
Var *color = NULL;
|
||||
if (fd.features[MFT_DeferredTerrainDetailMap])
|
||||
color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) );
|
||||
else
|
||||
color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) );
|
||||
|
||||
Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
|
||||
if ( !color || !blendTotal )
|
||||
return;
|
||||
|
|
@ -1035,3 +1087,40 @@ void TerrainAdditiveFeatGLSL::processPix( Vector<ShaderComponent*> &componentLis
|
|||
|
||||
output = meta;
|
||||
}
|
||||
|
||||
//standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
|
||||
//.b = specular strength, a= spec power.
|
||||
//here, it's merely a cutout for now, so that lightmapping (target3) doesn't get mangled.
|
||||
//we'll most likely revisit that later. possibly several ways...
|
||||
|
||||
U32 TerrainBlankInfoMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
|
||||
{
|
||||
return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
|
||||
}
|
||||
|
||||
void TerrainBlankInfoMapFeatGLSL::processPix(Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd)
|
||||
{
|
||||
// search for material var
|
||||
Var *material;
|
||||
OutputTarget targ = RenderTarget1;
|
||||
if (fd.features[MFT_isDeferred])
|
||||
{
|
||||
targ = RenderTarget2;
|
||||
}
|
||||
material = (Var*)LangElement::find(getOutputTargetVarName(targ));
|
||||
|
||||
MultiLine * meta = new MultiLine;
|
||||
if (!material)
|
||||
{
|
||||
// create color var
|
||||
material = new Var;
|
||||
material->setType("vec4");
|
||||
material->setName(getOutputTargetVarName(targ));
|
||||
material->setStructName("OUT");
|
||||
}
|
||||
|
||||
meta->addStatement(new GenOp(" @ = float4(0.0,0.0,0.0,0.0001);\r\n", material));
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue