Direct3D11 Engine/source changes

This commit is contained in:
rextimmy 2016-03-20 21:52:11 +10:00
parent 3a9b50f702
commit 41e5caf22b
81 changed files with 1291 additions and 617 deletions

View file

@ -85,7 +85,8 @@ void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// accu constants
Var *accuScale = (Var*)LangElement::find( "accuScale" );
if ( !accuScale ) {
if ( !accuScale )
{
accuScale = new Var;
accuScale->setType( "float" );
accuScale->setName( "accuScale" );
@ -94,7 +95,8 @@ void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
accuScale->constSortPos = cspPotentialPrimitive;
}
Var *accuDirection = (Var*)LangElement::find( "accuDirection" );
if ( !accuDirection ) {
if ( !accuDirection )
{
accuDirection = new Var;
accuDirection->setType( "float" );
accuDirection->setName( "accuDirection" );
@ -103,7 +105,8 @@ void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
accuDirection->constSortPos = cspPotentialPrimitive;
}
Var *accuStrength = (Var*)LangElement::find( "accuStrength" );
if ( !accuStrength ) {
if ( !accuStrength )
{
accuStrength = new Var;
accuStrength->setType( "float" );
accuStrength->setName( "accuStrength" );
@ -112,7 +115,8 @@ void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
accuStrength->constSortPos = cspPotentialPrimitive;
}
Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" );
if ( !accuCoverage ) {
if ( !accuCoverage )
{
accuCoverage = new Var;
accuCoverage->setType( "float" );
accuCoverage->setName( "accuCoverage" );
@ -121,7 +125,8 @@ void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
accuCoverage->constSortPos = cspPotentialPrimitive;
}
Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" );
if ( !accuSpecular ) {
if ( !accuSpecular )
{
accuSpecular = new Var;
accuSpecular->setType( "float" );
accuSpecular->setName( "accuSpecular" );
@ -133,14 +138,26 @@ void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
Var *accuVec = getInTexCoord( "accuVec", "float3", true, componentList );
Var *bumpNorm = (Var *)LangElement::find( "bumpSample" );
if( bumpNorm == NULL ) {
if( bumpNorm == NULL )
{
bumpNorm = (Var *)LangElement::find( "bumpNormal" );
if (!bumpNorm)
return;
}
// get the accu pixel color
meta->addStatement( new GenOp( " @ = tex2D(@, @ * @);\r\n", colorAccuDecl, accuMap, inTex, accuScale ) );
if (mIsDirect3D11)
{
Var *accuMapTex = new Var;
accuMapTex->setType("Texture2D");
accuMapTex->setName("accuMapTex");
accuMapTex->uniform = true;
accuMapTex->texture = true;
accuMapTex->constNum = accuMap->constNum;
meta->addStatement(new GenOp(" @ = @.Sample(@, @ * @);\r\n", colorAccuDecl, accuMapTex, accuMap, inTex, accuScale));
}
else
meta->addStatement(new GenOp(" @ = tex2D(@, @ * @);\r\n", colorAccuDecl, accuMap, inTex, accuScale));
// scale up normals
meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) );

View file

@ -70,6 +70,13 @@ void BumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
Var *bumpMap = getNormalMapTex();
LangElement *texOp = NULL;
//if it's D3D11 let's create the texture object
Var* bumpMapTex = NULL;
if (mIsDirect3D11)
{
bumpMapTex = (Var*)LangElement::find("bumpMapTex");
}
// Handle atlased textures
// http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
if(fd.features[MFT_NormalMapAtlas])
@ -127,18 +134,25 @@ void BumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// Add a newline
meta->addStatement( new GenOp( "\r\n" ) );
if(is_sm3)
if (mIsDirect3D11)
{
texOp = new GenOp( "tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord );
texOp = new GenOp("@.SampleLevel(@, @, mipLod_bump)", bumpMapTex, bumpMap, texCoord);
}
else if (is_sm3)
{
texOp = new GenOp("tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord);
}
else
{
texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
texOp = new GenOp("tex2D(@, @)", bumpMap, texCoord);
}
}
else
{
texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
if (mIsDirect3D11)
texOp = new GenOp("@.Sample(@, @)", bumpMapTex, bumpMap, texCoord);
else
texOp = new GenOp("tex2D(@, @)", bumpMap, texCoord);
}
Var *bumpNorm = new Var( "bumpNormal", "float4" );
@ -156,8 +170,26 @@ void BumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
bumpMap->sampler = true;
bumpMap->constNum = Var::getTexUnitNum();
Var* detailBumpTex = NULL;
if (mIsDirect3D11)
{
bumpMap->setType("SamplerState");
detailBumpTex = new Var;
detailBumpTex->setName("detailBumpTex");
detailBumpTex->setType("Texture2D");
detailBumpTex->uniform = true;
detailBumpTex->texture = true;
detailBumpTex->constNum = bumpMap->constNum;
}
else
bumpMap->setType("sampler2D");
texCoord = getInTexCoord( "detCoord", "float2", true, componentList );
texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
if (mIsDirect3D11)
texOp = new GenOp("@.Sample(@, @)", detailBumpTex, bumpMap, texCoord);
else
texOp = new GenOp("tex2D(@, @)", bumpMap, texCoord);
Var *detailBump = new Var;
detailBump->setName( "detailBump" );
@ -345,17 +377,26 @@ void ParallaxFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// Get the rest of our inputs.
Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float", cspPotentialPrimitive );
Var *normalMap = getNormalMapTex();
Var *bumpMapTexture = (Var*)LangElement::find("bumpMapTex");
// Call the library function to do the rest.
if(fd.features.hasFeature( MFT_IsDXTnm, getProcessIndex() ))
if (fd.features.hasFeature(MFT_IsDXTnm, getProcessIndex()))
{
meta->addStatement( new GenOp( " @.xy += parallaxOffsetDxtnm( @, @.xy, @, @ );\r\n",
texCoord, normalMap, texCoord, negViewTS, parallaxInfo ) );
if (mIsDirect3D11)
meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @, @.xy, @, @ );\r\n",
texCoord, bumpMapTexture, normalMap, texCoord, negViewTS, parallaxInfo));
else
meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @.xy, @, @ );\r\n",
texCoord, normalMap, texCoord, negViewTS, parallaxInfo));
}
else
{
meta->addStatement( new GenOp( " @.xy += parallaxOffset( @, @.xy, @, @ );\r\n",
texCoord, normalMap, texCoord, negViewTS, parallaxInfo ) );
if (mIsDirect3D11)
meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @, @.xy, @, @ );\r\n",
texCoord, bumpMapTexture, normalMap, texCoord, negViewTS, parallaxInfo));
else
meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @.xy, @, @ );\r\n",
texCoord, normalMap, texCoord, negViewTS, parallaxInfo));
}
// TODO: Fix second UV maybe?

View file

@ -46,9 +46,13 @@ void ParaboloidVertTransformHLSL::processVert( Vector<ShaderComponent*> &compon
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
// Grab connector out position.
Var *outPosition = connectComp->getElement( RT_POSITION );
outPosition->setName( "hpos" );
outPosition->setStructName( "OUT" );
RegisterType type = RT_POSITION;
if (mIsDirect3D11)
type = RT_SVPOSITION;
Var *outPosition = connectComp->getElement(type);
outPosition->setName("hpos");
outPosition->setStructName("OUT");
// Get the atlas scale.
Var *atlasScale = new Var;

View file

@ -77,8 +77,12 @@ void PixelSpecularHLSL::processPix( Vector<ShaderComponent*> &componentList,
{
LangElement * lightMap = LangElement::find( "lightMap" );
LangElement * lmCoord = LangElement::find( "texCoord2" );
LangElement * lightMapTex = LangElement::find("lightMapTex"); //used only DX11 shaders
lmColor = new GenOp( "tex2D(@, @)", lightMap, lmCoord );
if (lightMapTex)
lmColor = new GenOp("@.Sample(@, @)", lightMapTex, lightMap, lmCoord);
else
lmColor = new GenOp("tex2D(@, @)", lightMap, lmCoord);
}
final = new GenOp( "@ * float4(@.rgb,0)", specMul, lmColor );
@ -138,11 +142,33 @@ void SpecularMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const
specularMap->uniform = true;
specularMap->sampler = true;
specularMap->constNum = Var::getTexUnitNum();
LangElement *texOp = new GenOp( "tex2D(@, @)", specularMap, texCoord );
Var *specularMapTex = NULL;
Var *specularColor = new Var( "specularColor", "float4" );
if (mIsDirect3D11)
{
specularMap->setType("SamplerState");
specularMapTex = new Var;
specularMapTex->setName("specularMapTex");
specularMapTex->setType("Texture2D");
specularMapTex->uniform = true;
specularMapTex->texture = true;
specularMapTex->constNum = specularMap->constNum;
}
else
{
specularMap->setType("sampler2D");
}
output = new GenOp( " @ = @;\r\n", new DecOp( specularColor ), texOp );
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");
output = new GenOp(" @ = @;\r\n", new DecOp(specularColor), texOp);
}
ShaderFeature::Resources SpecularMapHLSL::getResources( const MaterialFeatureData &fd )

View file

@ -55,6 +55,25 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "POSITION" );
newVar->rank = 0;
return newVar;
}
case RT_VPOS:
{
Var *newVar = new Var;
mElementList.push_back(newVar);
newVar->setConnectName("VPOS");
newVar->rank = 0;
return newVar;
}
case RT_SVPOSITION:
{
Var *newVar = new Var;
mElementList.push_back(newVar);
newVar->setConnectName("SV_Position");
newVar->rank = 0;
return newVar;
}
@ -63,6 +82,7 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "NORMAL" );
newVar->rank = 1;
return newVar;
}
@ -71,6 +91,7 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "BINORMAL" );
newVar->rank = 2;
return newVar;
}
@ -79,6 +100,7 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "TANGENT" );
newVar->rank = 3;
return newVar;
}
@ -87,14 +109,7 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "COLOR" );
return newVar;
}
case RT_VPOS:
{
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "VPOS" );
newVar->rank = 4;
return newVar;
}
@ -113,6 +128,7 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
newVar->setConnectName( out );
newVar->constNum = index;
newVar->arraySize = numElements;
newVar->rank = 5 + index;
return newVar;
}
@ -124,67 +140,27 @@ Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32
return NULL;
}
S32 QSORT_CALLBACK ShaderConnectorHLSL::_hlsl4VarSort(const void* e1, const void* e2)
{
Var* a = *((Var **)e1);
Var* b = *((Var **)e2);
return a->rank - b->rank;
}
void ShaderConnectorHLSL::sortVars()
{
if ( GFX->getPixelShaderVersion() >= 2.0 )
// If shader model 4+ than we gotta sort the vars to make sure the order is consistent
if (GFX->getPixelShaderVersion() >= 4.f)
{
dQsort((void *)&mElementList[0], mElementList.size(), sizeof(Var *), _hlsl4VarSort);
return;
// Sort connector variables - They must be sorted on hardware that is running
// ps 1.4 and below. The reason is that texture coordinate registers MUST
// map exactly to their respective texture stage. Ie. if you have fog
// coordinates being passed into a pixel shader in texture coordinate register
// number 4, the fog texture MUST reside in texture stage 4 for it to work.
// The problem is solved by pushing non-texture coordinate data to the end
// of the structure so that the texture coodinates are all at the "top" of the
// structure in the order that the features are processed.
// create list of just the texCoords, sorting by 'mapsToSampler'
Vector< Var * > texCoordList;
// - first pass is just coords mapped to a sampler
for( U32 i=0; i<mElementList.size(); i++ )
{
Var *var = mElementList[i];
if( var->mapsToSampler )
{
texCoordList.push_back( var );
}
}
// - next pass is for the others
for( U32 i=0; i<mElementList.size(); i++ )
{
Var *var = mElementList[i];
if( dStrstr( (const char *)var->connectName, "TEX" ) &&
!var->mapsToSampler )
{
texCoordList.push_back( var );
}
}
// rename the connectNames
for( U32 i=0; i<texCoordList.size(); i++ )
{
char out[32];
dSprintf( (char*)out, sizeof(out), "TEXCOORD%d", i );
texCoordList[i]->setConnectName( out );
}
// write new, sorted list over old one
if( texCoordList.size() )
{
U32 index = 0;
for( U32 i=0; i<mElementList.size(); i++ )
{
Var *var = mElementList[i];
if( dStrstr( (const char *)var->connectName, "TEX" ) )
{
mElementList[i] = texCoordList[index];
index++;
}
}
}
return;
}
void ShaderConnectorHLSL::setName( char *newName )
@ -246,7 +222,7 @@ void ParamsDefHLSL::assignConstantNumbers()
Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
if( var )
{
bool shaderConst = var->uniform && !var->sampler;
bool shaderConst = var->uniform && !var->sampler && !var->texture;
AssertFatal((!shaderConst) || var->constSortPos != cspUninit, "Const sort position has not been set, variable will not receive a constant number!!");
if( shaderConst && var->constSortPos == bin)
{
@ -328,6 +304,10 @@ void PixelParamsDefHLSL::print( Stream &stream, bool isVerterShader )
{
dSprintf( (char*)varNum, sizeof(varNum), ": register(S%d)", var->constNum );
}
else if (var->texture)
{
dSprintf((char*)varNum, sizeof(varNum), ": register(T%d)", var->constNum);
}
else
{
dSprintf( (char*)varNum, sizeof(varNum), ": register(C%d)", var->constNum );

View file

@ -30,6 +30,8 @@
class ShaderConnectorHLSL : public ShaderConnector
{
private:
static S32 QSORT_CALLBACK _hlsl4VarSort(const void* e1, const void* e2);
public:
// ShaderConnector

View file

@ -173,6 +173,7 @@ LangElement *ShaderFeatureHLSL::expandNormalMap( LangElement *sampleNormalOp,
ShaderFeatureHLSL::ShaderFeatureHLSL()
{
output = NULL;
mIsDirect3D11 = GFX->getAdapterType() == Direct3D11;
}
Var * ShaderFeatureHLSL::getVertTexCoord( const String &name )
@ -354,7 +355,7 @@ Var* ShaderFeatureHLSL::getOutTexCoord( const char *name,
if ( useTexAnim )
{
inTex->setType( "float4" );
inTex->setType( "float2" );
// create texture mat var
Var *texMat = new Var;
@ -365,7 +366,7 @@ Var* ShaderFeatureHLSL::getOutTexCoord( const char *name,
// Statement allows for casting of different types which
// eliminates vector truncation problems.
String statement = String::ToString( " @ = (%s)mul(@, @).xy;\r\n", type );
String statement = String::ToString(" @ = (%s)mul(@, float4(@,1,1));\r\n", type);
meta->addStatement( new GenOp( statement, texCoord, texMat, inTex ) );
}
else
@ -464,7 +465,17 @@ Var* ShaderFeatureHLSL::getInVpos( MultiLine *meta,
ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
if ( GFX->getPixelShaderVersion() >= 3.0f )
F32 pixelShaderVer = GFX->getPixelShaderVersion();
if ( pixelShaderVer >= 4.0f )
{
inVpos = connectComp->getElement( RT_SVPOSITION );
inVpos->setName( "vpos" );
inVpos->setStructName( "IN" );
inVpos->setType( "float4" );
return inVpos;
}
else if ( pixelShaderVer >= 3.0f )
{
inVpos = connectComp->getElement( RT_VPOS );
inVpos->setName( "vpos" );
@ -516,15 +527,30 @@ Var* ShaderFeatureHLSL::getInViewToTangent( Vector<ShaderComponent*> &componentL
Var* ShaderFeatureHLSL::getNormalMapTex()
{
Var *normalMap = (Var*)LangElement::find( "bumpMap" );
if ( !normalMap )
Var *normalMap = (Var*)LangElement::find("bumpMap");
if (!normalMap)
{
normalMap = new Var;
normalMap->setType( "sampler2D" );
normalMap->setName( "bumpMap" );
normalMap->setType("sampler2D");
normalMap->setName("bumpMap");
normalMap->uniform = true;
normalMap->sampler = true;
normalMap->constNum = Var::getTexUnitNum();
// D3D11
Var* normalMapTex = NULL;
if (GFX->getAdapterType() == Direct3D11)
{
normalMap->setType("SamplerState");
normalMapTex = new Var;
normalMapTex->setName("bumpMapTex");
normalMapTex->setType("Texture2D");
normalMapTex->uniform = true;
normalMapTex->texture = true;
normalMapTex->constNum = normalMap->constNum;
}
}
return normalMap;
@ -780,6 +806,7 @@ Var* ShaderFeatureHLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &compon
// Grab incoming texture coords.
Var *inTex = getVertTexCoord( "texCoord" );
inTex->setType("float2");
// create detail variable
Var *detScale = new Var;
@ -798,8 +825,6 @@ Var* ShaderFeatureHLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &compon
if ( useTexAnim )
{
inTex->setType( "float4" );
// Find or create the texture matrix.
Var *texMat = (Var*)LangElement::find( "texMat" );
if ( !texMat )
@ -811,7 +836,7 @@ Var* ShaderFeatureHLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &compon
texMat->constSortPos = cspPass;
}
meta->addStatement( new GenOp( " @ = mul(@, @).xy * @;\r\n", outTex, texMat, inTex, detScale ) );
meta->addStatement(new GenOp(" @ = mul(@, float4(@,1,1)).xy * @;\r\n", outTex, texMat, inTex, detScale));
}
else
{
@ -869,6 +894,19 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
diffuseMap->sampler = true;
diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var* diffuseMapTex = NULL;
if (mIsDirect3D11)
{
diffuseMap->setType("SamplerState");
diffuseMapTex = new Var;
diffuseMapTex->setName("diffuseMapTex");
diffuseMapTex->setType("Texture2D");
diffuseMapTex->uniform = true;
diffuseMapTex->texture = true;
diffuseMapTex->constNum = diffuseMap->constNum;
}
// create sample color
Var *diffColor = new Var;
diffColor->setType("float4");
@ -880,13 +918,14 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
if ( fd.features[MFT_CubeMap] )
{
meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
colorDecl,
diffuseMap,
inTex ) );
if (mIsDirect3D11)
meta->addStatement(new GenOp(" @ = @.Sample(@, @);\r\n", colorDecl, diffuseMapTex, diffuseMap, inTex));
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, NULL, targ)));
}
else if(fd.features[MFT_DiffuseMapAtlas])
@ -958,15 +997,19 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
return;
}
#endif
if(is_sm3)
if (mIsDirect3D11)
{
meta->addStatement(new GenOp(" @ = @.SampleLevel(@,@,mipLod);\r\n",
new DecOp(diffColor), diffuseMapTex, diffuseMap, inTex));
}
else if(is_sm3)
{
meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
new DecOp(diffColor), diffuseMap, inTex));
}
else
{
meta->addStatement(new GenOp( " @ = tex2D(@, @);\r\n",
meta->addStatement(new GenOp( " @ = tex2D(@, @);\r\n",
new DecOp(diffColor), diffuseMap, inTex));
}
if (!fd.features[MFT_Imposter])
@ -976,7 +1019,11 @@ void DiffuseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
}
else
{
meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
if (mIsDirect3D11)
meta->addStatement(new GenOp("@ = @.Sample(@, @);\r\n", colorDecl, diffuseMapTex, diffuseMap, inTex));
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, NULL, targ)));
@ -1067,7 +1114,24 @@ void OverlayTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
diffuseMap->sampler = true;
diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
Var* diffuseMapTex = NULL;
if (mIsDirect3D11)
{
diffuseMap->setType("SamplerState");
diffuseMapTex = new Var;
diffuseMapTex->setName("overlayMapTex");
diffuseMapTex->setType("Texture2D");
diffuseMapTex->uniform = true;
diffuseMapTex->texture = true;
diffuseMapTex->constNum = diffuseMap->constNum;
}
LangElement *statement = NULL;
if (mIsDirect3D11)
statement = new GenOp("@.Sample(@, @)", diffuseMapTex, diffuseMap, inTex);
else
statement = new GenOp("tex2D(@, @)", diffuseMap, inTex);
output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
}
@ -1240,6 +1304,17 @@ void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
lightMap->sampler = true;
lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var *lightMapTex = NULL;
if (mIsDirect3D11)
{
lightMap->setType("SamplerState");
lightMapTex->setName("lightMapTex");
lightMapTex->setType("Texture2D");
lightMapTex->uniform = true;
lightMapTex->texture = true;
lightMapTex->constNum = lightMap->constNum;
}
// argh, pixel specular should prob use this too
if( fd.features[MFT_NormalMap] )
@ -1249,7 +1324,10 @@ void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
lmColor->setType( "float4" );
LangElement *lmColorDecl = new DecOp( lmColor );
output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
if (mIsDirect3D11)
output = new GenOp(" @ = @.Sample(@, @);\r\n", lmColorDecl, lightMapTex, lightMap, inTex);
else
output = new GenOp(" @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex);
return;
}
@ -1269,16 +1347,26 @@ void LightmapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// Lightmap has already been included in the advanced light bin, so
// no need to do any sampling or anything
if(bPreProcessedLighting)
statement = new GenOp( "float4(@, 1.0)", inColor );
if (bPreProcessedLighting)
statement = new GenOp("float4(@, 1.0)", inColor);
else
statement = new GenOp( "tex2D(@, @) + float4(@.rgb, 0.0)", lightMap, inTex, inColor );
{
if (mIsDirect3D11)
statement = new GenOp("@.Sample(@, @) + float4(@.rgb, 0.0)", lightMapTex, lightMap, inTex, inColor);
else
statement = new GenOp("tex2D(@, @) + float4(@.rgb, 0.0)", lightMap, inTex, inColor);
}
}
}
// If we still don't have it... then just sample the lightmap.
if ( !statement )
statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
if (!statement)
{
if (mIsDirect3D11)
statement = new GenOp("@.Sample(@, @)", lightMapTex, lightMap, inTex);
else
statement = new GenOp("tex2D(@, @)", lightMap, inTex);
}
// Assign to proper render target
MultiLine *meta = new MultiLine;
@ -1365,6 +1453,18 @@ void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
toneMap->sampler = true;
toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var *toneMapTex = NULL;
if (mIsDirect3D11)
{
toneMap->setType("SamplerState");
toneMapTex = new Var;
toneMapTex->setName("toneMapTex");
toneMapTex->setType("Texture2D");
toneMapTex->uniform = true;
toneMapTex->texture = true;
toneMapTex->constNum = toneMap->constNum;
}
MultiLine * meta = new MultiLine;
// First get the toneMap color
@ -1373,7 +1473,10 @@ void TonemapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
toneMapColor->setName( "toneMapColor" );
LangElement *toneMapColorDecl = new DecOp( toneMapColor );
meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
if (mIsDirect3D11)
meta->addStatement(new GenOp(" @ = @.Sample(@, @);\r\n", toneMapColorDecl, toneMapTex, toneMap, inTex2));
else
meta->addStatement(new GenOp(" @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2));
// We do a different calculation if there is a diffuse map or not
Material::BlendOp blendOp = Material::Mul;
@ -1602,6 +1705,18 @@ void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
detailMap->sampler = true;
detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var* detailMapTex = NULL;
if (mIsDirect3D11)
{
detailMap->setType("SamplerState");
detailMapTex = new Var;
detailMapTex->setName("detailMapTex");
detailMapTex->setType("Texture2D");
detailMapTex->uniform = true;
detailMapTex->texture = true;
detailMapTex->constNum = detailMap->constNum;
}
// We're doing the standard greyscale detail map
// technique which can darken and lighten the
// diffuse texture.
@ -1609,7 +1724,12 @@ void DetailFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// TODO: We could add a feature to toggle between this
// and a simple multiplication with the detail map.
LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
LangElement *statement = NULL;
if (mIsDirect3D11)
statement = new GenOp("( @.Sample(@, @) * 2.0 ) - 1.0", detailMapTex, detailMap, inTex);
else
statement = new GenOp("( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex);
if ( fd.features[MFT_isDeferred])
output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
else
@ -1665,7 +1785,12 @@ void VertPositionHLSL::processVert( Vector<ShaderComponent*> &componentList,
// grab connector position
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outPosition = connectComp->getElement( RT_POSITION );
Var *outPosition = NULL;
if (mIsDirect3D11)
outPosition = connectComp->getElement(RT_SVPOSITION);
else
outPosition = connectComp->getElement(RT_POSITION);
outPosition->setName( "hpos" );
outPosition->setStructName( "OUT" );
@ -1679,6 +1804,19 @@ void VertPositionHLSL::processVert( Vector<ShaderComponent*> &componentList,
output = meta;
}
void VertPositionHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd)
{
if (mIsDirect3D11)
{
// grab connector position
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
Var *outPosition = connectComp->getElement(RT_SVPOSITION);
outPosition->setName("vpos");
outPosition->setStructName("IN");
}
}
//****************************************************************************
// Reflect Cubemap
@ -1738,8 +1876,11 @@ 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",
cubeNormDecl, cubeTrans, inNormal ) );
meta->addStatement(new GenOp(" @ = ( mul( (@), float4(@, 0) ) ).xyz;\r\n",
cubeNormDecl, cubeTrans, inNormal));
meta->addStatement(new GenOp(" @ = bool(length(@)) ? normalize(@) : @;\r\n",
cubeNormal, cubeNormal, cubeNormal, cubeNormal));
// grab the eye position
Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
@ -1782,10 +1923,10 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// current pass - we need to add one to the current pass to use
// its alpha channel as a gloss map.
if( !fd.features[MFT_DiffuseMap] &&
!fd.features[MFT_NormalMap] )
!fd.features[MFT_NormalMap])
{
if( fd.materialFeatures[MFT_DiffuseMap] ||
fd.materialFeatures[MFT_NormalMap] )
fd.materialFeatures[MFT_NormalMap])
{
// grab connector texcoord register
Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
@ -1797,6 +1938,19 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
newMap->uniform = true;
newMap->sampler = true;
newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var* glowMapTex = NULL;
if (mIsDirect3D11)
{
newMap->setType("SamplerState");
glowMapTex = new Var;
glowMapTex->setName("glowMapTex");
glowMapTex->setType("Texture2D");
glowMapTex->uniform = true;
glowMapTex->texture = true;
glowMapTex->constNum = newMap->constNum;
}
// create sample color
Var *color = new Var;
@ -1806,7 +1960,10 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
glossColor = color;
meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
if (mIsDirect3D11)
meta->addStatement(new GenOp(" @ = @.Sample( @, @ );\r\n", colorDecl, glowMapTex, newMap, inTex));
else
meta->addStatement(new GenOp(" @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex));
}
}
else
@ -1837,6 +1994,18 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
cubeMap->sampler = true;
cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
Var* cubeMapTex = NULL;
if (mIsDirect3D11)
{
cubeMap->setType("SamplerState");
cubeMapTex = new Var;
cubeMapTex->setName("cubeMapTex");
cubeMapTex->setType("TextureCube"); // cubeMapTex->setType("TextureCube");
cubeMapTex->uniform = true;
cubeMapTex->texture = true;
cubeMapTex->constNum = cubeMap->constNum;
}
// TODO: Restore the lighting attenuation here!
Var *attn = NULL;
//if ( fd.materialFeatures[MFT_DynamicLight] )
@ -1855,15 +2024,37 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
//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);
{
if (mIsDirect3D11)
texCube = new GenOp("@.SampleLevel( @, @, @.a*5)", cubeMapTex, cubeMap, reflectVec, matinfo);
else
texCube = new GenOp("texCUBElod( @, float4(@, (@.a*5)) )", cubeMap, reflectVec, matinfo);
}
else
texCube = new GenOp("texCUBElod( @, float4(@, ((1.0-@.a)*6)) )", cubeMap, reflectVec, matinfo);
{
if (mIsDirect3D11)
texCube = new GenOp("@.SampleLevel( @, @, (1.0-@.a)*6 )", cubeMapTex, cubeMap, reflectVec, matinfo);
else
texCube = new GenOp("texCUBElod( @, float4(@, ((1.0-@.a)*6)) )", cubeMap, reflectVec, matinfo);
}
}
else
{
if (glossColor) //failing that, rtry and find color data
texCube = new GenOp("texCUBElod( @, float4(@, @.a*5))", cubeMap, reflectVec, glossColor);
{
if (mIsDirect3D11)
texCube = new GenOp("@.SampleLevel( @, @, @.a*5)", cubeMapTex, cubeMap, reflectVec, glossColor);
else
texCube = new GenOp("texCUBElod( @, float4(@, @.a*5))", cubeMap, reflectVec, glossColor);
}
else //failing *that*, just draw the cubemap
texCube = new GenOp("texCUBE( @, @)", cubeMap, reflectVec);
{
if (mIsDirect3D11)
texCube = new GenOp("@.Sample( @, @ )", cubeMapTex, cubeMap, reflectVec);
else
texCube = new GenOp("texCUBE( @, @ )", cubeMap, reflectVec);
}
}
LangElement *lerpVal = NULL;
Material::BlendOp blendOp = Material::LerpAlpha;
@ -1898,7 +2089,7 @@ void ReflectCubeFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
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));\r\n", targ, targ, texCube, lerpVal));
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 ) ) );
@ -1951,7 +2142,7 @@ void ReflectCubeFeatHLSL::setTexData( Material::StageData &stageDat,
}
}
}
if( stageDat.getCubemap() )
{
passData.mCubeMap = stageDat.getCubemap();
@ -2397,7 +2588,8 @@ void VisibilityFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
// Everything else does a fizzle.
Var *vPos = getInVpos( meta, componentList );
meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
// vpos is a float4 in d3d11
meta->addStatement( new GenOp( " fizzle( @.xy, @ );\r\n", vPos, visibility ) );
}
ShaderFeature::Resources VisibilityFeatHLSL::getResources( const MaterialFeatureData &fd )

View file

@ -33,6 +33,8 @@ struct RenderPassData;
class ShaderFeatureHLSL : public ShaderFeature
{
protected:
bool mIsDirect3D11;
public:
ShaderFeatureHLSL();
@ -188,6 +190,9 @@ class VertPositionHLSL : public ShaderFeatureHLSL
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd);
virtual String getName()
{

View file

@ -63,21 +63,33 @@ void ShaderGenPrinterHLSL::printPixelShaderOutputStruct(Stream& stream, const Ma
for( U32 i = 0; i < FEATUREMGR->getFeatureCount(); i++ )
{
const FeatureInfo &info = FEATUREMGR->getAt( i );
if( featureData.features.hasFeature( *info.type ) )
if ( featureData.features.hasFeature( *info.type ) )
numMRTs |= info.feature->getOutputTargets( featureData );
}
WRITESTR( "struct Fragout\r\n" );
WRITESTR( "{\r\n" );
WRITESTR( " float4 col : COLOR0;\r\n" );
for( U32 i = 1; i < 4; i++ )
WRITESTR("struct Fragout\r\n");
WRITESTR("{\r\n");
if (GFX->getAdapterType() == Direct3D11)
{
if( numMRTs & 1 << i )
WRITESTR( avar( " float4 col%d : COLOR%d;\r\n", i, i ) );
WRITESTR(" float4 col : SV_Target0;\r\n");
for (U32 i = 1; i < 4; i++)
{
if (numMRTs & 1 << i)
WRITESTR(avar(" float4 col%d : SV_Target%d;\r\n", i, i));
}
}
WRITESTR( "};\r\n" );
WRITESTR( "\r\n" );
WRITESTR( "\r\n" );
else
{
WRITESTR(" float4 col : COLOR0;\r\n");
for (U32 i = 1; i < 4; i++)
{
if (numMRTs & 1 << i)
WRITESTR(avar(" float4 col%d : COLOR%d;\r\n", i, i));
}
}
WRITESTR("};\r\n");
WRITESTR("\r\n");
WRITESTR("\r\n");
}
void ShaderGenPrinterHLSL::printPixelShaderCloser(Stream& stream)
@ -141,8 +153,8 @@ ShaderComponent* ShaderGenComponentFactoryHLSL::createVertexInputConnector( cons
}
else if ( element.isSemantic( GFXSemantic::TANGENTW ) )
{
var = vertComp->getIndexedElement( element.getSemanticIndex(), RT_TEXCOORD );
var->setName( "tangentW" );
var = vertComp->getIndexedElement(element.getSemanticIndex(), RT_TEXCOORD);
var->setName("tangentW");
}
else if ( element.isSemantic( GFXSemantic::BINORMAL ) )
{

View file

@ -119,6 +119,7 @@ MODULE_BEGIN( ShaderGenHLSL )
sInitDelegate.bind(_initShaderGenHLSL);
SHADERGEN->registerInitDelegate(Direct3D9, sInitDelegate);
SHADERGEN->registerInitDelegate(Direct3D9_360, sInitDelegate);
SHADERGEN->registerInitDelegate(Direct3D11, sInitDelegate);
}
MODULE_END;

View file

@ -99,6 +99,8 @@ Var::Var()
sampler = false;
mapsToSampler = false;
arraySize = 1;
texture = false;
rank = 0;
}
Var::Var( const char *inName, const char *inType )
@ -113,6 +115,8 @@ Var::Var( const char *inName, const char *inType )
texCoordNum = 0;
constSortPos = cspUninit;
arraySize = 1;
texture = false;
rank = 0;
setName( inName );
setType( inType );

