void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd) { // search for material var Var *material; OutputTarget targ = RenderTarget1; if (fd.features[MFT_isDeferred]) { targ = RenderTarget2; } material = (Var*)LangElement::find(getOutputTargetVarName(targ)); MultiLine * meta = new MultiLine; if (!material) { // create color var material = new Var; material->setType("fragout"); material->setName(getOutputTargetVarName(targ)); material->setStructName("OUT"); } meta->addStatement(new GenOp(" @ = float4(0.0,0.0,0.0,0.0001);\r\n", material)); output = meta; }
void TerrainAdditiveFeatHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { Var *color = NULL; Var *normal = NULL; if (fd.features[MFT_DeferredTerrainDetailMap]) { color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) ); normal = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) ); } else color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) ); Var *blendTotal = (Var*)LangElement::find( "blendTotal" ); if ( !color || !blendTotal ) return; MultiLine *meta = new MultiLine; meta->addStatement( new GenOp( " clip( @ - 0.0001 );\r\n", blendTotal ) ); meta->addStatement( new GenOp( " @.a = @;\r\n", color, blendTotal ) ); if (normal) meta->addStatement(new GenOp(" @.a = @;\r\n", normal, blendTotal)); output = meta; }
LangElement *ConditionerFeature::assignOutput( Var *unconditionedOutput, ShaderFeature::OutputTarget outputTarget /* = ShaderFeature::DefaultTarget*/ ) { LangElement *assign; MultiLine *meta = new MultiLine; meta->addStatement( new GenOp( avar( "\r\n\r\n // output buffer format: %s\r\n", GFXStringTextureFormat[getBufferFormat()] ) ) ); // condition the output Var *conditionedOutput = _conditionOutput( unconditionedOutput, meta ); // search for color var Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) ); if ( !color ) { // create color var color = new Var; if(GFX->getAdapterType() == OpenGL) { color->setName( getOutputTargetVarName(outputTarget) ); color->setType( "vec4" ); DecOp* colDecl = new DecOp(color); assign = new GenOp( "@ = vec4(@)", colDecl, conditionedOutput ); } else { color->setType( "fragout" ); color->setName( getOutputTargetVarName(outputTarget) ); color->setStructName( "OUT" ); assign = new GenOp( "@ = @", color, conditionedOutput ); } } else { if (GFX->getAdapterType() == OpenGL) assign = new GenOp( "@ = vec4(@)", color, conditionedOutput); else assign = new GenOp( "@ = @", color, conditionedOutput ); } meta->addStatement( new GenOp( " @;\r\n", assign ) ); return 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 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; }
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; }