Example #1
0
	void PostProcessing::ReLoadShader()
	{

		RasterizerState noCull(RasterizerState::CULL_MODE::NONE, RasterizerState::FILL_MODE::SOLID);
		DepthStencilState noDepthTest(DepthStencilState::COMPARISON_FUNC::ALWAYS, false);

		// Shader setup
		delete m_screenOutputEffect;
		m_screenOutputEffect = new Effect("shader/postprocessing/screentri.vs", "shader/postprocessing/output.ps", "", 
											m_ambientOcclusionConfig != AmbientOcclusionConfig::OFF ? "#define AMBIENT_OCCULSION" : "");
		m_screenOutputEffect->SetRasterizerState(noCull);
		m_screenOutputEffect->SetDepthStencilState(noDepthTest);
		m_screenOutputEffect->BindUniformBuffer(Resources::GetUBO(UniformBuffers::GLOBAL));
		m_screenOutputEffect->BindTexture("screenTex", 0, Resources::GetSamplerState(SamplerStates::POINT));
		m_screenOutputEffect->BindTexture("ambientOcclusionMap", 1, Resources::GetSamplerState(SamplerStates::LINEAR_NOMIPMAP));
		
		delete m_ambientOcclusionEffect;
		m_ambientOcclusionEffect = nullptr;
		delete m_ambientOcclusionBlurXEffect;
		m_ambientOcclusionBlurXEffect = nullptr;
		delete m_ambientOcclusionBlurYEffect;
		m_ambientOcclusionBlurYEffect = nullptr;

		if (m_ambientOcclusionConfig != AmbientOcclusionConfig::OFF)
		{
			static const char* qualityDefines[] = { "#define LOWQ", "#define MEDIUMQ", "#define HIGHQ" };
			const std::string curQualityDefine = qualityDefines[static_cast<unsigned int>(m_ambientOcclusionConfig)-1];

			m_ambientOcclusionEffect = new Effect("shader/postprocessing/screentri.vs", "shader/postprocessing/ambientocclusion.ps", "", curQualityDefine);
			m_ambientOcclusionEffect->SetRasterizerState(noCull);
			m_ambientOcclusionEffect->SetDepthStencilState(noDepthTest);
			m_ambientOcclusionEffect->BindTexture("depthTex", 0, Resources::GetSamplerState(SamplerStates::POINT));
			m_ambientOcclusionEffect->BindUniformBuffer(Resources::GetUBO(UniformBuffers::CAMERA));

			m_ambientOcclusionBlurXEffect = new Effect("shader/postprocessing/screentri.vs", "shader/postprocessing/ambientocclusionblur.ps", "", curQualityDefine);
			m_ambientOcclusionBlurXEffect->SetRasterizerState(noCull);
			m_ambientOcclusionBlurXEffect->SetDepthStencilState(noDepthTest);
			m_ambientOcclusionBlurXEffect->BindUniformBuffer(Resources::GetUBO(UniformBuffers::GLOBAL));
			m_ambientOcclusionBlurXEffect->BindUniformBuffer(Resources::GetUBO(UniformBuffers::CAMERA));
			m_ambientOcclusionBlurXEffect->BindTexture("depthTex", 0, Resources::GetSamplerState(SamplerStates::POINT));
			m_ambientOcclusionBlurXEffect->BindTexture("ambientOcclusionMap", 1, Resources::GetSamplerState(SamplerStates::POINT));

			m_ambientOcclusionBlurYEffect = new Effect("shader/postprocessing/screentri.vs", "shader/postprocessing/ambientocclusionblur.ps", "", "#define BLUR_Y_FIN\n" + curQualityDefine);
			m_ambientOcclusionBlurYEffect->SetRasterizerState(noCull);
			m_ambientOcclusionBlurYEffect->SetDepthStencilState(noDepthTest);
			m_ambientOcclusionBlurYEffect->BindUniformBuffer(Resources::GetUBO(UniformBuffers::GLOBAL));
			m_ambientOcclusionBlurYEffect->BindUniformBuffer(Resources::GetUBO(UniformBuffers::CAMERA));
			m_ambientOcclusionBlurYEffect->BindTexture("depthTex", 0, Resources::GetSamplerState(SamplerStates::POINT));
			m_ambientOcclusionBlurYEffect->BindTexture("ambientOcclusionMap", 1, Resources::GetSamplerState(SamplerStates::POINT));
		}
	}
