Engine directory for ticket #1

This commit is contained in:
DavidWyand-GG 2012-09-19 11:15:01 -04:00
parent 352279af7a
commit 7dbfe6994d
3795 changed files with 1363358 additions and 0 deletions

View file

@ -0,0 +1,461 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/HLSL/bumpHLSL.h"
#include "shaderGen/shaderOp.h"
#include "gfx/gfxDevice.h"
#include "materials/matInstance.h"
#include "materials/processedMaterial.h"
#include "materials/materialFeatureTypes.h"
#include "shaderGen/shaderGenVars.h"
void BumpFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
output = meta;
const bool useTexAnim = fd.features[MFT_TexAnim];
// Output the texture coord.
getOutTexCoord( "texCoord",
"float2",
true,
useTexAnim,
meta,
componentList );
if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
addOutDetailTexCoord( componentList,
meta,
useTexAnim );
// Also output the worldToTanget transform which
// we use to create the world space normal.
getOutWorldToTangent( componentList, meta, fd );
}
void BumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
output = meta;
// Get the texture coord.
Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
// Sample the bumpmap.
Var *bumpMap = getNormalMapTex();
LangElement *texOp = NULL;
// 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])
{
// This is a big block of code, so put a comment in the shader code
meta->addStatement( new GenOp( " // Atlased texture coordinate calculation (see BumpFeat*LSL for details)\r\n") );
Var *atlasedTex = new Var;
atlasedTex->setName("atlasedBumpCoord");
atlasedTex->setType( "float2" );
LangElement *atDecl = new DecOp( atlasedTex );
// Parameters of the texture atlas
Var *atParams = new Var;
atParams->setType( "float4" );
atParams->setName("bumpAtlasParams");
atParams->uniform = true;
atParams->constSortPos = cspPotentialPrimitive;
// Parameters of the texture (tile) this object is using in the atlas
Var *tileParams = new Var;
tileParams->setType( "float4" );
tileParams->setName("bumpAtlasTileParams");
tileParams->uniform = true;
tileParams->constSortPos = cspPotentialPrimitive;
const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
if(is_sm3)
{
// Figure out the mip level
meta->addStatement( new GenOp( " float2 _dx_bump = ddx(@ * @.z);\r\n", texCoord, atParams ) );
meta->addStatement( new GenOp( " float2 _dy_bump = ddy(@ * @.z);\r\n", texCoord, atParams ) );
meta->addStatement( new GenOp( " float mipLod_bump = 0.5 * log2(max(dot(_dx_bump, _dx_bump), dot(_dy_bump, _dy_bump)));\r\n" ) );
meta->addStatement( new GenOp( " mipLod_bump = clamp(mipLod_bump, 0.0, @.w);\r\n", atParams ) );
// And the size of the mip level
meta->addStatement( new GenOp( " float mipPixSz_bump = pow(2.0, @.w - mipLod_bump);\r\n", atParams ) );
meta->addStatement( new GenOp( " float2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams ) );
}
else
{
meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
}
// Tiling mode
if( true ) // Wrap
meta->addStatement( new GenOp( " @ = frac(@);\r\n", atDecl, texCoord ) );
else // Clamp
meta->addStatement( new GenOp( " @ = saturate(@);\r\n", atDecl, texCoord ) );
// Finally scale/offset, and correct for filtering
meta->addStatement( new GenOp( " @ = @ * ((mipSz_bump * @.xy - 1.0) / mipSz_bump) + 0.5 / mipSz_bump + @.xy * @.xy;\r\n",
atlasedTex, atlasedTex, atParams, atParams, tileParams ) );
// Add a newline
meta->addStatement( new GenOp( "\r\n" ) );
if(is_sm3)
{
texOp = new GenOp( "tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord );
}
else
{
texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
}
}
else
{
texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
}
Var *bumpNorm = new Var( "bumpNormal", "float4" );
meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) );
// If we have a detail normal map we add the xy coords of
// it to the base normal map. This gives us the effect we
// want with few instructions and minial artifacts.
if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
{
bumpMap = new Var;
bumpMap->setType( "sampler2D" );
bumpMap->setName( "detailBumpMap" );
bumpMap->uniform = true;
bumpMap->sampler = true;
bumpMap->constNum = Var::getTexUnitNum();
texCoord = getInTexCoord( "detCoord", "float2", true, componentList );
texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
Var *detailBump = new Var;
detailBump->setName( "detailBump" );
detailBump->setType( "float4" );
meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
Var *detailBumpScale = new Var;
detailBumpScale->setType( "float" );
detailBumpScale->setName( "detailBumpStrength" );
detailBumpScale->uniform = true;
detailBumpScale->constSortPos = cspPass;
meta->addStatement( new GenOp( " @.xy += @.xy * @;\r\n", bumpNorm, detailBump, detailBumpScale ) );
}
// We transform it into world space by reversing the
// multiplication by the worldToTanget transform.
Var *wsNormal = new Var( "wsNormal", "float3" );
Var *worldToTanget = getInWorldToTangent( componentList );
meta->addStatement( new GenOp( " @ = normalize( mul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );
}
ShaderFeature::Resources BumpFeatHLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
// If we have no parallax then we bring on the normal tex.
if ( !fd.features[MFT_Parallax] )
res.numTex = 1;
// Only the parallax or diffuse map will add texture
// coords other than us.
if ( !fd.features[MFT_Parallax] &&
!fd.features[MFT_DiffuseMap] &&
!fd.features[MFT_OverlayMap] &&
!fd.features[MFT_DetailMap] )
res.numTexReg++;
// We pass the world to tanget space transform.
res.numTexReg += 3;
// Do we have detail normal mapping?
if ( fd.features[MFT_DetailNormalMap] )
{
res.numTex++;
if ( !fd.features[MFT_DetailMap] )
res.numTexReg++;
}
return res;
}
void BumpFeatHLSL::setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex )
{
// If we had a parallax feature then it takes
// care of hooking up the normal map texture.
if ( fd.features[MFT_Parallax] )
return;
if ( fd.features[MFT_NormalMap] )
{
passData.mTexType[ texIndex ] = Material::Bump;
passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
}
if ( fd.features[ MFT_DetailNormalMap ] )
{
passData.mTexType[ texIndex ] = Material::DetailBump;
passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
}
}
ParallaxFeatHLSL::ParallaxFeatHLSL()
: mIncludeDep( "shaders/common/torque.hlsl" )
{
addDependency( &mIncludeDep );
}
Var* ParallaxFeatHLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
{
Var *theVar = (Var*)LangElement::find( name );
if ( !theVar )
{
theVar = new Var;
theVar->setType( type );
theVar->setName( name );
theVar->uniform = true;
theVar->constSortPos = csp;
}
return theVar;
}
void ParallaxFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
"ParallaxFeatHLSL::processVert - We don't support SM 1.x!" );
MultiLine *meta = new MultiLine;
// Add the texture coords.
getOutTexCoord( "texCoord",
"float2",
true,
fd.features[MFT_TexAnim],
meta,
componentList );
// Grab the input position.
Var *inPos = (Var*)LangElement::find( "inPosition" );
if ( !inPos )
inPos = (Var*)LangElement::find( "position" );
// Get the object space eye position and the
// object to tangent space transform.
Var *eyePos = _getUniformVar( "eyePos", "float3", cspPrimitive );
Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
// Now send the negative view vector in tangent space to the pixel shader.
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
outNegViewTS->setName( "outNegViewTS" );
outNegViewTS->setStructName( "OUT" );
outNegViewTS->setType( "float3" );
meta->addStatement( new GenOp( " @ = mul( @, float3( @.xyz - @ ) );\r\n",
outNegViewTS, objToTangentSpace, inPos, eyePos ) );
// TODO: I'm at a loss at why i need to flip the binormal/y coord
// to get a good view vector for parallax. Lighting works properly
// with the TS matrix as is... but parallax does not.
//
// Someone figure this out!
//
meta->addStatement( new GenOp( " @.y = -@.y;\r\n", outNegViewTS, outNegViewTS ) );
// If we have texture anim matrix the tangent
// space view vector may need to be rotated.
Var *texMat = (Var*)LangElement::find( "texMat" );
if ( texMat )
{
meta->addStatement( new GenOp( " @ = mul(@, float4(@,0)).xyz;\r\n",
outNegViewTS, texMat, outNegViewTS ) );
}
output = meta;
}
void ParallaxFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
"ParallaxFeatHLSL::processPix - We don't support SM 1.x!" );
MultiLine *meta = new MultiLine;
// Order matters... get this first!
Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
// We need the negative tangent space view vector
// as in parallax mapping we step towards the camera.
Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
if ( !negViewTS )
{
Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
if ( !inNegViewTS )
{
inNegViewTS = connectComp->getElement( RT_TEXCOORD );
inNegViewTS->setName( "outNegViewTS" );
inNegViewTS->setStructName( "IN" );
inNegViewTS->setType( "float3" );
}
negViewTS = new Var( "negViewTS", "float3" );
meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
}
// Get the rest of our inputs.
Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float", cspPotentialPrimitive );
Var *normalMap = getNormalMapTex();
// Call the library function to do the rest.
meta->addStatement( new GenOp( " @.xy += parallaxOffset( @, @.xy, @, @ );\r\n",
texCoord, normalMap, texCoord, negViewTS, parallaxInfo ) );
// TODO: Fix second UV maybe?
output = meta;
}
ShaderFeature::Resources ParallaxFeatHLSL::getResources( const MaterialFeatureData &fd )
{
AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
"ParallaxFeatHLSL::getResources - We don't support SM 1.x!" );
Resources res;
// We add the outViewTS to the outputstructure.
res.numTexReg = 1;
// If this isn't a prepass then we will be
// creating the normal map here.
if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
res.numTex = 1;
return res;
}
void ParallaxFeatHLSL::setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex )
{
AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
"ParallaxFeatHLSL::setTexData - We don't support SM 1.x!" );
GFXTextureObject *tex = stageDat.getTex( MFT_NormalMap );
if ( tex )
{
passData.mTexType[ texIndex ] = Material::Bump;
passData.mTexSlot[ texIndex++ ].texObject = tex;
}
}
void NormalsOutFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
// If we have normal maps then we can count
// on it to generate the world space normal.
if ( fd.features[MFT_NormalMap] )
return;
MultiLine *meta = new MultiLine;
output = meta;
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outNormal = connectComp->getElement( RT_TEXCOORD );
outNormal->setName( "wsNormal" );
outNormal->setStructName( "OUT" );
outNormal->setType( "float3" );
outNormal->mapsToSampler = false;
// Find the incoming vertex normal.
Var *inNormal = (Var*)LangElement::find( "normal" );
if ( inNormal )
{
// Transform the normal to world space.
Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
meta->addStatement( new GenOp( " @ = mul( @, normalize( @ ) );\r\n", outNormal, objTrans, inNormal ) );
}
else
{
// If we don't have a vertex normal... just pass the
// camera facing normal to the pixel shader.
meta->addStatement( new GenOp( " @ = float3( 0.0, 0.0, 1.0 );\r\n", outNormal ) );
}
}
void NormalsOutFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
output = meta;
Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
if ( !wsNormal )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
wsNormal = connectComp->getElement( RT_TEXCOORD );
wsNormal->setName( "wsNormal" );
wsNormal->setStructName( "IN" );
wsNormal->setType( "float3" );
// If we loaded the normal its our resposibility
// to normalize it... the interpolators won't.
//
// Note we cast to half here to get partial precision
// optimized code which is an acceptable loss of
// precision for normals and performs much better
// on older Geforce cards.
//
meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
}
LangElement *normalOut;
Var *outColor = (Var*)LangElement::find( "col" );
if ( outColor && !fd.features[MFT_AlphaTest] )
normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, @.a )", wsNormal, outColor );
else
normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, 1 )", wsNormal );
meta->addStatement( new GenOp( " @;\r\n",
assignColor( normalOut, Material::None ) ) );
}

