c-o
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using StgSharp.Graphics.GL;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics.ShaderEdit
|
||||
{
|
||||
public interface IglConvertable
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void SetAllUniforms();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void GainAllUniforms(ShaderProgram source, params string[] uniformName);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void DisplayGLtypeDefinition();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public ShaderStruct GetConvertedGLtype();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="Shader.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp;
|
||||
using StgSharp.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.Graphics.GL
|
||||
{
|
||||
public abstract partial class glRenderStream : RenderStream
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected sealed override Shader CreateShaderSegment(ShaderType type, int count)
|
||||
{
|
||||
return new Shader( glFunc.CreateShaderSet(count, type), type);
|
||||
}
|
||||
/// <summary>
|
||||
/// Create an instance of <see cref="Shader"/> program
|
||||
/// </summary>
|
||||
/// <returns>New instance of <see cref="Shader"/> instance. </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected sealed override ShaderProgram CreateShaderProgram()
|
||||
{
|
||||
return new ShaderProgram(this);
|
||||
}
|
||||
}
|
||||
|
||||
public class Shader : ISSDSerializable
|
||||
{
|
||||
|
||||
private readonly IntPtr context;
|
||||
internal readonly glHandleSet handle;
|
||||
public readonly ShaderType type;
|
||||
|
||||
public SerializableTypeCode SSDTypeCode => SerializableTypeCode.Shader;
|
||||
|
||||
internal unsafe Shader(glHandleSet handle, ShaderType usage)
|
||||
{
|
||||
this.handle = handle;
|
||||
context = glFunc.CurrentContextHandle;
|
||||
type = usage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attach one shader code to a shader program.
|
||||
/// </summary>
|
||||
/// <param name="target">Shader program to attach</param>
|
||||
/// <param name="index">Index of shader code in current shader code set.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AttachTo(int index, ShaderProgram target)
|
||||
{
|
||||
glFunc.AttachShader(target.handle, handle[index]);
|
||||
}
|
||||
|
||||
public static byte[] FromCodeFile(string fileRoute)
|
||||
{
|
||||
string str = File.ReadAllText(fileRoute);
|
||||
if (string.IsNullOrEmpty(str))
|
||||
{
|
||||
throw new InvalidOperationException($"Cannot load and GLSL code from {fileRoute}");
|
||||
}
|
||||
return Encoding.UTF8.GetBytes(str);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Compile(int index, byte[] codeStream)
|
||||
{
|
||||
if (codeStream == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(codeStream));
|
||||
}
|
||||
glFunc.LoadShaderSource(handle[index], codeStream);
|
||||
glFunc.CompileShader(handle[index]);
|
||||
CheckStatus(index,ShaderStatus.CompileStatus);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Compile(int index, string codeStream)
|
||||
{
|
||||
if (codeStream == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(codeStream));
|
||||
}
|
||||
glFunc.LoadShaderSource(handle[index],Encoding.ASCII.GetBytes( codeStream));
|
||||
glFunc.CompileShader(handle[index]);
|
||||
CheckStatus(index, ShaderStatus.CompileStatus);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"This is a {type} shader, its shader handles are {handle.ToString()}.";
|
||||
}
|
||||
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
public void CheckStatus(int index, ShaderStatus status)
|
||||
{
|
||||
string log = glFunc.GetShaderStatus(this,index, (int)status);
|
||||
if (!string.IsNullOrEmpty(log))
|
||||
{
|
||||
throw new Exception($"Shader Error \t{status}\n" + log);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] GetBytes(out int length)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void FromBytes(byte[] stream)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public enum ShaderStatus : int
|
||||
{
|
||||
CompileStatus = GLconst.COMPILE_STATUS,
|
||||
}
|
||||
|
||||
public class ShaderProgram
|
||||
{
|
||||
private readonly IntPtr binding;
|
||||
internal readonly glHandle handle;
|
||||
|
||||
/// <summary>
|
||||
/// Init a program with no shader attached.
|
||||
/// </summary>
|
||||
internal unsafe ShaderProgram(glRenderStream binding)
|
||||
{
|
||||
this.binding = binding.ContextHandle;
|
||||
handle = glFunc.CreateProgram();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">ContextName of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe Uniform<T> GetUniform<T>(string name) where T : struct
|
||||
{
|
||||
return new Uniform<T>(this, name, binding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">ContextName of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T, U> GetUniform<T, U>(string name)
|
||||
where T : struct
|
||||
where U : struct
|
||||
{
|
||||
return new Uniform<T, U>(this, name, binding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">ContextName of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T, U, V> GetUniform<T, U, V>(string name)
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
{
|
||||
return new Uniform<T, U, V>(this, name);
|
||||
}
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">ContextName of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T, U, V, W> GetUniform<T, U, V, W>(string name)
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
where W : struct
|
||||
{
|
||||
return new Uniform<T, U, V, W>(this, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Link the shader to current context.
|
||||
/// </summary>
|
||||
public unsafe void Link()
|
||||
{
|
||||
if (InternalIO.InternalLinkShaderProgram((GLcontext*)this.binding, handle.Value) == 0)
|
||||
{
|
||||
IntPtr logPtr = InternalIO.InternalReadSSCLog();
|
||||
try
|
||||
{
|
||||
byte[] logByte = new byte[512];
|
||||
Marshal.Copy(logPtr, logByte, 0, 512);
|
||||
string log = Encoding.UTF8.GetString(logByte);
|
||||
log = log.Replace("\0", string.Empty);
|
||||
InternalIO.InternalWriteLog(log, LogType.Error);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Let the current context use this shader.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Use()
|
||||
{
|
||||
glFunc.UseProgram(handle);
|
||||
}
|
||||
|
||||
~ShaderProgram()
|
||||
{
|
||||
//handle.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="Uniform.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Graphics;
|
||||
using StgSharp.Math;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics.GL
|
||||
{
|
||||
public class Uniform
|
||||
{
|
||||
protected IntPtr binding;
|
||||
internal glSHandle id;
|
||||
|
||||
|
||||
public glSHandle Handle
|
||||
{
|
||||
get => id;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Uniform<T> : Uniform
|
||||
where T : struct
|
||||
{
|
||||
|
||||
internal unsafe Uniform(glSHandle id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
internal unsafe Uniform(
|
||||
ShaderProgram program, string name, glRenderStream binding)
|
||||
{
|
||||
this.binding = binding.ContextHandle;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal unsafe Uniform(
|
||||
ShaderProgram program, string name, IntPtr binding)
|
||||
{
|
||||
this.binding = binding;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal Uniform()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static Uniform<T> FromHandle(glSHandle handle)
|
||||
{
|
||||
return new Uniform<T>() { id = handle };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public sealed class Uniform<T, U>: Uniform
|
||||
where T : struct
|
||||
where U : struct
|
||||
{
|
||||
|
||||
internal unsafe Uniform(glSHandle id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
internal unsafe Uniform(
|
||||
ShaderProgram program, string name, glRenderStream binding)
|
||||
{
|
||||
this.binding = binding.ContextHandle;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal unsafe Uniform(
|
||||
ShaderProgram program, string name, IntPtr binding)
|
||||
{
|
||||
this.binding = binding;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal Uniform()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static Uniform<T,U> FromHandle(glSHandle handle)
|
||||
{
|
||||
return new Uniform<T, U>() { id = handle };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public sealed class Uniform<T, U, V>:Uniform
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
{
|
||||
|
||||
internal unsafe Uniform(glSHandle id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
internal unsafe Uniform(
|
||||
ShaderProgram program, string name)
|
||||
{
|
||||
this.binding = glFunc.CurrentContextHandle;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
public unsafe Uniform(ShaderProgram program, string name, glRenderStream binding)
|
||||
{
|
||||
this.binding = binding.ContextHandle;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal Uniform()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static Uniform<T,U,V> FromHandle(glSHandle handle)
|
||||
{
|
||||
return new Uniform<T, U, V>() { id = handle };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public sealed class Uniform<T, U, V, W>:Uniform
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
where W : struct
|
||||
{
|
||||
|
||||
|
||||
internal unsafe Uniform(glSHandle id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
internal unsafe Uniform(ShaderProgram program, string name, glRenderStream binding)
|
||||
{
|
||||
this.binding = binding.ContextHandle;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal unsafe Uniform(
|
||||
ShaderProgram program, string name)
|
||||
{
|
||||
this.binding = glFunc.CurrentContextHandle;
|
||||
id = glFunc.GetUniformLocation(program.handle, name);
|
||||
}
|
||||
|
||||
internal Uniform()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static Uniform<T, U, V, W> FromHandle(glSHandle handle)
|
||||
{
|
||||
return new Uniform<T, U, V, W>() { id = handle };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal static partial class glFunc
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe static void SetUniformValue
|
||||
(Uniform<Matrix44> uniform, Matrix44 mat)
|
||||
{
|
||||
Matrix44 temp = mat;
|
||||
Context.glUniformMatrix4fv(uniform.id.Value, 1, false, (float*)&temp);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe static void SetUniformValue(Uniform<float, float, float, float> uniform, vec4d vec)
|
||||
{
|
||||
vec4d temp = vec;
|
||||
Context.glUniform1fv(uniform.id.Value , 4, &temp);
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe static void SetUniformValue(Uniform<vec4d> uniform, vec4d vec)
|
||||
{
|
||||
vec4d temp = vec;
|
||||
Context.glUniform1fv(uniform.id.Value , 4, &temp);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetUniformValue(Uniform<float> uniform, float v0)
|
||||
{
|
||||
Context.glUniform1f(uniform.id.Value , v0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetUniformValue(Uniform<int> uniform, int i0)
|
||||
{
|
||||
Context.glUniform1i(uniform.id.Value , i0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetUniformValue(Uniform<float, float> uniform, float v0, float v1)
|
||||
{
|
||||
Context.glUniform2f(uniform.id.Value , v0, v1);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetUniformValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
|
||||
{
|
||||
Context.glUniform3f(uniform.id.Value , v0, v1, v2);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetUniformValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
|
||||
{
|
||||
Context.glUniform4f(uniform.id.Value , v0, v1, v2, v3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user