2009-01-28 16:18:34 +00:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Security;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SFML
|
|
|
|
{
|
|
|
|
namespace Graphics
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Wrapper for pixel shaders
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-11-03 09:04:40 +00:00
|
|
|
public class Shader : ObjectBase
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Default constructor (invalid shader)
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <exception cref="LoadingFailedException" />
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-11-03 09:04:40 +00:00
|
|
|
public Shader() :
|
|
|
|
base(sfShader_Create())
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
|
|
|
if (This == IntPtr.Zero)
|
2009-11-03 09:04:40 +00:00
|
|
|
throw new LoadingFailedException("shader");
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Load the shader from a file
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="filename">Path of the shader file to load</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <exception cref="LoadingFailedException" />
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-11-03 09:04:40 +00:00
|
|
|
public Shader(string filename) :
|
|
|
|
base(sfShader_CreateFromFile(filename))
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
|
|
|
if (This == IntPtr.Zero)
|
2009-11-03 09:04:40 +00:00
|
|
|
throw new LoadingFailedException("shader", filename);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Load the shader from a text in memory
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="shader">String containing the shader code</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <exception cref="LoadingFailedException" />
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-11-03 09:04:40 +00:00
|
|
|
void LoadFromString(string shader)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-11-03 09:04:40 +00:00
|
|
|
SetThis(sfShader_CreateFromMemory(shader));
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
if (This == IntPtr.Zero)
|
2009-11-03 09:04:40 +00:00
|
|
|
throw new LoadingFailedException("shader");
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Change a vector2 parameter of the shader
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="name">Name of the parameter in the shader</param>
|
|
|
|
/// <param name="v">Value of the parameter</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void SetParameter(string name, Vector2 v)
|
|
|
|
{
|
|
|
|
SetParameter(name, v.X, v.Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Change a 1-component parameter of the shader
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">Name of the parameter in the shader</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <param name="x">Value of the parameter</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void SetParameter(string name, float x)
|
|
|
|
{
|
2009-11-03 09:04:40 +00:00
|
|
|
sfShader_SetParameter1(This, name, x);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Change a 2-component parameter of the shader
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="name">Name of the parameter in the shader</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <param name="x">X component of the value</param>
|
|
|
|
/// <param name="y">Y component of the value</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void SetParameter(string name, float x, float y)
|
|
|
|
{
|
2009-11-03 09:04:40 +00:00
|
|
|
sfShader_SetParameter2(This, name, x, y);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Change a 3-component parameter of the shader
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="name">Name of the parameter in the shader</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <param name="x">X component of the value</param>
|
|
|
|
/// <param name="y">Y component of the value</param>
|
|
|
|
/// <param name="z">Z component of the value</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void SetParameter(string name, float x, float y, float z)
|
|
|
|
{
|
2009-11-03 09:04:40 +00:00
|
|
|
sfShader_SetParameter3(This, name, x, y, z);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Change a 4-component parameter of the shader
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="name">Name of the parameter in the shader</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <param name="x">X component of the value</param>
|
|
|
|
/// <param name="y">Y component of the value</param>
|
|
|
|
/// <param name="z">Z component of the value</param>
|
|
|
|
/// <param name="w">W component of the value</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void SetParameter(string name, float x, float y, float z, float w)
|
|
|
|
{
|
2009-11-03 09:04:40 +00:00
|
|
|
sfShader_SetParameter4(This, name, x, y, z, w);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Set a texture parameter
|
|
|
|
/// </summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// <param name="name">Name of the texture in the shader</param>
|
|
|
|
/// <param name="texture">Image to set (pass null to use the texture of the object being drawn)</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void SetTexture(string name, Image texture)
|
|
|
|
{
|
|
|
|
myTextures[name] = texture;
|
2009-11-03 09:04:40 +00:00
|
|
|
sfShader_SetTexture(This, name, texture != null ? texture.This : IntPtr.Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Bind the shader for rendering
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void Bind()
|
|
|
|
{
|
|
|
|
sfShader_Bind(This);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Unbind the shader
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public void Unbind()
|
|
|
|
{
|
|
|
|
sfShader_Unbind(This);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Tell whether or not the system supports shaders
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public static bool IsAvailable
|
|
|
|
{
|
|
|
|
get {return sfShader_IsAvailable();}
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-11-03 09:04:40 +00:00
|
|
|
/// Special image representing the texture used by the object being drawn
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-11-03 09:04:40 +00:00
|
|
|
public static Image CurrentTexture
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-11-03 09:04:40 +00:00
|
|
|
get {return null;}
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Handle the destruction of the object
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="disposing">Is the GC disposing the object, or is it an explicit call ?</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
protected override void Destroy(bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposing)
|
|
|
|
Context.Global.SetActive(true);
|
|
|
|
|
|
|
|
myTextures.Clear();
|
2009-11-03 09:04:40 +00:00
|
|
|
sfShader_Destroy(This);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
if (!disposing)
|
|
|
|
Context.Global.SetActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary<string, Image> myTextures = new Dictionary<string, Image>();
|
|
|
|
|
|
|
|
#region Imports
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern IntPtr sfShader_Create();
|
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
|
|
|
static extern IntPtr sfShader_CreateFromFile(string Filename);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern IntPtr sfShader_CreateFromMemory(string Shader);
|
|
|
|
|
2009-01-28 16:18:34 +00:00
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_Destroy(IntPtr Shader);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_SetParameter1(IntPtr Shader, string Name, float X);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_SetParameter2(IntPtr Shader, string Name, float X, float Y);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_SetParameter3(IntPtr Shader, string Name, float X, float Y, float Z);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_SetParameter4(IntPtr Shader, string Name, float X, float Y, float Z, float W);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_SetTexture(IntPtr Shader, string Name, IntPtr Texture);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_Bind(IntPtr Shader);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern void sfShader_Unbind(IntPtr Shader);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-11-03 09:04:40 +00:00
|
|
|
static extern bool sfShader_IsAvailable();
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|