View file

@ -0,0 +1,106 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _BUMP_HLSL_H_
#define _BUMP_HLSL_H_
#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#endif
#ifndef _LANG_ELEMENT_H_
#include "shaderGen/langElement.h"
#endif
struct RenderPassData;
class MultiLine;
/// The Bumpmap feature will read the normal map and
/// transform it by the inverse of the worldToTanget
/// matrix. This normal is then used by subsequent
/// shader features.
class BumpFeatHLSL : public ShaderFeatureHLSL
{
public:
// ShaderFeatureHLSL
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
virtual Resources getResources( const MaterialFeatureData &fd );
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName() { return "Bumpmap"; }
};
/// This feature either generates the cheap yet effective offset
/// mapping style parallax or the much more expensive occlusion
/// mapping technique based on the enabled feature flags.
class ParallaxFeatHLSL : public ShaderFeatureHLSL
{
protected:
static Var* _getUniformVar( const char *name,
const char *type,
ConstantSortPosition csp );
ShaderIncludeDependency mIncludeDep;
public:
ParallaxFeatHLSL();
// ShaderFeatureHLSL
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName() { return "Parallax"; }
};
/// This feature is used to render normals to the
/// diffuse target for imposter rendering.
class NormalsOutFeatHLSL : public ShaderFeatureHLSL
{
public:
// ShaderFeatureHLSL
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
virtual String getName() { return "NormalsOut"; }
};
#endif // _BUMP_HLSL_H_

View file

