mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-13 15:44:36 +00:00
Engine directory for ticket #1
This commit is contained in:
parent
352279af7a
commit
7dbfe6994d
3795 changed files with 1363358 additions and 0 deletions
|
|
@ -0,0 +1,636 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "lighting/advanced/hlsl/advancedLightingFeaturesHLSL.h"
|
||||
|
||||
#include "lighting/advanced/advancedLightBinManager.h"
|
||||
#include "shaderGen/langElement.h"
|
||||
#include "shaderGen/shaderOp.h"
|
||||
#include "shaderGen/conditionerFeature.h"
|
||||
#include "renderInstance/renderPrePassMgr.h"
|
||||
#include "materials/processedMaterial.h"
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
|
||||
|
||||
void DeferredRTLightingFeatHLSL::processPixMacros( Vector<GFXShaderMacro> ¯os,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// Skip deferred features, and use forward shading instead
|
||||
if ( fd.features[MFT_ForwardShading] )
|
||||
{
|
||||
Parent::processPixMacros( macros, fd );
|
||||
return;
|
||||
}
|
||||
|
||||
// Pull in the uncondition method for the light info buffer
|
||||
NamedTexTarget *texTarget = NamedTexTarget::find( AdvancedLightBinManager::smBufferName );
|
||||
if ( texTarget && texTarget->getConditioner() )
|
||||
{
|
||||
ConditionerMethodDependency *unconditionMethod = texTarget->getConditioner()->getConditionerMethodDependency(ConditionerFeature::UnconditionMethod);
|
||||
unconditionMethod->createMethodMacro( String::ToLower( AdvancedLightBinManager::smBufferName ) + "Uncondition", macros );
|
||||
addDependency(unconditionMethod);
|
||||
}
|
||||
}
|
||||
|
||||
void DeferredRTLightingFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// Skip deferred features, and use forward shading instead
|
||||
if ( fd.features[MFT_ForwardShading] )
|
||||
{
|
||||
Parent::processVert( componentList, fd );
|
||||
return;
|
||||
}
|
||||
|
||||
// Pass screen space position to pixel shader to compute a full screen buffer uv
|
||||
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
|
||||
Var *ssPos = connectComp->getElement( RT_TEXCOORD );
|
||||
ssPos->setName( "screenspacePos" );
|
||||
ssPos->setStructName( "OUT" );
|
||||
ssPos->setType( "float4" );
|
||||
|
||||
Var *outPosition = (Var*) LangElement::find( "hpos" );
|
||||
AssertFatal( outPosition, "No hpos, ohnoes." );
|
||||
|
||||
output = new GenOp( " @ = @;\r\n", ssPos, outPosition );
|
||||
}
|
||||
|
||||
void DeferredRTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// Skip deferred features, and use forward shading instead
|
||||
if ( fd.features[MFT_ForwardShading] )
|
||||
{
|
||||
Parent::processPix( componentList, fd );
|
||||
return;
|
||||
}
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
|
||||
Var *ssPos = connectComp->getElement( RT_TEXCOORD );
|
||||
ssPos->setName( "screenspacePos" );
|
||||
ssPos->setStructName( "IN" );
|
||||
ssPos->setType( "float4" );
|
||||
|
||||
Var *uvScene = new Var;
|
||||
uvScene->setType( "float2" );
|
||||
uvScene->setName( "uvScene" );
|
||||
LangElement *uvSceneDecl = new DecOp( uvScene );
|
||||
|
||||
String rtParamName = String::ToString( "rtParams%d", mLastTexIndex );
|
||||
Var *rtParams = (Var*) LangElement::find( rtParamName );
|
||||
if( !rtParams )
|
||||
{
|
||||
rtParams = new Var;
|
||||
rtParams->setType( "float4" );
|
||||
rtParams->setName( rtParamName );
|
||||
rtParams->uniform = true;
|
||||
rtParams->constSortPos = cspPass;
|
||||
}
|
||||
|
||||
meta->addStatement( new GenOp( " @ = @.xy / @.w;\r\n", uvSceneDecl, ssPos, ssPos ) ); // get the screen coord... its -1 to +1
|
||||
meta->addStatement( new GenOp( " @ = ( @ + 1.0 ) / 2.0;\r\n", uvScene, uvScene ) ); // get the screen coord to 0 to 1
|
||||
meta->addStatement( new GenOp( " @.y = 1.0 - @.y;\r\n", uvScene, uvScene ) ); // flip the y axis
|
||||
meta->addStatement( new GenOp( " @ = ( @ * @.zw ) + @.xy;\r\n", uvScene, uvScene, rtParams, rtParams) ); // scale it down and offset it to the rt size
|
||||
|
||||
Var *lightInfoSamp = new Var;
|
||||
lightInfoSamp->setType( "float4" );
|
||||
lightInfoSamp->setName( "lightInfoSample" );
|
||||
|
||||
// create texture var
|
||||
Var *lightInfoBuffer = new Var;
|
||||
lightInfoBuffer->setType( "sampler2D" );
|
||||
lightInfoBuffer->setName( "lightInfoBuffer" );
|
||||
lightInfoBuffer->uniform = true;
|
||||
lightInfoBuffer->sampler = true;
|
||||
lightInfoBuffer->constNum = Var::getTexUnitNum(); // used as texture unit num here
|
||||
|
||||
// Declare the RTLighting variables in this feature, they will either be assigned
|
||||
// in this feature, or in the tonemap/lightmap feature
|
||||
Var *d_lightcolor = new Var( "d_lightcolor", "float3" );
|
||||
meta->addStatement( new GenOp( " @;\r\n", new DecOp( d_lightcolor ) ) );
|
||||
|
||||
Var *d_NL_Att = new Var( "d_NL_Att", "float" );
|
||||
meta->addStatement( new GenOp( " @;\r\n", new DecOp( d_NL_Att ) ) );
|
||||
|
||||
Var *d_specular = new Var( "d_specular", "float" );
|
||||
meta->addStatement( new GenOp( " @;\r\n", new DecOp( d_specular ) ) );
|
||||
|
||||
|
||||
// Perform the uncondition here.
|
||||
String unconditionLightInfo = String::ToLower( AdvancedLightBinManager::smBufferName ) + "Uncondition";
|
||||
meta->addStatement( new GenOp( avar( " %s(tex2D(@, @), @, @, @);\r\n",
|
||||
unconditionLightInfo.c_str() ), lightInfoBuffer, uvScene, d_lightcolor, d_NL_Att, d_specular ) );
|
||||
|
||||
// If this has an interlaced pre-pass, do averaging here
|
||||
if( fd.features[MFT_InterlacedPrePass] )
|
||||
{
|
||||
Var *oneOverTargetSize = (Var*) LangElement::find( "oneOverTargetSize" );
|
||||
if( !oneOverTargetSize )
|
||||
{
|
||||
oneOverTargetSize = new Var;
|
||||
oneOverTargetSize->setType( "float2" );
|
||||
oneOverTargetSize->setName( "oneOverTargetSize" );
|
||||
oneOverTargetSize->uniform = true;
|
||||
oneOverTargetSize->constSortPos = cspPass;
|
||||
}
|
||||
|
||||
meta->addStatement( new GenOp( " float id_NL_Att, id_specular;\r\n float3 id_lightcolor;\r\n" ) );
|
||||
meta->addStatement( new GenOp( avar( " %s(tex2D(@, @ + float2(0.0, @.y)), id_lightcolor, id_NL_Att, id_specular);\r\n",
|
||||
unconditionLightInfo.c_str() ), lightInfoBuffer, uvScene, oneOverTargetSize ) );
|
||||
|
||||
meta->addStatement( new GenOp(" @ = lerp(@, id_lightcolor, 0.5);\r\n", d_lightcolor, d_lightcolor ) );
|
||||
meta->addStatement( new GenOp(" @ = lerp(@, id_NL_Att, 0.5);\r\n", d_NL_Att, d_NL_Att ) );
|
||||
meta->addStatement( new GenOp(" @ = lerp(@, id_specular, 0.5);\r\n", d_specular, d_specular ) );
|
||||
}
|
||||
|
||||
// This is kind of weak sauce
|
||||
if( !fd.features[MFT_VertLit] && !fd.features[MFT_ToneMap] && !fd.features[MFT_LightMap] && !fd.features[MFT_SubSurface] )
|
||||
meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "float4(@, 1.0)", d_lightcolor ), Material::Mul ) ) );
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
||||
ShaderFeature::Resources DeferredRTLightingFeatHLSL::getResources( const MaterialFeatureData &fd )
|
||||
{
|
||||
// Skip deferred features, and use forward shading instead
|
||||
if ( fd.features[MFT_ForwardShading] )
|
||||
return Parent::getResources( fd );
|
||||
|
||||
// HACK: See DeferredRTLightingFeatHLSL::setTexData.
|
||||
mLastTexIndex = 0;
|
||||
|
||||
Resources res;
|
||||
res.numTex = 1;
|
||||
res.numTexReg = 1;
|
||||
return res;
|
||||
}
|
||||
|
||||
void DeferredRTLightingFeatHLSL::setTexData( Material::StageData &stageDat,
|
||||
const MaterialFeatureData &fd,
|
||||
RenderPassData &passData,
|
||||
U32 &texIndex )
|
||||
{
|
||||
// Skip deferred features, and use forward shading instead
|
||||
if ( fd.features[MFT_ForwardShading] )
|
||||
{
|
||||
Parent::setTexData( stageDat, fd, passData, texIndex );
|
||||
return;
|
||||
}
|
||||
|
||||
NamedTexTarget *texTarget = NamedTexTarget::find( AdvancedLightBinManager::smBufferName );
|
||||
if( texTarget )
|
||||
{
|
||||
// HACK: We store this for use in DeferredRTLightingFeatHLSL::processPix()
|
||||
// which cannot deduce the texture unit itself.
|
||||
mLastTexIndex = texIndex;
|
||||
|
||||
passData.mTexType[ texIndex ] = Material::TexTarget;
|
||||
passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DeferredBumpFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
if( fd.features[MFT_PrePassConditioner] )
|
||||
{
|
||||
// There is an output conditioner active, so we need to supply a transform
|
||||
// to the pixel shader.
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
// We need the view to tangent space transform in the pixel shader.
|
||||
getOutViewToTangent( componentList, meta, fd );
|
||||
|
||||
// Make sure there are texcoords
|
||||
if( !fd.features[MFT_Parallax] && !fd.features[MFT_DiffuseMap] )
|
||||
{
|
||||
const bool useTexAnim = fd.features[MFT_TexAnim];
|
||||
|
||||
getOutTexCoord( "texCoord",
|
||||
"float2",
|
||||
true,
|
||||
useTexAnim,
|
||||
meta,
|
||||
componentList );
|
||||
|
||||
if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
|
||||
addOutDetailTexCoord( componentList,
|
||||
meta,
|
||||
useTexAnim );
|
||||
}
|
||||
|
||||
output = meta;
|
||||
}
|
||||
else if ( fd.materialFeatures[MFT_NormalsOut] ||
|
||||
fd.features[MFT_ForwardShading] ||
|
||||
!fd.features[MFT_RTLighting] )
|
||||
{
|
||||
Parent::processVert( componentList, fd );
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
output = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void DeferredBumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// NULL output in case nothing gets handled
|
||||
output = NULL;
|
||||
|
||||
if( fd.features[MFT_PrePassConditioner] )
|
||||
{
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
Var *viewToTangent = getInViewToTangent( componentList );
|
||||
|
||||
// create texture var
|
||||
Var *bumpMap = getNormalMapTex();
|
||||
Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
|
||||
LangElement *texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
|
||||
|
||||
// create bump normal
|
||||
Var *bumpNorm = new Var;
|
||||
bumpNorm->setName( "bumpNormal" );
|
||||
bumpNorm->setType( "float4" );
|
||||
|
||||
LangElement *bumpNormDecl = new DecOp( bumpNorm );
|
||||
meta->addStatement( expandNormalMap( texOp, bumpNormDecl, 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 ) );
|
||||
}
|
||||
|
||||
// This var is read from GBufferConditionerHLSL and
|
||||
// used in the prepass output.
|
||||
//
|
||||
// By using the 'half' type here we get a bunch of partial
|
||||
// precision optimized code on further operations on the normal
|
||||
// which helps alot on older Geforce cards.
|
||||
//
|
||||
Var *gbNormal = new Var;
|
||||
gbNormal->setName( "gbNormal" );
|
||||
gbNormal->setType( "half3" );
|
||||
LangElement *gbNormalDecl = new DecOp( gbNormal );
|
||||
|
||||
// Normalize is done later...
|
||||
// Note: The reverse mul order is intentional. Affine matrix.
|
||||
meta->addStatement( new GenOp( " @ = (half3)mul( @.xyz, @ );\r\n", gbNormalDecl, bumpNorm, viewToTangent ) );
|
||||
|
||||
output = meta;
|
||||
return;
|
||||
}
|
||||
else if ( fd.materialFeatures[MFT_NormalsOut] ||
|
||||
fd.features[MFT_ForwardShading] ||
|
||||
!fd.features[MFT_RTLighting] )
|
||||
{
|
||||
Parent::processPix( componentList, fd );
|
||||
return;
|
||||
}
|
||||
else if ( fd.features[MFT_PixSpecular] && !fd.features[MFT_SpecularMap] )
|
||||
{
|
||||
Var *bumpSample = (Var *)LangElement::find( "bumpSample" );
|
||||
if( bumpSample == NULL )
|
||||
{
|
||||
Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );
|
||||
|
||||
Var *bumpMap = getNormalMapTex();
|
||||
|
||||
bumpSample = new Var;
|
||||
bumpSample->setType( "float4" );
|
||||
bumpSample->setName( "bumpSample" );
|
||||
LangElement *bumpSampleDecl = new DecOp( bumpSample );
|
||||
|
||||
output = new GenOp( " @ = tex2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
output = NULL;
|
||||
}
|
||||
|
||||
ShaderFeature::Resources DeferredBumpFeatHLSL::getResources( const MaterialFeatureData &fd )
|
||||
{
|
||||
if ( fd.materialFeatures[MFT_NormalsOut] ||
|
||||
fd.features[MFT_ForwardShading] ||
|
||||
fd.features[MFT_Parallax] ||
|
||||
!fd.features[MFT_RTLighting] )
|
||||
return Parent::getResources( fd );
|
||||
|
||||
Resources res;
|
||||
if(!fd.features[MFT_SpecularMap])
|
||||
{
|
||||
res.numTex = 1;
|
||||
res.numTexReg = 1;
|
||||
|
||||
if ( fd.features[MFT_PrePassConditioner] &&
|
||||
fd.features.hasFeature( MFT_DetailNormalMap ) )
|
||||
{
|
||||
res.numTex += 1;
|
||||
if ( !fd.features.hasFeature( MFT_DetailMap ) )
|
||||
res.numTexReg += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void DeferredBumpFeatHLSL::setTexData( Material::StageData &stageDat,
|
||||
const MaterialFeatureData &fd,
|
||||
RenderPassData &passData,
|
||||
U32 &texIndex )
|
||||
{
|
||||
if ( fd.materialFeatures[MFT_NormalsOut] ||
|
||||
fd.features[MFT_ForwardShading] ||
|
||||
!fd.features[MFT_RTLighting] )
|
||||
{
|
||||
Parent::setTexData( stageDat, fd, passData, texIndex );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !fd.features[MFT_Parallax] && !fd.features[MFT_SpecularMap] &&
|
||||
( fd.features[MFT_PrePassConditioner] ||
|
||||
fd.features[MFT_PixSpecular] ) )
|
||||
{
|
||||
passData.mTexType[ texIndex ] = Material::Bump;
|
||||
passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
|
||||
|
||||
if ( fd.features[MFT_PrePassConditioner] &&
|
||||
fd.features.hasFeature( MFT_DetailNormalMap ) )
|
||||
{
|
||||
passData.mTexType[ texIndex ] = Material::DetailBump;
|
||||
passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DeferredPixelSpecularHLSL::processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
|
||||
{
|
||||
Parent::processVert( componentList, fd );
|
||||
return;
|
||||
}
|
||||
output = NULL;
|
||||
}
|
||||
|
||||
void DeferredPixelSpecularHLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
|
||||
{
|
||||
Parent::processPix( componentList, fd );
|
||||
return;
|
||||
}
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
Var *specular = new Var;
|
||||
specular->setType( "float" );
|
||||
specular->setName( "specular" );
|
||||
LangElement * specDecl = new DecOp( specular );
|
||||
|
||||
Var *specCol = (Var*)LangElement::find( "specularColor" );
|
||||
if(specCol == NULL)
|
||||
{
|
||||
specCol = new Var;
|
||||
specCol->setType( "float4" );
|
||||
specCol->setName( "specularColor" );
|
||||
specCol->uniform = true;
|
||||
specCol->constSortPos = cspPotentialPrimitive;
|
||||
}
|
||||
|
||||
Var *specPow = new Var;
|
||||
specPow->setType( "float" );
|
||||
specPow->setName( "specularPower" );
|
||||
|
||||
// If the gloss map flag is set, than the specular power is in the alpha
|
||||
// channel of the specular map
|
||||
if( fd.features[ MFT_GlossMap ] )
|
||||
meta->addStatement( new GenOp( " @ = @.a * 255;\r\n", new DecOp( specPow ), specCol ) );
|
||||
else
|
||||
{
|
||||
specPow->uniform = true;
|
||||
specPow->constSortPos = cspPotentialPrimitive;
|
||||
}
|
||||
|
||||
Var *lightInfoSamp = (Var *)LangElement::find( "lightInfoSample" );
|
||||
Var *d_specular = (Var*)LangElement::find( "d_specular" );
|
||||
Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
|
||||
|
||||
AssertFatal( lightInfoSamp && d_specular && d_NL_Att,
|
||||
"DeferredPixelSpecularHLSL::processPix - Something hosed the deferred features!" );
|
||||
|
||||
// (a^m)^n = a^(m*n)
|
||||
meta->addStatement( new GenOp( " @ = pow( @, ceil(@ / AL_ConstantSpecularPower)) * @;\r\n",
|
||||
specDecl, d_specular, specPow, d_NL_Att ) );
|
||||
|
||||
LangElement *specMul = new GenOp( "float4( @.rgb, 0 ) * @", specCol, specular );
|
||||
LangElement *final = specMul;
|
||||
|
||||
// We we have a normal map then mask the specular
|
||||
if( !fd.features[MFT_SpecularMap] && fd.features[MFT_NormalMap] )
|
||||
{
|
||||
Var *bumpSample = (Var*)LangElement::find( "bumpSample" );
|
||||
final = new GenOp( "@ * @.a", final, bumpSample );
|
||||
}
|
||||
|
||||
// add to color
|
||||
meta->addStatement( new GenOp( " @;\r\n", assignColor( final, Material::Add ) ) );
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
||||
ShaderFeature::Resources DeferredPixelSpecularHLSL::getResources( const MaterialFeatureData &fd )
|
||||
{
|
||||
if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
|
||||
return Parent::getResources( fd );
|
||||
|
||||
Resources res;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
ShaderFeature::Resources DeferredMinnaertHLSL::getResources( const MaterialFeatureData &fd )
|
||||
{
|
||||
Resources res;
|
||||
if( !fd.features[MFT_ForwardShading] && fd.features[MFT_RTLighting] )
|
||||
{
|
||||
res.numTex = 1;
|
||||
res.numTexReg = 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void DeferredMinnaertHLSL::setTexData( Material::StageData &stageDat,
|
||||
const MaterialFeatureData &fd,
|
||||
RenderPassData &passData,
|
||||
U32 &texIndex )
|
||||
{
|
||||
if( !fd.features[MFT_ForwardShading] && fd.features[MFT_RTLighting] )
|
||||
{
|
||||
NamedTexTarget *texTarget = NamedTexTarget::find(RenderPrePassMgr::BufferName);
|
||||
if ( texTarget )
|
||||
{
|
||||
passData.mTexType[ texIndex ] = Material::TexTarget;
|
||||
passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DeferredMinnaertHLSL::processPixMacros( Vector<GFXShaderMacro> ¯os,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
if( !fd.features[MFT_ForwardShading] && fd.features[MFT_RTLighting] )
|
||||
{
|
||||
// Pull in the uncondition method for the g buffer
|
||||
NamedTexTarget *texTarget = NamedTexTarget::find( RenderPrePassMgr::BufferName );
|
||||
if ( texTarget && texTarget->getConditioner() )
|
||||
{
|
||||
ConditionerMethodDependency *unconditionMethod = texTarget->getConditioner()->getConditionerMethodDependency(ConditionerFeature::UnconditionMethod);
|
||||
unconditionMethod->createMethodMacro( String::ToLower(RenderPrePassMgr::BufferName) + "Uncondition", macros );
|
||||
addDependency(unconditionMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DeferredMinnaertHLSL::processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// If there is no deferred information, bail on this feature
|
||||
if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
|
||||
{
|
||||
output = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we pass the world space position to the
|
||||
// pixel shader so we can calculate a view vector.
|
||||
MultiLine *meta = new MultiLine;
|
||||
addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
|
||||
output = meta;
|
||||
}
|
||||
|
||||
void DeferredMinnaertHLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// If there is no deferred information, bail on this feature
|
||||
if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
|
||||
{
|
||||
output = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
Var *minnaertConstant = new Var;
|
||||
minnaertConstant->setType( "float" );
|
||||
minnaertConstant->setName( "minnaertConstant" );
|
||||
minnaertConstant->uniform = true;
|
||||
minnaertConstant->constSortPos = cspPotentialPrimitive;
|
||||
|
||||
// create texture var
|
||||
Var *prepassBuffer = new Var;
|
||||
prepassBuffer->setType( "sampler2D" );
|
||||
prepassBuffer->setName( "prepassBuffer" );
|
||||
prepassBuffer->uniform = true;
|
||||
prepassBuffer->sampler = true;
|
||||
prepassBuffer->constNum = Var::getTexUnitNum(); // used as texture unit num here
|
||||
|
||||
// Texture coord
|
||||
Var *uvScene = (Var*) LangElement::find( "uvScene" );
|
||||
AssertFatal(uvScene != NULL, "Unable to find UVScene, no RTLighting feature?");
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
// Get the world space view vector.
|
||||
Var *wsViewVec = getWsView( getInWsPosition( componentList ), meta );
|
||||
|
||||
String unconditionPrePassMethod = String::ToLower(RenderPrePassMgr::BufferName) + "Uncondition";
|
||||
|
||||
Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
|
||||
|
||||
meta->addStatement( new GenOp( avar( " float4 normalDepth = %s(@, @);\r\n", unconditionPrePassMethod.c_str() ), prepassBuffer, uvScene ) );
|
||||
meta->addStatement( new GenOp( " float vDotN = dot(normalDepth.xyz, @);\r\n", wsViewVec ) );
|
||||
meta->addStatement( new GenOp( " float Minnaert = pow( @, @) * pow(vDotN, 1.0 - @);\r\n", d_NL_Att, minnaertConstant, minnaertConstant ) );
|
||||
meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "float4(Minnaert, Minnaert, Minnaert, 1.0)" ), Material::Mul ) ) );
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
||||
|
||||
void DeferredSubSurfaceHLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// If there is no deferred information, bail on this feature
|
||||
if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
|
||||
{
|
||||
output = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
Var *subSurfaceParams = new Var;
|
||||
subSurfaceParams->setType( "float4" );
|
||||
subSurfaceParams->setName( "subSurfaceParams" );
|
||||
subSurfaceParams->uniform = true;
|
||||
subSurfaceParams->constSortPos = cspPotentialPrimitive;
|
||||
|
||||
Var *d_lightcolor = (Var*)LangElement::find( "d_lightcolor" );
|
||||
Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
meta->addStatement( new GenOp( " float subLamb = smoothstep(-@.a, 1.0, @) - smoothstep(0.0, 1.0, @);\r\n", subSurfaceParams, d_NL_Att, d_NL_Att ) );
|
||||
meta->addStatement( new GenOp( " subLamb = max(0.0, subLamb);\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "float4(@ + (subLamb * @.rgb), 1.0)", d_lightcolor, subSurfaceParams ), Material::Mul ) ) );
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
|
@ -0,0 +1,170 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 _DEFERREDFEATURESHLSL_H_
|
||||
#define _DEFERREDFEATURESHLSL_H_
|
||||
|
||||
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
|
||||
#include "shaderGen/HLSL/bumpHLSL.h"
|
||||
#include "shaderGen/HLSL/pixSpecularHLSL.h"
|
||||
|
||||
class ConditionerMethodDependency;
|
||||
|
||||
|
||||
/// Lights the pixel by sampling from the light prepass
|
||||
/// buffer. It will fall back to forward lighting
|
||||
/// functionality for non-deferred rendered surfaces.
|
||||
///
|
||||
/// Also note that this feature is only used in the
|
||||
/// forward rendering pass. It is not used during the
|
||||
/// prepass step.
|
||||
///
|
||||
class DeferredRTLightingFeatHLSL : public RTLightingFeatHLSL
|
||||
{
|
||||
typedef RTLightingFeatHLSL Parent;
|
||||
|
||||
protected:
|
||||
|
||||
/// @see DeferredRTLightingFeatHLSL::processPix()
|
||||
U32 mLastTexIndex;
|
||||
|
||||
public:
|
||||
|
||||
virtual void processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd );
|
||||
|
||||
virtual void processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd );
|
||||
|
||||
virtual void processPixMacros( Vector<GFXShaderMacro> ¯os,
|
||||
const MaterialFeatureData &fd );
|
||||
|
||||
virtual Material::BlendOp getBlendOp(){ return Material::None; }
|
||||
|
||||
virtual Resources getResources( const MaterialFeatureData &fd );
|
||||
|
||||
virtual void setTexData( Material::StageData &stageDat,
|
||||
const MaterialFeatureData &fd,
|
||||
RenderPassData &passData,
|
||||
U32 &texIndex );
|
||||
|
||||
virtual String getName()
|
||||
{
|
||||
return "Deferred RT Lighting";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// This is used during the
|
||||
class DeferredBumpFeatHLSL : public BumpFeatHLSL
|
||||
{
|
||||
typedef BumpFeatHLSL Parent;
|
||||
|
||||
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 );
|
||||
|
||||
virtual void setTexData( Material::StageData &stageDat,
|
||||
const MaterialFeatureData &fd,
|
||||
RenderPassData &passData,
|
||||
U32 &texIndex );
|
||||
|
||||
virtual String getName()
|
||||
{
|
||||
return "Bumpmap [Deferred]";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// Generates specular highlights in the forward pass
|
||||
/// from the light prepass buffer.
|
||||
class DeferredPixelSpecularHLSL : public PixelSpecularHLSL
|
||||
{
|
||||
typedef PixelSpecularHLSL Parent;
|
||||
|
||||
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 String getName()
|
||||
{
|
||||
return "Pixel Specular [Deferred]";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
class DeferredMinnaertHLSL : public ShaderFeatureHLSL
|
||||
{
|
||||
typedef ShaderFeatureHLSL Parent;
|
||||
|
||||
public:
|
||||
virtual void processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd );
|
||||
virtual void processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd );
|
||||
|
||||
virtual void processPixMacros( Vector<GFXShaderMacro> ¯os,
|
||||
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 "Minnaert Shading [Deferred]";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
class DeferredSubSurfaceHLSL : public ShaderFeatureHLSL
|
||||
{
|
||||
typedef ShaderFeatureHLSL Parent;
|
||||
|
||||
public:
|
||||
virtual void processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd );
|
||||
|
||||
virtual String getName()
|
||||
{
|
||||
return "Sub-Surface Approximation [Deferred]";
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _DEFERREDFEATURESHLSL_H_
|
||||
404
Engine/source/lighting/advanced/hlsl/gBufferConditionerHLSL.cpp
Normal file
404
Engine/source/lighting/advanced/hlsl/gBufferConditionerHLSL.cpp
Normal file
|
|
@ -0,0 +1,404 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "lighting/advanced/hlsl/gBufferConditionerHLSL.h"
|
||||
|
||||
#include "shaderGen/featureMgr.h"
|
||||
#include "gfx/gfxStringEnumTranslate.h"
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
#include "materials/materialFeatureData.h"
|
||||
#include "shaderGen/hlsl/shaderFeatureHLSL.h"
|
||||
|
||||
|
||||
GBufferConditionerHLSL::GBufferConditionerHLSL( const GFXFormat bufferFormat, const NormalSpace nrmSpace ) :
|
||||
Parent( bufferFormat )
|
||||
{
|
||||
// Figure out how we should store the normal data. These are the defaults.
|
||||
mCanWriteNegativeValues = false;
|
||||
mNormalStorageType = CartesianXYZ;
|
||||
|
||||
// Note: We clear to a depth 1 (the w component) so
|
||||
// that the unrendered parts of the scene end up
|
||||
// farthest to the camera.
|
||||
const NormalStorage &twoCmpNrmStorageType = ( nrmSpace == WorldSpace ? Spherical : LambertAzimuthal );
|
||||
switch(bufferFormat)
|
||||
{
|
||||
case GFXFormatR8G8B8A8:
|
||||
mNormalStorageType = twoCmpNrmStorageType;
|
||||
mBitsPerChannel = 8;
|
||||
break;
|
||||
|
||||
case GFXFormatR16G16B16A16F:
|
||||
// Floating point buffers don't need to encode negative values
|
||||
mCanWriteNegativeValues = true;
|
||||
mNormalStorageType = twoCmpNrmStorageType;
|
||||
mBitsPerChannel = 16;
|
||||
break;
|
||||
|
||||
// Store a 32bit depth with a sperical normal in the
|
||||
// integer 16 format. This gives us perfect depth
|
||||
// precision and high quality normals within a 64bit
|
||||
// buffer format.
|
||||
case GFXFormatR16G16B16A16:
|
||||
mNormalStorageType = twoCmpNrmStorageType;
|
||||
mBitsPerChannel = 16;
|
||||
break;
|
||||
|
||||
case GFXFormatR32G32B32A32F:
|
||||
mCanWriteNegativeValues = true;
|
||||
mNormalStorageType = CartesianXYZ;
|
||||
mBitsPerChannel = 32;
|
||||
break;
|
||||
|
||||
default:
|
||||
AssertFatal(false, "Unsupported G-Buffer format");
|
||||
}
|
||||
}
|
||||
|
||||
GBufferConditionerHLSL::~GBufferConditionerHLSL()
|
||||
{
|
||||
}
|
||||
|
||||
void GBufferConditionerHLSL::processVert( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// If we have a normal map then that feature will
|
||||
// take care of passing gbNormal to the pixel shader.
|
||||
if ( fd.features[MFT_NormalMap] )
|
||||
return;
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
output = meta;
|
||||
|
||||
// grab incoming vert normal
|
||||
Var *inNormal = (Var*) LangElement::find( "normal" );
|
||||
AssertFatal( inNormal, "Something went bad with ShaderGen. The normal should be already defined." );
|
||||
|
||||
// grab output for gbuffer normal
|
||||
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
|
||||
Var *outNormal = connectComp->getElement( RT_TEXCOORD );
|
||||
outNormal->setName( "gbNormal" );
|
||||
outNormal->setStructName( "OUT" );
|
||||
outNormal->setType( "float3" );
|
||||
|
||||
if( !fd.features[MFT_ParticleNormal] )
|
||||
{
|
||||
// Kick out the view-space normal
|
||||
|
||||
// TODO: Total hack because Conditioner is directly derived
|
||||
// from ShaderFeature and not from ShaderFeatureHLSL.
|
||||
NamedFeatureHLSL dummy( String::EmptyString );
|
||||
dummy.mInstancingFormat = mInstancingFormat;
|
||||
Var *worldViewOnly = dummy.getWorldView( componentList, fd.features[MFT_UseInstancing], meta );
|
||||
|
||||
meta->addStatement( new GenOp(" @ = mul(@, float4( normalize(@), 0.0 ) ).xyz;\r\n",
|
||||
outNormal, worldViewOnly, inNormal ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Assume the particle normal generator has already put this in view space
|
||||
// and normalized it
|
||||
meta->addStatement( new GenOp( " @ = @;\r\n", outNormal, inNormal ) );
|
||||
}
|
||||
}
|
||||
|
||||
void GBufferConditionerHLSL::processPix( Vector<ShaderComponent*> &componentList,
|
||||
const MaterialFeatureData &fd )
|
||||
{
|
||||
// sanity
|
||||
AssertFatal( fd.features[MFT_EyeSpaceDepthOut], "No depth-out feature enabled! Bad news!" );
|
||||
|
||||
MultiLine *meta = new MultiLine;
|
||||
|
||||
// grab connector normal
|
||||
ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
|
||||
Var *gbNormal = (Var*) LangElement::find( "gbNormal" );
|
||||
if( !gbNormal )
|
||||
{
|
||||
gbNormal = connectComp->getElement( RT_TEXCOORD );
|
||||
gbNormal->setName( "gbNormal" );
|
||||
gbNormal->setStructName( "IN" );
|
||||
gbNormal->setType( "float3" );
|
||||
gbNormal->mapsToSampler = false;
|
||||
gbNormal->uniform = false;
|
||||
}
|
||||
|
||||
// find depth
|
||||
ShaderFeature *depthFeat = FEATUREMGR->getByType( MFT_EyeSpaceDepthOut );
|
||||
AssertFatal( depthFeat != NULL, "No eye space depth feature found!" );
|
||||
|
||||
Var *depth = (Var*) LangElement::find(depthFeat->getOutputVarName());
|
||||
AssertFatal( depth, "Something went bad with ShaderGen. The depth should be already generated by the EyeSpaceDepthOut feature." );
|
||||
|
||||
|
||||
Var *unconditionedOut = new Var;
|
||||
unconditionedOut->setType("float4");
|
||||
unconditionedOut->setName("normal_depth");
|
||||
|
||||
LangElement *outputDecl = new DecOp( unconditionedOut );
|
||||
|
||||
// If we're doing prepass blending then we need
|
||||
// to steal away the alpha channel before the
|
||||
// conditioner stomps on it.
|
||||
Var *alphaVal = NULL;
|
||||
if ( fd.features[ MFT_IsTranslucentZWrite ] )
|
||||
{
|
||||
alphaVal = new Var( "outAlpha", "float" );
|
||||
meta->addStatement( new GenOp( " @ = OUT.col.a; // MFT_IsTranslucentZWrite\r\n", new DecOp( alphaVal ) ) );
|
||||
}
|
||||
|
||||
// If using interlaced normals, invert the normal
|
||||
if(fd.features[MFT_InterlacedPrePass])
|
||||
{
|
||||
// NOTE: Its safe to not call ShaderFeatureHLSL::addOutVpos() in the vertex
|
||||
// shader as for SM 3.0 nothing is needed there.
|
||||
Var *Vpos = ShaderFeatureHLSL::getInVpos( meta, componentList );
|
||||
|
||||
Var *iGBNormal = new Var( "interlacedGBNormal", "float3" );
|
||||
meta->addStatement(new GenOp(" @ = (frac(@.y * 0.5) < 0.1 ? reflect(@, float3(0.0, -1.0, 0.0)) : @);\r\n", new DecOp(iGBNormal), Vpos, gbNormal, gbNormal));
|
||||
gbNormal = iGBNormal;
|
||||
}
|
||||
|
||||
// NOTE: We renormalize the normal here as they
|
||||
// will not stay normalized during interpolation.
|
||||
meta->addStatement( new GenOp(" @ = @;", outputDecl, new GenOp( "float4(normalize(@), @)", gbNormal, depth ) ) );
|
||||
meta->addStatement( assignOutput( unconditionedOut ) );
|
||||
|
||||
// If we have an alpha var then we're doing prepass lerp blending.
|
||||
if ( alphaVal )
|
||||
{
|
||||
Var *outColor = (Var*)LangElement::find( getOutputTargetVarName( DefaultTarget ) );
|
||||
meta->addStatement( new GenOp( " @.ba = float2( 0, @ ); // MFT_IsTranslucentZWrite\r\n", outColor, alphaVal ) );
|
||||
}
|
||||
|
||||
output = meta;
|
||||
}
|
||||
|
||||
ShaderFeature::Resources GBufferConditionerHLSL::getResources( const MaterialFeatureData &fd )
|
||||
{
|
||||
Resources res;
|
||||
|
||||
// Passing from VS->PS:
|
||||
// - world space normal (gbNormal)
|
||||
res.numTexReg = 1;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Var* GBufferConditionerHLSL::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
|
||||
{
|
||||
const bool isCondition = ( methodType == ConditionerFeature::ConditionMethod );
|
||||
|
||||
Var *retVal = NULL;
|
||||
|
||||
// The uncondition method inputs are changed
|
||||
if( isCondition )
|
||||
retVal = Parent::printMethodHeader( methodType, methodName, stream, meta );
|
||||
else
|
||||
{
|
||||
Var *methodVar = new Var;
|
||||
methodVar->setName(methodName);
|
||||
methodVar->setType("inline float4");
|
||||
DecOp *methodDecl = new DecOp(methodVar);
|
||||
|
||||
Var *prepassSampler = new Var;
|
||||
prepassSampler->setName("prepassSamplerVar");
|
||||
prepassSampler->setType("sampler2D");
|
||||
DecOp *prepassSamplerDecl = new DecOp(prepassSampler);
|
||||
|
||||
Var *screenUV = new Var;
|
||||
screenUV->setName("screenUVVar");
|
||||
screenUV->setType("float2");
|
||||
DecOp *screenUVDecl = new DecOp(screenUV);
|
||||
|
||||
Var *bufferSample = new Var;
|
||||
bufferSample->setName("bufferSample");
|
||||
bufferSample->setType("float4");
|
||||
DecOp *bufferSampleDecl = new DecOp(bufferSample);
|
||||
|
||||
meta->addStatement( new GenOp( "@(@, @)\r\n", methodDecl, prepassSamplerDecl, screenUVDecl ) );
|
||||
|
||||
meta->addStatement( new GenOp( "{\r\n" ) );
|
||||
|
||||
meta->addStatement( new GenOp( " // Sampler g-buffer\r\n" ) );
|
||||
|
||||
#ifdef TORQUE_OS_XENON
|
||||
meta->addStatement( new GenOp( " @;\r\n", bufferSampleDecl ) );
|
||||
meta->addStatement( new GenOp( " asm { tfetch2D @, @, @, MagFilter = point, MinFilter = point, MipFilter = point };\r\n", bufferSample, screenUV, prepassSampler ) );
|
||||
#else
|
||||
// The gbuffer has no mipmaps, so use tex2dlod when
|
||||
// possible so that the shader compiler can optimize.
|
||||
meta->addStatement( new GenOp( " #if TORQUE_SM >= 30\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = tex2Dlod(@, float4(@,0,0));\r\n", bufferSampleDecl, prepassSampler, screenUV ) );
|
||||
meta->addStatement( new GenOp( " #else\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", bufferSampleDecl, prepassSampler, screenUV ) );
|
||||
meta->addStatement( new GenOp( " #endif\r\n\r\n" ) );
|
||||
#endif
|
||||
|
||||
// We don't use this way of passing var's around, so this should cause a crash
|
||||
// if something uses this improperly
|
||||
retVal = bufferSample;
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
GenOp* GBufferConditionerHLSL::_posnegEncode( GenOp *val )
|
||||
{
|
||||
if(mNormalStorageType == LambertAzimuthal)
|
||||
return mCanWriteNegativeValues ? val : new GenOp(avar("(%f * (@ + %f))", 1.0f/(M_SQRT2_F * 2.0f), M_SQRT2_F), val);
|
||||
else
|
||||
return mCanWriteNegativeValues ? val : new GenOp("(0.5 * (@ + 1.0))", val);
|
||||
}
|
||||
|
||||
GenOp* GBufferConditionerHLSL::_posnegDecode( GenOp *val )
|
||||
{
|
||||
if(mNormalStorageType == LambertAzimuthal)
|
||||
return mCanWriteNegativeValues ? val : new GenOp(avar("(@ * %f - %f)", M_SQRT2_F * 2.0f, M_SQRT2_F), val);
|
||||
else
|
||||
return mCanWriteNegativeValues ? val : new GenOp("(@ * 2.0 - 1.0)", val);
|
||||
}
|
||||
|
||||
Var* GBufferConditionerHLSL::_conditionOutput( Var *unconditionedOutput, MultiLine *meta )
|
||||
{
|
||||
Var *retVar = new Var;
|
||||
retVar->setType("float4");
|
||||
retVar->setName("_gbConditionedOutput");
|
||||
LangElement *outputDecl = new DecOp( retVar );
|
||||
|
||||
switch(mNormalStorageType)
|
||||
{
|
||||
case CartesianXYZ:
|
||||
meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.xyz, depth)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(@, @.a);\r\n", outputDecl,
|
||||
_posnegEncode(new GenOp("@.xyz", unconditionedOutput)), unconditionedOutput ) );
|
||||
break;
|
||||
|
||||
case CartesianXY:
|
||||
meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(@, @.a);", outputDecl,
|
||||
_posnegEncode(new GenOp("float3(@.xy, sign(@.z))", unconditionedOutput, unconditionedOutput)), unconditionedOutput ) );
|
||||
break;
|
||||
|
||||
case Spherical:
|
||||
meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(@, 0.0, @.a);\r\n", outputDecl,
|
||||
_posnegEncode(new GenOp("float2(atan2(@.y, @.x) / 3.14159265358979323846f, @.z)", unconditionedOutput, unconditionedOutput, unconditionedOutput ) ),
|
||||
unconditionedOutput ) );
|
||||
|
||||
// HACK: This fixes the noise present when using a floating point
|
||||
// gbuffer on Geforce cards and the "flat areas unlit" issues.
|
||||
//
|
||||
// We need work around atan2() above to fix this issue correctly
|
||||
// without the extra overhead of this test.
|
||||
//
|
||||
meta->addStatement( new GenOp( " if ( abs( dot( @.xyz, float3( 0.0, 0.0, 1.0 ) ) ) > 0.999f ) @ = float4( 0, 1 * sign( @.z ), 0, @.a );\r\n",
|
||||
unconditionedOutput, retVar, unconditionedOutput, unconditionedOutput ) );
|
||||
break;
|
||||
|
||||
case LambertAzimuthal:
|
||||
//http://en.wikipedia.org/wiki/Lambert_azimuthal_equal-area_projection
|
||||
//
|
||||
// Note we're casting to half to use partial precision
|
||||
// sqrt which is much faster on older Geforces while
|
||||
// still being acceptable for normals.
|
||||
//
|
||||
meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(@, 0.0, @.a);\r\n", outputDecl,
|
||||
_posnegEncode(new GenOp("sqrt(half(2.0/(1.0 - @.y))) * half2(@.xz)", unconditionedOutput, unconditionedOutput)),
|
||||
unconditionedOutput ) );
|
||||
break;
|
||||
}
|
||||
|
||||
// Encode depth into two channels
|
||||
if(mNormalStorageType != CartesianXYZ)
|
||||
{
|
||||
const U64 maxValPerChannel = 1 << mBitsPerChannel;
|
||||
meta->addStatement( new GenOp( " \r\n // Encode depth into hi/lo\r\n" ) );
|
||||
meta->addStatement( new GenOp( avar( " float2 _tempDepth = frac(@.a * float2(1.0, %llu.0));\r\n", maxValPerChannel - 1 ),
|
||||
unconditionedOutput ) );
|
||||
meta->addStatement( new GenOp( avar( " @.zw = _tempDepth.xy - _tempDepth.yy * float2(1.0/%llu.0, 0.0);\r\n\r\n", maxValPerChannel - 1 ),
|
||||
retVar ) );
|
||||
}
|
||||
|
||||
AssertFatal( retVar != NULL, avar( "Cannot condition output to buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) );
|
||||
return retVar;
|
||||
}
|
||||
|
||||
Var* GBufferConditionerHLSL::_unconditionInput( Var *conditionedInput, MultiLine *meta )
|
||||
{
|
||||
Var *retVar = new Var;
|
||||
retVar->setType("float4");
|
||||
retVar->setName("_gbUnconditionedInput");
|
||||
LangElement *outputDecl = new DecOp( retVar );
|
||||
|
||||
switch(mNormalStorageType)
|
||||
{
|
||||
case CartesianXYZ:
|
||||
meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.xyz, depth)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(@, @.a);\r\n", outputDecl,
|
||||
_posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
|
||||
break;
|
||||
|
||||
case CartesianXY:
|
||||
meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(@, @.a);\r\n", outputDecl,
|
||||
_posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
|
||||
meta->addStatement( new GenOp( " @.z *= sqrt(1.0 - dot(@.xy, @.xy));\r\n", retVar, retVar, retVar ) );
|
||||
break;
|
||||
|
||||
case Spherical:
|
||||
meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " float2 spGPUAngles = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
|
||||
meta->addStatement( new GenOp( " float2 sincosTheta;\r\n" ) );
|
||||
meta->addStatement( new GenOp( " sincos(spGPUAngles.x * 3.14159265358979323846f, sincosTheta.x, sincosTheta.y);\r\n" ) );
|
||||
meta->addStatement( new GenOp( " float2 sincosPhi = float2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y, @.a);\r\n", outputDecl, conditionedInput ) );
|
||||
break;
|
||||
|
||||
case LambertAzimuthal:
|
||||
// Note we're casting to half to use partial precision
|
||||
// sqrt which is much faster on older Geforces while
|
||||
// still being acceptable for normals.
|
||||
//
|
||||
meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) );
|
||||
meta->addStatement( new GenOp( " float2 _inpXY = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
|
||||
meta->addStatement( new GenOp( " float _xySQ = dot(_inpXY, _inpXY);\r\n" ) );
|
||||
meta->addStatement( new GenOp( " @ = float4( sqrt(half(1.0 - (_xySQ / 4.0))) * _inpXY, -1.0 + (_xySQ / 2.0), @.a).xzyw;\r\n", outputDecl, conditionedInput ) );
|
||||
break;
|
||||
}
|
||||
|
||||
// Recover depth from encoding
|
||||
if(mNormalStorageType != CartesianXYZ)
|
||||
{
|
||||
const U64 maxValPerChannel = 1 << mBitsPerChannel;
|
||||
meta->addStatement( new GenOp( " \r\n // Decode depth\r\n" ) );
|
||||
meta->addStatement( new GenOp( avar( " @.w = dot( @.zw, float2(1.0, 1.0/%llu.0));\r\n", maxValPerChannel - 1 ),
|
||||
retVar, conditionedInput ) );
|
||||
}
|
||||
|
||||
|
||||
AssertFatal( retVar != NULL, avar( "Cannot uncondition input from buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) );
|
||||
return retVar;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 _GBUFFER_CONDITIONER_HLSL_H_
|
||||
#define _GBUFFER_CONDITIONER_HLSL_H_
|
||||
|
||||
#ifndef _CONDITIONER_BASE_H_
|
||||
#include "shaderGen/conditionerFeature.h"
|
||||
#endif
|
||||
#ifndef _SHADEROP_H_
|
||||
#include "shaderGen/shaderOp.h"
|
||||
#endif
|
||||
|
||||
|
||||
///
|
||||
class GBufferConditionerHLSL : public ConditionerFeature
|
||||
{
|
||||
typedef ConditionerFeature Parent;
|
||||
|
||||
public:
|
||||
enum NormalStorage
|
||||
{
|
||||
CartesianXYZ,
|
||||
CartesianXY,
|
||||
Spherical,
|
||||
LambertAzimuthal,
|
||||
};
|
||||
|
||||
enum NormalSpace
|
||||
{
|
||||
WorldSpace,
|
||||
ViewSpace,
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
NormalStorage mNormalStorageType;
|
||||
bool mCanWriteNegativeValues;
|
||||
U32 mBitsPerChannel;
|
||||
|
||||
public:
|
||||
|
||||
GBufferConditionerHLSL( const GFXFormat bufferFormat, const NormalSpace nrmSpace );
|
||||
virtual ~GBufferConditionerHLSL();
|
||||
|
||||
|
||||
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 "GBuffer Conditioner"; }
|
||||
|
||||
protected:
|
||||
|
||||
virtual Var *printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta );
|
||||
|
||||
virtual GenOp* _posnegEncode( GenOp *val );
|
||||
virtual GenOp* _posnegDecode( GenOp *val );
|
||||
virtual Var* _conditionOutput( Var *unconditionedOutput, MultiLine *meta );
|
||||
virtual Var* _unconditionInput( Var *conditionedInput, MultiLine *meta );
|
||||
};
|
||||
|
||||
#endif // _GBUFFER_CONDITIONER_HLSL_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue