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 TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { MultiLine *meta = new MultiLine; Var *viewToTangent = getInViewToTangent( componentList ); // This var is read from GBufferConditionerHLSL and // used in the prepass output. Var *gbNormal = (Var*)LangElement::find( "gbNormal" ); if ( !gbNormal ) { gbNormal = new Var; gbNormal->setName( "gbNormal" ); gbNormal->setType( "float3" ); meta->addStatement( new GenOp( " @ = @[2];\r\n", new DecOp( gbNormal ), viewToTangent ) ); } const S32 normalIndex = getProcessIndex(); Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) ); AssertFatal( detailBlend, "The detail blend is missing!" ); // 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" ) ); // Get the normal map texture. Var *normalMap = _getNormalMapTex(); /// Get the texture coord. Var *inDet = _getInDetailCoord( componentList ); Var *inTex = getVertTexCoord( "texCoord" ); // Sample the normal map. // // We take two normal samples and lerp between them for // side projection layers... else a single sample. LangElement *texOp; if (mIsDirect3D11) { String name(String::ToString("normalMapTex%d", getProcessIndex())); Var *normalMapTex = (Var*)LangElement::find(name); if (!normalMapTex) { normalMapTex = new Var; normalMapTex->setName(String::ToString("normalMapTex%d", getProcessIndex())); normalMapTex->setType("Texture2D"); normalMapTex->uniform = true; normalMapTex->texture = true; normalMapTex->constNum = normalMap->constNum; } if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex)) { texOp = new GenOp("lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z )", normalMapTex, normalMap, inDet, normalMapTex, normalMap, inDet, inTex); } else texOp = new GenOp("@.Sample(@, @.xy)", normalMapTex, normalMap, inDet); } else { if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex)) { texOp = new GenOp("lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z )", normalMap, inDet, normalMap, inDet, inTex); } else texOp = new GenOp("tex2D(@, @.xy)", normalMap, inDet); } // 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 ) ); // Normalize is done later... // Note: The reverse mul order is intentional. Affine matrix. meta->addStatement( new GenOp( " @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n", gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet ) ); // End the conditional block. meta->addStatement( new GenOp( " }\r\n" ) ); // If this is the last normal map then we // can test to see the total blend value // to see if we should clip the result. //if ( fd.features.getNextFeatureIndex( MFT_TerrainNormalMap, normalIndex ) == -1 ) //meta->addStatement( new GenOp( " clip( @ - 0.0001f );\r\n", blendTotal ) ); output = meta; }
void DeferredBumpFeatGLSL::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", "vec2", true, componentList ); LangElement *texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord ); // create bump normal Var *bumpNorm = new Var; bumpNorm->setName( "bumpNormal" ); bumpNorm->setType( "vec4" ); 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", "vec2", true, componentList ); texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord ); Var *detailBump = new Var; detailBump->setName( "detailBump" ); detailBump->setType( "vec4" ); 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(tMul( @.xyz, @ ));\r\n", gbNormalDecl, bumpNorm, viewToTangent ) ); output = meta; return; } else if (fd.features[MFT_AccuMap]) { Var *bumpSample = (Var *)LangElement::find("bumpSample"); if (bumpSample == NULL) { MultiLine *meta = new MultiLine; Var *texCoord = getInTexCoord("texCoord", "vec2", true, componentList); Var *bumpMap = getNormalMapTex(); bumpSample = new Var; bumpSample->setType("vec4"); bumpSample->setName("bumpSample"); LangElement *bumpSampleDecl = new DecOp(bumpSample); meta->addStatement(new GenOp(" @ = tex2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord)); if (fd.features.hasFeature(MFT_DetailNormalMap)) { Var *bumpMap = (Var*)LangElement::find("detailBumpMap"); if (!bumpMap) { bumpMap = new Var; bumpMap->setType("sampler2D"); bumpMap->setName("detailBumpMap"); bumpMap->uniform = true; bumpMap->sampler = true; bumpMap->constNum = Var::getTexUnitNum(); } texCoord = getInTexCoord("detCoord", "vec2", true, componentList); LangElement *texOp = new GenOp("tex2D(@, @)", bumpMap, texCoord); Var *detailBump = new Var; detailBump->setName("detailBump"); detailBump->setType("vec4"); 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", bumpSample, detailBump, detailBumpScale)); } 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", "vec2", true, componentList ); Var *bumpMap = getNormalMapTex(); bumpSample = new Var; bumpSample->setType( "vec4" ); bumpSample->setName( "bumpSample" ); LangElement *bumpSampleDecl = new DecOp( bumpSample ); output = new GenOp( " @ = tex2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord ); return; } } output = NULL; }