2011-10-31 05:36:24 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2015-10-14 23:59:27 +02:00
|
|
|
using System.Linq;
|
2012-01-16 15:03:28 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2012-09-05 19:50:10 +00:00
|
|
|
using ANX.Framework.NonXNA;
|
2012-10-10 19:26:53 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
2011-10-31 05:36:24 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.Graphics
|
|
|
|
{
|
2012-10-13 10:51:27 +00:00
|
|
|
[PercentageComplete(100)]
|
2012-10-10 19:26:53 +00:00
|
|
|
[Developer("Glatzemann")]
|
|
|
|
[TestState(TestStateAttribute.TestState.Untested)]
|
|
|
|
public class Effect : GraphicsResource, IGraphicsResource
|
2012-09-05 19:50:10 +00:00
|
|
|
{
|
|
|
|
#region Private
|
|
|
|
private INativeEffect nativeEffect;
|
2012-10-13 10:51:27 +00:00
|
|
|
private readonly byte[] byteCode;
|
2012-09-05 19:50:10 +00:00
|
|
|
private EffectSourceLanguage sourceLanguage;
|
2015-10-14 23:59:27 +02:00
|
|
|
private EffectTechnique currentTechnique;
|
2012-09-05 19:50:10 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public
|
|
|
|
internal INativeEffect NativeEffect
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.nativeEffect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:59:27 +02:00
|
|
|
public EffectTechnique CurrentTechnique
|
|
|
|
{
|
|
|
|
get { return this.currentTechnique; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == null)
|
|
|
|
throw new ArgumentNullException("value");
|
|
|
|
|
|
|
|
if (!Techniques.Contains(value))
|
|
|
|
throw new InvalidOperationException("CurrentTechnique must be a technique of the same effect instance.");
|
|
|
|
|
|
|
|
this.currentTechnique = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-13 10:51:27 +00:00
|
|
|
public EffectParameterCollection Parameters { get; private set; }
|
|
|
|
public EffectTechniqueCollection Techniques { get; private set; }
|
|
|
|
#endregion
|
2012-09-05 19:50:10 +00:00
|
|
|
|
|
|
|
protected Effect(Effect cloneSource)
|
|
|
|
: this(cloneSource.GraphicsDevice, cloneSource.byteCode)
|
|
|
|
{
|
2015-10-14 23:59:27 +02:00
|
|
|
CreateNativeEffect(cloneSource.sourceLanguage);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Effect(GraphicsDevice graphicsDevice, byte[] byteCode)
|
|
|
|
: this(graphicsDevice, byteCode, EffectSourceLanguage.HLSL_FX)
|
|
|
|
{
|
2015-10-14 23:59:27 +02:00
|
|
|
CreateNativeEffect(EffectSourceLanguage.HLSL_FX);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Effect(GraphicsDevice graphicsDevice, byte[] byteCode, EffectSourceLanguage sourceLanguage)
|
|
|
|
: base(graphicsDevice)
|
|
|
|
{
|
|
|
|
this.byteCode = new byte[byteCode.Length];
|
|
|
|
Array.Copy(byteCode, this.byteCode, byteCode.Length);
|
|
|
|
|
|
|
|
base.GraphicsDevice.ResourceCreated += GraphicsDevice_ResourceCreated;
|
|
|
|
base.GraphicsDevice.ResourceDestroyed += GraphicsDevice_ResourceDestroyed;
|
|
|
|
|
|
|
|
CreateNativeEffect(sourceLanguage);
|
|
|
|
|
2012-10-13 10:51:27 +00:00
|
|
|
this.CurrentTechnique = this.Techniques[0];
|
2012-09-05 19:50:10 +00:00
|
|
|
|
|
|
|
this.sourceLanguage = sourceLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region GraphicsDevice_ResourceCreated
|
|
|
|
private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e)
|
|
|
|
{
|
|
|
|
if (nativeEffect != null)
|
|
|
|
{
|
|
|
|
nativeEffect.Dispose();
|
|
|
|
nativeEffect = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateNativeEffect(this.sourceLanguage);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GraphicsDevice_ResourceDestroyed
|
|
|
|
private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e)
|
|
|
|
{
|
|
|
|
if (nativeEffect != null)
|
|
|
|
{
|
|
|
|
nativeEffect.Dispose();
|
|
|
|
nativeEffect = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-10-13 10:51:27 +00:00
|
|
|
#region Clone
|
2012-09-05 19:50:10 +00:00
|
|
|
public virtual Effect Clone()
|
|
|
|
{
|
2012-10-13 10:51:27 +00:00
|
|
|
return new Effect(this);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2015-10-14 23:59:27 +02:00
|
|
|
protected internal virtual void OnApply()
|
|
|
|
{
|
|
|
|
}
|
2012-09-05 19:50:10 +00:00
|
|
|
|
2012-10-13 10:51:27 +00:00
|
|
|
#region Dispose
|
2015-10-04 21:30:00 +02:00
|
|
|
protected override void Dispose(bool disposeManaged)
|
2012-09-05 19:50:10 +00:00
|
|
|
{
|
2015-10-04 21:30:00 +02:00
|
|
|
if (disposeManaged)
|
|
|
|
{
|
|
|
|
if (nativeEffect != null)
|
|
|
|
{
|
|
|
|
nativeEffect.Dispose();
|
|
|
|
nativeEffect = null;
|
|
|
|
}
|
2015-10-14 23:59:27 +02:00
|
|
|
|
|
|
|
if (Parameters != null)
|
|
|
|
{
|
|
|
|
foreach (var parameter in Parameters)
|
|
|
|
parameter.NativeParameter.Dispose();
|
|
|
|
Parameters = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Techniques != null)
|
|
|
|
{
|
|
|
|
foreach (var technique in Techniques)
|
|
|
|
technique.NativeTechnique.Dispose();
|
|
|
|
Techniques = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
base.GraphicsDevice.ResourceCreated -= GraphicsDevice_ResourceCreated;
|
|
|
|
base.GraphicsDevice.ResourceDestroyed -= GraphicsDevice_ResourceDestroyed;
|
2015-10-04 21:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
base.Dispose(disposeManaged);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region CreateNativeEffect
|
|
|
|
private void CreateNativeEffect(EffectSourceLanguage sourceLanguage)
|
|
|
|
{
|
|
|
|
var creator = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>();
|
|
|
|
|
|
|
|
if (creator.IsLanguageSupported(sourceLanguage))
|
|
|
|
{
|
|
|
|
this.nativeEffect = creator.CreateEffect(GraphicsDevice, this, new MemoryStream(this.byteCode, false));
|
2015-10-14 23:59:27 +02:00
|
|
|
this.Techniques = new EffectTechniqueCollection(this.nativeEffect.Techniques);
|
|
|
|
this.Parameters = new EffectParameterCollection(this.nativeEffect.Parameters);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new InvalidOperationException("couldn't create " + sourceLanguage + " native effect using RenderSystem " +
|
|
|
|
creator.Name);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|