View file

@ -120,8 +120,10 @@ struct Var : public LangElement
bool vertData; // argument coming from vertex data
bool connector; // variable that will be passed to pixel shader
bool sampler; // texture
bool texture; //for D3D11 texture variables
bool mapsToSampler; // for ps 1.x shaders - texcoords must be mapped to same sampler stage
U32 arraySize; // 1 = no array, > 1 array of "type"
U32 rank; // optional rank system to assist in sorting vars if needed
static U32 texUnitCount;
static U32 getTexUnitNum(U32 numElements = 1);

View file

@ -84,4 +84,9 @@ Var* ShaderFeature::findOrCreateLocal( const char *name,
}
return outVar;
}
void ShaderFeature::setInstancingFormat(GFXVertexFormat *format)
{
mInstancingFormat = format;
}

View file

@ -99,11 +99,10 @@ protected:
///
S32 mProcessIndex;
public:
// TODO: Make this protected and give it a proper API.
GFXVertexFormat *mInstancingFormat;
public:
//**************************************************************************
/*!
The Resources structure is used by ShaderFeature to indicate how many
@ -293,6 +292,8 @@ public:
static Var* findOrCreateLocal( const char *name,
const char *type,
MultiLine *multi );
// Set the instancing format
void setInstancingFormat(GFXVertexFormat *format);
};
#endif // _SHADERFEATURE_H_

View file

@ -264,7 +264,7 @@ void ShaderGen::_processVertFeatures( Vector<GFXShaderMacro> &macros, bool macro
if ( macrosOnly )
continue;
feature->mInstancingFormat = &mInstancingFormat;
feature->setInstancingFormat( &mInstancingFormat );
feature->processVert( mComponents, mFeatureData );
String line;
@ -304,7 +304,7 @@ void ShaderGen::_processPixFeatures( Vector<GFXShaderMacro> &macros, bool macros
if ( macrosOnly )
continue;
feature->mInstancingFormat = &mInstancingFormat;
feature->setInstancingFormat( &mInstancingFormat );
feature->processPix( mComponents, mFeatureData );
String line;
@ -488,8 +488,7 @@ GFXShader* ShaderGen::getShader( const MaterialFeatureData &featureData, const G
generateShader( featureData, vertFile, pixFile, &pixVersion, vertexFormat, cacheKey, shaderMacros );
GFXShader *shader = GFX->createShader();
shader->mInstancingFormat.copy( mInstancingFormat ); // TODO: Move to init() below!
if ( !shader->init( vertFile, pixFile, pixVersion, shaderMacros, samplers ) )
if (!shader->init(vertFile, pixFile, pixVersion, shaderMacros, samplers, &mInstancingFormat))
{
delete shader;
return NULL;