|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ShaderEffectBase.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Base class for shader effects in Catel.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using Catel.Windows.Properties;
using log4net;
namespace Catel.Windows.Media.Effects
{
/// <summary>
/// Base class for shader effects in Catel.
/// </summary>
public abstract class ShaderEffectBase : ShaderEffect
{
#region Variables
/// <summary>
/// The <see cref="ILog">log</see> object.
/// </summary>
protected static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static PixelShader _pixelShader;
#endregion
#region Constructor & destructor
/// <summary>
/// Initializes static members of the <see cref="ShaderEffectBase"/> class.
/// </summary>
static ShaderEffectBase()
{
#if !SILVERLIGHT
// Subscribe to events so the software doesn't crash on invalid pixel shaders
PixelShader.InvalidPixelShaderEncountered += OnPixelShaderInvalidPixelShaderEncountered;
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Windows.Media.Effects.ShaderEffect"/> class.
/// </summary>
protected ShaderEffectBase()
{
InitializePixelShader();
UpdateShaderValue(InputProperty);
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether this shader effect is enabled.
/// </summary>
/// <value>
/// <c>true</c> if this shader effect is enabled; otherwise, <c>false</c>.
/// </value>
public bool IsEnabled { get { return StyleHelper.PixelShaderMode != PixelShaderMode.Off; } }
/// <summary>
/// Gets or sets the input brush.
/// </summary>
/// <value>The input.</value>
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}
/// <summary>
/// Property definition for <see cref="Input"/>.
/// </summary>
public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ShaderEffectBase), 0);
#endregion
#region Methods
/// <summary>
/// Initializes the pixel shader.
/// </summary>
private void InitializePixelShader()
{
if (_pixelShader == null)
{
if (!IsEnabled)
{
Log.Debug(TraceMessages.PixelShadersAreDisabled);
_pixelShader = new PixelShader() { UriSource = new Uri(@"/Catel.Windows;component/Windows/Media/Effects/EmptyEffect/EmptyEffect.ps", UriKind.RelativeOrAbsolute) };
#if !SILVERLIGHT
// Only render in software (so limited video cards can break the software)
_pixelShader.ShaderRenderMode = ShaderRenderMode.SoftwareOnly;
#endif
}
else
{
_pixelShader = CreatePixelShader();
#if !SILVERLIGHT
switch (StyleHelper.PixelShaderMode)
{
case PixelShaderMode.Auto:
_pixelShader.ShaderRenderMode = ShaderRenderMode.Auto;
break;
case PixelShaderMode.Hardware:
Log.Debug(TraceMessages.ForcingHardwareRenderingForPixelShader, GetType());
_pixelShader.ShaderRenderMode = ShaderRenderMode.HardwareOnly;
break;
case PixelShaderMode.Software:
Log.Debug(TraceMessages.ForcingSoftwareRenderingForPixelShader, GetType());
_pixelShader.ShaderRenderMode = ShaderRenderMode.SoftwareOnly;
break;
}
#endif
}
}
PixelShader = _pixelShader;
}
/// <summary>
/// Creates the pixel shader.
/// </summary>
/// <returns><see cref="PixelShader"/>.</returns>
protected abstract PixelShader CreatePixelShader();
#if !SILVERLIGHT
/// <summary>
/// Handles the InvalidPixelShaderEncountered event of the PixelShader control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
/// <remarks>
/// This is added to prevent a real crash on the app because of an invalid pixel shader.
/// </remarks>
private static void OnPixelShaderInvalidPixelShaderEncountered(object sender, EventArgs e)
{
Log.Warn(TraceMessages.InvalidPixelShaderEncountered);
}
#endif
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.