@ -0,0 +1,173 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/HLSL/depthHLSL.h"
#include "materials/materialFeatureTypes.h"
#include "materials/materialFeatureData.h"
void EyeSpaceDepthOutHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
output = meta;
// grab output
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *outWSEyeVec = connectComp->getElement( RT_TEXCOORD );
outWSEyeVec->setName( "wsEyeVec" );
outWSEyeVec->setStructName( "OUT" );
// grab incoming vert position
Var *wsPosition = new Var( "depthPos", "float3" );
getWsPosition( componentList, fd.features[MFT_UseInstancing], meta, new DecOp( wsPosition ) );
Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
if( !eyePos )
{
eyePos = new Var;
eyePos->setType("float3");
eyePos->setName("eyePosWorld");
eyePos->uniform = true;
eyePos->constSortPos = cspPass;
}
meta->addStatement( new GenOp( " @ = float4( @.xyz - @, 1 );\r\n", outWSEyeVec, wsPosition, eyePos ) );
}
void EyeSpaceDepthOutHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
// grab connector position
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
Var *wsEyeVec = connectComp->getElement( RT_TEXCOORD );
wsEyeVec->setName( "wsEyeVec" );
wsEyeVec->setStructName( "IN" );
wsEyeVec->setType( "float4" );
wsEyeVec->mapsToSampler = false;
wsEyeVec->uniform = false;
// get shader constants
Var *vEye = new Var;
vEye->setType("float3");
vEye->setName("vEye");
vEye->uniform = true;
vEye->constSortPos = cspPass;
// Expose the depth to the depth format feature
Var *depthOut = new Var;
depthOut->setType("float");
depthOut->setName(getOutputVarName());
LangElement *depthOutDecl = new DecOp( depthOut );
meta->addStatement( new GenOp( "#ifndef CUBE_SHADOW_MAP\r\n" ) );
meta->addStatement( new GenOp( " @ = dot(@, (@.xyz / @.w));\r\n", depthOutDecl, vEye, wsEyeVec, wsEyeVec ) );
meta->addStatement( new GenOp( "#else\r\n" ) );
Var *farDist = (Var*)Var::find( "oneOverFarplane" );
if ( !farDist )
{
farDist = new Var;
farDist->setType("float4");
farDist->setName("oneOverFarplane");
farDist->uniform = true;
farDist->constSortPos = cspPass;
}
meta->addStatement( new GenOp( " @ = length( @.xyz / @.w ) * @.x;\r\n", depthOutDecl, wsEyeVec, wsEyeVec, farDist ) );
meta->addStatement( new GenOp( "#endif\r\n" ) );
// If there isn't an output conditioner for the pre-pass, than just write
// out the depth to rgba and return.
if( !fd.features[MFT_PrePassConditioner] )
meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "float4(@.rrr,1)", depthOut ), Material::None ) ) );
output = meta;
}
ShaderFeature::Resources EyeSpaceDepthOutHLSL::getResources( const MaterialFeatureData &fd )
{
Resources temp;
// Passing from VS->PS:
// - world space position (wsPos)
temp.numTexReg = 1;
return temp;
}
void DepthOutHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
// Grab the output vert.
Var *outPosition = (Var*)LangElement::find( "hpos" );
// Grab our output depth.
Var *outDepth = connectComp->getElement( RT_TEXCOORD );
outDepth->setName( "depth" );
outDepth->setStructName( "OUT" );
outDepth->setType( "float" );
output = new GenOp( " @ = @.z / @.w;\r\n", outDepth, outPosition, outPosition );
}
void DepthOutHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
// grab connector position
Var *depthVar = connectComp->getElement( RT_TEXCOORD );
depthVar->setName( "depth" );
depthVar->setStructName( "IN" );
depthVar->setType( "float" );
depthVar->mapsToSampler = false;
depthVar->uniform = false;
/*
// Expose the depth to the depth format feature
Var *depthOut = new Var;
depthOut->setType("float");
depthOut->setName(getOutputVarName());
*/
LangElement *depthOut = new GenOp( "float4( @, 0, 0, 1 )", depthVar );
output = new GenOp( " @;\r\n", assignColor( depthOut, Material::None ) );
}
ShaderFeature::Resources DepthOutHLSL::getResources( const MaterialFeatureData &fd )
{
// We pass the depth to the pixel shader.
Resources temp;
temp.numTexReg = 1;
return temp;
}

View file

@ -0,0 +1,60 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _DEPTH_HLSL_H_
#define _DEPTH_HLSL_H_
#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#endif
#ifndef _SHADEROP_H_
#include "shaderGen/shaderOp.h"
#endif
class EyeSpaceDepthOutHLSL : public ShaderFeatureHLSL
{
public:
// ShaderFeature
virtual void processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual String getName() { return "Eye Space Depth (Out)"; }
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual const char* getOutputVarName() const { return "eyeSpaceDepth"; }
};
class DepthOutHLSL : public ShaderFeatureHLSL
{
public:
// ShaderFeature
virtual void processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual String getName() { return "Depth (Out)"; }
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual const char* getOutputVarName() const { return "IN.depth"; }
};
#endif // _DEPTH_HLSL_H_

View file

@ -0,0 +1,163 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/HLSL/paraboloidHLSL.h"
#include "lighting/lightInfo.h"
#include "materials/sceneData.h"
#include "materials/materialFeatureTypes.h"
#include "materials/materialFeatureData.h"
#include "gfx/gfxShader.h"
void ParaboloidVertTransformHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
MultiLine *meta = new MultiLine;
// First check for an input position from a previous feature
// then look for the default vertex position.
Var *inPosition = (Var*)LangElement::find( "inPosition" );
if ( !inPosition )
inPosition = (Var*)LangElement::find( "position" );
const bool isSinglePass = fd.features[ MFT_IsSinglePassParaboloid ];
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
// Grab connector out position.
Var *outPosition = connectComp->getElement( RT_POSITION );
outPosition->setName( "hpos" );
outPosition->setStructName( "OUT" );
// Get the atlas scale.
Var *atlasScale = new Var;
atlasScale->setType( "float2" );
atlasScale->setName( "atlasScale" );
atlasScale->uniform = true;
atlasScale->constSortPos = cspPass;
// Transform into camera space
Var *worldViewOnly = getWorldView( componentList, fd.features[MFT_UseInstancing], meta );
// So what we're doing here is transforming into camera space, and
// then directly manipulate into shadowmap space.
//
// http://www.gamedev.net/reference/articles/article2308.asp
// Swizzle z and y post-transform
meta->addStatement( new GenOp( " @ = mul(@, float4(@.xyz,1)).xzyw;\r\n", outPosition, worldViewOnly, inPosition ) );
meta->addStatement( new GenOp( " float L = length(@.xyz);\r\n", outPosition ) );
if ( isSinglePass )
{
// Flip the z in the back case
Var *outIsBack = connectComp->getElement( RT_TEXCOORD );
outIsBack->setType( "float" );
outIsBack->setName( "isBack" );
outIsBack->setStructName( "OUT" );
meta->addStatement( new GenOp( " bool isBack = @.z < 0.0;\r\n", outPosition ) );
meta->addStatement( new GenOp( " @ = isBack ? -1.0 : 1.0;\r\n", outIsBack ) );
meta->addStatement( new GenOp( " if ( isBack ) @.z = -@.z;\r\n", outPosition, outPosition ) );
}
meta->addStatement( new GenOp( " @ /= L;\r\n", outPosition ) );
meta->addStatement( new GenOp( " @.z = @.z + 1.0;\r\n", outPosition, outPosition ) );
meta->addStatement( new GenOp( " @.xy /= @.z;\r\n", outPosition, outPosition ) );
// Get the light parameters.
Var *lightParams = new Var;
lightParams->setType( "float4" );
lightParams->setName( "lightParams" );
lightParams->uniform = true;
lightParams->constSortPos = cspPass;
// TODO: If we change other shadow shaders to write out
// linear depth, than fix this as well!
//
// (L - zNear)/(lightParams.x - zNear);
//
meta->addStatement( new GenOp( " @.z = L / @.x;\r\n", outPosition, lightParams ) );
meta->addStatement( new GenOp( " @.w = 1.0;\r\n", outPosition ) );
// Pass unmodified to pixel shader to allow it to clip properly.
Var *outPosXY = connectComp->getElement( RT_TEXCOORD );
outPosXY->setType( "float2" );
outPosXY->setName( "posXY" );
outPosXY->setStructName( "OUT" );
meta->addStatement( new GenOp( " @ = @.xy;\r\n", outPosXY, outPosition ) );
// Scale and offset so it shows up in the atlas properly.
meta->addStatement( new GenOp( " @.xy *= @.xy;\r\n", outPosition, atlasScale ) );
if ( isSinglePass )
meta->addStatement( new GenOp( " @.x += isBack ? 0.5 : -0.5;\r\n", outPosition ) );
else
{
Var *atlasOffset = new Var;
atlasOffset->setType( "float2" );
atlasOffset->setName( "atlasXOffset" );
atlasOffset->uniform = true;
atlasOffset->constSortPos = cspPass;
meta->addStatement( new GenOp( " @.xy += @;\r\n", outPosition, atlasOffset ) );
}
output = meta;
}
void ParaboloidVertTransformHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
MultiLine *meta = new MultiLine;
const bool isSinglePass = fd.features[ MFT_IsSinglePassParaboloid ];
if ( isSinglePass )
{
// Cull things on the back side of the map.
Var *isBack = connectComp->getElement( RT_TEXCOORD );
isBack->setName( "isBack" );
isBack->setStructName( "IN" );
isBack->setType( "float" );
meta->addStatement( new GenOp( " clip( abs( @ ) - 0.999 );\r\n", isBack ) );
}
// Cull pixels outside of the valid paraboloid.
Var *posXY = connectComp->getElement( RT_TEXCOORD );
posXY->setName( "posXY" );
posXY->setStructName( "IN" );
posXY->setType( "float2" );
meta->addStatement( new GenOp( " clip( 1.0 - abs(@.x) );\r\n", posXY ) );
output = meta;
}
ShaderFeature::Resources ParaboloidVertTransformHLSL::getResources( const MaterialFeatureData &fd )
{
Resources temp;
temp.numTexReg = 2;
return temp;
}

View file

@ -0,0 +1,48 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _PARABOLOID_HLSL_H_
#define _PARABOLOID_HLSL_H_
#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#endif
#ifndef _SHADEROP_H_
#include "shaderGen/shaderOp.h"
#endif
class GFXShaderConstHandle;
class ParaboloidVertTransformHLSL : public ShaderFeatureHLSL
{
public:
// ShaderFeature
virtual void processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual String getName() { return "Paraboloid Vert Transform"; }
virtual Material::BlendOp getBlendOp() { return Material::None; }
};
#endif // _PARABOLOID_HLSL_H_

View file

@ -0,0 +1,152 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/HLSL/pixSpecularHLSL.h"
#include "materials/processedMaterial.h"
#include "materials/materialFeatureTypes.h"
#include "shaderGen/shaderOp.h"
#include "shaderGen/shaderGenVars.h"
#include "gfx/gfxStructs.h"
PixelSpecularHLSL::PixelSpecularHLSL()
: mDep( "shaders/common/lighting.hlsl" )
{
addDependency( &mDep );
}
void PixelSpecularHLSL::processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
AssertFatal( fd.features[MFT_RTLighting],
"PixelSpecularHLSL requires RTLighting to be enabled!" );
// Nothing to do here... MFT_RTLighting should have
// taken care of passing everything to the pixel shader.
}
void PixelSpecularHLSL::processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd )
{
AssertFatal( fd.features[MFT_RTLighting],
"PixelSpecularHLSL requires RTLighting to be enabled!" );
// RTLighting should have spit out the 4 specular
// powers for the 4 potential lights on this pass.
//
// This can sometimes be NULL if RTLighting skips out
// on us for lightmaps or missing normals.
Var *specular = (Var*)LangElement::find( "specular" );
if ( !specular )
return;
MultiLine *meta = new MultiLine;
LangElement *specMul = new GenOp( "@", specular );
LangElement *final = specMul;
// mask out with lightmap if present
if ( fd.features[MFT_LightMap] )
{
LangElement *lmColor = NULL;
// find lightmap color
lmColor = LangElement::find( "lmColor" );
if ( !lmColor )
{
LangElement * lightMap = LangElement::find( "lightMap" );
LangElement * lmCoord = LangElement::find( "texCoord2" );
lmColor = new GenOp( "tex2D(@, @)", lightMap, lmCoord );
}
final = new GenOp( "@ * float4(@.rgb,0)", specMul, lmColor );
}
// If we have a normal map then mask the specular
if ( fd.features[MFT_SpecularMap] )
{
Var *specularColor = (Var*)LangElement::find( "specularColor" );
if (specularColor)
final = new GenOp( "@ * @", final, specularColor );
}
else if ( fd.features[MFT_NormalMap] && !fd.features[MFT_IsDXTnm] )
{
Var *bumpColor = (Var*)LangElement::find( "bumpNormal" );
final = new GenOp( "@ * @.a", final, bumpColor );
}
// Add the specular to the final color.
// search for color var
Var *color = (Var*)LangElement::find( "col" );
meta->addStatement( new GenOp( " @.rgb += ( @ ).rgb;\r\n", color, final ) );
output = meta;
}
ShaderFeature::Resources PixelSpecularHLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
return res;
}
void SpecularMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
{
// Get the texture coord.
Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
// create texture var
Var *specularMap = new Var;
specularMap->setType( "sampler2D" );
specularMap->setName( "specularMap" );
specularMap->uniform = true;
specularMap->sampler = true;
specularMap->constNum = Var::getTexUnitNum();
LangElement *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 )
{
Resources res;
res.numTex = 1;
return res;
}
void SpecularMapHLSL::setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex )
{
GFXTextureObject *tex = stageDat.getTex( MFT_SpecularMap );
if ( tex )
{
passData.mTexType[ texIndex ] = Material::Standard;
passData.mTexSlot[ texIndex++ ].texObject = tex;
}
}

View file

@ -0,0 +1,77 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _PIXSPECULAR_HLSL_H_
#define _PIXSPECULAR_HLSL_H_
#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#endif
/// A per-pixel specular feature.
class PixelSpecularHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mDep;
public:
PixelSpecularHLSL();
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual String getName()
{
return "Pixel Specular";
}
};
/// A texture source for the PixSpecular feature
class SpecularMapHLSL : public ShaderFeatureHLSL
{
public:
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Specular Map";
}
};
#endif // _PIXSPECULAR_HLSL_H_

View file

@ -0,0 +1,349 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/HLSL/shaderCompHLSL.h"
#include "shaderGen/shaderComp.h"
#include "shaderGen/langElement.h"
#include "gfx/gfxDevice.h"
Var * ShaderConnectorHLSL::getElement( RegisterType type,
U32 numElements,
U32 numRegisters )
{
Var *ret = getIndexedElement( mCurTexElem, type, numElements, numRegisters );
// Adjust texture offset if this is a texcoord type
if( type == RT_TEXCOORD )
{
if ( numRegisters != -1 )
mCurTexElem += numRegisters;
else
mCurTexElem += numElements;
}
return ret;
}
Var * ShaderConnectorHLSL::getIndexedElement( U32 index, RegisterType type, U32 numElements /*= 1*/, U32 numRegisters /*= -1 */ )
{
switch( type )
{
case RT_POSITION:
{
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "POSITION" );
return newVar;
}
case RT_NORMAL:
{
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "NORMAL" );
return newVar;
}
case RT_BINORMAL:
{
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "BINORMAL" );
return newVar;
}
case RT_TANGENT:
{
Var *newVar = new Var;
mElementList.push_back( newVar );
newVar->setConnectName( "TANGENT" );
return newVar;
}
case RT_COLOR:
{
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" );
return newVar;
}
case RT_TEXCOORD:
{
Var *newVar = new Var;
mElementList.push_back( newVar );
// This was needed for hardware instancing, but
// i don't really remember why right now.
if ( index > mCurTexElem )
mCurTexElem = index + 1;
char out[32];
dSprintf( (char*)out, sizeof(out), "TEXCOORD%d", index );
newVar->setConnectName( out );
newVar->constNum = index;
newVar->arraySize = numElements;
return newVar;
}
default:
break;
}
return NULL;
}
void ShaderConnectorHLSL::sortVars()
{
if ( GFX->getPixelShaderVersion() >= 2.0 )
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++;
}
}
}
}
void ShaderConnectorHLSL::setName( char *newName )
{
dStrcpy( (char*)mName, newName );
}
void ShaderConnectorHLSL::reset()
{
for( U32 i=0; i<mElementList.size(); i++ )
{
mElementList[i] = NULL;
}
mElementList.setSize( 0 );
mCurTexElem = 0;
}
void ShaderConnectorHLSL::print( Stream &stream )
{
const char * header = "struct ";
const char * header2 = "\r\n{\r\n";
const char * footer = "};\r\n\r\n\r\n";
stream.write( dStrlen(header), header );
stream.write( dStrlen((char*)mName), mName );
stream.write( dStrlen(header2), header2 );
// print out elements
for( U32 i=0; i<mElementList.size(); i++ )
{
U8 output[256];
Var *var = mElementList[i];
if (var->arraySize <= 1)
dSprintf( (char*)output, sizeof(output), " %s %-15s : %s;\r\n", var->type, var->name, var->connectName );
else
dSprintf( (char*)output, sizeof(output), " %s %s[%d] : %s;\r\n", var->type, var->name, var->arraySize, var->connectName );
stream.write( dStrlen((char*)output), output );
}
stream.write( dStrlen(footer), footer );
}
void ParamsDefHLSL::assignConstantNumbers()
{
// Here we assign constant number to uniform vars, sorted
// by their update frequency.
U32 mCurrConst = 0;
for (U32 bin = cspUninit+1; bin < csp_Count; bin++)
{
// Find all the uniform variables that are part of this group and assign constant numbers
for( U32 i=0; i<LangElement::elementList.size(); i++)
{
Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
if( var )
{
bool shaderConst = var->uniform && !var->sampler;
AssertFatal((!shaderConst) || var->constSortPos != cspUninit, "Const sort position has not been set, variable will not receive a constant number!!");
if( shaderConst && var->constSortPos == bin)
{
var->constNum = mCurrConst;
// Increment our constant number based on the variable type
if (dStrcmp((const char*)var->type, "float4x4") == 0)
{
mCurrConst += (4 * var->arraySize);
} else {
if (dStrcmp((const char*)var->type, "float3x3") == 0)
{
mCurrConst += (3 * var->arraySize);
} else {
mCurrConst += var->arraySize;
}
}
}
}
}
}
}
void VertexParamsDefHLSL::print( Stream &stream )
{
assignConstantNumbers();
const char *opener = "ConnectData main( VertData IN";
stream.write( dStrlen(opener), opener );
// find all the uniform variables and print them out
for( U32 i=0; i<LangElement::elementList.size(); i++)
{
Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
if( var )
{
if( var->uniform )
{
const char* nextVar = ",\r\n ";
stream.write( dStrlen(nextVar), nextVar );
U8 varNum[64];
dSprintf( (char*)varNum, sizeof(varNum), "register(C%d)", var->constNum );
U8 output[256];
if (var->arraySize <= 1)
dSprintf( (char*)output, sizeof(output), "uniform %-8s %-15s : %s", var->type, var->name, varNum );
else
dSprintf( (char*)output, sizeof(output), "uniform %-8s %s[%d] : %s", var->type, var->name, var->arraySize, varNum );
stream.write( dStrlen((char*)output), output );
}
}
}
const char *closer = "\r\n)\r\n{\r\n ConnectData OUT;\r\n\r\n";
stream.write( dStrlen(closer), closer );
}
void PixelParamsDefHLSL::print( Stream &stream )
{
assignConstantNumbers();
const char * opener = "Fragout main( ConnectData IN";
stream.write( dStrlen(opener), opener );
// find all the sampler & uniform variables and print them out
for( U32 i=0; i<LangElement::elementList.size(); i++)
{
Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
if( var )
{
if( var->uniform )
{
WRITESTR( ",\r\n " );
U8 varNum[32];
if( var->sampler )
{
dSprintf( (char*)varNum, sizeof(varNum), "register(S%d)", var->constNum );
}
else
{
dSprintf( (char*)varNum, sizeof(varNum), "register(C%d)", var->constNum );
}
U8 output[256];
if (var->arraySize <= 1)
dSprintf( (char*)output, sizeof(output), "uniform %-9s %-15s : %s", var->type, var->name, varNum );
else
dSprintf( (char*)output, sizeof(output), "uniform %-9s %s[%d] : %s", var->type, var->name, var->arraySize, varNum );
WRITESTR( (char*) output );
}
}
}
const char *closer = "\r\n)\r\n{\r\n Fragout OUT;\r\n\r\n";
stream.write( dStrlen(closer), closer );
}

View file

