2
0
Эх сурвалжийг харах

Update GLSL Shadergen. Not used on DX9.

LuisAntonRebollo 11 жил өмнө
parent
commit
9221b4dd10

+ 31 - 34
Engine/source/forest/glsl/windDeformationGLSL.cpp

@@ -40,6 +40,9 @@
 
 static void _onRegisterFeatures( GFXAdapterType type )
 {
+   if ( type != OpenGL )
+      return;
+
    FEATUREMGR->registerFeature( MFT_WindEffect, new WindDeformationGLSL );
 }
 
@@ -83,29 +86,14 @@ void WindDeformationGLSL::processVert( Vector<ShaderComponent*> &componentList,
    // save constant space and reduce the memory copied to the
    // card.
    //
-   // This in particular helps when we're instancing.
-   //
    // .x = bend scale
    // .y = branch amplitude
    // .z = detail amplitude
    // .w = detail frequency
    //
-   Var *windParams;
-   if ( fd.features[MFT_UseInstancing] ) 
-   {
-      ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
-      windParams = vertStruct->getElement( RT_TEXCOORD );
-      windParams->setName( "inst_windParams" );
-      windParams->setType( "vec4" );
-
-      mInstancingFormat->addElement( "windParams", GFXDeclType_Float4, windParams->constNum );
-   }
-   else
-   {
-      windParams = new Var( "windParams", "vec4" );
+   Var *windParams = new Var( "windParams", "vec4" );
       windParams->uniform = true;
       windParams->constSortPos = cspPotentialPrimitive;
-   }
 
    // If we're instancing then we need to instance the wind direction
    // and speed as its unique for each tree instance.
@@ -114,6 +102,7 @@ void WindDeformationGLSL::processVert( Vector<ShaderComponent*> &componentList,
    {
       ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
       windDirAndSpeed = vertStruct->getElement( RT_TEXCOORD );
+      windDirAndSpeed->setStructName( "IN" );
       windDirAndSpeed->setName( "inst_windDirAndSpeed" );
       windDirAndSpeed->setType( "vec3" );
 
@@ -143,16 +132,31 @@ void WindDeformationGLSL::processVert( Vector<ShaderComponent*> &componentList,
    if ( !inPosition )
       inPosition = (Var*)LangElement::find( "position" );
 
+   // Copy the input position to the output first as 
+   // the wind effects are conditional.
+   Var *outPosition = (Var*)LangElement::find( "inPosition" );
+   if ( !outPosition )
+   {
+      outPosition = new Var;
+      outPosition->setType( "vec3" );
+      outPosition->setName( "inPosition" );
+      meta->addStatement( new GenOp("   @ = @.xyz;\r\n", new DecOp( outPosition ), inPosition ) );
+   }
+
    // Get the incoming color data
    Var *inColor = (Var*)LangElement::find( "diffuse" );
 
+   // Do a dynamic branch based on wind force.
+   if ( GFX->getPixelShaderVersion() >= 3.0f )
+      meta->addStatement( new GenOp("   if ( any( bvec3(@) ) ) {\r\n", windDirAndSpeed ) );
+
    // Do the branch and detail bending first so that 
    // it can work in pure object space of the tree.
    LangElement *effect = 
       new GenOp(  "windBranchBending( "
 
-                     "@.xyz, "                  // vPos
-                     "normalize( normal ), " // vNormal
+                     "@, "                  // vPos
+                     "normalize( IN_normal ), " // vNormal
 
                      "@, " // fTime
                      "@.z, " // fWindSpeed
@@ -161,13 +165,13 @@ void WindDeformationGLSL::processVert( Vector<ShaderComponent*> &componentList,
                      "@.y, "    // fBranchAmp
                      "@.r, "  // fBranchAtten
 
-                     "dot( @[3], vec4( 1.0 ) ), "    // fDetailPhase
+                     "dot( @[3], vec4(1) ), "    // fDetailPhase
                      "@.z, "  // fDetailAmp
                      "@.w, "  // fDetailFreq
-
+                     
                      "@.b )", // fEdgeAtten
 
-         inPosition,    // vPos
+         outPosition,    // vPos
                         // vNormal
 
          accumTime,  // fTime
@@ -183,22 +187,15 @@ void WindDeformationGLSL::processVert( Vector<ShaderComponent*> &componentList,
 
          inColor ); // fEdgeAtten
 
-   Var *outPosition = (Var*)LangElement::find( "inPosition" );
-   if ( outPosition )
-      meta->addStatement( new GenOp( "   @.xyz = @;\r\n", outPosition, effect, inPosition ) );
-   else    
-   {
-      outPosition = new Var;
-      outPosition->setType( "vec3" );
-      outPosition->setName( "inPosition" );
-      meta->addStatement( new GenOp("   vec3 inPosition = @;\r\n", effect, inPosition ) );
-   }
+   meta->addStatement( new GenOp( "   @ = @;\r\n", outPosition, effect ) );
 
    // Now do the trunk bending.
-   effect = new GenOp( "windTrunkBending( @, @.xy, @.z * @.x )",
-                        outPosition, windDirAndSpeed, outPosition, windParams );
+   meta->addStatement( new GenOp("   @ = windTrunkBending( @, @.xy, @.z * @.x );\r\n", 
+      outPosition, outPosition, windDirAndSpeed, outPosition, windParams ) );
 
-   meta->addStatement( new GenOp("   @ = @;\r\n", outPosition, effect ) );
+   // End the dynamic branch.
+   if ( GFX->getPixelShaderVersion() >= 3.0f )
+      meta->addStatement( new GenOp("   } // [branch]\r\n" ) );
 }
 
 ShaderFeatureConstHandles* WindDeformationGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )

+ 189 - 274
Engine/source/lighting/advanced/glsl/advancedLightingFeaturesGLSL.cpp

@@ -35,16 +35,12 @@
 void DeferredRTLightingFeatGLSL::processPixMacros( Vector<GFXShaderMacro> &macros, 
                                                    const MaterialFeatureData &fd  )
 {
-   /// TODO: This needs to be done via some sort of material
-   /// feature and not just allow all translucent elements to
-   /// read from the light prepass.
-   /*
-   if ( fd.features[MFT_IsTranslucent] )
+   // Skip deferred features, and use forward shading instead
+   if ( fd.features[MFT_ForwardShading] )
    {
       Parent::processPixMacros( macros, fd );
       return;
    }
-   */
 
    // Pull in the uncondition method for the light info buffer
    NamedTexTarget *texTarget = NamedTexTarget::find( AdvancedLightBinManager::smBufferName );
@@ -59,48 +55,42 @@ void DeferredRTLightingFeatGLSL::processPixMacros( Vector<GFXShaderMacro> &macro
 void DeferredRTLightingFeatGLSL::processVert(   Vector<ShaderComponent*> &componentList, 
                                                 const MaterialFeatureData &fd )
 {
-   /// TODO: This needs to be done via some sort of material
-   /// feature and not just allow all translucent elements to
-   /// read from the light prepass.
-   /*
-   if ( fd.features[MFT_IsTranslucent] )
+   // 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( "hpos" );
-//   AssertFatal( outPosition, "No hpos, ohnoes." );
+   Var *outPosition = (Var*) LangElement::find( "gl_Position" );
+   AssertFatal( outPosition, "No gl_Position, ohnoes." );
 
-   output = new GenOp( "   @ = gl_Position;\r\n", ssPos );
+   output = new GenOp( "   @ = @;\r\n", ssPos, outPosition );
 }
 
 void DeferredRTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
                                              const MaterialFeatureData &fd )
 {
-   /// TODO: This needs to be done via some sort of material
-   /// feature and not just allow all translucent elements to
-   /// read from the light prepass.
-   /*
-   if ( fd.features[MFT_IsTranslucent] )
+   // 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;
@@ -108,18 +98,20 @@ void DeferredRTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &component
    uvScene->setName( "uvScene" );
    LangElement *uvSceneDecl = new DecOp( uvScene );
 
-   Var *rtParams = (Var*) LangElement::find( "renderTargetParams" );
+   String rtParamName = String::ToString( "rtParams%s", "lightInfoBuffer" );
+   Var *rtParams = (Var*) LangElement::find( rtParamName );
    if( !rtParams )
    {
       rtParams = new Var;
       rtParams->setType( "vec4" );
-      rtParams->setName( "renderTargetParams" );
+      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;
@@ -134,36 +126,60 @@ void DeferredRTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &component
    lightInfoBuffer->sampler = true;
    lightInfoBuffer->constNum = Var::getTexUnitNum();     // used as texture unit num here
 
-   String unconditionLightInfo = String::ToLower( AdvancedLightBinManager::smBufferName ) + "Uncondition";
+   // 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 ) ) );
    
-   meta->addStatement( new GenOp( "   vec3 d_lightcolor;\r\n" ) );
-   meta->addStatement( new GenOp( "   float d_NL_Att;\r\n" ) );
-   meta->addStatement( new GenOp( "   float d_specular;\r\n" ) );
-   meta->addStatement( new GenOp( avar( "   %s(texture2D(@, @), d_lightcolor, d_NL_Att, d_specular);\r\n", unconditionLightInfo.c_str() ), 
-      lightInfoBuffer, uvScene ) );
-
-   Var *rtShading = new Var;
-   rtShading->setType( "vec4" );
-   rtShading->setName( "rtShading" );
-   LangElement *rtShadingDecl = new DecOp( rtShading );
-   meta->addStatement( new GenOp( "   @ = vec4( d_lightcolor, 1.0 );\r\n", rtShadingDecl ) );
+
+   // 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_SubSurface] && !fd.features[MFT_ToneMap] && !fd.features[MFT_LightMap] )
-      meta->addStatement( new GenOp( "   @;\r\n", assignColor( rtShading, Material::Mul ) ) );
+   if( !fd.features[MFT_VertLit] && !fd.features[MFT_ToneMap] && !fd.features[MFT_LightMap] && !fd.features[MFT_SubSurface] )
+      meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "float4(@, 1.0)", d_lightcolor ), Material::Mul ) ) );
 
    output = meta;
 }
 
 ShaderFeature::Resources DeferredRTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
 {
-   /// TODO: This needs to be done via some sort of material
-   /// feature and not just allow all translucent elements to
-   /// read from the light prepass.
-   /*
-   if( fd.features[MFT_IsTranslucent] )
+   // Skip deferred features, and use forward shading instead
+   if ( fd.features[MFT_ForwardShading] )
       return Parent::getResources( fd );
-   */
+
+   // HACK: See DeferredRTLightingFeatGLSL::setTexData.
+   mLastTexIndex = 0;
 
    Resources res; 
    res.numTex = 1;
@@ -176,21 +192,22 @@ void DeferredRTLightingFeatGLSL::setTexData( Material::StageData &stageDat,
                                              RenderPassData &passData, 
                                              U32 &texIndex )
 {
-   /// TODO: This needs to be done via some sort of material
-   /// feature and not just allow all translucent elements to
-   /// read from the light prepass.
-   /*
-   if( fd.features[MFT_IsTranslucent] )
+   // Skip deferred features, and use forward shading instead
+   if ( fd.features[MFT_ForwardShading] )
    {
       Parent::setTexData( stageDat, fd, passData, texIndex );
       return;
    }
-   */
 
    NamedTexTarget *texTarget = NamedTexTarget::find( AdvancedLightBinManager::smBufferName );
    if( texTarget )
    {
-      passData.mTexType[ texIndex ] = Material::TexTarget;
+      // HACK: We store this for use in DeferredRTLightingFeatGLSL::processPix()
+      // which cannot deduce the texture unit itself.
+      mLastTexIndex = texIndex;
+
+      passData.mTexType[ texIndex ] = Material::TexTarget;      
+      passData.mSamplerNames[ texIndex ]= "lightInfoBuffer";
       passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
    }
 }
@@ -205,92 +222,31 @@ void DeferredBumpFeatGLSL::processVert(   Vector<ShaderComponent*> &componentLis
       // to the pixel shader. 
       MultiLine *meta = new MultiLine;
 
-      // setup texture space matrix
-      Var *texSpaceMat = (Var*) LangElement::find( "objToTangentSpace" );
-      if( !texSpaceMat )
-      {
-         LangElement * texSpaceSetup = setupTexSpaceMat( componentList, &texSpaceMat );
-         meta->addStatement( texSpaceSetup );
-         texSpaceMat = (Var*) LangElement::find( "objToTangentSpace" );
-      }
-
-      // turn obj->tangent into world->tangent
-      Var *worldToTangent = new Var;
-      worldToTangent->setType( "mat3" );
-      worldToTangent->setName( "worldToTangent" );
-      LangElement *worldToTangentDecl = new DecOp( worldToTangent );
-
-      // Get the world->obj transform
-      Var *worldToObj = new Var;
-      worldToObj->setType( "mat4" );
-      worldToObj->setName( "worldToObj" );
-      worldToObj->uniform = true;
-      worldToObj->constSortPos = cspPrimitive;
-      
-      Var *mat3Conversion = new Var;
-      mat3Conversion->setType( "mat3" );
-      mat3Conversion->setName( "worldToObjMat3" );
-      LangElement* mat3Lang = new DecOp(mat3Conversion);
-      meta->addStatement( new GenOp( "   @ = mat3(@[0].xyz, @[1].xyz, @[2].xyz);\r\n ", mat3Lang, worldToObj, worldToObj, worldToObj) );
-
-      // assign world->tangent transform
-      meta->addStatement( new GenOp( "   @ = @ * @;\r\n", worldToTangentDecl, texSpaceMat, mat3Conversion ) );
-
-      // send transform to pixel shader
-      ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-
-      Var *worldToTangentR1 = connectComp->getElement( RT_TEXCOORD );
-      worldToTangentR1->setName( "worldToTangentR1" );
-      worldToTangentR1->setType( "vec3" );
-      meta->addStatement( new GenOp( "   @ = @[0];\r\n", worldToTangentR1, worldToTangent ) );
-
-      Var *worldToTangentR2 = connectComp->getElement( RT_TEXCOORD );
-      worldToTangentR2->setName( "worldToTangentR2" );
-      worldToTangentR2->setType( "vec3" );
-      meta->addStatement( new GenOp( "   @ = @[1];\r\n", worldToTangentR2, worldToTangent ) );
-
-      Var *worldToTangentR3 = connectComp->getElement( RT_TEXCOORD );
-      worldToTangentR3->setName( "worldToTangentR3" );
-      worldToTangentR3->setType( "vec3" );
-      meta->addStatement( new GenOp( "   @ = @[2];\r\n", worldToTangentR3, worldToTangent ) );
+      // We need the view to tangent space transform in the pixel shader.
+      getOutViewToTangent( componentList, meta, fd );
 
       // Make sure there are texcoords
-      if( !fd.features[MFT_DiffuseMap] )
+      if( !fd.features[MFT_Parallax] && !fd.features[MFT_DiffuseMap] )
       {
-         // find incoming texture var
-         Var *inTex = getVertTexCoord( "texCoord" );
-
-         // grab connector texcoord register
-         ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-         Var *outTex = connectComp->getElement( RT_TEXCOORD );
-         outTex->setName( "outTexCoord" );
-         outTex->setType( "vec2" );
-         outTex->mapsToSampler = true;
-
-         if( fd.features[MFT_TexAnim] )
-         {
-            inTex->setType( "vec4" );
-
-            // create texture mat var
-            Var *texMat = new Var;
-            texMat->setType( "mat4" );
-            texMat->setName( "texMat" );
-            texMat->uniform = true;
-            texMat->constSortPos = cspPotentialPrimitive;
-
-            meta->addStatement( new GenOp( "   @ = @ * @;\r\n", outTex, texMat, inTex ) );
-         }
-         else
-         {
-            // setup language elements to output incoming tex coords to output
-            meta->addStatement( new GenOp( "   @ = @;\r\n", outTex, inTex ) );
-         }
+         const bool useTexAnim = fd.features[MFT_TexAnim];
+
+         getOutTexCoord(   "texCoord", 
+                           "vec2", 
+                           true, 
+                           useTexAnim, 
+                           meta, 
+                           componentList );
+
+         if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
+            addOutDetailTexCoord( componentList, 
+                                  meta,
+                                  useTexAnim );
       }
 
       output = meta;
    }
    else if (   fd.materialFeatures[MFT_NormalsOut] || 
-               fd.features[MFT_IsTranslucent] || 
+               fd.features[MFT_ForwardShading] || 
                !fd.features[MFT_RTLighting] )
    {
       Parent::processVert( componentList, fd );
@@ -312,52 +268,12 @@ void DeferredBumpFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
    {
       MultiLine *meta = new MultiLine;
 
-      // Pull the world->tangent transform from the vertex shader
-      ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-
-      Var *worldToTangentR1 = connectComp->getElement( RT_TEXCOORD );
-      worldToTangentR1->setName( "worldToTangentR1" );
-      worldToTangentR1->setType( "vec3" );
-
-      Var *worldToTangentR2 = connectComp->getElement( RT_TEXCOORD );
-      worldToTangentR2->setName( "worldToTangentR2" );
-      worldToTangentR2->setType( "vec3" );
-
-      Var *worldToTangentR3 = connectComp->getElement( RT_TEXCOORD );
-      worldToTangentR3->setName( "worldToTangentR3" );
-      worldToTangentR3->setType( "vec3" );
-
-      Var *worldToTangent = new Var;
-      worldToTangent->setType( "mat3" );
-      worldToTangent->setName( "worldToTangent" );
-      LangElement *worldToTangentDecl = new DecOp( worldToTangent );
-
-      // Build world->tangent matrix
-      meta->addStatement( new GenOp( "   @;\r\n", worldToTangentDecl ) );
-      meta->addStatement( new GenOp( "   @[0] = @;\r\n", worldToTangent, worldToTangentR1 ) );
-      meta->addStatement( new GenOp( "   @[1] = @;\r\n", worldToTangent, worldToTangentR2 ) );
-      meta->addStatement( new GenOp( "   @[2] = @;\r\n", worldToTangent, worldToTangentR3 ) );
+      Var *viewToTangent = getInViewToTangent( componentList );
 
       // create texture var
-      Var *bumpMap = new Var;
-      bumpMap->setType( "sampler2D" );
-      bumpMap->setName( "bumpMap" );
-      bumpMap->uniform = true;
-      bumpMap->sampler = true;
-      bumpMap->constNum = Var::getTexUnitNum();     // used as texture unit num here
-
-      Var *texCoord = (Var*) LangElement::find( "outTexCoord" );
-      if( !texCoord )
-      {
-         // grab connector texcoord register
-         ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-         texCoord = connectComp->getElement( RT_TEXCOORD );
-         texCoord->setName( "outTexCoord" );
-         texCoord->setType( "vec2" );
-         texCoord->mapsToSampler = true;
-      }
-
-      LangElement * texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
+      Var *bumpMap = getNormalMapTex();
+      Var *texCoord = getInTexCoord( "texCoord", "vec2", true, componentList );
+      LangElement *texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
 
       // create bump normal
       Var *bumpNorm = new Var;
@@ -367,56 +283,75 @@ void DeferredBumpFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
       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( "vec3" );
+      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( "   @ = @.xyz * @;\r\n", gbNormalDecl, bumpNorm, worldToTangent ) );
+      meta->addStatement( new GenOp( "   @ = half3(tMul( @.xyz, @ ));\r\n", gbNormalDecl, bumpNorm, viewToTangent ) );
 
       output = meta;
       return;
    }
    else if (   fd.materialFeatures[MFT_NormalsOut] || 
-               fd.features[MFT_IsTranslucent] || 
+               fd.features[MFT_ForwardShading] || 
                !fd.features[MFT_RTLighting] )
    {
       Parent::processPix( componentList, fd );
       return;
    }
-   else if ( fd.features[MFT_PixSpecular] )
+   else if ( fd.features[MFT_PixSpecular] && !fd.features[MFT_SpecularMap] )
    {
       Var *bumpSample = (Var *)LangElement::find( "bumpSample" );
       if( bumpSample == NULL )
       {
-         Var *texCoord = (Var*) LangElement::find( "outTexCoord" );
-         if( !texCoord )
-         {
-            // grab connector texcoord register
-            ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-            texCoord = connectComp->getElement( RT_TEXCOORD );
-            texCoord->setName( "outTexCoord" );
-            texCoord->setType( "vec2" );
-            texCoord->mapsToSampler = true;
-         }
-
-         Var *bumpMap = new Var;
-         bumpMap->setType( "sampler2D" );
-         bumpMap->setName( "bumpMap" );
-         bumpMap->uniform = true;
-         bumpMap->sampler = true;
-         bumpMap->constNum = Var::getTexUnitNum();     // used as texture unit num here
+         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( "   @ = texture2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord );
+         output = new GenOp( "   @ = tex2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord );
          return;
       }
    }
@@ -427,7 +362,7 @@ void DeferredBumpFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
 ShaderFeature::Resources DeferredBumpFeatGLSL::getResources( const MaterialFeatureData &fd )
 {
    if (  fd.materialFeatures[MFT_NormalsOut] || 
-         fd.features[MFT_IsTranslucent] || 
+         fd.features[MFT_ForwardShading] || 
          fd.features[MFT_Parallax] ||
          !fd.features[MFT_RTLighting] )
       return Parent::getResources( fd );
@@ -437,7 +372,16 @@ ShaderFeature::Resources DeferredBumpFeatGLSL::getResources( const MaterialFeatu
    {
       res.numTex = 1;
       res.numTexReg = 1;
+
+      if (  fd.features[MFT_PrePassConditioner] &&
+            fd.features.hasFeature( MFT_DetailNormalMap ) )
+      {
+         res.numTex += 1;
+         if ( !fd.features.hasFeature( MFT_DetailMap ) )
+            res.numTexReg += 1;
+      }
    }
+
    return res;
 }
 
@@ -447,21 +391,28 @@ void DeferredBumpFeatGLSL::setTexData( Material::StageData &stageDat,
                                        U32 &texIndex )
 {
    if (  fd.materialFeatures[MFT_NormalsOut] || 
-         fd.features[MFT_IsTranslucent] || 
+         fd.features[MFT_ForwardShading] || 
          !fd.features[MFT_RTLighting] )
    {
       Parent::setTexData( stageDat, fd, passData, texIndex );
       return;
    }
 
-   GFXTextureObject *normalMap = stageDat.getTex( MFT_NormalMap );
    if (  !fd.features[MFT_Parallax] && !fd.features[MFT_SpecularMap] &&
          ( fd.features[MFT_PrePassConditioner] ||
-           fd.features[MFT_PixSpecular] ) &&         
-         normalMap )
+           fd.features[MFT_PixSpecular] ) )
    {
       passData.mTexType[ texIndex ] = Material::Bump;
-      passData.mTexSlot[ texIndex++ ].texObject = normalMap;
+      passData.mSamplerNames[ texIndex ] = "bumpMap";
+      passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
+
+      if (  fd.features[MFT_PrePassConditioner] &&
+            fd.features.hasFeature( MFT_DetailNormalMap ) )
+      {
+         passData.mTexType[ texIndex ] = Material::DetailBump;
+         passData.mSamplerNames[ texIndex ] = "detailBumpMap";
+         passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
+      }
    }
 }
 
@@ -469,7 +420,7 @@ void DeferredBumpFeatGLSL::setTexData( Material::StageData &stageDat,
 void DeferredPixelSpecularGLSL::processVert( Vector<ShaderComponent*> &componentList, 
                                              const MaterialFeatureData &fd )
 {
-   if( fd.features[MFT_IsTranslucent] || !fd.features[MFT_RTLighting] )
+   if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
    {
       Parent::processVert( componentList, fd );
       return;
@@ -480,7 +431,7 @@ void DeferredPixelSpecularGLSL::processVert( Vector<ShaderComponent*> &component
 void DeferredPixelSpecularGLSL::processPix(  Vector<ShaderComponent*> &componentList, 
                                              const MaterialFeatureData &fd )
 {
-   if( fd.features[MFT_IsTranslucent] || !fd.features[MFT_RTLighting] )
+   if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
    {
       Parent::processPix( componentList, fd );
       return;
@@ -523,19 +474,18 @@ void DeferredPixelSpecularGLSL::processPix(  Vector<ShaderComponent*> &component
    specStrength->uniform = true;
    specStrength->constSortPos = cspPotentialPrimitive;
 
-   Var *constSpecPow = new Var;
-   constSpecPow->setType( "float" );
-   constSpecPow->setName( "constantSpecularPower" );
-   constSpecPow->uniform = true;
-   constSpecPow->constSortPos = cspPass;
-
    Var *lightInfoSamp = (Var *)LangElement::find( "lightInfoSample" );
-   AssertFatal( lightInfoSamp, "Something hosed the deferred features! Can't 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,
+      "DeferredPixelSpecularGLSL::processPix - Something hosed the deferred features!" );
 
    // (a^m)^n = a^(m*n)
-   meta->addStatement( new GenOp( "   @ = pow(d_specular, ceil(@ / @)) * @;\r\n", specDecl, specPow, constSpecPow, specStrength ) );
+   meta->addStatement( new GenOp( "   @ = pow( abs(@), max((@ / AL_ConstantSpecularPower),1.0f)) * @;\r\n", 
+      specDecl, d_specular, specPow, specStrength ) );
 
-   LangElement *specMul = new GenOp( "@ * @", specCol, specular );
+   LangElement *specMul = new GenOp( "float4( @.rgb, 0 ) * @", specCol, specular );
    LangElement *final = specMul;
 
    // We we have a normal map then mask the specular 
@@ -545,14 +495,15 @@ void DeferredPixelSpecularGLSL::processPix(  Vector<ShaderComponent*> &component
       final = new GenOp( "@ * @.a", final, bumpSample );
    }
 
-   // add to color   meta->addStatement( new GenOp( "   @;\r\n", assignColor( final, Material::Add ) ) );
+   // add to color
+   meta->addStatement( new GenOp( "   @;\r\n", assignColor( final, Material::Add ) ) );
 
    output = meta;
 }
 
 ShaderFeature::Resources DeferredPixelSpecularGLSL::getResources( const MaterialFeatureData &fd )
 {
-   if( fd.features[MFT_IsTranslucent] || !fd.features[MFT_RTLighting] )
+   if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
       return Parent::getResources( fd );
 
    Resources res; 
@@ -563,7 +514,7 @@ ShaderFeature::Resources DeferredPixelSpecularGLSL::getResources( const Material
 ShaderFeature::Resources DeferredMinnaertGLSL::getResources( const MaterialFeatureData &fd )
 {
    Resources res;
-   if( !fd.features[MFT_IsTranslucent] && fd.features[MFT_RTLighting] )
+   if( !fd.features[MFT_ForwardShading] && fd.features[MFT_RTLighting] )
    {
       res.numTex = 1;
       res.numTexReg = 1;
@@ -576,7 +527,7 @@ void DeferredMinnaertGLSL::setTexData( Material::StageData &stageDat,
                                        RenderPassData &passData, 
                                        U32 &texIndex )
 {
-   if( !fd.features[MFT_IsTranslucent] && fd.features[MFT_RTLighting] )
+   if( !fd.features[MFT_ForwardShading] && fd.features[MFT_RTLighting] )
    {
       NamedTexTarget *texTarget = NamedTexTarget::find(RenderPrePassMgr::BufferName);
       if ( texTarget )
@@ -590,7 +541,7 @@ void DeferredMinnaertGLSL::setTexData( Material::StageData &stageDat,
 void DeferredMinnaertGLSL::processPixMacros( Vector<GFXShaderMacro> &macros, 
                                              const MaterialFeatureData &fd  )
 {
-   if( !fd.features[MFT_IsTranslucent] && fd.features[MFT_RTLighting] )
+   if( !fd.features[MFT_ForwardShading] && fd.features[MFT_RTLighting] )
    {
       // Pull in the uncondition method for the g buffer
       NamedTexTarget *texTarget = NamedTexTarget::find( RenderPrePassMgr::BufferName );
@@ -607,55 +558,24 @@ void DeferredMinnaertGLSL::processVert(   Vector<ShaderComponent*> &componentLis
                                           const MaterialFeatureData &fd )
 {
    // If there is no deferred information, bail on this feature
-   if( fd.features[MFT_IsTranslucent] || !fd.features[MFT_RTLighting] )
+   if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
    {
       output = NULL;
       return;
    }
 
-   // grab incoming vert position
-   Var *inVertPos = (Var*) LangElement::find( "position" );
-   AssertFatal( inVertPos, "Something went bad with ShaderGen. The vertex position should be already defined." );
-
-   // grab output for gbuffer normal
-   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-   Var *outWSEyeVec= connectComp->getElement( RT_TEXCOORD );
-   outWSEyeVec->setName( "outWSViewVec" );
-   outWSEyeVec->setType( "vec4" );
-
-   // create objToWorld variable
-   Var *objToWorld = (Var*) LangElement::find( "objTrans" );
-   if( !objToWorld )
-   {
-      objToWorld = new Var;
-      objToWorld->setType( "mat4x4" );
-      objToWorld->setName( "objTrans" );
-      objToWorld->uniform = true;
-      objToWorld->constSortPos = cspPrimitive;
-   }
-
-   // Eye Pos world
-   Var *eyePosWorld = (Var*) LangElement::find( "eyePosWorld" );
-   if( !eyePosWorld )
-   {
-      eyePosWorld = new Var;
-      eyePosWorld->setType( "vec3" );
-      eyePosWorld->setName( "eyePosWorld" );
-      eyePosWorld->uniform = true;
-      eyePosWorld->constSortPos = cspPass;
-   }
-
-   // Kick out the world-space normal
-   LangElement *statement = new GenOp( "   @ = vec4(@, @) - vec4(@, 0.0);\r\n", 
-      outWSEyeVec, objToWorld, inVertPos, eyePosWorld );
-   output = statement;
+   // Make sure we pass the world space position to the
+   // pixel shader so we can calculate a view vector.
+   MultiLine *meta = new MultiLine;
+   addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
+   output = meta;
 }
 
 void DeferredMinnaertGLSL::processPix( Vector<ShaderComponent*> &componentList, 
                                        const MaterialFeatureData &fd )
 {
    // If there is no deferred information, bail on this feature
-   if( fd.features[MFT_IsTranslucent] || !fd.features[MFT_RTLighting] )
+   if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
    {
       output = NULL;
       return;
@@ -679,25 +599,19 @@ void DeferredMinnaertGLSL::processPix( Vector<ShaderComponent*> &componentList,
    Var *uvScene = (Var*) LangElement::find( "uvScene" );
    AssertFatal(uvScene != NULL, "Unable to find UVScene, no RTLighting feature?");
 
-   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-   Var *wsViewVec = (Var*) LangElement::find( "wsPos" );
-   if( !wsViewVec )
-   {
-      wsViewVec = connectComp->getElement( RT_TEXCOORD );
-      wsViewVec->setName( "outWSViewVec" );
-      wsViewVec->setType( "vec4" );
-      wsViewVec->mapsToSampler = false;
-      wsViewVec->uniform = false;
-   }
+   MultiLine *meta = new MultiLine;
+
+   // Get the world space view vector.
+   Var *wsViewVec = getWsView( getInWsPosition( componentList ), meta );
 
    String unconditionPrePassMethod = String::ToLower(RenderPrePassMgr::BufferName) + "Uncondition";
 
-   MultiLine *meta = new MultiLine;
-   meta->addStatement( new GenOp( avar( "   vec4 normalDepth = %s(texture2D(@, @));\r\n", unconditionPrePassMethod.c_str() ), prepassBuffer, uvScene ) );
-   meta->addStatement( new GenOp( "   vec3 worldViewVec = normalize(@.xyz / @.w);\r\n", wsViewVec, wsViewVec ) );
-   meta->addStatement( new GenOp( "   float vDotN = dot(normalDepth.xyz, worldViewVec);\r\n" ) );
-   meta->addStatement( new GenOp( "   float Minnaert = pow(d_NL_Att, @) * pow(vDotN, 1.0 - @);\r\n", minnaertConstant, minnaertConstant ) );
-   meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "vec4(Minnaert, Minnaert, Minnaert, 1.0)" ), Material::Mul ) ) );
+   Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
+
+   meta->addStatement( new GenOp( avar( "   float4 normalDepth = %s(@, @);\r\n", unconditionPrePassMethod.c_str() ), prepassBuffer, uvScene ) );
+   meta->addStatement( new GenOp( "   float vDotN = dot(normalDepth.xyz, @);\r\n", wsViewVec ) );
+   meta->addStatement( new GenOp( "   float Minnaert = pow( @, @) * pow(vDotN, 1.0 - @);\r\n", d_NL_Att, minnaertConstant, minnaertConstant ) );
+   meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "float4(Minnaert, Minnaert, Minnaert, 1.0)" ), Material::Mul ) ) );
 
    output = meta;
 }
@@ -707,7 +621,7 @@ void DeferredSubSurfaceGLSL::processPix(  Vector<ShaderComponent*> &componentLis
                                           const MaterialFeatureData &fd )
 {
    // If there is no deferred information, bail on this feature
-   if( fd.features[MFT_IsTranslucent] || !fd.features[MFT_RTLighting] )
+   if( fd.features[MFT_ForwardShading] || !fd.features[MFT_RTLighting] )
    {
       output = NULL;
       return;
@@ -719,12 +633,13 @@ void DeferredSubSurfaceGLSL::processPix(  Vector<ShaderComponent*> &componentLis
    subSurfaceParams->uniform = true;
    subSurfaceParams->constSortPos = cspPotentialPrimitive;
 
-   Var *inColor = (Var*) LangElement::find( "rtShading" );
+   Var *d_lightcolor = (Var*)LangElement::find( "d_lightcolor" );
+   Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
 
    MultiLine *meta = new MultiLine;
-   meta->addStatement( new GenOp( "   float subLamb = smoothstep([email protected], 1.0, d_NL_Att) - smoothstep(0.0, 1.0, d_NL_Att);\r\n", subSurfaceParams ) );
+   meta->addStatement( new GenOp( "   float subLamb = smoothstep([email protected], 1.0, @) - smoothstep(0.0, 1.0, @);\r\n", subSurfaceParams, d_NL_Att, d_NL_Att ) );
    meta->addStatement( new GenOp( "   subLamb = max(0.0, subLamb);\r\n" ) );
-   meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "vec4(@.rgb + (subLamb * @.rgb), 1.0)", inColor, subSurfaceParams ), Material::Mul ) ) );
+   meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "float4(@ + (subLamb * @.rgb), 1.0)", d_lightcolor, subSurfaceParams ), Material::Mul ) ) );
 
    output = meta;
 }

+ 16 - 4
Engine/source/lighting/advanced/glsl/advancedLightingFeaturesGLSL.h

@@ -30,13 +30,25 @@
 class ConditionerMethodDependency;
 
 
-/// Lights the pixel by sampling from the light prepass buffer.  It will
-/// fall back to default vertex lighting functionality if  
+/// Lights the pixel by sampling from the light prepass 
+/// buffer.  It will fall back to forward lighting 
+/// functionality for non-deferred rendered surfaces.
+///
+/// Also note that this feature is only used in the
+/// forward rendering pass.  It is not used during the
+/// prepass step.
+///
 class DeferredRTLightingFeatGLSL : public RTLightingFeatGLSL
 {
    typedef RTLightingFeatGLSL Parent;
 
+protected:
+
+   /// @see DeferredRTLightingFeatHLSL::processPix()
+   U32 mLastTexIndex;
+
 public:
+
    virtual void processVert( Vector<ShaderComponent*> &componentList,
                               const MaterialFeatureData &fd );
 
@@ -57,12 +69,12 @@ public:
 
    virtual String getName()
    {
-      return "Deferred RT Lighting Feature";
+      return "Deferred RT Lighting";
    }
 };
 
 
-/// Used to write the normals during the depth/normal prepass.
+/// This is used during the 
 class DeferredBumpFeatGLSL : public BumpFeatGLSL
 {
    typedef BumpFeatGLSL Parent;

+ 152 - 68
Engine/source/lighting/advanced/glsl/gBufferConditionerGLSL.cpp

@@ -27,9 +27,10 @@
 #include "gfx/gfxStringEnumTranslate.h"
 #include "materials/materialFeatureTypes.h"
 #include "materials/materialFeatureData.h"
+#include "shaderGen/GLSL/shaderFeatureGLSL.h"
 
 
-GBufferConditionerGLSL::GBufferConditionerGLSL( const GFXFormat bufferFormat ) :
+GBufferConditionerGLSL::GBufferConditionerGLSL( const GFXFormat bufferFormat, const NormalSpace nrmSpace ) : 
       Parent( bufferFormat )
 {
    // Figure out how we should store the normal data. These are the defaults.
@@ -39,20 +40,18 @@ GBufferConditionerGLSL::GBufferConditionerGLSL( const GFXFormat bufferFormat ) :
    // Note:  We clear to a depth 1 (the w component) so
    // that the unrendered parts of the scene end up 
    // farthest to the camera.
-
+   const NormalStorage &twoCmpNrmStorageType = ( nrmSpace == WorldSpace ? Spherical : LambertAzimuthal );
    switch(bufferFormat)
    {
       case GFXFormatR8G8B8A8:
-         // TODO: Some kind of logic here. Spherical is better, but is more
-         // expensive. 
-         mNormalStorageType = Spherical;
+         mNormalStorageType = twoCmpNrmStorageType;
          mBitsPerChannel = 8;
          break;
 
       case GFXFormatR16G16B16A16F:
          // Floating point buffers don't need to encode negative values
          mCanWriteNegativeValues = true;
-         mNormalStorageType = Spherical;
+         mNormalStorageType = twoCmpNrmStorageType;
          mBitsPerChannel = 16;
          break;
 
@@ -61,7 +60,7 @@ GBufferConditionerGLSL::GBufferConditionerGLSL( const GFXFormat bufferFormat ) :
       // precision and high quality normals within a 64bit
       // buffer format.
       case GFXFormatR16G16B16A16:
-         mNormalStorageType = Spherical;
+         mNormalStorageType = twoCmpNrmStorageType;
          mBitsPerChannel = 16;
          break;
 
@@ -83,34 +82,43 @@ GBufferConditionerGLSL::~GBufferConditionerGLSL()
 void GBufferConditionerGLSL::processVert( Vector<ShaderComponent*> &componentList, 
                                           const MaterialFeatureData &fd )
 {
-   output = NULL;
+   // 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;
 
-   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->setStructName( "OUT" );
+   outNormal->setType( "float3" );
+
+   if( !fd.features[MFT_ParticleNormal] )
    {
-      // 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;
+      // 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 ) );
    }
 }
 
@@ -129,7 +137,8 @@ void GBufferConditionerGLSL::processPix(  Vector<ShaderComponent*> &componentLis
    {
       gbNormal = connectComp->getElement( RT_TEXCOORD );
       gbNormal->setName( "gbNormal" );
-      gbNormal->setType( "vec3" );
+      gbNormal->setStructName( "IN" );
+      gbNormal->setType( "float3" );
       gbNormal->mapsToSampler = false;
       gbNormal->uniform = false;
    }
@@ -143,16 +152,45 @@ void GBufferConditionerGLSL::processPix(  Vector<ShaderComponent*> &componentLis
 
 
    Var *unconditionedOut = new Var;
-   unconditionedOut->setType("vec4");
+   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( "vec4(normalize(@), @)", gbNormal, depth ) ) );
+   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;
 }
 
@@ -180,7 +218,7 @@ Var* GBufferConditionerGLSL::printMethodHeader( MethodType methodType, const Str
    {
       Var *methodVar = new Var;
       methodVar->setName(methodName);
-      methodVar->setType("vec4");
+      methodVar->setType("float4");
       DecOp *methodDecl = new DecOp(methodVar);
 
       Var *prepassSampler = new Var;
@@ -190,12 +228,12 @@ Var* GBufferConditionerGLSL::printMethodHeader( MethodType methodType, const Str
 
       Var *screenUV = new Var;
       screenUV->setName("screenUVVar");
-      screenUV->setType("vec2");
+      screenUV->setType("float2");
       DecOp *screenUVDecl = new DecOp(screenUV);
 
       Var *bufferSample = new Var;
       bufferSample->setName("bufferSample");
-      bufferSample->setType("vec4");
+      bufferSample->setType("float4");
       DecOp *bufferSampleDecl = new DecOp(bufferSample); 
 
       meta->addStatement( new GenOp( "@(@, @)\r\n", methodDecl, prepassSamplerDecl, screenUVDecl ) );
@@ -204,9 +242,18 @@ Var* GBufferConditionerGLSL::printMethodHeader( MethodType methodType, const Str
 
       meta->addStatement( new GenOp( "   // Sampler g-buffer\r\n" ) );
 
+#ifdef TORQUE_OS_XENON
+      meta->addStatement( new GenOp( "   @;\r\n", bufferSampleDecl ) );
+      meta->addStatement( new GenOp( "   asm { tfetch2D @, @, @, MagFilter = point, MinFilter = point, MipFilter = point };\r\n", bufferSample, screenUV, prepassSampler ) );
+#else
       // The gbuffer has no mipmaps, so use tex2dlod when 
-      // so that the shader compiler can optimize.
-      meta->addStatement( new GenOp( "   @ = texture2DLod(@, @, 0.0);\r\n", bufferSampleDecl, prepassSampler, screenUV ) );
+      // possible so that the shader compiler can optimize.
+      meta->addStatement( new GenOp( "   #if TORQUE_SM >= 30\r\n" ) );
+      meta->addStatement( new GenOp( "      @ = tex2Dlod(@, float4(@,0,0));\r\n", bufferSampleDecl, prepassSampler, screenUV ) );
+      meta->addStatement( new GenOp( "   #else\r\n" ) );
+      meta->addStatement( new GenOp( "      @ = tex2D(@, @);\r\n", bufferSampleDecl, prepassSampler, screenUV ) );
+      meta->addStatement( new GenOp( "   #endif\r\n\r\n" ) );
+#endif
 
       // We don't use this way of passing var's around, so this should cause a crash
       // if something uses this improperly
@@ -218,39 +265,67 @@ Var* GBufferConditionerGLSL::printMethodHeader( MethodType methodType, const Str
 
 GenOp* GBufferConditionerGLSL::_posnegEncode( GenOp *val )
 {
-   return mCanWriteNegativeValues ? val : new GenOp("0.5 * (@ + 1.0)", val);
+   if(mNormalStorageType == LambertAzimuthal)
+      return mCanWriteNegativeValues ? val : new GenOp(avar("(%f * (@ + %f))", 1.0f/(M_SQRT2_F * 2.0f), M_SQRT2_F), val);
+   else
+      return mCanWriteNegativeValues ? val : new GenOp("(0.5 * (@ + 1.0))", val);
 }
 
 GenOp* GBufferConditionerGLSL::_posnegDecode( GenOp *val )
 {
-   return mCanWriteNegativeValues ? val : new GenOp("@ * 2.0 - 1.0", val);
+   if(mNormalStorageType == LambertAzimuthal)
+      return mCanWriteNegativeValues ? val : new GenOp(avar("(@ * %f - %f)", M_SQRT2_F * 2.0f, M_SQRT2_F), val);
+   else
+      return mCanWriteNegativeValues ? val : new GenOp("(@ * 2.0 - 1.0)", val);
 }
 
 Var* GBufferConditionerGLSL::_conditionOutput( Var *unconditionedOutput, MultiLine *meta )
 {
    Var *retVar = new Var;
-   retVar->setType("vec4");
+   retVar->setType("float4");
    retVar->setName("_gbConditionedOutput");
    LangElement *outputDecl = new DecOp( retVar );
 
    switch(mNormalStorageType)
    {
       case CartesianXYZ:
-         meta->addStatement( new GenOp( "   // g-buffer conditioner: vec4(normal.xyz, depth)\r\n" ) );
-         meta->addStatement( new GenOp( "   @ = vec4(@, @.a);\r\n", outputDecl, 
+         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.xyz, depth)\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(@, @.a);\r\n", outputDecl, 
             _posnegEncode(new GenOp("@.xyz", unconditionedOutput)), unconditionedOutput ) );
          break;
 
       case CartesianXY:
-         meta->addStatement( new GenOp( "   // g-buffer conditioner: vec4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
-         meta->addStatement( new GenOp( "   @ = vec4(@, @.a);", outputDecl, 
-            _posnegEncode(new GenOp("vec3(@.xy, sign(@.z))", unconditionedOutput, unconditionedOutput)), unconditionedOutput ) );
+         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(@, @.a);", outputDecl, 
+            _posnegEncode(new GenOp("float3(@.xy, sign(@.z))", unconditionedOutput, unconditionedOutput)), unconditionedOutput ) );
          break;
 
       case Spherical:
-         meta->addStatement( new GenOp( "   // g-buffer conditioner: vec4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
-         meta->addStatement( new GenOp( "   @ = vec4(@, 0.0, @.a);\r\n", outputDecl, 
-            _posnegEncode(new GenOp("vec2(atan2(@.y, @.x) / 3.14159265358979323846f, @.z)", unconditionedOutput, unconditionedOutput, unconditionedOutput ) ), 
+         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(@, 0.0, @.a);\r\n", outputDecl, 
+            _posnegEncode(new GenOp("float2(atan2(@.y, @.x) / 3.14159265358979323846f, @.z)", unconditionedOutput, unconditionedOutput, unconditionedOutput ) ), 
+            unconditionedOutput ) );
+
+         // HACK: This fixes the noise present when using a floating point
+         // gbuffer on Geforce cards and the "flat areas unlit" issues.
+         //
+         // We need work around atan2() above to fix this issue correctly
+         // without the extra overhead of this test.
+         //
+         meta->addStatement( new GenOp( "   if ( abs( dot( @.xyz, float3( 0.0, 0.0, 1.0 ) ) ) > 0.999f ) @ = float4( 0, 1 * sign( @.z ), 0, @.a );\r\n", 
+            unconditionedOutput, retVar, unconditionedOutput, unconditionedOutput ) );
+         break;
+
+      case LambertAzimuthal:
+         //http://en.wikipedia.org/wiki/Lambert_azimuthal_equal-area_projection
+         //
+         // Note we're casting to half to use partial precision
+         // sqrt which is much faster on older Geforces while
+         // still being acceptable for normals.
+         //
+         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(@, 0.0, @.a);\r\n", outputDecl, 
+            _posnegEncode(new GenOp("sqrt(half(2.0/(1.0 - @.y))) * half2(@.xz)", unconditionedOutput, unconditionedOutput)), 
             unconditionedOutput ) );
          break;
    }
@@ -259,11 +334,10 @@ Var* GBufferConditionerGLSL::_conditionOutput( Var *unconditionedOutput, MultiLi
    if(mNormalStorageType != CartesianXYZ)
    {
       const U64 maxValPerChannel = 1 << mBitsPerChannel;
-      const U64 extraVal = (maxValPerChannel * maxValPerChannel - 1) - (maxValPerChannel - 1) * 2;
       meta->addStatement( new GenOp( "   \r\n   // Encode depth into hi/lo\r\n" ) );
-      meta->addStatement( new GenOp( avar( "   vec3 _tempDepth = fract(@.a * vec3(1.0, %llu.0, %llu.0));\r\n", maxValPerChannel - 1, extraVal ), 
+      meta->addStatement( new GenOp( avar( "   float2 _tempDepth = frac(@.a * float2(1.0, %llu.0));\r\n", maxValPerChannel - 1 ), 
          unconditionedOutput ) );
-      meta->addStatement( new GenOp( avar( "   @.zw = _tempDepth.xy - _tempDepth.yz * vec2(1.0/%llu.0, 1.0/%llu.0);\r\n\r\n", maxValPerChannel - 1, maxValPerChannel - 1  ), 
+      meta->addStatement( new GenOp( avar( "   @.zw = _tempDepth.xy - _tempDepth.yy * float2(1.0/%llu.0, 0.0);\r\n\r\n", maxValPerChannel - 1 ), 
          retVar ) );
    }
 
@@ -274,33 +348,43 @@ Var* GBufferConditionerGLSL::_conditionOutput( Var *unconditionedOutput, MultiLi
 Var* GBufferConditionerGLSL::_unconditionInput( Var *conditionedInput, MultiLine *meta )
 {
    Var *retVar = new Var;
-   retVar->setType("vec4");
+   retVar->setType("float4");
    retVar->setName("_gbUnconditionedInput");
    LangElement *outputDecl = new DecOp( retVar );
 
    switch(mNormalStorageType)
    {
       case CartesianXYZ:
-         meta->addStatement( new GenOp( "   // g-buffer unconditioner: vec4(normal.xyz, depth)\r\n" ) );
-         meta->addStatement( new GenOp( "   @ = vec4(@, @.a);\r\n", outputDecl, 
+         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.xyz, depth)\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(@, @.a);\r\n", outputDecl, 
             _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
          break;
 
       case CartesianXY:
-         meta->addStatement( new GenOp( "   // g-buffer unconditioner: vec4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
-         meta->addStatement( new GenOp( "   @ = vec4(@, @.a);\r\n", outputDecl, 
+         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(@, @.a);\r\n", outputDecl, 
             _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
          meta->addStatement( new GenOp( "   @.z *= sqrt(1.0 - dot(@.xy, @.xy));\r\n", retVar, retVar, retVar ) );
          break;
 
       case Spherical:
-         meta->addStatement( new GenOp( "   // g-buffer unconditioner: vec4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
-         meta->addStatement( new GenOp( "   vec2 spGPUAngles = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
-         meta->addStatement( new GenOp( "   vec2 sincosTheta;\r\n" ) );
-         meta->addStatement( new GenOp( "   sincosTheta.x = sin(spGPUAngles.x * 3.14159265358979323846);\r\n" ) );
-         meta->addStatement( new GenOp( "   sincosTheta.y = cos(spGPUAngles.x * 3.14159265358979323846);\r\n" ) );
-         meta->addStatement( new GenOp( "   vec2 sincosPhi = vec2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);\r\n" ) );
-         meta->addStatement( new GenOp( "   @ = vec4(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y, @.a);\r\n", outputDecl, conditionedInput ) );
+         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
+         meta->addStatement( new GenOp( "   float2 spGPUAngles = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
+         meta->addStatement( new GenOp( "   float2 sincosTheta;\r\n" ) );
+         meta->addStatement( new GenOp( "   sincos(spGPUAngles.x * 3.14159265358979323846f, sincosTheta.x, sincosTheta.y);\r\n" ) );
+         meta->addStatement( new GenOp( "   float2 sincosPhi = float2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y, @.a);\r\n", outputDecl, conditionedInput ) );
+         break;
+
+      case LambertAzimuthal:
+         // Note we're casting to half to use partial precision
+         // sqrt which is much faster on older Geforces while
+         // still being acceptable for normals.
+         //      
+         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) );
+         meta->addStatement( new GenOp( "   float2 _inpXY = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
+         meta->addStatement( new GenOp( "   float _xySQ = dot(_inpXY, _inpXY);\r\n" ) );
+         meta->addStatement( new GenOp( "   @ = float4( sqrt(half(1.0 - (_xySQ / 4.0))) * _inpXY, -1.0 + (_xySQ / 2.0), @.a).xzyw;\r\n", outputDecl, conditionedInput ) );
          break;
    }
 
@@ -309,7 +393,7 @@ Var* GBufferConditionerGLSL::_unconditionInput( Var *conditionedInput, MultiLine
    {
       const U64 maxValPerChannel = 1 << mBitsPerChannel;
       meta->addStatement( new GenOp( "   \r\n   // Decode depth\r\n" ) );
-      meta->addStatement( new GenOp( avar( "   @.w = dot( @.zw, vec2(1.0, 1.0/%llu.0));\r\n", maxValPerChannel - 1 ), 
+      meta->addStatement( new GenOp( avar( "   @.w = dot( @.zw, float2(1.0, 1.0/%llu.0));\r\n", maxValPerChannel - 1 ), 
          retVar, conditionedInput ) );
    }
 

+ 8 - 1
Engine/source/lighting/advanced/glsl/gBufferConditionerGLSL.h

@@ -42,6 +42,13 @@ public:
       CartesianXYZ,
       CartesianXY,
       Spherical,
+      LambertAzimuthal,
+   };
+
+   enum NormalSpace
+   {
+      WorldSpace,
+      ViewSpace,
    };
    
 protected:
@@ -52,7 +59,7 @@ protected:
 
 public:
 
-   GBufferConditionerGLSL( const GFXFormat bufferFormat );
+   GBufferConditionerGLSL( const GFXFormat bufferFormat, const NormalSpace nrmSpace );
    virtual ~GBufferConditionerGLSL();
 
 

+ 87 - 72
Engine/source/shaderGen/GLSL/bumpGLSL.cpp

@@ -64,14 +64,14 @@ void BumpFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList,
 	output = meta;
 
    // Get the texture coord.
-   Var *texCoord = getInTexCoord( "out_texCoord", "vec2", true, componentList );
+   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
@@ -79,52 +79,49 @@ void BumpFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList,
 		
       Var *atlasedTex = new Var;
       atlasedTex->setName("atlasedBumpCoord");
-      atlasedTex->setType("vec2");
+      atlasedTex->setType( "vec2" );
       LangElement *atDecl = new DecOp(atlasedTex);
 		
       // Parameters of the texture atlas
       Var *atParams  = new Var;
-      atParams->setType("vec4");
+      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("vec4");
+      tileParams->setType( "float4" );
       tileParams->setName("bumpAtlasTileParams");
       tileParams->uniform = true;
       tileParams->constSortPos = cspPotentialPrimitive;
 		
 		const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
-		// getPixelShaderVersion() on Mac currently returns 2.0,
-		// or 3.0 if Advanced Lighting is enabled
 		if(is_sm3)
       {
          // Figure out the mip level
-         meta->addStatement(new GenOp("   vec2 _dx_bump = dFdx(@ * @.z);\r\n", texCoord, atParams));
-         meta->addStatement(new GenOp("   vec2 _dy_bump = dFdy(@ * @.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));
+         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("   vec2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams));
+         meta->addStatement( new GenOp( "   float2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams ) );
       }
       else
       {
-         meta->addStatement(new GenOp("   vec2 mipSz = float2(1.0, 1.0);\r\n"));
+         meta->addStatement(new GenOp("   float2 mipSz = float2(1.0, 1.0);\r\n"));
       }
 		
 		// Tiling mode
-      // TODO: Select wrap or clamp somehow
       if( true ) // Wrap
-         meta->addStatement(new GenOp("   @ = fract(@);\r\n", atDecl, texCoord));
+         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", 
+      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
@@ -132,19 +129,19 @@ void BumpFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList,
 		
       if(is_sm3)
 		{
-			texOp = new GenOp( "texture2DLod(@, vec4(@, 0.0, mipLod_bump)", bumpMap, texCoord );
+         texOp = new GenOp( "tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord );
 		}
 		else
 		{
-			texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
+         texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
 		}
 	}
 	else
 	{
-		texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
+      texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
 	}
 		
-   Var *bumpNorm = new Var( "bumpNormal", "vec4" );
+   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
@@ -160,11 +157,11 @@ void BumpFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList,
       bumpMap->constNum = Var::getTexUnitNum();
 		
       texCoord = getInTexCoord( "detCoord", "vec2", true, componentList );
-      texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
+      texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
 		
       Var *detailBump = new Var;
       detailBump->setName( "detailBump" );
-      detailBump->setType( "vec4" );
+      detailBump->setType( "float4" );
       meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
 		
       Var *detailBumpScale = new Var;
@@ -175,13 +172,11 @@ void BumpFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList,
       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( vec3( @.xyz * @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );
-
+   meta->addStatement( new GenOp( "   @ = normalize( tMul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );
 }
 
 ShaderFeature::Resources BumpFeatGLSL::getResources( const MaterialFeatureData &fd )
@@ -227,20 +222,26 @@ void BumpFeatGLSL::setTexData(   Material::StageData &stageDat,
    if ( fd.features[MFT_NormalMap] )
    {
       passData.mTexType[ texIndex ] = Material::Bump;
+      passData.mSamplerNames[ texIndex ] = "bumpMap";
       passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
    }
 	
-	
    if ( fd.features[ MFT_DetailNormalMap ] )
    {
       passData.mTexType[ texIndex ] = Material::DetailBump;
+      passData.mSamplerNames[ texIndex ] = "detailBumpMap";
       passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
    }
 }
 
 
-//
-Var* ParallaxFeatGLSL::_getUniformVar( const char *name, const char *type )
+ParallaxFeatGLSL::ParallaxFeatGLSL()
+   : mIncludeDep( "shaders/common/gl/torque.glsl" )
+{
+   addDependency( &mIncludeDep );
+}
+
+Var* ParallaxFeatGLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
 {
    Var *theVar = (Var*)LangElement::find( name );
    if ( !theVar )
@@ -249,7 +250,7 @@ Var* ParallaxFeatGLSL::_getUniformVar( const char *name, const char *type )
       theVar->setType( type );
       theVar->setName( name );
       theVar->uniform = true;
-      theVar->constSortPos = cspPass;
+      theVar->constSortPos = csp;
    }
 	
    return theVar;
@@ -259,13 +260,13 @@ void ParallaxFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
 											  const MaterialFeatureData &fd )
 {
    AssertFatal( GFX->getPixelShaderVersion() >= 2.0, 
-					"ParallaxFeatGLSL::processVert - We don't support SM 1.x!" );
+      "ParallaxFeatGLSL::processVert - We don't support SM 1.x!" );
 	
    MultiLine *meta = new MultiLine;
 	
    // Add the texture coords.
    getOutTexCoord(   "texCoord", 
-						"vec2", 
+                     "vec2", 
 						true, 
 						fd.features[MFT_TexAnim], 
 						meta, 
@@ -276,18 +277,36 @@ void ParallaxFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
    if ( !inPos )
       inPos = (Var*)LangElement::find( "position" );
 	
-   // Get the object space eye position and the world
-   // to tangent transform.
-   Var *eyePos = _getUniformVar( "eyePos", "vec3" );
+   // 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 );
 	
-   // send transform to pixel shader
+   // Now send the negative view vector in tangent space to the pixel shader.
    ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-   Var *outViewTS = connectComp->getElement( RT_TEXCOORD, 1 );
-   outViewTS->setName( "outViewTS" );
-   outViewTS->setType( "vec3" );
-   meta->addStatement( new GenOp( "   @ = ( @ - @.xyz ) * transpose( @ );\r\n", 
-											outViewTS, inPos, eyePos, objToTangentSpace ) );
+   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;
 }
@@ -296,7 +315,7 @@ void ParallaxFeatGLSL::processPix(  Vector<ShaderComponent*> &componentList,
 											 const MaterialFeatureData &fd )
 {
    AssertFatal( GFX->getPixelShaderVersion() >= 2.0, 
-					"ParallaxFeatGLSL::processPix - We don't support SM 1.x!" );
+      "ParallaxFeatGLSL::processPix - We don't support SM 1.x!" );
 	
    MultiLine *meta = new MultiLine;
 	
@@ -310,38 +329,28 @@ void ParallaxFeatGLSL::processPix(  Vector<ShaderComponent*> &componentList,
    Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
    if ( !negViewTS )
    {
-      Var *inViewTS = (Var*)LangElement::find( "outViewTS" );
-      if ( !inViewTS )
+      Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
+      if ( !inNegViewTS )
       {
-         inViewTS = connectComp->getElement( RT_TEXCOORD, 1 );
-         inViewTS->setName( "outViewTS" );
-         inViewTS->setType( "vec3" );
+         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 ), inViewTS ) );
+      meta->addStatement( new GenOp( "   @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
    }
 	
    // Get the rest of our inputs.
-   Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float" );
+   Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float", cspPotentialPrimitive );
    Var *normalMap = getNormalMapTex();
 	
-   // Do 3 parallax samples to get acceptable
-   // quality without too much overhead.
-   Var *pdepth = findOrCreateLocal( "pdepth", "float", meta );
-   Var *poffset = findOrCreateLocal( "poffset", "vec2", meta );
-   meta->addStatement( new GenOp( "   @ = texture2D( @, @.xy ).a;\r\n", pdepth, normalMap, texCoord ) );
-   meta->addStatement( new GenOp( "   @ = @.xy * ( @ * @ );\r\n", poffset, negViewTS, pdepth, parallaxInfo ) );
-	
-   meta->addStatement( new GenOp( "   @ = ( @ + texture2D( @, @.xy + @ ).a ) * 0.5;\r\n", pdepth, pdepth, normalMap, texCoord, poffset ) );
-   meta->addStatement( new GenOp( "   @ = @.xy * ( @ * @ );\r\n", poffset, negViewTS, pdepth, parallaxInfo ) );
-	
-   meta->addStatement( new GenOp( "   @ = ( @ + texture2D( @, @.xy + @ ).a ) * 0.5;\r\n", pdepth, pdepth, normalMap, texCoord, poffset ) );
-   meta->addStatement( new GenOp( "   @ = @.xy * ( @ * @ );\r\n", poffset, negViewTS, pdepth, parallaxInfo ) );
-	
-   meta->addStatement( new GenOp( "   @.xy += @;\r\n", texCoord, poffset ) );
+   // Call the library function to do the rest.
+   meta->addStatement( new GenOp( "   @.xy += parallaxOffset( @, @.xy, @, @ );\r\n", 
+      texCoord, normalMap, texCoord, negViewTS, parallaxInfo ) );
    
-   // TODO: Fix second UV.
+   // TODO: Fix second UV maybe?
 	
    output = meta;
 }
@@ -349,7 +358,7 @@ void ParallaxFeatGLSL::processPix(  Vector<ShaderComponent*> &componentList,
 ShaderFeature::Resources ParallaxFeatGLSL::getResources( const MaterialFeatureData &fd )
 {
    AssertFatal( GFX->getPixelShaderVersion() >= 2.0, 
-					"ParallaxFeatGLSL::getResources - We don't support SM 1.x!" );
+      "ParallaxFeatGLSL::getResources - We don't support SM 1.x!" );
 	
    Resources res;
 	
@@ -370,7 +379,7 @@ void ParallaxFeatGLSL::setTexData(  Material::StageData &stageDat,
 											 U32 &texIndex )
 {
    AssertFatal( GFX->getPixelShaderVersion() >= 2.0, 
-					"ParallaxFeatGLSL::setTexData - We don't support SM 1.x!" );
+      "ParallaxFeatGLSL::setTexData - We don't support SM 1.x!" );
 	
    GFXTextureObject *tex = stageDat.getTex( MFT_NormalMap );
    if ( tex )
@@ -381,7 +390,6 @@ void ParallaxFeatGLSL::setTexData(  Material::StageData &stageDat,
 }
 
 
-//
 void NormalsOutFeatGLSL::processVert(  Vector<ShaderComponent*> &componentList, 
 												 const MaterialFeatureData &fd )
 {
@@ -397,6 +405,7 @@ void NormalsOutFeatGLSL::processVert(  Vector<ShaderComponent*> &componentList,
 	
    Var *outNormal = connectComp->getElement( RT_TEXCOORD );
    outNormal->setName( "wsNormal" );
+   outNormal->setStructName( "OUT" );
    outNormal->setType( "vec3" );
    outNormal->mapsToSampler = false;
 	
@@ -406,13 +415,13 @@ void NormalsOutFeatGLSL::processVert(  Vector<ShaderComponent*> &componentList,
    {
       // Transform the normal to world space.
       Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
-      meta->addStatement( new GenOp( "   @ = @ * normalize( @ );\r\n", outNormal, objTrans, inNormal ) );
+      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( "   @ = vec3( 0.0, 0.0, 1.0 );\r\n", outNormal ) );
+      meta->addStatement( new GenOp( "   @ = float3( 0.0, 0.0, 1.0 );\r\n", outNormal ) );
    }
 }
 
@@ -428,20 +437,26 @@ void NormalsOutFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList,
       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.
       //
-      meta->addStatement( new GenOp( "   @ = normalize( @ );\r\n", wsNormal, wsNormal ) );      
+      // 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 )
-      normalOut = new GenOp( "vec4( ( -@ + 1 ) * 0.5, @.a )", wsNormal, outColor );
+   if ( outColor && !fd.features[MFT_AlphaTest] )
+      normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, @.a )", wsNormal, outColor );
    else
-      normalOut = new GenOp( "vec4( ( -@ + 1 ) * 0.5, 1 )", wsNormal );
+      normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, 1 )", wsNormal );
 	
    meta->addStatement( new GenOp( "   @;\r\n", 
 											assignColor( normalOut, Material::None ) ) );

+ 10 - 3
Engine/source/shaderGen/GLSL/bumpGLSL.h

@@ -26,6 +26,9 @@
 #ifndef _SHADERGEN_GLSL_SHADERFEATUREGLSL_H_
 #include "shaderGen/GLSL/shaderFeatureGLSL.h"
 #endif
+#ifndef _LANG_ELEMENT_H_
+#include "shaderGen/langElement.h"
+#endif
 
 struct RenderPassData;
 class MultiLine;
@@ -50,7 +53,6 @@ public:
                             const MaterialFeatureData &fd,
                             RenderPassData &passData,
                             U32 &texIndex );
-
    virtual String getName() { return "Bumpmap"; }
 };
 
@@ -62,10 +64,16 @@ class ParallaxFeatGLSL : public ShaderFeatureGLSL
 {
 protected:
 
-   static Var* _getUniformVar( const char *name, const char *type );
+   static Var* _getUniformVar(   const char *name, 
+                                 const char *type, 
+                                 ConstantSortPosition csp );
+
+   ShaderIncludeDependency mIncludeDep;
 
 public:
 
+   ParallaxFeatGLSL();
+
    // ShaderFeatureGLSL
    virtual void processVert(  Vector<ShaderComponent*> &componentList,
                               const MaterialFeatureData &fd );
@@ -80,7 +88,6 @@ public:
 };
 
 
-
 /// This feature is used to render normals to the 
 /// diffuse target for imposter rendering.
 class NormalsOutFeatGLSL : public ShaderFeatureGLSL

+ 36 - 18
Engine/source/shaderGen/GLSL/depthGLSL.cpp

@@ -24,36 +24,36 @@
 #include "shaderGen/GLSL/depthGLSL.h"
 
 #include "materials/materialFeatureTypes.h"
+#include "materials/materialFeatureData.h"
 
 
 void EyeSpaceDepthOutGLSL::processVert(   Vector<ShaderComponent*> &componentList, 
                                           const MaterialFeatureData &fd )
 {
-	
-	MultiLine *meta = new MultiLine;
+   MultiLine *meta = new MultiLine;
    output = meta;
 
-	// grab output
+   // grab output
    ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
    Var *outWSEyeVec = connectComp->getElement( RT_TEXCOORD );
-   outWSEyeVec->setName( "outWSEyeVec" );
-	
-	
-   // grab incoming vert position
-   Var *wsPosition = new Var( "depthPos", "vec3" );
+   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("vec3");
+      eyePos->setType("float3");
       eyePos->setName("eyePosWorld");
       eyePos->uniform = true;
       eyePos->constSortPos = cspPass;
    }
 
-meta->addStatement( new GenOp( "   @ = vec4( @.xyz - @, 1 );\r\n", outWSEyeVec, wsPosition, eyePos ) );
+   meta->addStatement( new GenOp( "   @ = float4( @.xyz - @, 1 );\r\n", outWSEyeVec, wsPosition, eyePos ) );
 }
 
 void EyeSpaceDepthOutGLSL::processPix( Vector<ShaderComponent*> &componentList, 
@@ -64,14 +64,15 @@ void EyeSpaceDepthOutGLSL::processPix( Vector<ShaderComponent*> &componentList,
    // grab connector position
    ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
    Var *wsEyeVec = connectComp->getElement( RT_TEXCOORD );
-   wsEyeVec->setName( "outWSEyeVec" );
-   wsEyeVec->setType( "vec4" );
+   wsEyeVec->setName( "wsEyeVec" );
+   wsEyeVec->setStructName( "IN" );
+   wsEyeVec->setType( "float4" );
    wsEyeVec->mapsToSampler = false;
    wsEyeVec->uniform = false;
 
    // get shader constants
    Var *vEye = new Var;
-   vEye->setType("vec3");
+   vEye->setType("float3");
    vEye->setName("vEye");
    vEye->uniform = true;
    vEye->constSortPos = cspPass;
@@ -83,12 +84,27 @@ void EyeSpaceDepthOutGLSL::processPix( Vector<ShaderComponent*> &componentList,
 
    LangElement *depthOutDecl = new DecOp( depthOut );
 
+   meta->addStatement( new GenOp( "#ifndef CUBE_SHADOW_MAP\r\n" ) );
    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( "vec4(@)", depthOut ), Material::None ) ) );
+      meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "float4(float3(@),1)", depthOut ), Material::None ) ) );
    
    output = meta;
 }
@@ -111,11 +127,12 @@ void DepthOutGLSL::processVert(  Vector<ShaderComponent*> &componentList,
    ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
 
    // Grab the output vert.
-   Var *outPosition = (Var*)LangElement::find( "gl_Position" );
+   Var *outPosition = (Var*)LangElement::find( "gl_Position" ); //hpos
 
    // Grab our output depth.
    Var *outDepth = connectComp->getElement( RT_TEXCOORD );
-   outDepth->setName( "outDepth" );
+   outDepth->setName( "depth" );
+   outDepth->setStructName( "OUT" );
    outDepth->setType( "float" );
 
    output = new GenOp( "   @ = @.z / @.w;\r\n", outDepth, outPosition, outPosition );
@@ -128,7 +145,8 @@ void DepthOutGLSL::processPix(   Vector<ShaderComponent*> &componentList,
 
    // grab connector position
    Var *depthVar = connectComp->getElement( RT_TEXCOORD );
-   depthVar->setName( "outDepth" );
+   depthVar->setName( "depth" );
+   depthVar->setStructName( "IN" );
    depthVar->setType( "float" );
    depthVar->mapsToSampler = false;
    depthVar->uniform = false;
@@ -140,7 +158,7 @@ void DepthOutGLSL::processPix(   Vector<ShaderComponent*> &componentList,
    depthOut->setName(getOutputVarName());
    */
 
-   LangElement *depthOut = new GenOp( "vec4( @, @ * @, 0, 1 )", depthVar, depthVar, depthVar );
+   LangElement *depthOut = new GenOp( "float4( @, 0, 0, 1 )", depthVar );
 
    output = new GenOp( "   @;\r\n", assignColor( depthOut, Material::None ) );
 }

+ 1 - 1
Engine/source/shaderGen/GLSL/depthGLSL.h

@@ -54,7 +54,7 @@ public:
    virtual Resources getResources( const MaterialFeatureData &fd );
    virtual String getName() { return "Depth (Out)"; }
    virtual Material::BlendOp getBlendOp() { return Material::None; }
-   virtual const char* getOutputVarName() const { return "outDepth"; }
+   virtual const char* getOutputVarName() const { return "IN_depth"; }
 };
 
 #endif // _DEPTH_GLSL_H_

+ 13 - 9
Engine/source/shaderGen/GLSL/paraboloidGLSL.cpp

@@ -65,7 +65,7 @@ void ParaboloidVertTransformGLSL::processVert(  Vector<ShaderComponent*> &compon
    // http://www.gamedev.net/reference/articles/article2308.asp
 
    // Swizzle z and y post-transform
-   meta->addStatement( new GenOp( "   @ = vec4(@ * vec4(@.xyz,1)).xzyw;\r\n", outPosition, worldViewOnly, inPosition ) );
+   meta->addStatement( new GenOp( "   @ = tMul(@, float4(@.xyz,1)).xzyw;\r\n", outPosition, worldViewOnly, inPosition ) );
    meta->addStatement( new GenOp( "   float L = length(@.xyz);\r\n", outPosition ) ); 
 
    if ( isSinglePass )
@@ -73,7 +73,8 @@ void ParaboloidVertTransformGLSL::processVert(  Vector<ShaderComponent*> &compon
       // Flip the z in the back case
       Var *outIsBack = connectComp->getElement( RT_TEXCOORD );
       outIsBack->setType( "float" );
-      outIsBack->setName( "outIsBack" );
+      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 ) ); 
@@ -94,15 +95,16 @@ void ParaboloidVertTransformGLSL::processVert(  Vector<ShaderComponent*> &compon
    // TODO: If we change other shadow shaders to write out
    // linear depth, than fix this as well!
    //
-   // (L - 1.0)/(lightParams.x - 1.0);
+   // (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( "vec2" );
-   outPosXY->setName( "outPosXY" );
+   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.
@@ -136,16 +138,18 @@ void ParaboloidVertTransformGLSL::processPix(   Vector<ShaderComponent*> &compon
    {
       // Cull things on the back side of the map.
       Var *isBack = connectComp->getElement( RT_TEXCOORD );
-      isBack->setName( "outIsBack" );
+      isBack->setName( "isBack" );
+      isBack->setStructName( "IN" );
       isBack->setType( "float" );
       meta->addStatement( new GenOp( "   if ( ( abs( @ ) - 0.999 ) < 0 ) discard;\r\n", isBack ) );
    }
 
    // Cull pixels outside of the valid paraboloid.
    Var *posXY = connectComp->getElement( RT_TEXCOORD );
-   posXY->setName( "outPosXY" );
-   posXY->setType( "vec2" );
-   meta->addStatement( new GenOp( "   if ( ( 1.0 - length( @ ) ) < 0 ) discard;\r\n", posXY ) );
+   posXY->setName( "posXY" );
+   posXY->setStructName( "IN" );
+   posXY->setType( "float2" );
+   meta->addStatement( new GenOp( "   clip( 1.0 - abs(@.x) );\r\n", posXY ) );
 
    output = meta;
 }

+ 31 - 86
Engine/source/shaderGen/GLSL/pixSpecularGLSL.cpp

@@ -38,98 +38,35 @@ PixelSpecularGLSL::PixelSpecularGLSL()
 void PixelSpecularGLSL::processVert(   Vector<ShaderComponent*> &componentList, 
                                        const MaterialFeatureData &fd )
 {
-   /*
    AssertFatal( fd.features[MFT_RTLighting], 
       "PixelSpecularHLSL requires RTLighting to be enabled!" );
 
-   MultiLine *meta = new MultiLine;
-
-   // Get the eye world position.
-   Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
-   if( !eyePos )
-   {
-      eyePos = new Var;
-      eyePos->setType( "float3" );
-      eyePos->setName( "eyePosWorld" );
-      eyePos->uniform = true;
-      eyePos->constSortPos = cspPass;
-   }
-
-   // Grab a register for passing the 
-   // world space view vector.
-   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-   Var *wsView = connectComp->getElement( RT_TEXCOORD );
-   wsView->setName( "wsView" );
-   wsView->setStructName( "OUT" );
-   wsView->setType( "float3" );
-
-   // Get the input position.
-   Var *position = (Var*)LangElement::find( "inPosition" );
-   if ( !position )
-      position = (Var*)LangElement::find( "position" );
-   
-   // Get the object to world transform.
-   Var *objTrans = (Var*) LangElement::find( "objTrans" );
-   if ( !objTrans )
-   {
-      objTrans = new Var;
-      objTrans->setType( "float4x4" );
-      objTrans->setName( "objTrans" );
-      objTrans->uniform = true;
-      objTrans->constSortPos = cspPrimitive;      
-   }
-
-   meta->addStatement( new GenOp( "   @ = @ - mul( @, float4( @.xyz,1 ) ).xyz;\r\n", 
-      wsView, eyePos, objTrans, position ) );
-
-   output = meta;
-   */
+   // Nothing to do here... MFT_RTLighting should have
+   // taken care of passing everything to the pixel shader.
 }
 
 void PixelSpecularGLSL::processPix( Vector<ShaderComponent*> &componentList, 
                                     const MaterialFeatureData &fd )
-{
-   /*
+{   
    AssertFatal( fd.features[MFT_RTLighting], 
       "PixelSpecularHLSL requires RTLighting to be enabled!" );
 
-   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
+  // RTLighting should have spit out the 4 specular
+   // powers for the 4 potential lights on this pass.
+   // 
+   // This can sometimes be NULL if RTLighting skips out
+   // on us for lightmaps or missing normals.
+   Var *specular = (Var*)LangElement::find( "specular" );
+   if ( !specular )
+      return;
 
    MultiLine *meta = new MultiLine;
 
-   // Get the normal and light vectors from which the
-   // RTLighting feature should have already setup.
-   Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
-   Var *inLightVec = (Var*)LangElement::find( "inLightVec" );
-
-   // Grab the world space position to eye vector.
-   Var *wsView = connectComp->getElement( RT_TEXCOORD );
-   wsView->setName( "wsView" );
-   wsView->setStructName( "IN" );
-   wsView->setType( "float3" );
-
-   // Get the specular power and color.
-   Var *specPow = new Var( "specularPower", "float" );
-   specPow->uniform = true;
-   specPow->constSortPos = cspPass;
-   Var *specCol = (Var*)LangElement::find("specularColor");
-   if(specCol == NULL)
-   {
-      specCol = new Var( "specularColor", "vec4" );
-      specCol->uniform = true;
-      specCol->constSortPos = cspPass;
-   }
-
-   // Calcuate the specular factor.
-   Var *specular = new Var( "specular", "float" );
-   meta->addStatement( new GenOp( "   @ = calcSpecular( -@, normalize( @ ), normalize( @ ), @ );\r\n", 
-      new DecOp( specular ), inLightVec, wsNormal, wsView, specPow ) );
-
-   LangElement *specMul = new GenOp( "float4(@.rgb,0) * @", specCol, specular );
+   LangElement *specMul = new GenOp( "@", specular );
    LangElement *final = specMul;
    
    // mask out with lightmap if present
-   if( fd.features[MFT_LightMap] )
+   if ( fd.features[MFT_LightMap] )
    {
       LangElement *lmColor = NULL;
       
@@ -141,37 +78,44 @@ void PixelSpecularGLSL::processPix( Vector<ShaderComponent*> &componentList,
          LangElement * lightMap = LangElement::find( "lightMap" );
          LangElement * lmCoord = LangElement::find( "texCoord2" );
 
-         lmColor = new GenOp( "tex2D(@, @)", lightMap, lmCoord );
+         lmColor = new GenOp( "texture(@, @)", lightMap, lmCoord );
       }
    
-      final = new GenOp( "@ * float4(@.rgb,0)", specMul, lmColor );
+      final = new GenOp( "@ * vec4(@.rgb,0)", specMul, lmColor );
    }
 
-   // We we have a normal map then mask the specular 
-   if ( !fd.features[MFT_SpecularMap] && fd.features[MFT_NormalMap] )
+   // If we have a normal map then mask the specular 
+   if ( fd.features[MFT_SpecularMap] )
+   {
+      Var *specularColor = (Var*)LangElement::find( "specularColor" );
+      if (specularColor)
+         final = new GenOp( "@ * @", final, specularColor );
+   }
+   else if ( fd.features[MFT_NormalMap] && !fd.features[MFT_IsDXTnm] )
    {
       Var *bumpColor = (Var*)LangElement::find( "bumpNormal" );
       final = new GenOp( "@ * @.a", final, bumpColor );
    }
 
-   // Add the specular to the final color.
-   meta->addStatement( new GenOp( "   @;\r\n", assignColor( final, Material::Add ) ) );
+   // Add the specular to the final color.   
+   // search for color var
+   Var *color = (Var*)LangElement::find( "col" );   
+   meta->addStatement( new GenOp( "   @.rgb += ( @ ).rgb;\r\n", color, final ) );
 
    output = meta;
-   */
 }
 
 ShaderFeature::Resources PixelSpecularGLSL::getResources( const MaterialFeatureData &fd )
 {
    Resources res;
-   res.numTexReg = 1;
    return res;
 }
 
+
 void SpecularMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
 {
    // Get the texture coord.
-   Var *texCoord = getInTexCoord( "out_texCoord", "vec2", true, componentList );
+   Var *texCoord = getInTexCoord( "texCoord", "vec2", true, componentList );
 
    // create texture var
    Var *specularMap = new Var;
@@ -180,7 +124,7 @@ void SpecularMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const
    specularMap->uniform = true;
    specularMap->sampler = true;
    specularMap->constNum = Var::getTexUnitNum();
-   LangElement *texOp = new GenOp( "texture2D(@, @)", specularMap, texCoord );
+   LangElement *texOp = new GenOp( "texture(@, @)", specularMap, texCoord );
 
    Var *specularColor = new Var( "specularColor", "vec4" );
 
@@ -203,6 +147,7 @@ void SpecularMapGLSL::setTexData( Material::StageData &stageDat,
    if ( tex )
    {
       passData.mTexType[ texIndex ] = Material::Standard;
+      passData.mSamplerNames[ texIndex ] = "specularMap";
       passData.mTexSlot[ texIndex++ ].texObject = tex;
    }
 }

+ 1 - 3
Engine/source/shaderGen/GLSL/pixSpecularGLSL.h

@@ -53,7 +53,6 @@ public:
    }
 };
 
-
 /// A texture source for the PixSpecular feature
 class SpecularMapGLSL : public ShaderFeatureGLSL
 {
@@ -75,5 +74,4 @@ public:
    }
 };
 
-
-#endif // _PIXSPECULAR_GLSL_H_
+#endif // _PIXSPECULAR_HLSL_H_

+ 249 - 26
Engine/source/shaderGen/GLSL/shaderCompGLSL.cpp

@@ -38,7 +38,7 @@ Var * AppVertConnectorGLSL::getElement(   RegisterType type,
       {
          Var *newVar = new Var;
          mElementList.push_back( newVar );
-         newVar->setConnectName( "gl_Vertex" );
+         newVar->setConnectName( "vPosition" );
          return newVar;
       }
 
@@ -46,28 +46,49 @@ Var * AppVertConnectorGLSL::getElement(   RegisterType type,
       {
          Var *newVar = new Var;
          mElementList.push_back( newVar );
-         newVar->setConnectName( "gl_Normal" );
+         newVar->setConnectName( "vNormal" );
          return newVar;
       }
-      
+
+      case RT_BINORMAL:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );
+         newVar->setConnectName( "vBinormal" );
+         return newVar;
+      }      
 
       case RT_COLOR:
       {
          Var *newVar = new Var;
          mElementList.push_back( newVar );
-         newVar->setConnectName( "gl_Color" );
+         newVar->setConnectName( "vColor" );
          return newVar;
       }
 
-      case RT_TEXCOORD:
-      case RT_BINORMAL:
       case RT_TANGENT:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );         
+         newVar->setConnectName( "vTangent" );
+         return newVar;
+      }
+
+      case RT_TANGENTW:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );         
+         newVar->setConnectName( "vTangentW" );
+         return newVar;
+      }
+
+      case RT_TEXCOORD:
       {
          Var *newVar = new Var;
          mElementList.push_back( newVar );
          
          char out[32];
-         dSprintf( (char*)out, sizeof(out), "gl_MultiTexCoord%d", mCurTexElem );
+         dSprintf( (char*)out, sizeof(out), "vTexCoord%d", mCurTexElem );
          newVar->setConnectName( out );
          newVar->constNum = mCurTexElem;
          newVar->arraySize = numElements;
@@ -108,29 +129,55 @@ void AppVertConnectorGLSL::reset()
    mCurTexElem = 0;
 }
 
-void AppVertConnectorGLSL::print( Stream &stream )
+void AppVertConnectorGLSL::print( Stream &stream, bool isVertexShader )
 {
-   // print out elements
+   if(!isVertexShader)
+      return;
+
+   U8 output[256];
+
+   // print struct
+   dSprintf( (char*)output, sizeof(output), "struct VertexData\r\n" );
+   stream.write( dStrlen((char*)output), output );
+   dSprintf( (char*)output, sizeof(output), "{\r\n" );
+   stream.write( dStrlen((char*)output), output );
+
    for( U32 i=0; i<mElementList.size(); i++ )
    {
       Var *var = mElementList[i];
-      U8 output[256];
-      const char* swizzle;
-      if(!dStrcmp((const char*)var->type, "float"))
-         swizzle = "x";
-      else if(!dStrcmp((const char*)var->type, "vec2"))
-         swizzle = "xy";
-      else if(!dStrcmp((const char*)var->type, "vec3"))
-         swizzle = "xyz";
+      
+      if( var->arraySize == 1)
+      {         
+         dSprintf( (char*)output, sizeof(output), "   %s %s;\r\n", var->type, (char*)var->name );
+         stream.write( dStrlen((char*)output), output );
+      }
       else
-         swizzle = "xyzw";
+      {
+         dSprintf( (char*)output, sizeof(output), "   %s %s[%d];\r\n", var->type, (char*)var->name, var->arraySize );
+         stream.write( dStrlen((char*)output), output );
+      }
+   }
 
-      // This is ugly.  We use #defines to match user defined names with 
-      // built in vars.  There is no cleaner way to do this.
-      dSprintf( (char*)output, sizeof(output), "#define %s %s.%s\r\n", var->name, var->connectName, swizzle );
+   dSprintf( (char*)output, sizeof(output), "} IN;\r\n\r\n" );
+   stream.write( dStrlen((char*)output), output );   
 
+   // print in elements
+   for( U32 i=0; i<mElementList.size(); i++ )
+   {
+      Var *var = mElementList[i];
+      
+      for(int j = 0; j < var->arraySize; ++j)
+      {        
+         const char *name = j == 0 ? var->connectName : avar("vTexCoord%d", var->constNum + j) ;
+         dSprintf( (char*)output, sizeof(output), "in %s %s;\r\n", var->type, name );
+         stream.write( dStrlen((char*)output), output );         
+      }
+
+      dSprintf( (char*)output, sizeof(output), "#define IN_%s IN.%s\r\n", var->name, var->name ); // TODO REMOVE
       stream.write( dStrlen((char*)output), output );
    }
+   const char* newLine ="\r\n";
+   stream.write( dStrlen((char*)newLine), newLine );
 }
 
 Var * VertPixelConnectorGLSL::getElement( RegisterType type, 
@@ -140,14 +187,45 @@ Var * VertPixelConnectorGLSL::getElement( RegisterType type,
    switch( type )
    {
    case RT_POSITION:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );
+         newVar->setConnectName( "POSITION" );
+         return newVar;
+      }
+
    case RT_NORMAL:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );
+         newVar->setConnectName( "NORMAL" );
+         return newVar;
+      }
+
    case RT_COLOR:
       {
          Var *newVar = new Var;
          mElementList.push_back( newVar );
+         newVar->setConnectName( "COLOR" );
          return newVar;
       }
 
+   /*case RT_BINORMAL:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );
+         newVar->setConnectName( "BINORMAL" );
+         return newVar;
+      }
+
+   case RT_TANGENT:
+      {
+         Var *newVar = new Var;
+         mElementList.push_back( newVar );
+         newVar->setConnectName( "TANGENT" );
+         return newVar;
+      }   */
+
    case RT_TEXCOORD:
    case RT_BINORMAL:
    case RT_TANGENT:
@@ -155,6 +233,10 @@ Var * VertPixelConnectorGLSL::getElement( RegisterType type,
          Var *newVar = new Var;
          newVar->arraySize = numElements;
 
+         char out[32];
+         dSprintf( (char*)out, sizeof(out), "TEXCOORD%d", mCurTexElem );
+         newVar->setConnectName( out );
+
          if ( numRegisters != -1 )
             mCurTexElem += numRegisters;
          else
@@ -192,7 +274,7 @@ void VertPixelConnectorGLSL::reset()
    mCurTexElem = 0;
 }
 
-void VertPixelConnectorGLSL::print( Stream &stream )
+void VertPixelConnectorGLSL::print( Stream &stream, bool isVerterShader )
 {
    // print out elements
    for( U32 i=0; i<mElementList.size(); i++ )
@@ -204,15 +286,138 @@ void VertPixelConnectorGLSL::print( Stream &stream )
          continue;
 
       if(var->arraySize <= 1)
-         dSprintf((char*)output, sizeof(output), "varying %s %s;\r\n", var->type, var->name);
+         dSprintf((char*)output, sizeof(output), "%s %s _%s_;\r\n", (isVerterShader ? "out" : "in"), var->type, var->connectName);
       else
-         dSprintf((char*)output, sizeof(output), "varying %s %s[%d];\r\n", var->type, var->name, var->arraySize);
+         dSprintf((char*)output, sizeof(output), "%s %s _%s_[%d];\r\n", (isVerterShader ? "out" : "in"),var->type, var->connectName, var->arraySize);      
 
       stream.write( dStrlen((char*)output), output );
    }
+
+   printStructDefines(stream, !isVerterShader);
 }
 
-void VertexParamsDefGLSL::print( Stream &stream )
+void VertPixelConnectorGLSL::printOnMain( Stream &stream, bool isVerterShader )
+{
+   if(isVerterShader)
+      return;
+
+   const char *newLine = "\r\n";
+   const char *header = "   //-------------------------\r\n";
+   stream.write( dStrlen((char*)newLine), newLine );
+   stream.write( dStrlen((char*)header), header );
+
+   // print out elements
+   for( U32 i=0; i<mElementList.size(); i++ )
+   {
+      U8 output[256];
+
+      Var *var = mElementList[i];
+      if(!dStrcmp((const char*)var->name, "gl_Position"))
+         continue;
+  
+      dSprintf((char*)output, sizeof(output), "   %s IN_%s = _%s_;\r\n", var->type, var->name, var->connectName);      
+
+      stream.write( dStrlen((char*)output), output );
+   }
+
+   stream.write( dStrlen((char*)header), header );
+   stream.write( dStrlen((char*)newLine), newLine );
+}
+
+
+void AppVertConnectorGLSL::printOnMain( Stream &stream, bool isVerterShader )
+{
+   if(!isVerterShader)
+      return;   
+
+   const char *newLine = "\r\n";
+   const char *header = "   //-------------------------\r\n";
+   stream.write( dStrlen((char*)newLine), newLine );
+   stream.write( dStrlen((char*)header), header );
+
+   // print out elements
+   for( U32 i=0; i<mElementList.size(); i++ )
+   {
+      Var *var = mElementList[i];
+      U8 output[256];  
+
+      if(var->arraySize <= 1)
+      {
+         dSprintf((char*)output, sizeof(output), "   IN.%s = %s;\r\n", var->name, var->connectName);
+         stream.write( dStrlen((char*)output), output );
+      }
+      else
+      {
+         for(int j = 0; j < var->arraySize; ++j)
+         {
+            const char *name = j == 0 ? var->connectName : avar("vTexCoord%d", var->constNum + j) ;
+            dSprintf((char*)output, sizeof(output), "   IN.%s[%d] = %s;\r\n", var->name, j, name );
+            stream.write( dStrlen((char*)output), output );
+         }
+      }
+   }
+
+   stream.write( dStrlen((char*)header), header );
+   stream.write( dStrlen((char*)newLine), newLine );
+}
+
+
+
+
+Vector<String> initDeprecadedDefines()
+{
+   Vector<String> vec;
+   vec.push_back( "isBack"); 
+   return vec;
+}
+
+void VertPixelConnectorGLSL::printStructDefines( Stream &stream, bool in )
+{
+   const char* connectionDir;
+
+   if(in)
+   {       
+      connectionDir = "IN";
+   }
+   else
+   {
+     
+      connectionDir = "OUT";
+   }
+
+   static Vector<String> deprecatedDefines = initDeprecadedDefines();
+
+   const char *newLine = "\r\n";
+   const char *header = "// Struct defines\r\n";
+   stream.write( dStrlen((char*)newLine), newLine );
+   stream.write( dStrlen((char*)header), header );
+
+   // print out elements
+   for( U32 i=0; i<mElementList.size(); i++ )
+   {
+      U8 output[256];
+
+      Var *var = mElementList[i];
+      if(!dStrcmp((const char*)var->name, "gl_Position"))
+         continue;      
+  
+      if(!in)
+      {
+         dSprintf((char*)output, sizeof(output), "#define %s_%s _%s_\r\n", connectionDir, var->name, var->connectName);
+         stream.write( dStrlen((char*)output), output );
+      }
+
+      if( deprecatedDefines.contains((char*)var->name))
+         continue;
+
+      dSprintf((char*)output, sizeof(output), "#define %s %s_%s\r\n", var->name, connectionDir, var->name);
+      stream.write( dStrlen((char*)output), output );
+   }
+
+   stream.write( dStrlen((char*)newLine), newLine );
+}
+
+void VertexParamsDefGLSL::print( Stream &stream, bool isVerterShader )
 {
    // find all the uniform variables and print them out
    for( U32 i=0; i<LangElement::elementList.size(); i++)
@@ -237,7 +442,7 @@ void VertexParamsDefGLSL::print( Stream &stream )
    stream.write( dStrlen(closer), closer );
 }
 
-void PixelParamsDefGLSL::print( Stream &stream )
+void PixelParamsDefGLSL::print( Stream &stream, bool isVerterShader )
 {
    // find all the uniform variables and print them out
    for( U32 i=0; i<LangElement::elementList.size(); i++)
@@ -260,4 +465,22 @@ void PixelParamsDefGLSL::print( Stream &stream )
 
    const char *closer = "\r\nvoid main()\r\n{\r\n";
    stream.write( dStrlen(closer), closer );
+
+   for( U32 i=0; i<LangElement::elementList.size(); i++)
+   {
+      Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
+      if( var )
+      {
+         if( var->uniform && !var->sampler)
+         {
+            U8 output[256];
+            if(var->arraySize <= 1)
+               dSprintf((char*)output, sizeof(output), "   %s %s = %s;\r\n", var->type, var->name, var->name);
+            else
+               dSprintf((char*)output, sizeof(output), "   %s %s[%d] = %s;\r\n", var->type, var->name, var->arraySize, var->name);
+
+            stream.write( dStrlen((char*)output), output );
+         }
+      }
+   }
 }

+ 7 - 4
Engine/source/shaderGen/GLSL/shaderCompGLSL.h

@@ -40,7 +40,9 @@ public:
    virtual void reset();
    virtual void sortVars();
 
-   virtual void print( Stream &stream );
+   virtual void print( Stream &stream, bool isVerterShader );
+   void printStructDefines( Stream &stream, bool in );
+   virtual void printOnMain( Stream &stream, bool isVerterShader );
 };
 
 class AppVertConnectorGLSL : public ShaderConnector
@@ -53,21 +55,22 @@ public:
    virtual void reset();
    virtual void sortVars();
    
-   virtual void print( Stream &stream );
+   virtual void print( Stream &stream, bool isVerterShader );   
+   virtual void printOnMain( Stream &stream, bool isVerterShader );
 };
 
 
 class VertexParamsDefGLSL : public ParamsDef
 {
 public:
-   virtual void print( Stream &stream );
+   virtual void print( Stream &stream, bool isVerterShader );   
 };
 
 
 class PixelParamsDefGLSL : public ParamsDef
 {
 public:
-   virtual void print( Stream &stream );
+   virtual void print( Stream &stream, bool isVerterShader );
 };
 
 #endif // _SHADERCOMP_GLSL_H_

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 349 - 92
Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp


+ 45 - 34
Engine/source/shaderGen/GLSL/shaderFeatureGLSL.h

@@ -25,9 +25,6 @@
 #ifndef _SHADERFEATURE_H_
    #include "shaderGen/shaderFeature.h"
 #endif
-#ifndef _MATERIALFEATUREDATA_H_
-   #include "materials/materialFeatureData.h"
-#endif
 
 struct LangElement;
 struct MaterialFeatureData;
@@ -54,13 +51,25 @@ public:
                               bool mapsToSampler,
                               Vector<ShaderComponent*> &componentList );
 
+   static Var* getInColor( const char *name,
+                           const char *type,
+                           Vector<ShaderComponent*> &componentList );
+
+   ///
+   static Var* addOutVpos( MultiLine *meta,
+                           Vector<ShaderComponent*> &componentList );
+
+   /// Returns the VPOS input register for the pixel shader.
+   static Var* getInVpos(  MultiLine *meta,
+                           Vector<ShaderComponent*> &componentList );
+
    /// Returns the "objToTangentSpace" transform or creates one if this
    /// is the first feature to need it.
    Var* getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
                                  MultiLine *meta,
                                  const MaterialFeatureData &fd );
 
-   /// Returns the existing output "worldToTangent" transform or 
+   /// Returns the existing output "outWorldToTangent" transform or 
    /// creates one if this is the first feature to need it.
    Var* getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
                               MultiLine *meta,
@@ -70,7 +79,7 @@ public:
    /// adding it to the input connector if it doesn't exist.
    static Var* getInWorldToTangent( Vector<ShaderComponent*> &componentList );
    
-   /// Returns the existing output "viewToTangent" transform or 
+   /// Returns the existing output "outViewToTangent" transform or 
    /// creates one if this is the first feature to need it.
    Var* getOutViewToTangent( Vector<ShaderComponent*> &componentList,
       MultiLine *meta,
@@ -81,17 +90,16 @@ public:
    static Var* getInViewToTangent( Vector<ShaderComponent*> &componentList );
 	
 	/// Calculates the world space position in the vertex shader and 
-   /// assigns it to the passed language element.  It does not pass    /// it across the connector to the pixel shader.
+   /// assigns it to the passed language element.  It does not pass 
+   /// it across the connector to the pixel shader.
    /// @see addOutWsPosition
    void getWsPosition(  Vector<ShaderComponent*> &componentList,                
-							 
 							 bool useInstancing,
 							 MultiLine *meta,
 							 LangElement *wsPosition );
 	
    /// Adds the "wsPosition" to the input connector if it doesn't exist.
    Var* addOutWsPosition(  Vector<ShaderComponent*> &componentList,             
-								 
 								 bool useInstancing,
 								 MultiLine *meta );
 	
@@ -129,7 +137,6 @@ public:
 								bool useInstancing,
 								MultiLine *meta );
 		
-
    // ShaderFeature
    Var* getVertTexCoord( const String &name );
    LangElement* setupTexSpaceMat(  Vector<ShaderComponent*> &componentList, Var **texSpaceMat );
@@ -151,25 +158,27 @@ public:
    virtual String getName() { return mName; }
 };
 
-
 class RenderTargetZeroGLSL : public ShaderFeatureGLSL
 {
-protected:   ShaderFeature::OutputTarget mOutputTargetMask;
+protected:
+   ShaderFeature::OutputTarget mOutputTargetMask;
    String mFeatureName;
 	
 public:
    RenderTargetZeroGLSL( const ShaderFeature::OutputTarget target )
 	: mOutputTargetMask( target )
    {
-      char buffer[256];      dSprintf(buffer, sizeof(buffer), "Render Target Output = 0.0, output mask %04b", mOutputTargetMask);
-      mFeatureName = buffer;   }
+      char buffer[256];
+      dSprintf(buffer, sizeof(buffer), "Render Target Output = 0.0, output mask %04b", mOutputTargetMask);
+      mFeatureName = buffer;
+   }
 	
    virtual String getName() { return mFeatureName; }
 	
    virtual void processPix( Vector<ShaderComponent*> &componentList, 
 									const MaterialFeatureData &fd );
-	virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const { return 
-		mOutputTargetMask; }
+   
+   virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const { return mOutputTargetMask; }
 };
 
 
@@ -190,11 +199,7 @@ public:
                                     U32 stageNum,
                                     const FeatureType &type,
                                     const FeatureSet &features,
-                                    MaterialFeatureData *outFeatureData )
-   {
-      // This feature is always on!
-      outFeatureData->features.addFeature( type );
-   }
+                                    MaterialFeatureData *outFeatureData );
 
 };
 
@@ -297,7 +302,6 @@ public:
    }
 };
 
-
 /// Diffuse vertex color
 class DiffuseVertColorFeatureGLSL : public ShaderFeatureGLSL
 {
@@ -316,7 +320,6 @@ public:
    }
 };
 
-
 /// Lightmap
 class LightmapFeatGLSL : public ShaderFeatureGLSL
 {
@@ -491,7 +494,14 @@ public:
 /// Visibility
 class VisibilityFeatGLSL : public ShaderFeatureGLSL
 {
+protected:
+
+   ShaderIncludeDependency mTorqueDep;
+
 public:
+
+   VisibilityFeatGLSL();
+
    virtual void processVert( Vector<ShaderComponent*> &componentList,
                              const MaterialFeatureData &fd );
 
@@ -500,11 +510,6 @@ public:
 
    virtual Resources getResources( const MaterialFeatureData &fd );
 
-   virtual void setTexData( Material::StageData &stageDat,
-                            const MaterialFeatureData &fd,
-                            RenderPassData &passData,
-                            U32 &texIndex );
-
    virtual Material::BlendOp getBlendOp() { return Material::None; }
 
    virtual String getName()
@@ -547,10 +552,10 @@ public:
    }
 };
 
-
 /// This should be the final feature on most pixel shaders which
 /// encodes the color for the current HDR target format.
-/// @see HDRPostFx/// @see LightManager
+/// @see HDRPostFx
+/// @see LightManager
 /// @see torque.glsl
 class HDROutGLSL : public ShaderFeatureGLSL
 {
@@ -570,9 +575,9 @@ public:
    virtual String getName() { return "HDR Output"; }
 };
 
-
 ///
-class FoliageFeatureGLSL : public ShaderFeatureGLSL{
+class FoliageFeatureGLSL : public ShaderFeatureGLSL
+{
 protected:
 	
    ShaderIncludeDependency mDep;
@@ -583,6 +588,7 @@ public:
 	
    virtual void processVert( Vector<ShaderComponent*> &componentList,
 									 const MaterialFeatureData &fd );
+
    virtual void processPix( Vector<ShaderComponent*> &componentList,
                            const MaterialFeatureData &fd );   
 	
@@ -597,11 +603,10 @@ public:
 											const FeatureType &type,
 											const FeatureSet &features,
 											MaterialFeatureData *outFeatureData );
+
    virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject );   
 };
 
-
-///
 class ParticleNormalFeatureGLSL : public ShaderFeatureGLSL
 {
 public:
@@ -616,6 +621,9 @@ public:
 	
 };
 
+
+/// Special feature for unpacking imposter verts.
+/// @see RenderImposterMgr
 class ImposterVertFeatureGLSL : public ShaderFeatureGLSL
 {
 protected:
@@ -623,9 +631,12 @@ protected:
    ShaderIncludeDependency mDep;
 	
 public:
-	ImposterVertFeatureGLSL();
+
+   ImposterVertFeatureGLSL();
+
 	virtual void processVert(  Vector<ShaderComponent*> &componentList,
 									 const MaterialFeatureData &fd );
+
 	virtual void processPix(  Vector<ShaderComponent*> &componentList,
 									const MaterialFeatureData &fd );
 	

+ 10 - 3
Engine/source/shaderGen/GLSL/shaderGenGLSL.cpp

@@ -56,7 +56,9 @@ void ShaderGenPrinterGLSL::printMainComment( Stream& stream )
 
 void ShaderGenPrinterGLSL::printVertexShaderCloser( Stream& stream )
 {
-   const char *closer = "}\r\n";
+   // We are render OpenGL upside down for use DX9 texture coords.
+   // Must be the last vertex feature.
+   const char *closer = "   gl_Position.y *= -1;\r\n}\r\n";
    stream.write( dStrlen(closer), closer );
 }
 
@@ -67,7 +69,7 @@ void ShaderGenPrinterGLSL::printPixelShaderOutputStruct( Stream& stream, const M
 
 void ShaderGenPrinterGLSL::printPixelShaderCloser( Stream& stream )
 {
-   const char *closer = "   gl_FragColor = col;\r\n}\r\n";
+   const char *closer = "   OUT_FragColor0 = col;\r\n}\r\n";
    stream.write( dStrlen(closer), closer );
 }
 
@@ -124,6 +126,11 @@ ShaderComponent* ShaderGenComponentFactoryGLSL::createVertexInputConnector( cons
          var = vertComp->getElement( RT_TANGENT );
          var->setName( "T" );
       }
+      else if ( element.isSemantic( GFXSemantic::TANGENTW ) )
+      {
+         var = vertComp->getElement( RT_TANGENTW );
+         var->setName( "tangentW" );
+      }
       else if ( element.isSemantic( GFXSemantic::BINORMAL ) )
       {
          var = vertComp->getElement( RT_BINORMAL );
@@ -152,7 +159,7 @@ ShaderComponent* ShaderGenComponentFactoryGLSL::createVertexInputConnector( cons
       if ( !var )
          continue;
 
-      var->setStructName( "" );
+      var->setStructName( "IN" );
       var->setType( typeToString( element.getType() ) );
    }
 

+ 6 - 0
Engine/source/shaderGen/GLSL/shaderGenGLSLInit.cpp

@@ -76,6 +76,8 @@ void _initShaderGenGLSL( ShaderGen *shaderGen )
 	
    FEATUREMGR->registerFeature( MFT_ParaboloidVertTransform, new ParaboloidVertTransformGLSL );
    FEATUREMGR->registerFeature( MFT_IsSinglePassParaboloid, new NamedFeatureGLSL( "Single Pass Paraboloid" ) );
+   FEATUREMGR->registerFeature( MFT_UseInstancing, new NamedFeatureGLSL( "Hardware Instancing" ) );
+
 	FEATUREMGR->registerFeature( MFT_RenderTarget1_Zero, new RenderTargetZeroGLSL
 										 ( ShaderFeature::RenderTarget1 ) );
 	
@@ -89,6 +91,10 @@ void _initShaderGenGLSL( ShaderGen *shaderGen )
 
    FEATUREMGR->registerFeature( MFT_ImposterVert, new ImposterVertFeatureGLSL );
 
+   //FEATUREMGR->registerFeature( MFT_LightbufferMRT, new NamedFeatureGLSL( "Lightbuffer MRT" ) );
+   //FEATUREMGR->registerFeature( MFT_IsTranslucentZWrite, new NamedFeatureGLSL( "Translucent ZWrite" ) );
+   //FEATUREMGR->registerFeature( MFT_InterlacedPrePass, new NamedFeatureGLSL( "Interlaced Pre Pass" ) );
+
 }
 
 MODULE_BEGIN( ShaderGenGLSL )

+ 454 - 158
Engine/source/terrain/glsl/terrFeatureGLSL.cpp

@@ -30,22 +30,35 @@
 #include "shaderGen/langElement.h"
 #include "shaderGen/shaderOp.h"
 #include "shaderGen/featureMgr.h"
+#include "shaderGen/shaderGen.h"
 #include "core/module.h"
 
-
-MODULE_BEGIN( TerrainFeatGLSL )
-
-   MODULE_INIT_AFTER( ShaderGenFeatureMgr )
-
-   MODULE_INIT
+namespace 
+{
+   void register_glsl_shader_features_for_terrain(GFXAdapterType type)
    {
+      if(type != OpenGL)
+         return;
+
       FEATUREMGR->registerFeature( MFT_TerrainBaseMap, new TerrainBaseMapFeatGLSL );
-      FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new TerrainParallaxMapFeatGLSL );   
+      FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new NamedFeatureGLSL( "Terrain Parallax Texture" ) );   
       FEATUREMGR->registerFeature( MFT_TerrainDetailMap, new TerrainDetailMapFeatGLSL );
       FEATUREMGR->registerFeature( MFT_TerrainNormalMap, new TerrainNormalMapFeatGLSL );
+      FEATUREMGR->registerFeature( MFT_TerrainMacroMap, new TerrainMacroMapFeatGLSL );
       FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatGLSL );
       FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureGLSL( "Terrain Side Projection" ) );
-      FEATUREMGR->registerFeature( MFT_TerrainAdditive, new TerrainAdditiveFeatGLSL );
+      FEATUREMGR->registerFeature( MFT_TerrainAdditive, new TerrainAdditiveFeatGLSL );     
+   }
+
+};
+
+MODULE_BEGIN( TerrainFeatGLSL )
+
+   MODULE_INIT_AFTER( ShaderGen )
+
+   MODULE_INIT
+   {      
+      SHADERGEN->getFeatureInitSignal().notify(&register_glsl_shader_features_for_terrain);
    }
 
 MODULE_END;
@@ -68,7 +81,7 @@ Var* TerrainFeatGLSL::_getUniformVar( const char *name, const char *type, Consta
 
 Var* TerrainFeatGLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList )
 {
-   String name( String::ToString( "outDetCoord%d", getProcessIndex() ) );
+   String name( String::ToString( "detCoord%d", getProcessIndex() ) );
    Var *inDet = (Var*)LangElement::find( name );
    
    if ( !inDet )
@@ -77,6 +90,7 @@ Var* TerrainFeatGLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList
       
       inDet = connectComp->getElement( RT_TEXCOORD );
       inDet->setName( name );
+      inDet->setStructName( "IN" );
       inDet->setType( "vec4" );
       inDet->mapsToSampler = true;
    }
@@ -84,6 +98,25 @@ Var* TerrainFeatGLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList
    return inDet;
 }
 
+Var* TerrainFeatGLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList )
+{
+   String name( String::ToString( "macroCoord%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->setStructName( "IN" );
+      inDet->setType( "vec4" );
+      inDet->mapsToSampler = true;
+   }
+
+   return inDet;
+}
+
 Var* TerrainFeatGLSL::_getNormalMapTex()
 {
    String name( String::ToString( "normalMap%d", getProcessIndex() ) );
@@ -119,6 +152,24 @@ Var* TerrainFeatGLSL::_getDetailIdStrengthParallax()
    return detailInfo;
 }
 
+Var* TerrainFeatGLSL::_getMacroIdStrengthParallax()
+{
+   String name( String::ToString( "macroIdStrengthParallax%d", getProcessIndex() ) );
+
+   Var *detailInfo = (Var*)LangElement::find( name );
+   if ( !detailInfo )
+   {
+      detailInfo = new Var;
+      detailInfo->setType( "vec3" );
+      detailInfo->setName( name );
+      detailInfo->uniform = true;
+      detailInfo->constSortPos = cspPotentialPrimitive;
+   }
+
+   return detailInfo;
+}
+
+
 void TerrainBaseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList, 
                                           const MaterialFeatureData &fd )
 {
@@ -146,7 +197,7 @@ void TerrainBaseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentLis
       // So instead i fixed this by flipping the base and detail
       // coord y scale to compensate when rendering.
       //
-      meta->addStatement( new GenOp( "   @ = @.xyz * vec3( @, @, -@ );\r\n", 
+      meta->addStatement( new GenOp( "   @ = @.xyz * float3( @, @, -@ );\r\n", 
          new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
    }
 
@@ -155,6 +206,7 @@ void TerrainBaseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentLis
    // Pass the texture coord to the pixel shader.
    Var *outTex = connectComp->getElement( RT_TEXCOORD );
    outTex->setName( "outTexCoord" );
+   outTex->setStructName( "OUT" );
    outTex->setType( "vec3" );
    outTex->mapsToSampler = true;
    meta->addStatement( new GenOp( "   @.xy = @.xy;\r\n", outTex, inTex ) );
@@ -166,7 +218,7 @@ void TerrainBaseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentLis
    {
       Var *inNormal = (Var*)LangElement::find( "normal" );
       meta->addStatement( 
-         new GenOp( "   @.z = pow( abs( dot( normalize( vec3( @.x, @.y, 0.0 ) ), vec3( 0, 1, 0 ) ) ), 10.0 );\r\n", 
+         new GenOp( "   @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n", 
             outTex, inNormal, inNormal ) );
    }
    else
@@ -182,7 +234,7 @@ void TerrainBaseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentLis
    Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
    Var *inTanget = new Var( "T", "vec3" );
    Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
-   meta->addStatement( new GenOp( "   @ = normalize( vec3( @, 0.0, @ ) );\r\n", 
+   meta->addStatement( new GenOp( "   @ = normalize( float3( @, 0, @ ) );\r\n", 
       new DecOp( inTanget ), squareSize, inTangentZ ) );  
 }
 
@@ -190,7 +242,7 @@ void TerrainBaseMapFeatGLSL::processPix(  Vector<ShaderComponent*> &componentLis
                                           const MaterialFeatureData &fd )
 {
    // grab connector texcoord register
-   Var *texCoord = getInTexCoord( "outTexCoord", "vec3", true, componentList );
+   Var *texCoord = getInTexCoord( "texCoord", "vec3", true, componentList );
 
    // We do nothing more if this is a prepass.
    if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
@@ -209,7 +261,7 @@ void TerrainBaseMapFeatGLSL::processPix(  Vector<ShaderComponent*> &componentLis
    Var *baseColor = new Var;
    baseColor->setType( "vec4" );
    baseColor->setName( "baseColor" );
-   meta->addStatement( new GenOp( "   @ = texture2D( @, @.xy );\r\n", new DecOp( baseColor ), diffuseMap, texCoord ) );
+   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;
@@ -228,8 +280,11 @@ ShaderFeature::Resources TerrainBaseMapFeatGLSL::getResources( const MaterialFea
 }
 
 TerrainDetailMapFeatGLSL::TerrainDetailMapFeatGLSL()
-   : mTerrainDep( "shaders/common/terrain/terrain.glsl" )
+   :  mTorqueDep( "shaders/common/gl/torque.glsl" ),
+      mTerrainDep( "shaders/common/terrain/terrain.glsl" )
+      
 {
+   addDependency( &mTorqueDep );
    addDependency( &mTerrainDep );
 }
 
@@ -238,13 +293,6 @@ void TerrainDetailMapFeatGLSL::processVert(  Vector<ShaderComponent*> &component
 {
    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" );
@@ -260,6 +308,26 @@ void TerrainDetailMapFeatGLSL::processVert(  Vector<ShaderComponent*> &component
 
    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( "vec3" );
+      meta->addStatement( new GenOp( "   @ = tMul( @, 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 )
@@ -275,7 +343,8 @@ void TerrainDetailMapFeatGLSL::processVert(  Vector<ShaderComponent*> &component
    // 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->setName( String::ToString( "detCoord%d", detailIndex ) );
+   outTex->setStructName( "OUT" );
    outTex->setType( "vec4" );
    outTex->mapsToSampler = true;
 
@@ -293,7 +362,7 @@ void TerrainDetailMapFeatGLSL::processVert(  Vector<ShaderComponent*> &component
    // its scale is flipped to correct for the non negative y
    // in texCoord.
    //
-   // See TerrainBaseMapFeatHLSL::processVert().
+   // See TerrainBaseMapFeatGLSL::processVert().
    //
    meta->addStatement( new GenOp( "   @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
 
@@ -308,16 +377,29 @@ void TerrainDetailMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
                                              const MaterialFeatureData &fd )
 {
    const U32 detailIndex = getProcessIndex();
+   Var *inTex = getVertTexCoord( "texCoord" );
 
-   // 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;
+   MultiLine *meta = new MultiLine;
 
-   Var *inTex = getVertTexCoord( "outTexCoord" );
+   // 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" );
+      }
    
-   MultiLine *meta = new MultiLine;
+      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" );
@@ -336,7 +418,7 @@ void TerrainDetailMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
       layerTex->constNum = Var::getTexUnitNum();
 
       // Read the layer texture to get the samples.
-      meta->addStatement( new GenOp( "   @ = round( texture2D( @, @.xy ) * 255.0f );\r\n", 
+      meta->addStatement( new GenOp( "   @ = round( tex2D( @, @.xy ) * 255.0f );\r\n", 
                                        new DecOp( layerSample ), layerTex, inTex ) );
    }
 
@@ -372,17 +454,43 @@ void TerrainDetailMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
       blendTotal = new Var;
       blendTotal->setName( "blendTotal" );
       blendTotal->setType( "float" );
-      meta->addStatement( new GenOp( "   @ = 0.0;\r\n", new DecOp( blendTotal ) ) );
+      meta->addStatement( new GenOp( "   @ = 0;\r\n", new DecOp( blendTotal ) ) );
    }
 
    // Add to the blend total.
-   meta->addStatement( new GenOp( "   @ += @;\r\n", blendTotal, detailBlend ) );
-   //meta->addStatement( new GenOp( "   @ += @ * @.y * @.w;\r\n", 
-      //blendTotal, detailBlend, detailInfo, inDet ) );
+   meta->addStatement( new GenOp( "   @ = max( @, @ );\r\n", blendTotal, 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();
 
-   // Nothing more to do for a detail texture in prepass.
+      // Call the library function to do the rest.
+      meta->addStatement( new GenOp( "   @.xy += parallaxOffset( @, @.xy, @, @.z * @ );\r\n", 
+         inDet, normalMap, inDet, negViewTS, detailInfo, 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;
    }
@@ -407,6 +515,7 @@ void TerrainDetailMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
    // 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 ) );
 
@@ -421,12 +530,12 @@ void TerrainDetailMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
    //
    if ( fd.features.hasFeature( MFT_TerrainSideProject, detailIndex ) )
    {
-      meta->addStatement( new GenOp( "      @ = ( mix( texture2D( @, @.yz ), texture2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n", 
+      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( "      @ = ( texture2D( @, @.xy ) * 2.0 ) - 1.0;\r\n", 
+      meta->addStatement( new GenOp( "      @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n", 
                                        detailColor, detailMap, inDet ) );
    }
 
@@ -436,7 +545,7 @@ void TerrainDetailMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
    Var *baseColor = (Var*)LangElement::find( "baseColor" );
    Var *outColor = (Var*)LangElement::find( "col" );
 
-   meta->addStatement( new GenOp( "      @ = mix( @, @ + @, @ );\r\n",
+   meta->addStatement( new GenOp( "      @ = lerp( @, @ + @, @ );\r\n",
                                     outColor, outColor, baseColor, detailColor, detailBlend ) );
 
    meta->addStatement( new GenOp( "   }\r\n" ) );
@@ -448,28 +557,293 @@ ShaderFeature::Resources TerrainDetailMapFeatGLSL::getResources( const MaterialF
 {
    Resources res;
 
+   if ( getProcessIndex() == 0 )
+   {
+      // If this is the first detail pass then we 
+      // samples from the layer tex.
+      res.numTex += 1;
+
+      // If this material also does parallax then it
+      // will generate the negative view vector and the
+      // worldToTanget transform.
+      if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) )
+         res.numTexReg += 4;
+   }
+
+   // If this isn't the prepass then we sample 
+   // from the detail texture for diffuse coloring.
+   if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
+      res.numTex += 1;
+
+   // If we have parallax for this layer then we'll also
+   // be sampling the normal map for the parallax heightmap.
+   if ( fd.features.hasFeature( MFT_TerrainParallaxMap, getProcessIndex() ) )
+      res.numTex += 1;
+
+   // Finally we always send the detail texture 
+   // coord to the pixel shader.
+   res.numTexReg += 1;
+
+   return res;
+}
+
+
+TerrainMacroMapFeatGLSL::TerrainMacroMapFeatGLSL()
+   :  mTorqueDep( "shaders/common/gl/torque.glsl" ),
+      mTerrainDep( "shaders/common/terrain/terrain.glsl" )
+      
+{
+   addDependency( &mTorqueDep );
+   addDependency( &mTerrainDep );
+}
+
+
+void TerrainMacroMapFeatGLSL::processVert(  Vector<ShaderComponent*> &componentList, 
+                                             const MaterialFeatureData &fd )
+{
+   const U32 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", "vec3", 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( "vec4" );
+   outTex->mapsToSampler = true;
+
+   // Get the detail scale and fade info.
+   Var *detScaleAndFade = new Var;
+   detScaleAndFade->setType( "vec4" );
+   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 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 ) )
    {
-      // If this is a prepass and we don't have a 
-      // matching normal map... we use no resources.
-      if ( !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() ) )
-         return res;
+      // 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 );
 
-      // If this is the first matching normal map then
-      // it also samples from the layer tex.
-      if ( !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) )
-         res.numTex += 1;
+         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
    {
-      // If this is the first detail pass then it
-      // also samples from the layer tex.
-      if ( !fd.features.hasFeature( MFT_TerrainDetailMap, getProcessIndex() - 1 ) )
+      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;
+}
+
+
+
+ShaderFeature::Resources TerrainMacroMapFeatGLSL::getResources( const MaterialFeatureData &fd )
+{
+   Resources res;
+
+   if ( getProcessIndex() == 0 )
+   {
+      // If this is the first detail pass then we 
+      // samples from the layer tex.
          res.numTex += 1;
+   }
 
+   // If this isn't the prepass then we sample 
+   // from the detail texture for diffuse coloring.
+   if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
       res.numTex += 1;
-   }
 
+   // Finally we always send the detail texture 
+   // coord to the pixel shader.
    res.numTexReg += 1;
 
    return res;
@@ -510,7 +884,7 @@ void TerrainNormalMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
       gbNormal = new Var;
       gbNormal->setName( "gbNormal" );
       gbNormal->setType( "vec3" );
-      meta->addStatement( new GenOp( "   @ = @[2];\r\n", new DecOp( gbNormal ), viewToTangent ) );
+      meta->addStatement( new GenOp( "   @ = tGetMatrix3Row(@, 2);\r\n", new DecOp( gbNormal ), viewToTangent ) );
    }
 
    const U32 normalIndex = getProcessIndex();
@@ -520,7 +894,6 @@ void TerrainNormalMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
 
    // 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 ) );
 
@@ -531,7 +904,7 @@ void TerrainNormalMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
 
    /// Get the texture coord.
    Var *inDet = _getInDetailCoord( componentList );
-   Var *inTex = getVertTexCoord( "outTexCoord" );
+   Var *inTex = getVertTexCoord( "texCoord" );
 
    // Sample the normal map.
    //
@@ -540,11 +913,11 @@ void TerrainNormalMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
    LangElement *texOp;
    if ( fd.features.hasFeature( MFT_TerrainSideProject, normalIndex ) )
    {
-      texOp = new GenOp( "mix( texture2D( @, @.yz ), texture2D( @, @.xz ), @.z )", 
+      texOp = new GenOp( "lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z )", 
          normalMap, inDet, normalMap, inDet, inTex );
    }
    else
-      texOp = new GenOp( "texture2D(@, @.xy)", normalMap, inDet );
+      texOp = new GenOp( "tex2D(@, @.xy)", normalMap, inDet );
 
    // create bump normal
    Var *bumpNorm = new Var;
@@ -556,7 +929,7 @@ void TerrainNormalMapFeatGLSL::processPix(   Vector<ShaderComponent*> &component
 
    // Normalize is done later... 
    // Note: The reverse mul order is intentional. Affine matrix.
-   meta->addStatement( new GenOp( "      @ = mix( @, @.xyz * @, min( @, @.w ) );\r\n", 
+   meta->addStatement( new GenOp( "      @ = lerp( @, tMul( @.xyz, @ ), min( @, @.w ) );\r\n", 
       gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet ) );
 
    // End the conditional block.
@@ -578,9 +951,11 @@ ShaderFeature::Resources TerrainNormalMapFeatGLSL::getResources( const MaterialF
    // We only need to process normals during the prepass.
    if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
    {
-      // If this is the first normal map then it
-      // will generate the worldToTanget transform.
-      if ( !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) )
+      // If this is the first normal map and there
+      // are no parallax features then we will 
+      // generate the worldToTanget transform.
+      if (  !fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
+            ( getProcessIndex() == 0 || !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) ) )
          res.numTexReg = 3;
 
       res.numTex = 1;
@@ -589,100 +964,11 @@ ShaderFeature::Resources TerrainNormalMapFeatGLSL::getResources( const MaterialF
    return res;
 }
 
-TerrainParallaxMapFeatGLSL::TerrainParallaxMapFeatGLSL()
-   : mIncludeDep( "shaders/common/gl/torque.glsl" )
-{
-   addDependency( &mIncludeDep );
-}
-
-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 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;
-}
-
-ShaderFeature::Resources TerrainParallaxMapFeatGLSL::getResources( const MaterialFeatureData &fd )
-{
-   Resources res;
-   
-   // If this is the first parallax feature then
-   // it will generate the tangetEye vector and 
-   // the worldToTanget transform.
-   if ( getProcessIndex() == 0 || !fd.features.hasFeature( MFT_TerrainParallaxMap, getProcessIndex() - 1 ) )
-      res.numTexReg = 4;
-   
-   // If this isn't the prepass then we will 
-   // be adding a normal map.
-   if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
-      res.numTex = 1;
-   
-   return res;
-}
-
 void TerrainLightMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList, 
                                           const MaterialFeatureData &fd )
 {
    // grab connector texcoord register
-   Var *inTex = (Var*)LangElement::find( "outTexCoord" );
+   Var *inTex = (Var*)LangElement::find( "texCoord" );
    if ( !inTex )
       return;
 
@@ -694,13 +980,23 @@ void TerrainLightMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentLis
    lightMap->sampler = true;
    lightMap->constNum = Var::getTexUnitNum();
 
-   // Create a 'lightMask' value which is read by
-   // RTLighting to mask out the directional lighting.
-   Var *lightMask = new Var;
-   lightMask->setType( "vec3" );
-   lightMask->setName( "lightMask" );
+   MultiLine *meta = new MultiLine;
 
-   output = new GenOp( "   @ = texture2D( @, @.xy ).rgb;\r\n", new DecOp( lightMask ), lightMap, inTex );
+   // 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;
 }
 
 ShaderFeature::Resources TerrainLightMapFeatGLSL::getResources( const MaterialFeatureData &fd )
@@ -721,7 +1017,7 @@ void TerrainAdditiveFeatGLSL::processPix( Vector<ShaderComponent*> &componentLis
    
    MultiLine *meta = new MultiLine;
 
-   meta->addStatement( new GenOp( "   if ( @ - 0.0001 < 0.0 ) discard;\r\n", blendTotal ) );
+   meta->addStatement( new GenOp( "   clip( @ - 0.0001 );\r\n", blendTotal ) );
    meta->addStatement( new GenOp( "   @.a = @;\r\n", color, blendTotal ) );
 
    output = meta;

+ 19 - 10
Engine/source/terrain/glsl/terrFeatureGLSL.h

@@ -30,6 +30,7 @@
 #include "shaderGen/langElement.h"
 #endif
 
+
 /// A shared base class for terrain features which
 /// includes some helper functions.
 class TerrainFeatGLSL : public ShaderFeatureGLSL
@@ -38,14 +39,18 @@ protected:
    
    Var* _getInDetailCoord(Vector<ShaderComponent*> &componentList );
    
+   Var* _getInMacroCoord(Vector<ShaderComponent*> &componentList );
+
    Var* _getNormalMapTex();
    
    static Var* _getUniformVar( const char *name, const char *type, ConstantSortPosition csp );
    
    Var* _getDetailIdStrengthParallax();
+   Var* _getMacroIdStrengthParallax();
       
 };
 
+
 class TerrainBaseMapFeatGLSL : public TerrainFeatGLSL
 {
 public:
@@ -61,10 +66,12 @@ public:
    virtual String getName() { return "Terrain Base Texture"; }
 };
 
+
 class TerrainDetailMapFeatGLSL : public TerrainFeatGLSL
 {
 protected:
 
+   ShaderIncludeDependency mTorqueDep;
    ShaderIncludeDependency mTerrainDep;
 
 public:
@@ -83,10 +90,17 @@ public:
 };
 
 
-class TerrainNormalMapFeatGLSL : public TerrainFeatGLSL
+class TerrainMacroMapFeatGLSL : public TerrainFeatGLSL
 {
+protected:
+
+   ShaderIncludeDependency mTorqueDep;
+   ShaderIncludeDependency mTerrainDep;
+
 public:
 
+   TerrainMacroMapFeatGLSL();
+
    virtual void processVert(  Vector<ShaderComponent*> &componentList,
                               const MaterialFeatureData &fd );
 
@@ -95,19 +109,14 @@ public:
 
    virtual Resources getResources( const MaterialFeatureData &fd );
 
-   virtual String getName() { return "Terrain Normal Texture"; }
+   virtual String getName() { return "Terrain Macro Texture"; }
 };
 
-class TerrainParallaxMapFeatGLSL : public TerrainFeatGLSL
-{
-protected:
-
-   ShaderIncludeDependency mIncludeDep;
 
+class TerrainNormalMapFeatGLSL : public TerrainFeatGLSL
+{
 public:
 
-   TerrainParallaxMapFeatGLSL();
-   
    virtual void processVert(  Vector<ShaderComponent*> &componentList,
                             const MaterialFeatureData &fd );
    
@@ -116,7 +125,7 @@ public:
    
    virtual Resources getResources( const MaterialFeatureData &fd );
    
-   virtual String getName() { return "Terrain Parallax Texture"; }
+   virtual String getName() { return "Terrain Normal Texture"; }
 };
 
 class TerrainLightMapFeatGLSL : public TerrainFeatGLSL

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно