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 ;
#endregion
namespace CodeImp.DoomBuilder.Rendering
{
2019-08-09 21:22:16 +00:00
internal class ShaderManager : IRenderResource , IDisposable
2009-04-19 18:07:22 +00:00
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
// Shaders
2019-08-13 00:43:01 +00:00
private VertexDeclaration flatvertexdecl ;
2019-08-13 02:12:04 +00:00
private VertexDeclaration worldvertexdecl ;
2009-04-19 18:07:22 +00:00
// Device
2019-08-09 21:22:16 +00:00
private RenderDevice device ;
2009-04-19 18:07:22 +00:00
// Disposing
2014-02-21 14:42:12 +00:00
private bool isdisposed ;
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
2019-08-13 00:43:01 +00:00
public VertexDeclaration FlatVertexDecl { get { return flatvertexdecl ; } }
2019-08-13 02:12:04 +00:00
public VertexDeclaration WorldVertexDecl { get { return worldvertexdecl ; } }
2009-04-19 18:07:22 +00:00
public bool IsDisposed { get { return isdisposed ; } }
2019-08-09 21:22:16 +00:00
internal RenderDevice D3DDevice { get { return device ; } }
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
2019-08-09 21:22:16 +00:00
public ShaderManager ( RenderDevice device )
2009-04-19 18:07:22 +00:00
{
// Initialize
this . device = device ;
// Load
ReloadResource ( ) ;
// Register as resource
device . RegisterResource ( this ) ;
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
// Disposer
public void Dispose ( )
{
// Not already disposed?
if ( ! isdisposed )
{
// Clean up
UnloadResource ( ) ;
// Unregister as resource
device . UnregisterResource ( this ) ;
// Done
device = null ;
isdisposed = true ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Resources
// Clean up resources
public void UnloadResource ( )
{
2019-08-13 00:43:01 +00:00
flatvertexdecl . Dispose ( ) ;
2019-08-13 02:12:04 +00:00
worldvertexdecl . Dispose ( ) ;
2009-04-19 18:07:22 +00:00
}
// Load resources
public void ReloadResource ( )
{
2019-08-13 00:43:01 +00:00
flatvertexdecl = new VertexDeclaration ( new VertexElement [ ] {
new VertexElement ( 0 , 0 , DeclarationType . Float3 , DeclarationUsage . Position ) ,
new VertexElement ( 0 , 12 , DeclarationType . Color , DeclarationUsage . Color ) ,
new VertexElement ( 0 , 16 , DeclarationType . Float2 , DeclarationUsage . TextureCoordinate )
} ) ;
2019-08-13 02:12:04 +00:00
worldvertexdecl = new VertexDeclaration ( new VertexElement [ ] {
new VertexElement ( 0 , 0 , DeclarationType . Float3 , DeclarationUsage . Position ) ,
new VertexElement ( 0 , 12 , DeclarationType . Color , DeclarationUsage . Color ) ,
new VertexElement ( 0 , 16 , DeclarationType . Float2 , DeclarationUsage . TextureCoordinate ) ,
new VertexElement ( 0 , 24 , DeclarationType . Float3 , DeclarationUsage . Normal )
} ) ;
}
2019-08-13 00:43:01 +00:00
#endregion
public void SetDisplay2DSettings ( float texelx , float texely , float fsaafactor , float alpha , bool bilinear )
{
Vector4 values = new Vector4 ( texelx , texely , fsaafactor , alpha ) ;
D3DDevice . SetUniform ( Uniform . rendersettings , values ) ;
Matrix world = D3DDevice . GetTransform ( TransformState . World ) ;
Matrix view = D3DDevice . GetTransform ( TransformState . View ) ;
D3DDevice . SetUniform ( Uniform . transformsettings , world * view ) ;
TextureFilter filter = ( bilinear ? TextureFilter . Linear : TextureFilter . Point ) ;
D3DDevice . SetUniform ( Uniform . filtersettings , ( int ) filter ) ;
}
2019-08-13 02:12:04 +00:00
public void SetThings2DSettings ( float alpha )
{
Vector4 values = new Vector4 ( 0.0f , 0.0f , 1.0f , alpha ) ;
D3DDevice . SetUniform ( Uniform . rendersettings , values ) ;
Matrix world = D3DDevice . GetTransform ( TransformState . World ) ;
Matrix view = D3DDevice . GetTransform ( TransformState . View ) ;
D3DDevice . SetUniform ( Uniform . transformsettings , world * view ) ;
}
//mxd. Used to render models
public void SetThings2DTransformSettings ( Matrix world )
{
Matrix view = D3DDevice . GetTransform ( TransformState . View ) ;
D3DDevice . SetUniform ( Uniform . transformsettings , world * view ) ;
}
public void SetWorld3DConstants ( bool bilinear , float maxanisotropy )
{
//mxd. It's still nice to have anisotropic filtering when texture filtering is disabled
TextureFilter magminfilter = ( bilinear ? TextureFilter . Linear : TextureFilter . Point ) ;
D3DDevice . SetUniform ( Uniform . magfiltersettings , magminfilter ) ;
D3DDevice . SetUniform ( Uniform . minfiltersettings , ( maxanisotropy > 1.0f ? TextureFilter . Anisotropic : magminfilter ) ) ;
D3DDevice . SetUniform ( Uniform . mipfiltersettings , ( bilinear ? TextureFilter . Linear : TextureFilter . None ) ) ; // [SB] use None, otherwise textures are still filtered
D3DDevice . SetUniform ( Uniform . maxanisotropysetting , maxanisotropy ) ;
}
2019-08-13 00:43:01 +00:00
}
2009-04-19 18:07:22 +00:00
}