Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "renderInstance/renderDeferredMgr.h"
#include "materials/processedMaterial.h"
#include "materials/materialFeatureTypes.h"
#include "terrain/terrFeatureTypes.h"


void DeferredRTLightingFeatHLSL::processPixMacros( Vector<GFXShaderMacro> &macros,
Expand Down Expand Up @@ -132,6 +133,8 @@ void DeferredRTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &component
lightBufferTex->uniform = true;
lightBufferTex->texture = true;
lightBufferTex->constNum = lightInfoBuffer->constNum;
if (fd.features.hasFeature(MFT_TerrainDetailMap))
lightInfoBuffer->constNum = 6;

// Declare the RTLighting variables in this feature, they will either be assigned
// in this feature, or in the tonemap/lightmap feature
Expand Down
220 changes: 145 additions & 75 deletions Engine/source/terrain/hlsl/terrFeatureHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,51 @@ Var* TerrainFeatHLSL::_getUniformVar( const char *name, const char *type, Consta
return theVar;
}

Var* TerrainFeatHLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList )
Var* TerrainFeatHLSL::_computeAndGetInDetailCoord( MultiLine* meta, Vector<ShaderComponent*> &componentList )
{
String name( String::ToString( "detCoord%d", getProcessIndex() ) );
String name("detCoord");
Var *inDet = (Var*)LangElement::find( name );

if ( !inDet )
if (!inDet)
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);

inDet = connectComp->getElement( RT_TEXCOORD );
inDet->setName( name );
inDet->setStructName( "IN" );
inDet->setType( "float4" );
inDet = connectComp->getElement(RT_TEXCOORD);
inDet->setName(name);
inDet->setStructName("IN");
inDet->setType("float4");
}

return inDet;
S32 index = getProcessIndex();

// Get the detail scale and fade info.
Var *detScaleAndFade = (Var*)LangElement::find(String::ToString("detailScaleAndFade%d", index));
if (!detScaleAndFade)
{
detScaleAndFade = new Var;
detScaleAndFade->setType("float4");
detScaleAndFade->setName(String::ToString("detailScaleAndFade%d", index));
detScaleAndFade->uniform = true;
detScaleAndFade->constSortPos = cspPotentialPrimitive;
}

Var *detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", index));
if (!detCoord)
{
detCoord = new Var;
detCoord->setType("float4");
detCoord->setName(String::ToString("detCoord%d", index));
meta->addStatement(new GenOp(" @;\r\n", new DecOp(detCoord)));
meta->addStatement(new GenOp(" @.xyz = @.xyz * @.xyx;\r\n", detCoord, inDet, detScaleAndFade));
meta->addStatement(new GenOp(" @.w = clamp((@.z - @.w) * @.w, 0.0, 1.0);\r\n", detCoord, detScaleAndFade, inDet, detScaleAndFade));
}

return detCoord;
}

Var* TerrainFeatHLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList )
Var* TerrainFeatHLSL::_computeAndGetInMacroCoord(MultiLine* meta, Vector<ShaderComponent*> &componentList )
{
String name( String::ToString( "macroCoord%d", getProcessIndex() ) );
String name( "macroCoord" );
Var *inDet = (Var*)LangElement::find( name );

if ( !inDet )
Expand All @@ -122,12 +146,36 @@ Var* TerrainFeatHLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList
inDet->setType( "float4" );
}

return inDet;
S32 index = getProcessIndex();

// Get the macro scale and fade info.
Var *macroScaleAndFade = (Var*)LangElement::find(String::ToString("macroScaleAndFade%d", index));
if (!macroScaleAndFade)
{
macroScaleAndFade = new Var;
macroScaleAndFade->setType("float4");
macroScaleAndFade->setName(String::ToString("macroScaleAndFade%d", index));
macroScaleAndFade->uniform = true;
macroScaleAndFade->constSortPos = cspPotentialPrimitive;
}

