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 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 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 TerrainLightMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { // grab connector texcoord register Var *inTex = (Var*)LangElement::find( "texCoord" ); if ( !inTex ) return; // Get the lightmap texture. Var *lightMap = new Var; lightMap->setType( "sampler2D" ); lightMap->setName( "lightMapTex" ); lightMap->uniform = true; lightMap->sampler = true; lightMap->constNum = Var::getTexUnitNum(); MultiLine *meta = new MultiLine; // Find or create the lightMask value which is read by // RTLighting to mask out the lights. // // The first light is always the sunlight so we apply // the shadow mask to only the first channel. // Var *lightMask = (Var*)LangElement::find( "lightMask" ); if ( !lightMask ) { lightMask = new Var( "lightMask", "vec4" ); meta->addStatement( new GenOp( " @ = vec4(1);\r\n", new DecOp( lightMask ) ) ); } meta->addStatement( new GenOp( " @[0] = tex2D( @, @.xy ).r;\r\n", lightMask, lightMap, inTex ) ); output = meta; }
void TerrainBaseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { // grab connector texcoord register Var *texCoord = getInTexCoord( "texCoord", "vec3", true, componentList ); // We do nothing more if this is a prepass. if ( fd.features.hasFeature( MFT_PrePassConditioner ) ) return; // create texture var Var *diffuseMap = new Var; diffuseMap->setType( "sampler2D" ); diffuseMap->setName( "baseTexMap" ); diffuseMap->uniform = true; diffuseMap->sampler = true; diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here MultiLine *meta = new MultiLine; Var *baseColor = new Var; baseColor->setType( "vec4" ); baseColor->setName( "baseColor" ); meta->addStatement( new GenOp( " @ = tex2D( @, @.xy );\r\n", new DecOp( baseColor ), diffuseMap, texCoord ) ); meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul ) ) ); 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; }
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 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 TerrainBaseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { // grab connector texcoord register Var *texCoord = getInTexCoord( "texCoord", "vec3", true, componentList ); // create texture var Var *diffuseMap = new Var; diffuseMap->setType( "sampler2D" ); diffuseMap->setName( "baseTexMap" ); diffuseMap->uniform = true; diffuseMap->sampler = true; diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here MultiLine *meta = new MultiLine; Var *baseColor = new Var; baseColor->setType( "vec4" ); baseColor->setName( "baseColor" ); meta->addStatement( new GenOp( " @ = tex2D( @, @.xy );\r\n", new DecOp( baseColor ), diffuseMap, texCoord ) ); meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", baseColor, baseColor)); ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget; if(fd.features.hasFeature(MFT_isDeferred)) { target= ShaderFeature::RenderTarget1; } meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) ); output = meta; }
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 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 );
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 ParallaxFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { AssertFatal( GFX->getPixelShaderVersion() >= 2.0, "ParallaxFeatGLSL::processPix - We don't support SM 1.x!" ); MultiLine *meta = new MultiLine; // Order matters... get this first! Var *texCoord = getInTexCoord( "texCoord", "vec2", 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( "vec3" ); } negViewTS = new Var( "negViewTS", "vec3" ); meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) ); } // Get the rest of our inputs. Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float", cspPotentialPrimitive ); Var *normalMap = getNormalMapTex(); // Call the library function to do the rest. if (fd.features.hasFeature(MFT_IsDXTnm, getProcessIndex())) { meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @.xy, @, @ );\r\n", texCoord, 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; }
/* * Primordial root object creation and initialization function. A pointer * to this function is passed to the object manager constructor. */ void GraphicsInitialize (RefList* root) { Picture* pict = new Picture; FullGraphic dfault; InitPPaint(); root->Append(new RefList(pict)); dfault.FillBg(true); dfault.SetColors(pblack, pwhite); dfault.SetPattern(psolid); dfault.SetBrush(psingle); dfault.SetFont(pstdfont); Line* line = new Line (0, 0, 75, 75, &dfault); MultiLine* multiline = new MultiLine (x, y, 6, &dfault); BSpline* spline = new BSpline (x, y, 6, &dfault); Rect* rect = new Rect (0, 0, 100, 100, &dfault); FillRect* frect = new FillRect (0, 0, 100, 100, &dfault); Circle* circle = new Circle (0, 0, 50, &dfault); FillCircle* fcircle = new FillCircle (0, 0, 50, &dfault); Polygon* poly = new Polygon (x, y, 6, &dfault); FillPolygon* fpoly = new FillPolygon (x, y, 6, &dfault); ClosedBSpline* cspline = new ClosedBSpline (x, y, 6, &dfault); FillBSpline* fspline = new FillBSpline (x, y, 6, &dfault); Label* label = new Label ("Type 'q' to quit this program.", &dfault); Stencil* stencil = new Stencil(new Bitmap(iv_bits, iv_width, iv_height)); RasterRect* raster = new RasterRect(CreateRaster()); line->Translate(0, 300); multiline->Translate(100, 300); spline->Translate(250, 300); rect->Translate(100, 150); frect->Translate(100, 0); circle->Scale(1.0, 0.6); circle->Translate(0, 150); fcircle->Scale(1.0, 0.6); poly->Translate(250, 150); fpoly->Translate(250, 0); cspline->Translate(400, 150); fspline->Translate(400, 0); label->Translate(350, 175); stencil->Translate(400, 300); raster->Scale(5, 5); raster->Translate(350, 350); pict->Append(line, multiline, spline, rect); pict->Append(frect, circle, fcircle, poly); pict->Append(fpoly, cspline, fspline, label); pict->Append(stencil, raster); }
void TerrainAdditiveFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { Var *color = (Var*) LangElement::find( "col" ); 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 ) ); 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 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 ConditionerFeature::_printMethod( MethodType methodType, const String &methodName, Stream &stream ) { MultiLine *meta = new MultiLine; printHeaderComment( methodType, methodName, stream, meta ); Var *paramVar = printMethodHeader( methodType, methodName, stream, meta ); Var *unconditionedInput = NULL; if( methodType == UnconditionMethod ) unconditionedInput = _unconditionInput( paramVar, meta ); else unconditionedInput = _conditionOutput( paramVar, meta ); printMethodFooter( methodType, unconditionedInput, stream, meta ); printFooterComment( methodType, methodName, stream, meta ); meta->print(stream); }
void BumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { MultiLine *meta = new MultiLine; // Get the texture coord. Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList ); // Sample the bumpmap. Var *bumpMap = getNormalMapTex(); LangElement *texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord ); Var *bumpNorm = new Var( "bumpNormal", "float4" ); meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) ); // We transform it into world space by reversing the // multiplication by the worldToTanget transform. Var *wsNormal = new Var( "wsNormal", "float3" ); Var *worldToTanget = getInWorldToTangent( componentList ); meta->addStatement( new GenOp( " @ = normalize( mul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) ); // TODO: Restore this! /* // Check to see if we're rendering world space normals. if ( fd.materialFeatures[MFT_NormalsOut] ) { Var *inNormal = getInTexCoord( "normal", "float3", false, componentList ); LangElement *normalOut; Var *outColor = (Var*)LangElement::find( "col" ); if ( outColor ) normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, @.a )", inNormal, outColor ); else normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, 1 )", inNormal ); meta->addStatement( new GenOp( " @; // MFT_NormalsOut\r\n", assignColor( normalOut, Material::None ) ) ); output = meta; return; } */ output = meta; }
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 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 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 BumpFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) { MultiLine *meta = new MultiLine; output = meta; // Get the texture coord. Var *texCoord = getInTexCoord( "texCoord", "vec2", true, componentList ); // Sample the bumpmap. Var *bumpMap = getNormalMapTex(); LangElement *texOp = NULL; //Handle atlased textures // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47 if(fd.features[MFT_NormalMapAtlas]) { // This is a big block of code, so put a comment in the shader code meta->addStatement( new GenOp( " // Atlased texture coordinate calculation (see BumpFeat*LSL for details)\r\n") ); Var *atlasedTex = new Var; atlasedTex->setName("atlasedBumpCoord"); atlasedTex->setType( "vec2" ); LangElement *atDecl = new DecOp(atlasedTex); // Parameters of the texture atlas Var *atParams = new Var; atParams->setType( "float4" ); atParams->setName("bumpAtlasParams"); atParams->uniform = true; atParams->constSortPos = cspPotentialPrimitive; // Parameters of the texture (tile) this object is using in the atlas Var *tileParams = new Var; tileParams->setType( "float4" ); tileParams->setName("bumpAtlasTileParams"); tileParams->uniform = true; tileParams->constSortPos = cspPotentialPrimitive; const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f); if(is_sm3) { // Figure out the mip level meta->addStatement( new GenOp( " float2 _dx_bump = ddx(@ * @.z);\r\n", texCoord, atParams ) ); meta->addStatement( new GenOp( " float2 _dy_bump = ddy(@ * @.z);\r\n", texCoord, atParams ) ); meta->addStatement( new GenOp( " float mipLod_bump = 0.5 * log2(max(dot(_dx_bump, _dx_bump), dot(_dy_bump, _dy_bump)));\r\n")); meta->addStatement( new GenOp( " mipLod_bump = clamp(mipLod_bump, 0.0, @.w);\r\n", atParams)); // And the size of the mip level meta->addStatement(new GenOp(" float mipPixSz_bump = pow(2.0, @.w - mipLod_bump);\r\n", atParams)); meta->addStatement( new GenOp( " float2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams ) ); } else { meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n")); } // Tiling mode if( true ) // Wrap meta->addStatement( new GenOp( " @ = frac(@);\r\n", atDecl, texCoord ) ); else // Clamp meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, texCoord)); // Finally scale/offset, and correct for filtering meta->addStatement( new GenOp( " @ = @ * ((mipSz_bump * @.xy - 1.0) / mipSz_bump) + 0.5 / mipSz_bump + @.xy * @.xy;\r\n", atlasedTex, atlasedTex, atParams, atParams, tileParams)); // Add a newline meta->addStatement(new GenOp( "\r\n")); if(is_sm3) { texOp = new GenOp( "tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord ); } else { texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord ); } } else { texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord ); } Var *bumpNorm = new Var( "bumpNormal", "float4" ); meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) ); // If we have a detail normal map we add the xy coords of // it to the base normal map. This gives us the effect we // want with few instructions and minial artifacts. if ( fd.features.hasFeature( MFT_DetailNormalMap ) ) { bumpMap = new Var; bumpMap->setType( "sampler2D" ); bumpMap->setName( "detailBumpMap" ); bumpMap->uniform = true; bumpMap->sampler = true; bumpMap->constNum = Var::getTexUnitNum(); texCoord = getInTexCoord( "detCoord", "vec2", true, componentList ); texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord ); Var *detailBump = new Var; detailBump->setName( "detailBump" ); detailBump->setType( "float4" ); meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) ); Var *detailBumpScale = new Var; detailBumpScale->setType( "float" ); detailBumpScale->setName( "detailBumpStrength" ); detailBumpScale->uniform = true; detailBumpScale->constSortPos = cspPass; meta->addStatement( new GenOp( " @.xy += @.xy * @;\r\n", bumpNorm, detailBump, detailBumpScale ) ); } // We transform it into world space by reversing the // multiplication by the worldToTanget transform. Var *wsNormal = new Var( "wsNormal", "vec3" ); Var *worldToTanget = getInWorldToTangent( componentList ); meta->addStatement( new GenOp( " @ = normalize( tMul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) ); }
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 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 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 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; }