2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using SlimDX.Direct3D9;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Rendering
|
|
|
|
{
|
|
|
|
internal abstract class D3DShader
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// The manager
|
|
|
|
protected ShaderManager manager;
|
|
|
|
|
|
|
|
// The effect
|
|
|
|
protected Effect effect;
|
|
|
|
|
|
|
|
// The vertex declaration
|
|
|
|
protected VertexDeclaration vertexdecl;
|
|
|
|
|
|
|
|
// Disposing
|
2014-02-21 14:42:12 +00:00
|
|
|
protected bool isdisposed;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
// Disposing
|
|
|
|
public bool IsDisposed { get { return isdisposed; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2015-05-27 12:38:03 +00:00
|
|
|
protected D3DShader(ShaderManager manager)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
this.manager = manager;
|
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disposer
|
|
|
|
public virtual void Dispose()
|
|
|
|
{
|
|
|
|
// Not already disposed?
|
|
|
|
if(!isdisposed)
|
|
|
|
{
|
|
|
|
// Clean up
|
|
|
|
manager = null;
|
|
|
|
if(effect != null) effect.Dispose();
|
|
|
|
vertexdecl.Dispose();
|
|
|
|
|
|
|
|
// Done
|
|
|
|
isdisposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This loads an effect
|
|
|
|
protected Effect LoadEffect(string fxfile)
|
|
|
|
{
|
|
|
|
// Return null when not using shaders
|
|
|
|
if(!manager.Enabled) return null;
|
2013-12-20 09:24:43 +00:00
|
|
|
|
|
|
|
Effect fx;
|
2014-09-11 21:53:31 +00:00
|
|
|
string errors = string.Empty;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Load the resource
|
2013-12-20 09:24:43 +00:00
|
|
|
Stream fxdata = General.ThisAssembly.GetManifestResourceStream("CodeImp.DoomBuilder.Resources." + fxfile);
|
2009-04-19 18:07:22 +00:00
|
|
|
fxdata.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Compile effect
|
|
|
|
fx = Effect.FromStream(General.Map.Graphics.Device, fxdata, null, null, null, ShaderFlags.None, null, out errors);
|
2009-05-03 20:21:52 +00:00
|
|
|
if(!string.IsNullOrEmpty(errors))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
throw new Exception("Errors in effect file " + fxfile + ": " + errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception)
|
|
|
|
{
|
|
|
|
// Compiling failed, try with debug information
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Compile effect
|
|
|
|
fx = Effect.FromStream(General.Map.Graphics.Device, fxdata, null, null, null, ShaderFlags.Debug, null, out errors);
|
2009-05-03 20:21:52 +00:00
|
|
|
if(!string.IsNullOrEmpty(errors))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
throw new Exception("Errors in effect file " + fxfile + ": " + errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
// No debug information, just crash
|
2014-09-11 21:53:31 +00:00
|
|
|
throw new Exception(e.GetType().Name + " while loading effect " + fxfile + ": " + e.Message + "\nInitial message: " + errors);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fxdata.Dispose();
|
|
|
|
|
|
|
|
// Set the technique to use
|
|
|
|
fx.Technique = manager.ShaderTechnique;
|
|
|
|
|
|
|
|
// Return result
|
|
|
|
return fx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This applies the shader
|
|
|
|
public void Begin()
|
|
|
|
{
|
|
|
|
// Set vertex declaration
|
|
|
|
General.Map.Graphics.Device.VertexDeclaration = vertexdecl;
|
|
|
|
|
|
|
|
// Set effect
|
2009-05-03 20:21:52 +00:00
|
|
|
if(manager.Enabled) effect.Begin(FX.DoNotSaveState);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This begins a pass
|
|
|
|
public virtual void BeginPass(int index)
|
|
|
|
{
|
2009-05-03 20:21:52 +00:00
|
|
|
if(manager.Enabled) effect.BeginPass(index);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This ends a pass
|
|
|
|
public void EndPass()
|
|
|
|
{
|
2009-05-03 20:21:52 +00:00
|
|
|
if(manager.Enabled) effect.EndPass();
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This ends te shader
|
|
|
|
public void End()
|
|
|
|
{
|
2009-05-03 20:21:52 +00:00
|
|
|
if(manager.Enabled) effect.End();
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This applies properties during a pass
|
|
|
|
public void ApplySettings()
|
|
|
|
{
|
2009-05-03 20:21:52 +00:00
|
|
|
if(manager.Enabled) effect.CommitChanges();
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|