Var *macroCoord = (Var*)LangElement::find(String::ToString("macroCoord%d", index));
if (!macroCoord)
{
macroCoord = new Var;
macroCoord->setType("float4");
macroCoord->setName(String::ToString("macroCoord%d", index));
meta->addStatement(new GenOp(" @;\r\n", new DecOp(macroCoord)));
meta->addStatement(new GenOp(" @.xyz = @.xyz * @.xyx;\r\n", macroCoord, inDet, macroScaleAndFade));
meta->addStatement(new GenOp(" @.w = clamp((@.z - @.w) * @.w, 0.0, 1.0);\r\n", macroCoord, macroScaleAndFade, inDet, macroScaleAndFade));
}

return macroCoord;
}

Var* TerrainFeatHLSL::_getNormalMapTex()
{
String name(String::ToString("normalMap%d", getProcessIndex()));
String name("normalMap");
Var *normalMap = (Var*)LangElement::find(name);

if (!normalMap)
Expand Down Expand Up @@ -359,13 +407,6 @@ void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &component
new DecOp( dist ), inPos, eyePos ) );
}

// grab connector texcoord register
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outTex = connectComp->getElement( RT_TEXCOORD );
outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
outTex->setStructName( "OUT" );
outTex->setType( "float4" );

// Get the detail scale and fade info.
Var *detScaleAndFade = new Var;
detScaleAndFade->setType( "float4" );
Expand All @@ -382,11 +423,19 @@ void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &component
//
// See TerrainBaseMapFeatHLSL::processVert().
//
meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );

// And sneak the detail fade thru the w detailCoord.
meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
outTex, detScaleAndFade, dist, detScaleAndFade ) );
// grab connector texcoord register
Var *outTex = (Var*)LangElement::find("detCoord");
if (!outTex)
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
outTex = connectComp->getElement(RT_TEXCOORD);
outTex->setName("detCoord");
outTex->setStructName("OUT");
outTex->setType("float4");
}
meta->addStatement(new GenOp(" @.xyz = @.xyx;\r\n", outTex, inTex));
meta->addStatement(new GenOp(" @.w = @;\r\n", outTex, dist));

output = meta;
}
Expand Down Expand Up @@ -457,7 +506,7 @@ void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &component
}

// Grab the incoming detail coord.
Var *inDet = _getInDetailCoord( componentList );
Var *inDet = _computeAndGetInDetailCoord( meta, componentList );

// Get the detail id.
Var *detailInfo = _getDetailIdStrengthParallax();
Expand All @@ -483,14 +532,39 @@ void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &component

// Add to the blend total.

meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend));
meta->addStatement(new GenOp(" @ += @;\r\n", blendTotal, detailBlend));

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 = (Var*)LangElement::find("detailMap");
if (!detailMap)
{
detailMap = new Var;
detailMap->setType("SamplerState");
detailMap->setName("detailMap");
detailMap->uniform = true;
detailMap->sampler = true;
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
}

S32 detailTexIndex = Var::getTexUnitNum();

// If we had a parallax feature... then factor in the parallax
// amount so that it fades out with the layer blending.
if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
{
// Get the rest of our inputs.
Var *normalMap = _getNormalMapTex();
normalMap->constNum--;
detailTexIndex++;

String name(String::ToString("normalMapTex%d", getProcessIndex()));
Var *normalMapTex = (Var*)LangElement::find(name);
Expand All @@ -502,7 +576,7 @@ void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &component
normalMapTex->setType("Texture2D");
normalMapTex->uniform = true;
normalMapTex->texture = true;
normalMapTex->constNum = normalMap->constNum;
normalMapTex->constNum = Var::getTexUnitNum();
}

// Call the library function to do the rest.
Expand All @@ -518,32 +592,6 @@ void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &component
}
}

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( "SamplerState" );
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.


if ( GFX->getPixelShaderVersion() >= 3.0f )
meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );

meta->addStatement( new GenOp( " {\r\n" ) );

// Note that we're doing the standard greyscale detail
// map technique here which can darken and lighten the
// diffuse texture.
Expand All @@ -558,7 +606,17 @@ void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &component
detailTex->setType("Texture2D");
detailTex->uniform = true;
detailTex->texture = true;
detailTex->constNum = detailMap->constNum;
detailTex->constNum = detailTexIndex;


// If we're using SM 3.0 then take advantage of
// dynamic branching to skip layers per-pixel.


if ( GFX->getPixelShaderVersion() >= 3.0f )
meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );

meta->addStatement( new GenOp( " {\r\n" ) );

if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
{
Expand Down Expand Up @@ -605,6 +663,10 @@ ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialF
// worldToTanget transform.
if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) )
res.numTexReg += 4;

// We always send the detail texture
// coord information to the pixel shader.
res.numTexReg += 1;
}

// sample from the detail texture for diffuse coloring.
Expand All @@ -617,7 +679,7 @@ ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialF

// Finally we always send the detail texture
// coord to the pixel shader.
res.numTexReg += 1;
//res.numTexReg += 1;

return res;
}
Expand Down Expand Up @@ -671,11 +733,16 @@ void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentL
}

// grab connector texcoord register
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outTex = connectComp->getElement( RT_TEXCOORD );
outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
outTex->setStructName( "OUT" );
outTex->setType( "float4" );
Var *outTex = (Var*)LangElement::find("macroCoord");
if (!outTex)
{
// grab connector texcoord register
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
outTex = connectComp->getElement(RT_TEXCOORD);
outTex->setName("macroCoord");
outTex->setStructName("OUT");
outTex->setType("float4");
}

// Get the detail scale and fade info.
Var *detScaleAndFade = new Var;
Expand All @@ -685,11 +752,10 @@ void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentL
detScaleAndFade->constSortPos = cspPotentialPrimitive;

// Setup the detail coord.
meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
meta->addStatement(new GenOp(" @.xyz = @.xyx;\r\n", outTex, inTex));

// And sneak the detail fade thru the w detailCoord.
meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
outTex, detScaleAndFade, dist, detScaleAndFade ) );
meta->addStatement(new GenOp(" @.w = @;\r\n", outTex, dist));

output = meta;
}
Expand Down Expand Up @@ -761,7 +827,7 @@ void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentL
}

// Grab the incoming detail coord.
Var *inDet = _getInMacroCoord( componentList );
Var *inDet = _computeAndGetInMacroCoord( meta, componentList );

// Get the detail id.
Var *detailInfo = _getMacroIdStrengthParallax();
Expand All @@ -786,7 +852,7 @@ void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentL
}

// Add to the blend total.
meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend));
meta->addStatement(new GenOp(" @ += @;\r\n", blendTotal, detailBlend));

Var *detailColor = (Var*)LangElement::find( "macroColor" );
if ( !detailColor )
Expand All @@ -798,20 +864,24 @@ void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentL
}

// Get the detail texture.
Var *detailMap = new Var;
detailMap->setType( "SamplerState" );
detailMap->setName( String::ToString( "macroMap%d", detailIndex ) );
detailMap->uniform = true;
detailMap->sampler = true;
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here

Var *detailMap = (Var*)LangElement::find("macroMap");
if (!detailMap)
{
detailMap = new Var;
detailMap->setType("SamplerState");
detailMap->setName("macroMap");
detailMap->uniform = true;
detailMap->sampler = true;
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
}

//Create texture object for directx 11
Var *detailTex = new Var;
detailTex->setName(String::ToString("macroMapTex%d", detailIndex));
detailTex->setType("Texture2D");
detailTex->uniform = true;
detailTex->texture = true;
detailTex->constNum = detailMap->constNum;
detailTex->constNum = Var::getTexUnitNum();

// If we're using SM 3.0 then take advantage of
// dynamic branching to skip layers per-pixel.
Expand Down Expand Up @@ -932,7 +1002,7 @@ void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &component
Var *normalMap = _getNormalMapTex();

/// Get the texture coord.
Var *inDet = _getInDetailCoord( componentList );
Var *inDet = _computeAndGetInDetailCoord( meta, componentList );
Var *inTex = getVertTexCoord( "texCoord" );

// Sample the normal map.
Expand All @@ -950,7 +1020,7 @@ void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &component
normalMapTex->setType("Texture2D");
normalMapTex->uniform = true;
normalMapTex->texture = true;
normalMapTex->constNum = normalMap->constNum;
normalMapTex->constNum = Var::getTexUnitNum();
}

if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
Expand Down
Loading