Beispiel #1
0
GFXShader* ShaderGen::getShader( const MaterialFeatureData &featureData, const GFXVertexFormat *vertexFormat, const Vector<GFXShaderMacro> *macros )
{
   PROFILE_SCOPE( ShaderGen_GetShader );

   const FeatureSet &features = featureData.codify();

   // Build a description string from the features
   // and vertex format combination ( and macros ).
   String shaderDescription = vertexFormat->getDescription() + features.getDescription();
   if ( macros && !macros->empty() )
   {
      String macroStr;
      GFXShaderMacro::stringize( *macros, &macroStr );
      shaderDescription += macroStr;
   }

   // Generate a single 64bit hash from the description string.
   //
   // Don't get paranoid!  This has 1 in 18446744073709551616
   // chance for collision... it won't happen in this lifetime.
   //
   U64 hash = Torque::hash64( (const U8*)shaderDescription.c_str(), shaderDescription.length(), 0 );
   hash = convertHostToLEndian(hash);
   U32 high = (U32)( hash >> 32 );
   U32 low = (U32)( hash & 0x00000000FFFFFFFF );
   String cacheKey = String::ToString( "%x%x", high, low );

   // return shader if exists
   GFXShader *match = mProcShaders[cacheKey];
   if ( match )
      return match;

   // if not, then create it
   char vertFile[256];
   char pixFile[256];
   F32  pixVersion;

   Vector<GFXShaderMacro> shaderMacros;
   shaderMacros.push_back( GFXShaderMacro( "TORQUE_SHADERGEN" ) );
   if ( macros )
      shaderMacros.merge( *macros );
   generateShader( featureData, vertFile, pixFile, &pixVersion, vertexFormat, cacheKey, shaderMacros );

   GFXShader *shader = GFX->createShader();
   shader->mInstancingFormat.copy( mInstancingFormat ); // TODO: Move to init() below!
   if ( !shader->init( vertFile, pixFile, pixVersion, shaderMacros ) )
   {
      delete shader;
      return NULL;
   }

   mProcShaders[cacheKey] = shader;

   return shader;
}
Beispiel #2
0
bool ScatterSky::_initShader()
{
   ShaderData *shaderData;
   if ( !Sim::findObject( "ScatterSkyShaderData", shaderData ) )
   {
      Con::warnf( "ScatterSky::_initShader - failed to locate shader ScatterSkyShaderData!" );
      return false;
   }
      Vector<GFXShaderMacro> macros;
   if ( mColorizeAmt )
      macros.push_back( GFXShaderMacro( "USE_COLORIZE" ) );

   mShader = shaderData->getShader( macros );

   if ( !mShader )
      return false;

   if ( mStateBlock.isNull() )
   {
      GFXStateBlockData *data = NULL;
      if ( !Sim::findObject( "ScatterSkySBData", data ) )
         Con::warnf( "ScatterSky::_initShader - failed to locate ScatterSkySBData!" );
      else
         mStateBlock = GFX->createStateBlock( data->getState() );
	  }

   if ( !mStateBlock )
      return false;

   mShaderConsts = mShader->allocConstBuffer();
   mModelViewProjSC = mShader->getShaderConstHandle( "$modelView" );

   // Camera height, cam height squared, scale and scale over depth.
   mMiscSC = mShader->getShaderConstHandle( "$misc" );

   // Inner and out radius, and inner and outer radius squared.
   mSphereRadiiSC = mShader->getShaderConstHandle( "$sphereRadii" );

   // Rayleigh sun brightness, mie sun brightness and 4 * PI * coefficients.
   mScatteringCoefficientsSC = mShader->getShaderConstHandle( "$scatteringCoeffs" );
   mCamPosSC = mShader->getShaderConstHandle( "$camPos" );
   mLightDirSC = mShader->getShaderConstHandle( "$lightDir" );
   mSunDirSC = mShader->getShaderConstHandle( "$sunDir" );
   mNightColorSC = mShader->getShaderConstHandle( "$nightColor" );
   mInverseWavelengthSC = mShader->getShaderConstHandle( "$invWaveLength" );
   mNightInterpolantAndExposureSC = mShader->getShaderConstHandle( "$nightInterpAndExposure" );
   mUseCubemapSC = mShader->getShaderConstHandle( "$useCubemap" );
   mColorizeSC = mShader->getShaderConstHandle( "$colorize" );

   return true;
}
AdvancedLightBinManager::LightMaterialInfo* AdvancedLightBinManager::_getLightMaterial(   LightInfo::Type lightType, 
                                                                                          ShadowType shadowType, 
                                                                                          bool useCookieTex )
{
   PROFILE_SCOPE( AdvancedLightBinManager_GetLightMaterial );

   // Build the key.
   const LightMatKey key( lightType, shadowType, useCookieTex );

   // See if we've already built this one.
   LightMatTable::Iterator iter = mLightMaterials.find( key );
   if ( iter != mLightMaterials.end() )
      return iter->value;
   
   // If we got here we need to build a material for 
   // this light+shadow combination.

   LightMaterialInfo *info = NULL;

   // First get the light material name and make sure
   // this light has a material in the first place.
   const String &lightMatName = smLightMatNames[ lightType ];
   if ( lightMatName.isNotEmpty() )
   {
      Vector<GFXShaderMacro> shadowMacros;

      // Setup the shadow type macros for this material.
      if ( shadowType == ShadowType_None )
         shadowMacros.push_back( GFXShaderMacro( "NO_SHADOW" ) );
      else
      {
         shadowMacros.push_back( GFXShaderMacro( smShadowTypeMacro[ shadowType ] ) );

         // Do we need to do shadow filtering?
         if ( smShadowFilterMode != ShadowFilterMode_None )
         {
            shadowMacros.push_back( GFXShaderMacro( "SOFTSHADOW" ) );
           
            const F32 SM = GFX->getPixelShaderVersion();
            if ( SM >= 3.0f && smShadowFilterMode == ShadowFilterMode_SoftShadowHighQuality )
               shadowMacros.push_back( GFXShaderMacro( "SOFTSHADOW_HIGH_QUALITY" ) );
         }
      }
   
      if ( useCookieTex )
         shadowMacros.push_back( GFXShaderMacro( "USE_COOKIE_TEX" ) );

      // Its safe to add the PSSM debug macro to all the materials.
      if ( smPSSMDebugRender )
         shadowMacros.push_back( GFXShaderMacro( "PSSM_DEBUG_RENDER" ) );

      // If its a vector light see if we can enable SSAO.
      if ( lightType == LightInfo::Vector && smUseSSAOMask )
         shadowMacros.push_back( GFXShaderMacro( "USE_SSAO_MASK" ) );

      // Now create the material info object.
      info = new LightMaterialInfo( lightMatName, smLightMatVertex[ lightType ], shadowMacros );
   }

   // Push this into the map and return it.
   mLightMaterials.insertUnique( key, info );
   return info;
}