void ShadowMaterialHook::init( BaseMatInstance *inMat )
{
   if( !inMat->isValid() )
      return;

   // Tweak the feature data to include just what we need.
   FeatureSet features;
   features.addFeature( MFT_VertTransform );
   features.addFeature( MFT_DiffuseMap );
   features.addFeature( MFT_TexAnim );
   features.addFeature( MFT_AlphaTest );
   features.addFeature( MFT_Visibility );

   // Actually we want to include features from the inMat
   // if they operate on the preTransform verts so things
   // like wind/deformation effects will also affect the shadow.
   const FeatureSet &inFeatures = inMat->getFeatures();
   for ( U32 i = 0; i < inFeatures.getCount(); i++ )
   {      
      const FeatureType& ft = inFeatures.getAt(i);
      
      if ( ft.getGroup() == MFG_PreTransform )
         features.addFeature( ft );
   }

   // Do instancing in shadows if we can.
   if ( inFeatures.hasFeature( MFT_UseInstancing ) )
      features.addFeature( MFT_UseInstancing );

   Material *shadowMat = (Material*)inMat->getMaterial();
   if ( dynamic_cast<CustomMaterial*>( shadowMat ) )
   {
      // This is a custom material... who knows what it really does, but
      // if it wasn't already filtered out of the shadow render then just
      // give it some default depth out material.
      shadowMat = MATMGR->getMaterialDefinitionByName( "AL_DefaultShadowMaterial" );
   }

   // By default we want to disable some states
   // that the material might enable for us.
   GFXStateBlockDesc forced;
   forced.setBlend( false );
   forced.setAlphaTest( false );

   // We should force on zwrite as the prepass
   // will disable it by default.
   forced.setZReadWrite( true, true );
   
   // TODO: Should we render backfaces for 
   // shadows or does the ESM take care of 
   // all our acne issues?
   //forced.setCullMode( GFXCullCW );

   // Vector, and spotlights use the same shadow material.
   BaseMatInstance *newMat = new ShadowMatInstance( shadowMat );
   newMat->setUserObject( inMat->getUserObject() );
   newMat->getFeaturesDelegate().bind( &ShadowMaterialHook::_overrideFeatures );
   newMat->addStateBlockDesc( forced );
   if( !newMat->init( features, inMat->getVertexFormat() ) )
   {
      SAFE_DELETE( newMat );
      newMat = MATMGR->createWarningMatInstance();
   }
   
   mShadowMat[ShadowType_Spot] = newMat;

   newMat = new ShadowMatInstance( shadowMat );
   newMat->setUserObject( inMat->getUserObject() );
   newMat->getFeaturesDelegate().bind( &ShadowMaterialHook::_overrideFeatures );
   forced.setCullMode( GFXCullCW );   
   newMat->addStateBlockDesc( forced );
   forced.cullDefined = false;
   newMat->addShaderMacro( "CUBE_SHADOW_MAP", "" );
   newMat->init( features, inMat->getVertexFormat() );
   mShadowMat[ShadowType_CubeMap] = newMat;
   
   // A dual paraboloid shadow rendered in a single draw call.
   features.addFeature( MFT_ParaboloidVertTransform );
   features.addFeature( MFT_IsSinglePassParaboloid );
   features.removeFeature( MFT_VertTransform );
   newMat = new ShadowMatInstance( shadowMat );
   newMat->setUserObject( inMat->getUserObject() );
   GFXStateBlockDesc noCull( forced );
   noCull.setCullMode( GFXCullNone );
   newMat->addStateBlockDesc( noCull );
   newMat->getFeaturesDelegate().bind( &ShadowMaterialHook::_overrideFeatures );
   newMat->init( features, inMat->getVertexFormat() );
   mShadowMat[ShadowType_DualParaboloidSinglePass] = newMat;

   // Regular dual paraboloid shadow.
   features.addFeature( MFT_ParaboloidVertTransform );
   features.removeFeature( MFT_IsSinglePassParaboloid );
   features.removeFeature( MFT_VertTransform );
   newMat = new ShadowMatInstance( shadowMat );
   newMat->setUserObject( inMat->getUserObject() );
   newMat->addStateBlockDesc( forced );
   newMat->getFeaturesDelegate().bind( &ShadowMaterialHook::_overrideFeatures );
   newMat->init( features, inMat->getVertexFormat() );
   mShadowMat[ShadowType_DualParaboloid] = newMat;

   /*
   // A single paraboloid shadow.
   newMat = new ShadowMatInstance( startMatInstance );
   GFXStateBlockDesc noCull;
   noCull.setCullMode( GFXCullNone );
   newMat->addStateBlockDesc( noCull );
   newMat->getFeaturesDelegate().bind( &ShadowMaterialHook::_overrideFeatures );
   newMat->init( features, globalFeatures, inMat->getVertexFormat() );
   mShadowMat[ShadowType_DualParaboloidSinglePass] = newMat;
   */
}