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; }
void NormalsOutFeatGLSL::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( "vec3" ); 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( " @ = tMul( @, normalize( vec4(@, 0.0) ) ).xyz;\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 GBufferConditionerGLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { output = NULL; if( !fd.features[MFT_NormalMap] ) { // 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->setType( "vec3" ); // create objToWorld variable Var *objToWorld = (Var*) LangElement::find( "objTrans" ); if( !objToWorld ) { objToWorld = new Var; objToWorld->setType( "mat4" ); objToWorld->setName( "objTrans" ); objToWorld->uniform = true; objToWorld->constSortPos = cspPrimitive; } // Kick out the world-space normal LangElement *statement = new GenOp( " @ = vec3(@ * vec4(normalize(@), 0.0));\r\n", outNormal, objToWorld, inNormal ); output = statement; } }
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 TerrainParallaxMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { MultiLine *meta = new MultiLine; 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->setType( "vec3" ); } negViewTS = new Var( "negViewTS", "vec3" ); meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) ); } // Get the rest of our inputs. Var *detailInfo = _getDetailIdStrengthParallax(); Var *normalMap = _getNormalMapTex(); Var *texCoord = _getInDetailCoord( componentList ); // Call the library function to do the rest. meta->addStatement( new GenOp( " @.xy += parallaxOffset( @, @.xy, @, @.z );\r\n", texCoord, normalMap, texCoord, negViewTS, detailInfo ) ); output = meta; }
void TerrainParallaxMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { if ( LangElement::find( "outNegViewTS" ) ) return; MultiLine *meta = new MultiLine; // 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 transform. Var *eyePos = _getUniformVar( "eyePos", "vec3" , cspPotentialPrimitive ); 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->setType( "vec3" ); meta->addStatement( new GenOp( " @ = @ * vec3( @ - @.xyz );\r\n", outNegViewTS, objToTangentSpace, eyePos, inPos ) ); 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(); Var *bumpMapTexture = (Var*)LangElement::find("bumpMapTex"); // Call the library function to do the rest. if (fd.features.hasFeature(MFT_IsDXTnm, getProcessIndex())) { if (mIsDirect3D11) meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @, @.xy, @, @ );\r\n", texCoord, bumpMapTexture, normalMap, texCoord, negViewTS, parallaxInfo)); else meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @.xy, @, @ );\r\n", texCoord, normalMap, texCoord, negViewTS, parallaxInfo)); } else { if (mIsDirect3D11) meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @, @.xy, @, @ );\r\n", texCoord, bumpMapTexture, normalMap, texCoord, negViewTS, parallaxInfo)); else meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @.xy, @, @ );\r\n", texCoord, normalMap, texCoord, negViewTS, parallaxInfo)); } // TODO: Fix second UV maybe? output = meta; }
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" ) ); if (fd.features.hasFeature(MFT_TerrainBaseMap)) meta->addStatement(new GenOp(" @ =min(0.9999, dot(@, (@.xyz / @.w)));\r\n", depthOutDecl, vEye, wsEyeVec, wsEyeVec)); else 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; }
void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { const S32 detailIndex = getProcessIndex(); // Grab incoming texture coords... the base map feature // made sure this was created. Var *inTex = (Var*)LangElement::find( "texCoord" ); AssertFatal( inTex, "The texture coord is missing!" ); // Grab the input position. Var *inPos = (Var*)LangElement::find( "inPosition" ); if ( !inPos ) inPos = (Var*)LangElement::find( "position" ); // Get the object space eye position. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive ); MultiLine *meta = new MultiLine; // Get the distance from the eye to this vertex. Var *dist = (Var*)LangElement::find( "macroDist" ); if ( !dist ) { dist = new Var; dist->setType( "float" ); dist->setName( "macroDist" ); meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n", new DecOp( dist ), inPos, eyePos ) ); } // grab connector texcoord register ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); Var *outTex = connectComp->getElement( RT_TEXCOORD ); outTex->setName( String::ToString( "macroCoord%d", detailIndex ) ); outTex->setStructName( "OUT" ); outTex->setType( "float4" ); outTex->mapsToSampler = true; // Get the detail scale and fade info. Var *detScaleAndFade = new Var; detScaleAndFade->setType( "float4" ); detScaleAndFade->setName( String::ToString( "macroScaleAndFade%d", detailIndex ) ); detScaleAndFade->uniform = true; detScaleAndFade->constSortPos = cspPotentialPrimitive; // Setup the detail coord. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) ); // And sneak the detail fade thru the w detailCoord. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n", outTex, detScaleAndFade, dist, detScaleAndFade ) ); output = meta; }
void ParallaxFeatGLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { AssertFatal( GFX->getPixelShaderVersion() >= 2.0, "ParallaxFeatGLSL::processVert - We don't support SM 1.x!" ); MultiLine *meta = new MultiLine; // Add the texture coords. getOutTexCoord( "texCoord", "vec2", 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", "vec3", 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( "vec3" ); meta->addStatement( new GenOp( " @ = tMul( @, 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 = [email protected];\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( " @ = tMul(@, float4(@,0)).xyz;\r\n", outNegViewTS, texMat, outNegViewTS ) ); } output = meta; }
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 ); }
Var* TerrainFeatGLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList ) { String name( String::ToString( "outDetCoord%d", getProcessIndex() ) ); Var *inDet = (Var*)LangElement::find( name ); if ( !inDet ) { ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); inDet = connectComp->getElement( RT_TEXCOORD ); inDet->setName( name ); inDet->setType( "vec4" ); inDet->mapsToSampler = true; } return inDet; }
void GBufferConditionerGLSL::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 ShaderFeatureGLSL. NamedFeatureGLSL dummy( String::EmptyString ); dummy.mInstancingFormat = mInstancingFormat; Var *worldViewOnly = dummy.getWorldView( componentList, fd.features[MFT_UseInstancing], meta ); meta->addStatement( new GenOp(" @ = tMul(@, 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 EyeSpaceDepthOutHLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { // grab incoming vert position Var *inPosition = (Var*) LangElement::find( "inPosition" ); if ( !inPosition ) inPosition = (Var*) LangElement::find( "position" ); AssertFatal( inPosition, "Something went bad with ShaderGen. The position should be already defined." ); // grab output ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); Var *outWSEyeVec = connectComp->getElement( RT_TEXCOORD ); outWSEyeVec->setName( "wsEyeVec" ); outWSEyeVec->setStructName( "OUT" ); // create modelview variable Var *objToWorld = (Var*) LangElement::find( "objTrans" ); if( !objToWorld ) { objToWorld = new Var; objToWorld->setType( "float4x4" ); objToWorld->setName( "objTrans" ); objToWorld->uniform = true; objToWorld->constSortPos = cspPrimitive; } Var *eyePos = (Var*)LangElement::find( "eyePosWorld" ); if( !eyePos ) { eyePos = new Var; eyePos->setType("float3"); eyePos->setName("eyePosWorld"); eyePos->uniform = true; eyePos->constSortPos = cspPass; } LangElement *statement = new GenOp( " @ = mul(@, float4(@.xyz,1)) - float4(@, 0.0);\r\n", outWSEyeVec, objToWorld, inPosition, eyePos ); output = statement; }
void GBufferConditionerGLSL::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->setType( "vec3" ); 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("vec4"); unconditionedOut->setName("normal_depth"); LangElement *outputDecl = new DecOp( unconditionedOut ); // NOTE: We renormalize the normal here as they // will not stay normalized during interpolation. meta->addStatement( new GenOp(" @ = @;", outputDecl, new GenOp( "vec4(normalize(@), @)", gbNormal, depth ) ) ); meta->addStatement( assignOutput( unconditionedOut ) ); output = meta; }
void DeferredRTLightingFeatGLSL::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( "vec4" ); Var *outPosition = (Var*) LangElement::find( "gl_Position" ); AssertFatal( outPosition, "No gl_Position, ohnoes." ); output = new GenOp( " @ = @;\r\n", ssPos, outPosition ); }
void ShaderGen::_processPixFeatures( Vector<GFXShaderMacro> ¯os, bool macrosOnly ) { const FeatureSet &features = mFeatureData.features; for( U32 i=0; i < features.getCount(); i++ ) { S32 index; const FeatureType &type = features.getAt( i, &index ); ShaderFeature* feature = FEATUREMGR->getByType( type ); if ( feature ) { feature->setProcessIndex( index ); feature->processPixMacros( macros, mFeatureData ); if ( macrosOnly ) continue; feature->mInstancingFormat = &mInstancingFormat; feature->processPix( mComponents, mFeatureData ); String line; if ( index > -1 ) line = String::ToString( " // %s %d\r\n", feature->getName().c_str(), index ); else line = String::ToString( " // %s\r\n", feature->getName().c_str() ); mOutput->addStatement( new GenOp( line ) ); if ( feature->getOutput() ) mOutput->addStatement( feature->getOutput() ); feature->reset(); mOutput->addStatement( new GenOp( " \r\n" ) ); } } ShaderConnector *connect = dynamic_cast<ShaderConnector *>( mComponents[C_CONNECTOR] ); connect->sortVars(); }
void NormalsOutFeatGLSL::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( "vec3" ); // 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 ) ) ); }
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 ) ); }
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 = [email protected];\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 GBufferConditionerGLSL::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( " @ = 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 ShaderFeatureGLSL::addOutVpos() in the vertex // shader as for SM 3.0 nothing is needed there. Var *Vpos = (Var*) LangElement::find( "gl_Position" ); //Var *Vpos = ShaderFeatureGLSL::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; }
void TerrainDetailMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { const U32 detailIndex = getProcessIndex(); // If this is a prepass and we don't have a // matching normal map... we have nothing to do. if ( fd.features.hasFeature( MFT_PrePassConditioner ) && !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) ) return; // Grab incoming texture coords... the base map feature // made sure this was created. Var *inTex = (Var*)LangElement::find( "texCoord" ); AssertFatal( inTex, "The texture coord is missing!" ); // Grab the input position. Var *inPos = (Var*)LangElement::find( "inPosition" ); if ( !inPos ) inPos = (Var*)LangElement::find( "position" ); // Get the object space eye position. Var *eyePos = _getUniformVar( "eyePos", "vec3", cspPotentialPrimitive ); MultiLine *meta = new MultiLine; // Get the distance from the eye to this vertex. Var *dist = (Var*)LangElement::find( "dist" ); if ( !dist ) { dist = new Var; dist->setType( "float" ); dist->setName( "dist" ); meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n", new DecOp( dist ), inPos, eyePos ) ); } // grab connector texcoord register ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); Var *outTex = connectComp->getElement( RT_TEXCOORD ); outTex->setName( String::ToString( "outDetCoord%d", detailIndex ) ); outTex->setType( "vec4" ); outTex->mapsToSampler = true; // Get the detail scale and fade info. Var *detScaleAndFade = new Var; detScaleAndFade->setType( "vec4" ); detScaleAndFade->setName( String::ToString( "detailScaleAndFade%d", detailIndex ) ); detScaleAndFade->uniform = true; detScaleAndFade->constSortPos = cspPotentialPrimitive; // Setup the detail coord. // // NOTE: You see here we scale the texture coord by 'xyx' // to generate the detail coord. This y is here because // its scale is flipped to correct for the non negative y // in texCoord. // // See TerrainBaseMapFeatHLSL::processVert(). // meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) ); // And sneak the detail fade thru the w detailCoord. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n", outTex, detScaleAndFade, dist, detScaleAndFade ) ); output = meta; }
void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { const S32 detailIndex = getProcessIndex(); Var *inTex = getVertTexCoord( "texCoord" ); MultiLine *meta = new MultiLine; // 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 && fd.features.hasFeature( MFT_TerrainParallaxMap ) ) { Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" ); if ( !inNegViewTS ) { ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); 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 layer samples. Var *layerSample = (Var*)LangElement::find( "layerSample" ); if ( !layerSample ) { layerSample = new Var; layerSample->setType( "float4" ); layerSample->setName( "layerSample" ); // Get the layer texture var Var *layerTex = new Var; layerTex->setType( "sampler2D" ); layerTex->setName( "macrolayerTex" ); layerTex->uniform = true; layerTex->sampler = true; layerTex->constNum = Var::getTexUnitNum(); // Read the layer texture to get the samples. if (mIsDirect3D11) { layerTex->setType("SamplerState"); Var *layerTexObj = new Var; layerTexObj->setType("Texture2D"); layerTexObj->setName("macroLayerTexObj"); layerTexObj->uniform = true; layerTexObj->texture = true; layerTexObj->constNum = layerTex->constNum; meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n", new DecOp(layerSample), layerTexObj, layerTex, inTex)); } else meta->addStatement(new GenOp(" @ = round( tex2D( @, @.xy ) * 255.0f );\r\n", new DecOp(layerSample), layerTex, inTex)); } Var *layerSize = (Var*)LangElement::find( "layerSize" ); if ( !layerSize ) { layerSize = new Var; layerSize->setType( "float" ); layerSize->setName( "layerSize" ); layerSize->uniform = true; layerSize->constSortPos = cspPass; } // Grab the incoming detail coord. Var *inDet = _getInMacroCoord( componentList ); // Get the detail id. Var *detailInfo = _getMacroIdStrengthParallax(); // Create the detail blend var. Var *detailBlend = new Var; detailBlend->setType( "float" ); detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) ); // Calculate the blend for this detail texture. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n", new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) ); // Get a var and accumulate the blend amount. Var *blendTotal = (Var*)LangElement::find( "blendTotal" ); if ( !blendTotal ) { blendTotal = new Var; blendTotal->setName( "blendTotal" ); blendTotal->setType( "float" ); meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) ); } // Add to the blend total. meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend)); Var *detailColor = (Var*)LangElement::find( "macroColor" ); if ( !detailColor ) { detailColor = new Var; detailColor->setType( "float4" ); detailColor->setName( "macroColor" ); meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) ); } // Get the detail texture. Var *detailMap = new Var; detailMap->setType( "sampler2D" ); detailMap->setName( String::ToString( "macroMap%d", detailIndex ) ); detailMap->uniform = true; detailMap->sampler = true; detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here //Create texture object for directx 11 Var *detailTex = NULL; if (mIsDirect3D11) { detailMap->setType("SamplerState"); detailTex = new Var; detailTex->setName(String::ToString("macroMapTex%d", detailIndex)); detailTex->setType("Texture2D"); detailTex->uniform = true; detailTex->texture = true; detailTex->constNum = detailMap->constNum; } // If we're using SM 3.0 then take advantage of // dynamic branching to skip layers per-pixel. if ( GFX->getPixelShaderVersion() >= 3.0f ) meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) ); meta->addStatement( new GenOp( " {\r\n" ) ); // Note that we're doing the standard greyscale detail // map technique here which can darken and lighten the // diffuse texture. // // We take two color samples and lerp between them for // side projection layers... else a single sample. // if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex)) { if (mIsDirect3D11) { meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n", detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex)); } else meta->addStatement(new GenOp(" @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet, detailMap, inDet, inTex)); } else { if (mIsDirect3D11) meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n", detailColor, detailTex, detailMap, inDet)); else meta->addStatement(new GenOp(" @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet)); } meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n", detailColor, detailInfo, inDet ) ); ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget; if(fd.features.hasFeature(MFT_DeferredTerrainMacroMap)) target= ShaderFeature::RenderTarget1; Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) ); meta->addStatement(new GenOp(" @ += @ * @;\r\n", outColor, detailColor, detailBlend)); meta->addStatement( new GenOp( " }\r\n" ) ); output = meta; }
void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { const S32 detailIndex = getProcessIndex(); // Grab incoming texture coords... the base map feature // made sure this was created. Var *inTex = (Var*)LangElement::find( "texCoord" ); AssertFatal( inTex, "The texture coord is missing!" ); // Grab the input position. Var *inPos = (Var*)LangElement::find( "inPosition" ); if ( !inPos ) inPos = (Var*)LangElement::find( "position" ); // Get the object space eye position. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive ); MultiLine *meta = new MultiLine; // If we have parallax mapping then make sure we've sent // the negative view vector to the pixel shader. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) && !LangElement::find( "outNegViewTS" ) ) { // Get the object to tangent transform which // will consume 3 output registers. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd ); // Now use a single output register to 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, eyePos, inPos ) ); } // Get the distance from the eye to this vertex. Var *dist = (Var*)LangElement::find( "dist" ); if ( !dist ) { dist = new Var; dist->setType( "float" ); dist->setName( "dist" ); meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n", new DecOp( dist ), inPos, eyePos ) ); } // grab connector texcoord register ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); Var *outTex = connectComp->getElement( RT_TEXCOORD ); outTex->setName( String::ToString( "detCoord%d", detailIndex ) ); outTex->setStructName( "OUT" ); outTex->setType( "float4" ); outTex->mapsToSampler = true; // Get the detail scale and fade info. Var *detScaleAndFade = new Var; detScaleAndFade->setType( "float4" ); detScaleAndFade->setName( String::ToString( "detailScaleAndFade%d", detailIndex ) ); detScaleAndFade->uniform = true; detScaleAndFade->constSortPos = cspPotentialPrimitive; // Setup the detail coord. // // NOTE: You see here we scale the texture coord by 'xyx' // to generate the detail coord. This y is here because // its scale is flipped to correct for the non negative y // in texCoord. // // See TerrainBaseMapFeatHLSL::processVert(). // meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) ); // And sneak the detail fade thru the w detailCoord. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n", outTex, detScaleAndFade, dist, detScaleAndFade ) ); output = meta; }
void TerrainBaseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { MultiLine *meta = new MultiLine; output = meta; // Generate the incoming texture var. Var *inTex; { Var *inPos = (Var*)LangElement::find( "inPosition" ); if ( !inPos ) inPos = (Var*)LangElement::find( "position" ); inTex = new Var( "texCoord", "float3" ); Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass ); // NOTE: The y coord here should be negative to have // the texture maps not end up flipped which also caused // normal and parallax mapping to be incorrect. // // This mistake early in development means that the layer // id bilinear blend depends on it being that way. // // So instead i fixed this by flipping the base and detail // coord y scale to compensate when rendering. // meta->addStatement( new GenOp( " @ = @.xyz * float3( @, @, -@ );\r\n", new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) ); } ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); // Pass the texture coord to the pixel shader. Var *outTex = connectComp->getElement( RT_TEXCOORD ); outTex->setName( "outTexCoord" ); outTex->setStructName( "OUT" ); outTex->setType( "float3" ); outTex->mapsToSampler = true; meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) ); // If this shader has a side projected layer then we // pass the dot product between the +Y and the normal // thru outTexCoord.z for use in blending the textures. if ( fd.features.hasFeature( MFT_TerrainSideProject ) ) { Var *inNormal = (Var*)LangElement::find( "normal" ); meta->addStatement( new GenOp( " @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n", outTex, inNormal, inNormal ) ); } else meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) ); // HACK: This is sort of lazy... we generate the tanget // vector here so that we're sure it exists in the parallax // and normal features which will expect "T" to exist. // // If this shader doesn't use it the shader compiler will // optimize away this code. // Var *inTangentZ = getVertTexCoord( "tcTangentZ" ); Var *inTanget = new Var( "T", "float3" ); Var *squareSize = _getUniformVar( "squareSize", "float", cspPass ); meta->addStatement( new GenOp( " @ = normalize( float3( @, 0, @ ) );\r\n", new DecOp( inTanget ), squareSize, inTangentZ ) ); }
void TerrainMacroMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { const U32 detailIndex = getProcessIndex(); Var *inTex = getVertTexCoord( "texCoord" ); MultiLine *meta = new MultiLine; // 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 && fd.features.hasFeature( MFT_TerrainParallaxMap ) ) { Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" ); if ( !inNegViewTS ) { ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); inNegViewTS = connectComp->getElement( RT_TEXCOORD ); inNegViewTS->setName( "outNegViewTS" ); inNegViewTS->setStructName( "IN" ); inNegViewTS->setType( "vec3" ); } negViewTS = new Var( "negViewTS", "vec3" ); meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) ); } // Get the layer samples. Var *layerSample = (Var*)LangElement::find( "layerSample" ); if ( !layerSample ) { layerSample = new Var; layerSample->setType( "vec4" ); layerSample->setName( "layerSample" ); // Get the layer texture var Var *layerTex = new Var; layerTex->setType( "sampler2D" ); layerTex->setName( "macrolayerTex" ); layerTex->uniform = true; layerTex->sampler = true; layerTex->constNum = Var::getTexUnitNum(); // Read the layer texture to get the samples. meta->addStatement( new GenOp( " @ = round( tex2D( @, @.xy ) * 255.0f );\r\n", new DecOp( layerSample ), layerTex, inTex ) ); } Var *layerSize = (Var*)LangElement::find( "layerSize" ); if ( !layerSize ) { layerSize = new Var; layerSize->setType( "float" ); layerSize->setName( "layerSize" ); layerSize->uniform = true; layerSize->constSortPos = cspPass; } // Grab the incoming detail coord. Var *inDet = _getInMacroCoord( componentList ); // Get the detail id. Var *detailInfo = _getMacroIdStrengthParallax(); // Create the detail blend var. Var *detailBlend = new Var; detailBlend->setType( "float" ); detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) ); // Calculate the blend for this detail texture. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n", new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) ); // Get a var and accumulate the blend amount. Var *blendTotal = (Var*)LangElement::find( "blendTotal" ); if ( !blendTotal ) { blendTotal = new Var; //blendTotal->setName( "blendTotal" ); blendTotal->setName( "blendTotal" ); blendTotal->setType( "float" ); meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) ); } // Add to the blend total. meta->addStatement( new GenOp( " @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend ) ); // If this is a prepass then we skip color. if ( fd.features.hasFeature( MFT_PrePassConditioner ) ) { // Check to see if we have a gbuffer normal. Var *gbNormal = (Var*)LangElement::find( "gbNormal" ); // If we have a gbuffer normal and we don't have a // normal map feature then we need to lerp in a // default normal else the normals below this layer // will show thru. if ( gbNormal && !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) ) { Var *viewToTangent = getInViewToTangent( componentList ); meta->addStatement( new GenOp( " @ = lerp( @, tGetMatrix3Row(@, 2), min( @, @.w ) );\r\n", gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) ); } output = meta; return; } Var *detailColor = (Var*)LangElement::find( "macroColor" ); if ( !detailColor ) { detailColor = new Var; detailColor->setType( "vec4" ); detailColor->setName( "macroColor" ); meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) ); } // Get the detail texture. Var *detailMap = new Var; detailMap->setType( "sampler2D" ); detailMap->setName( String::ToString( "macroMap%d", detailIndex ) ); detailMap->uniform = true; detailMap->sampler = true; detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here // If we're using SM 3.0 then take advantage of // dynamic branching to skip layers per-pixel. if ( GFX->getPixelShaderVersion() >= 3.0f ) meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) ); meta->addStatement( new GenOp( " {\r\n" ) ); // Note that we're doing the standard greyscale detail // map technique here which can darken and lighten the // diffuse texture. // // We take two color samples and lerp between them for // side projection layers... else a single sample. // if ( fd.features.hasFeature( MFT_TerrainSideProject, detailIndex ) ) { meta->addStatement( new GenOp( " @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet, detailMap, inDet, inTex ) ); } else { meta->addStatement( new GenOp( " @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet ) ); } meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n", detailColor, detailInfo, inDet ) ); Var *baseColor = (Var*)LangElement::find( "baseColor" ); Var *outColor = (Var*)LangElement::find( "col" ); meta->addStatement( new GenOp( " @ = lerp( @, @ + @, @ );\r\n", outColor, outColor, outColor, detailColor, detailBlend ) ); //outColor, outColor, baseColor, detailColor, detailBlend ) ); meta->addStatement( new GenOp( " }\r\n" ) ); output = meta; }
void DeferredRTLightingFeatGLSL::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( "vec4" ); Var *uvScene = new Var; uvScene->setType( "vec2" ); uvScene->setName( "uvScene" ); LangElement *uvSceneDecl = new DecOp( uvScene ); String rtParamName = String::ToString( "rtParams%s", "lightInfoBuffer" ); Var *rtParams = (Var*) LangElement::find( rtParamName ); if( !rtParams ) { rtParams = new Var; rtParams->setType( "vec4" ); 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( "vec4" ); 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", "vec3" ); 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( "vec2" ); 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; }
void TerrainDetailMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { const S32 detailIndex = getProcessIndex(); Var *inTex = getVertTexCoord( "texCoord" ); MultiLine *meta = new MultiLine; // 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 && fd.features.hasFeature( MFT_TerrainParallaxMap ) ) { Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" ); if ( !inNegViewTS ) { ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); inNegViewTS = connectComp->getElement( RT_TEXCOORD ); inNegViewTS->setName( "outNegViewTS" ); inNegViewTS->setStructName( "IN" ); inNegViewTS->setType( "vec3" ); } negViewTS = new Var( "negViewTS", "vec3" ); meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) ); } // Get the layer samples. Var *layerSample = (Var*)LangElement::find( "layerSample" ); if ( !layerSample ) { layerSample = new Var; layerSample->setType( "vec4" ); layerSample->setName( "layerSample" ); // Get the layer texture var Var *layerTex = new Var; layerTex->setType( "sampler2D" ); layerTex->setName( "layerTex" ); layerTex->uniform = true; layerTex->sampler = true; layerTex->constNum = Var::getTexUnitNum(); // Read the layer texture to get the samples. meta->addStatement( new GenOp( " @ = round( tex2D( @, @.xy ) * 255.0f );\r\n", new DecOp( layerSample ), layerTex, inTex ) ); } Var *layerSize = (Var*)LangElement::find( "layerSize" ); if ( !layerSize ) { layerSize = new Var; layerSize->setType( "float" ); layerSize->setName( "layerSize" ); layerSize->uniform = true; layerSize->constSortPos = cspPass; } // Grab the incoming detail coord. Var *inDet = _getInDetailCoord( componentList ); // Get the detail id. Var *detailInfo = _getDetailIdStrengthParallax(); // Create the detail blend var. Var *detailBlend = new Var; detailBlend->setType( "float" ); detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) ); // Calculate the blend for this detail texture. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n", new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) ); // New terrain Var *lerpBlend = (Var*)LangElement::find("lerpBlend"); if (!lerpBlend) { lerpBlend = new Var; lerpBlend->setType("float"); lerpBlend->setName("lerpBlend"); lerpBlend->uniform = true; lerpBlend->constSortPos = cspPrimitive; } Var *blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", detailIndex)); if (!blendDepth) { blendDepth = new Var; blendDepth->setType("float"); blendDepth->setName(String::ToString("blendDepth%d", detailIndex)); blendDepth->uniform = true; blendDepth->constSortPos = cspPrimitive; } ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget; if(fd.features.hasFeature( MFT_DeferredTerrainDetailMap )) target= ShaderFeature::RenderTarget1; Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) ); if (!outColor) { // create color var outColor = new Var; outColor->setType("float4"); outColor->setName("col"); outColor->setStructName("OUT"); meta->addStatement(new GenOp(" @;\r\n", outColor)); } Var *detailColor = (Var*)LangElement::find("detailColor"); if (!detailColor) { detailColor = new Var; detailColor->setType("float4"); detailColor->setName("detailColor"); meta->addStatement(new GenOp(" @;\r\n", new DecOp(detailColor))); } // Get the detail texture. Var *detailMap = new Var; detailMap->setType("sampler2D"); detailMap->setName(String::ToString("detailMap%d", detailIndex)); detailMap->uniform = true; detailMap->sampler = true; detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here // Get the normal map texture. Var *normalMap = _getNormalMapTex(); // Issue happens somewhere here ----- // Sample the normal map. // // We take two normal samples and lerp between them for // side projection layers... else a single sample. LangElement *texOp; // Note that we're doing the standard greyscale detail // map technique here which can darken and lighten the // diffuse texture. // // We take two color samples and lerp between them for // side projection layers... else a single sample. // if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex)) { meta->addStatement(new GenOp(" @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet, detailMap, inDet, inTex)); texOp = new GenOp("lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z )", normalMap, inDet, normalMap, inDet, inTex); } else { meta->addStatement(new GenOp(" @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet)); texOp = new GenOp("tex2D(@, @.xy)", normalMap, inDet); } // New terrain // Get a var and accumulate the blend amount. Var *blendTotal = (Var*)LangElement::find( "blendTotal" ); if ( !blendTotal ) { blendTotal = new Var; blendTotal->setName( "blendTotal" ); blendTotal->setType( "float" ); meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) ); } // Add to the blend total. meta->addStatement( new GenOp( " @ += @;\r\n", blendTotal, detailBlend ) ); // If we had a parallax feature... then factor in the parallax // amount so that it fades out with the layer blending. if ( fd.features.hasFeature( MFT_TerrainParallaxMap, detailIndex ) ) { // Get the rest of our inputs. Var *normalMap = _getNormalMapTex(); // Call the library function to do the rest. if (fd.features.hasFeature(MFT_IsDXTnm, detailIndex)) { meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @.xy, @, @.z * @ );\r\n", inDet, normalMap, inDet, negViewTS, detailInfo, detailBlend)); } else { meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @.xy, @, @.z * @ );\r\n", inDet, normalMap, inDet, negViewTS, detailInfo, detailBlend)); } } // If we're using SM 3.0 then take advantage of // dynamic branching to skip layers per-pixel. if ( GFX->getPixelShaderVersion() >= 3.0f ) meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) ); meta->addStatement( new GenOp( " {\r\n" ) ); // Note that we're doing the standard greyscale detail // map technique here which can darken and lighten the // diffuse texture. // // We take two color samples and lerp between them for // side projection layers... else a single sample. // if ( fd.features.hasFeature( MFT_TerrainSideProject, detailIndex ) ) { meta->addStatement( new GenOp( " @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet, detailMap, inDet, inTex ) ); } else { meta->addStatement( new GenOp( " @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n", detailColor, detailMap, inDet ) ); } meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n", detailColor, detailInfo, inDet ) ); meta->addStatement( new GenOp( " @ += @ * @;\r\n", outColor, detailColor, detailBlend)); meta->addStatement( new GenOp( " }\r\n" ) ); output = meta; }