@ -0,0 +1,72 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _SHADERCOMP_HLSL_H_
#define _SHADERCOMP_HLSL_H_
#ifndef _SHADERCOMP_H_
#include "shaderGen/shaderComp.h"
#endif
class ShaderConnectorHLSL : public ShaderConnector
{
public:
// ShaderConnector
virtual Var* getElement( RegisterType type,
U32 numElements = 1,
U32 numRegisters = -1 );
virtual Var* getIndexedElement( U32 index,
RegisterType type,
U32 numElements = 1,
U32 numRegisters = -1 );
virtual void setName( char *newName );
virtual void reset();
virtual void sortVars();
virtual void print( Stream &stream );
};
class ParamsDefHLSL : public ParamsDef
{
protected:
virtual void assignConstantNumbers();
};
class VertexParamsDefHLSL : public ParamsDefHLSL
{
public:
virtual void print( Stream &stream );
};
class PixelParamsDefHLSL : public ParamsDefHLSL
{
public:
virtual void print( Stream &stream );
};
#endif // _SHADERCOMP_HLSL_H_

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,654 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#define _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#ifndef _SHADERFEATURE_H_
#include "shaderGen/shaderFeature.h"
#endif
struct LangElement;
struct MaterialFeatureData;
struct RenderPassData;
class ShaderFeatureHLSL : public ShaderFeature
{
public:
ShaderFeatureHLSL();
///
Var* getOutTexCoord( const char *name,
const char *type,
bool mapsToSampler,
bool useTexAnim,
MultiLine *meta,
Vector<ShaderComponent*> &componentList );
/// Returns an input texture coord by name adding it
/// to the input connector if it doesn't exist.
static Var* getInTexCoord( const char *name,
const char *type,
bool mapsToSampler,
Vector<ShaderComponent*> &componentList );
static Var* getInColor( const char *name,
const char *type,
Vector<ShaderComponent*> &componentList );
///
static Var* addOutVpos( MultiLine *meta,
Vector<ShaderComponent*> &componentList );
/// Returns the VPOS input register for the pixel shader.
static Var* getInVpos( MultiLine *meta,
Vector<ShaderComponent*> &componentList );
/// Returns the "objToTangentSpace" transform or creates one if this
/// is the first feature to need it.
Var* getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
const MaterialFeatureData &fd );
/// Returns the existing output "outWorldToTangent" transform or
/// creates one if this is the first feature to need it.
Var* getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
const MaterialFeatureData &fd );
/// Returns the input "worldToTanget" space transform
/// adding it to the input connector if it doesn't exist.
static Var* getInWorldToTangent( Vector<ShaderComponent*> &componentList );
/// Returns the existing output "outViewToTangent" transform or
/// creates one if this is the first feature to need it.
Var* getOutViewToTangent( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
const MaterialFeatureData &fd );
/// Returns the input "viewToTangent" space transform
/// adding it to the input connector if it doesn't exist.
static Var* getInViewToTangent( Vector<ShaderComponent*> &componentList );
/// Calculates the world space position in the vertex shader and
/// assigns it to the passed language element. It does not pass
/// it across the connector to the pixel shader.
/// @see addOutWsPosition
void getWsPosition( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta,
LangElement *wsPosition );
/// Adds the "wsPosition" to the input connector if it doesn't exist.
Var* addOutWsPosition( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta );
/// Returns the input world space position from the connector.
static Var* getInWsPosition( Vector<ShaderComponent*> &componentList );
/// Returns the world space view vector from the wsPosition.
static Var* getWsView( Var *wsPosition, MultiLine *meta );
/// Returns the input normal map texture.
static Var* getNormalMapTex();
///
Var* addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
MultiLine *meta,
bool useTexAnim );
///
Var* getObjTrans( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta );
///
Var* getModelView( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta );
///
Var* getWorldView( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta );
///
Var* getInvWorldView( Vector<ShaderComponent*> &componentList,
bool useInstancing,
MultiLine *meta );
// ShaderFeature
Var* getVertTexCoord( const String &name );
LangElement* setupTexSpaceMat( Vector<ShaderComponent*> &componentList, Var **texSpaceMat );
LangElement* assignColor( LangElement *elem, Material::BlendOp blend, LangElement *lerpElem = NULL, ShaderFeature::OutputTarget outputTarget = ShaderFeature::DefaultTarget );
LangElement* expandNormalMap( LangElement *sampleNormalOp, LangElement *normalDecl, LangElement *normalVar, const MaterialFeatureData &fd );
};
class NamedFeatureHLSL : public ShaderFeatureHLSL
{
protected:
String mName;
public:
NamedFeatureHLSL( const String &name )
: mName( name )
{}
virtual String getName() { return mName; }
};
class RenderTargetZeroHLSL : public ShaderFeatureHLSL
{
protected:
ShaderFeature::OutputTarget mOutputTargetMask;
String mFeatureName;
public:
RenderTargetZeroHLSL( const ShaderFeature::OutputTarget target )
: mOutputTargetMask( target )
{
char buffer[256];
dSprintf(buffer, sizeof(buffer), "Render Target Output = 0.0, output mask %04b", mOutputTargetMask);
mFeatureName = buffer;
}
virtual String getName() { return mFeatureName; }
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const { return mOutputTargetMask; }
};
/// Vertex position
class VertPositionHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual String getName()
{
return "Vert Position";
}
virtual void determineFeature( Material *material,
const GFXVertexFormat *vertexFormat,
U32 stageNum,
const FeatureType &type,
const FeatureSet &features,
MaterialFeatureData *outFeatureData );
};
/// Vertex lighting based on the normal and the light
/// direction passed through the vertex color.
class RTLightingFeatHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mDep;
public:
RTLightingFeatHLSL();
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::None; }
virtual Resources getResources( const MaterialFeatureData &fd );
virtual String getName()
{
return "RT Lighting";
}
};
/// Base texture
class DiffuseMapFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
virtual Resources getResources( const MaterialFeatureData &fd );
// Sets textures and texture flags for current pass
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Base Texture";
}
};
/// Overlay texture
class OverlayTexFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
virtual Resources getResources( const MaterialFeatureData &fd );
// Sets textures and texture flags for current pass
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Overlay Texture";
}
};
/// Diffuse color
class DiffuseFeatureHLSL : public ShaderFeatureHLSL
{
public:
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::None; }
virtual String getName()
{
return "Diffuse Color";
}
};
/// Diffuse vertex color
class DiffuseVertColorFeatureHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector< ShaderComponent* >& componentList,
const MaterialFeatureData& fd );
virtual void processPix( Vector< ShaderComponent* >&componentList,
const MaterialFeatureData& fd );
virtual Material::BlendOp getBlendOp(){ return Material::None; }
virtual String getName()
{
return "Diffuse Vertex Color";
}
};
/// Lightmap
class LightmapFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
virtual Resources getResources( const MaterialFeatureData &fd );
// Sets textures and texture flags for current pass
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Lightmap";
}
virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
};
/// Tonemap
class TonemapFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }
virtual Resources getResources( const MaterialFeatureData &fd );
// Sets textures and texture flags for current pass
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Tonemap";
}
virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
};
/// Baked lighting stored on the vertex color
class VertLitHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::None; }
virtual String getName()
{
return "Vert Lit";
}
virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const;
};
/// Detail map
class DetailFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp(){ return Material::Mul; }
// Sets textures and texture flags for current pass
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Detail";
}
};
/// Reflect Cubemap
class ReflectCubeFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
// Sets textures and texture flags for current pass
virtual void setTexData( Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex );
virtual String getName()
{
return "Reflect Cube";
}
};
/// Fog
class FogFeatHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mFogDep;
public:
FogFeatHLSL();
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp() { return Material::LerpAlpha; }
virtual String getName()
{
return "Fog";
}
};
/// Tex Anim
class TexAnimHLSL : public ShaderFeatureHLSL
{
public:
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual String getName()
{
return "Texture Animation";
}
};
/// Visibility
class VisibilityFeatHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mTorqueDep;
public:
VisibilityFeatHLSL();
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Resources getResources( const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual String getName()
{
return "Visibility";
}
};
///
class AlphaTestHLSL : public ShaderFeatureHLSL
{
public:
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual String getName()
{
return "Alpha Test";
}
};
/// Special feature used to mask out the RGB color for
/// non-glow passes of glow materials.
/// @see RenderGlowMgr
class GlowMaskHLSL : public ShaderFeatureHLSL
{
public:
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual String getName()
{
return "Glow Mask";
}
};
/// This should be the final feature on most pixel shaders which
/// encodes the color for the current HDR target format.
/// @see HDRPostFx
/// @see LightManager
/// @see torque.hlsl
class HDROutHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mTorqueDep;
public:
HDROutHLSL();
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual Material::BlendOp getBlendOp() { return Material::None; }
virtual String getName() { return "HDR Output"; }
};
///
class FoliageFeatureHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mDep;
public:
FoliageFeatureHLSL();
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual String getName()
{
return "Foliage Feature";
}
virtual void determineFeature( Material *material,
const GFXVertexFormat *vertexFormat,
U32 stageNum,
const FeatureType &type,
const FeatureSet &features,
MaterialFeatureData *outFeatureData );
virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject );
};
class ParticleNormalFeatureHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual String getName()
{
return "Particle Normal Generation Feature";
}
};
/// Special feature for unpacking imposter verts.
/// @see RenderImposterMgr
class ImposterVertFeatureHLSL : public ShaderFeatureHLSL
{
protected:
ShaderIncludeDependency mDep;
public:
ImposterVertFeatureHLSL();
virtual void processVert( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual String getName() { return "Imposter Vert"; }
virtual void determineFeature( Material *material,
const GFXVertexFormat *vertexFormat,
U32 stageNum,
const FeatureType &type,
const FeatureSet &features,
MaterialFeatureData *outFeatureData );
};
#endif // _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_

View file

@ -0,0 +1,199 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/HLSL/shaderGenHLSL.h"
#include "shaderGen/HLSL/shaderCompHLSL.h"
#include "shaderGen/featureMgr.h"
void ShaderGenPrinterHLSL::printShaderHeader(Stream& stream)
{
const char *header1 = "//*****************************************************************************\r\n";
const char *header2 = "// Torque -- HLSL procedural shader\r\n";
stream.write( dStrlen(header1), header1 );
stream.write( dStrlen(header2), header2 );
stream.write( dStrlen(header1), header1 );
const char* header3 = "\r\n";
stream.write( dStrlen(header3), header3 );
}
void ShaderGenPrinterHLSL::printMainComment(Stream& stream)
{
const char * header5 = "// Main\r\n";
const char * line = "//-----------------------------------------------------------------------------\r\n";
stream.write( dStrlen(line), line );
stream.write( dStrlen(header5), header5 );
stream.write( dStrlen(line), line );
}
void ShaderGenPrinterHLSL::printVertexShaderCloser(Stream& stream)
{
const char *closer = " return OUT;\r\n}\r\n";
stream.write( dStrlen(closer), closer );
}
void ShaderGenPrinterHLSL::printPixelShaderOutputStruct(Stream& stream, const MaterialFeatureData &featureData)
{
// Determine the number of output targets we need
U32 numMRTs = 0;
for( U32 i = 0; i < FEATUREMGR->getFeatureCount(); i++ )
{
const FeatureInfo &info = FEATUREMGR->getAt( i );
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++ )
{
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)
{
WRITESTR( "\r\n return OUT;\r\n}\r\n" );
}
void ShaderGenPrinterHLSL::printLine(Stream& stream, const String& line)
{
stream.write(line.length(), line.c_str());
const char* end = "\r\n";
stream.write(dStrlen(end), end);
}
const char* ShaderGenComponentFactoryHLSL::typeToString( GFXDeclType type )
{
switch ( type )
{
default:
case GFXDeclType_Float:
return "float";
case GFXDeclType_Float2:
return "float2";
case GFXDeclType_Float3:
return "float3";
case GFXDeclType_Float4:
case GFXDeclType_Color:
return "float4";
}
}
ShaderComponent* ShaderGenComponentFactoryHLSL::createVertexInputConnector( const GFXVertexFormat &vertexFormat )
{
ShaderConnectorHLSL *vertComp = new ShaderConnectorHLSL;
vertComp->setName( "VertData" );
// Loop thru the vertex format elements.
for ( U32 i=0; i < vertexFormat.getElementCount(); i++ )
{
const GFXVertexElement &element = vertexFormat.getElement( i );
Var *var = NULL;
if ( element.isSemantic( GFXSemantic::POSITION ) )
{
var = vertComp->getElement( RT_POSITION );
var->setName( "position" );
}
else if ( element.isSemantic( GFXSemantic::NORMAL ) )
{
var = vertComp->getElement( RT_NORMAL );
var->setName( "normal" );
}
else if ( element.isSemantic( GFXSemantic::TANGENT ) )
{
var = vertComp->getElement( RT_TANGENT );
var->setName( "T" );
}
else if ( element.isSemantic( GFXSemantic::TANGENTW ) )
{
var = vertComp->getIndexedElement( element.getSemanticIndex(), RT_TEXCOORD );
var->setName( "tangentW" );
}
else if ( element.isSemantic( GFXSemantic::BINORMAL ) )
{
var = vertComp->getElement( RT_BINORMAL );
var->setName( "B" );
}
else if ( element.isSemantic( GFXSemantic::COLOR ) )
{
var = vertComp->getElement( RT_COLOR );
var->setName( "diffuse" );
}
else if ( element.isSemantic( GFXSemantic::TEXCOORD ) )
{
var = vertComp->getIndexedElement( element.getSemanticIndex(), RT_TEXCOORD );
if ( element.getSemanticIndex() == 0 )
var->setName( "texCoord" );
else
var->setName( String::ToString( "texCoord%d", element.getSemanticIndex() + 1 ) );
}
else
{
// Everything else is a texcoord!
var = vertComp->getIndexedElement( element.getSemanticIndex(), RT_TEXCOORD );
var->setName( "tc" + element.getSemantic() );
}
if ( !var )
continue;
var->setStructName( "IN" );
var->setType( typeToString( element.getType() ) );
}
return vertComp;
}
ShaderComponent* ShaderGenComponentFactoryHLSL::createVertexPixelConnector()
{
ShaderComponent* comp = new ShaderConnectorHLSL;
((ShaderConnector*)comp)->setName("ConnectData");
return comp;
}
ShaderComponent* ShaderGenComponentFactoryHLSL::createVertexParamsDef()
{
ShaderComponent* comp = new VertexParamsDefHLSL;
return comp;
}
ShaderComponent* ShaderGenComponentFactoryHLSL::createPixelParamsDef()
{
ShaderComponent* comp = new PixelParamsDefHLSL;
return comp;
}

View file

@ -0,0 +1,60 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _SHADERGEN_HLSL_H_
#define _SHADERGEN_HLSL_H_
#ifndef _SHADERGEN_H_
#include "shaderGen/shaderGen.h"
#endif
class ShaderGenPrinterHLSL : public ShaderGenPrinter
{
public:
// ShaderGenPrinter
virtual void printShaderHeader(Stream& stream);
virtual void printMainComment(Stream& stream);
virtual void printVertexShaderCloser(Stream& stream);
virtual void printPixelShaderOutputStruct(Stream& stream, const MaterialFeatureData &featureData);
virtual void printPixelShaderCloser(Stream& stream);
virtual void printLine(Stream& stream, const String& line);
};
class ShaderGenComponentFactoryHLSL : public ShaderGenComponentFactory
{
public:
/// Helper function for converting a vertex decl
/// type to an HLSL type string.
static const char* typeToString( GFXDeclType type );
// ShaderGenComponentFactory
virtual ShaderComponent* createVertexInputConnector( const GFXVertexFormat &vertexFormat );
virtual ShaderComponent* createVertexPixelConnector();
virtual ShaderComponent* createVertexParamsDef();
virtual ShaderComponent* createPixelParamsDef();
};
#endif // _SHADERGEN_HLSL_H_

View file

@ -0,0 +1,110 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "shaderGen/shaderGen.h"
#include "shaderGen/HLSL/shaderGenHLSL.h"
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#include "shaderGen/featureMgr.h"
#include "shaderGen/HLSL/bumpHLSL.h"
#include "shaderGen/HLSL/pixSpecularHLSL.h"
#include "shaderGen/HLSL/depthHLSL.h"
#include "shaderGen/HLSL/paraboloidHLSL.h"
#include "materials/materialFeatureTypes.h"
#include "core/module.h"
static ShaderGen::ShaderGenInitDelegate sInitDelegate;
void _initShaderGenHLSL( ShaderGen *shaderGen )
{
shaderGen->setPrinter( new ShaderGenPrinterHLSL );
shaderGen->setComponentFactory( new ShaderGenComponentFactoryHLSL );
shaderGen->setFileEnding( "hlsl" );
FEATUREMGR->registerFeature( MFT_VertTransform, new VertPositionHLSL );
FEATUREMGR->registerFeature( MFT_RTLighting, new RTLightingFeatHLSL );
FEATUREMGR->registerFeature( MFT_IsDXTnm, new NamedFeatureHLSL( "DXTnm" ) );
FEATUREMGR->registerFeature( MFT_TexAnim, new TexAnimHLSL );
FEATUREMGR->registerFeature( MFT_DiffuseMap, new DiffuseMapFeatHLSL );
FEATUREMGR->registerFeature( MFT_OverlayMap, new OverlayTexFeatHLSL );
FEATUREMGR->registerFeature( MFT_DiffuseColor, new DiffuseFeatureHLSL );
FEATUREMGR->registerFeature( MFT_DiffuseVertColor, new DiffuseVertColorFeatureHLSL );
FEATUREMGR->registerFeature( MFT_AlphaTest, new AlphaTestHLSL );
FEATUREMGR->registerFeature( MFT_GlowMask, new GlowMaskHLSL );
FEATUREMGR->registerFeature( MFT_LightMap, new LightmapFeatHLSL );
FEATUREMGR->registerFeature( MFT_ToneMap, new TonemapFeatHLSL );
FEATUREMGR->registerFeature( MFT_VertLit, new VertLitHLSL );
FEATUREMGR->registerFeature( MFT_Parallax, new ParallaxFeatHLSL );
FEATUREMGR->registerFeature( MFT_NormalMap, new BumpFeatHLSL );
FEATUREMGR->registerFeature( MFT_DetailNormalMap, new NamedFeatureHLSL( "Detail Normal Map" ) );
FEATUREMGR->registerFeature( MFT_DetailMap, new DetailFeatHLSL );
FEATUREMGR->registerFeature( MFT_CubeMap, new ReflectCubeFeatHLSL );
FEATUREMGR->registerFeature( MFT_PixSpecular, new PixelSpecularHLSL );
FEATUREMGR->registerFeature( MFT_IsTranslucent, new NamedFeatureHLSL( "Translucent" ) );
FEATUREMGR->registerFeature( MFT_IsTranslucentZWrite, new NamedFeatureHLSL( "Translucent ZWrite" ) );
FEATUREMGR->registerFeature( MFT_Visibility, new VisibilityFeatHLSL );
FEATUREMGR->registerFeature( MFT_Fog, new FogFeatHLSL );
FEATUREMGR->registerFeature( MFT_SpecularMap, new SpecularMapHLSL );
FEATUREMGR->registerFeature( MFT_GlossMap, new NamedFeatureHLSL( "Gloss Map" ) );
FEATUREMGR->registerFeature( MFT_LightbufferMRT, new NamedFeatureHLSL( "Lightbuffer MRT" ) );
FEATUREMGR->registerFeature( MFT_RenderTarget1_Zero, new RenderTargetZeroHLSL( ShaderFeature::RenderTarget1 ) );
FEATUREMGR->registerFeature( MFT_DiffuseMapAtlas, new NamedFeatureHLSL( "Diffuse Map Atlas" ) );
FEATUREMGR->registerFeature( MFT_NormalMapAtlas, new NamedFeatureHLSL( "Normal Map Atlas" ) );
FEATUREMGR->registerFeature( MFT_NormalsOut, new NormalsOutFeatHLSL );
FEATUREMGR->registerFeature( MFT_DepthOut, new DepthOutHLSL );
FEATUREMGR->registerFeature( MFT_EyeSpaceDepthOut, new EyeSpaceDepthOutHLSL() );
FEATUREMGR->registerFeature( MFT_HDROut, new HDROutHLSL );
FEATUREMGR->registerFeature( MFT_ParaboloidVertTransform, new ParaboloidVertTransformHLSL );
FEATUREMGR->registerFeature( MFT_IsSinglePassParaboloid, new NamedFeatureHLSL( "Single Pass Paraboloid" ) );
FEATUREMGR->registerFeature( MFT_UseInstancing, new NamedFeatureHLSL( "Hardware Instancing" ) );
FEATUREMGR->registerFeature( MFT_Foliage, new FoliageFeatureHLSL );
FEATUREMGR->registerFeature( MFT_ParticleNormal, new ParticleNormalFeatureHLSL );
FEATUREMGR->registerFeature( MFT_InterlacedPrePass, new NamedFeatureHLSL( "Interlaced Pre Pass" ) );
FEATUREMGR->registerFeature( MFT_ForwardShading, new NamedFeatureHLSL( "Forward Shaded Material" ) );
FEATUREMGR->registerFeature( MFT_ImposterVert, new ImposterVertFeatureHLSL );
}
MODULE_BEGIN( ShaderGenHLSL )
MODULE_INIT_AFTER( ShaderGen )
MODULE_INIT_AFTER( ShaderGenFeatureMgr )
MODULE_INIT
{
sInitDelegate.bind(_initShaderGenHLSL);
SHADERGEN->registerInitDelegate(Direct3D9, sInitDelegate);
SHADERGEN->registerInitDelegate(Direct3D9_360, sInitDelegate);
}
MODULE_END;