daily update on 2024.03.18 by itload youmu
This commit is contained in:
@@ -1,4 +1,34 @@
|
||||
using StgSharp;
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// 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 System;
|
||||
using System.Collections.Generic;
|
||||
@@ -10,6 +40,12 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
public partial class Form
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected Shader CreateShader(ShaderType type, int count)
|
||||
{
|
||||
return new Shader(this.gl, gl.CreateShaderSet(count, type), type);
|
||||
}
|
||||
/// <summary>
|
||||
/// Create an instance of <see cref="Shader"/> program
|
||||
/// </summary>
|
||||
@@ -20,18 +56,14 @@ namespace StgSharp.Graphics
|
||||
return new ShaderProgram(GL);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected Shader CreateShader(ShaderType type, int count)
|
||||
{
|
||||
return new Shader(this.gl, gl.CreateShaderSet(count, type), type);
|
||||
}
|
||||
}
|
||||
|
||||
public class Shader
|
||||
{
|
||||
|
||||
private readonly IglFunc GL;
|
||||
internal readonly glHandleSet handle;
|
||||
public readonly ShaderType type;
|
||||
private readonly IglFunc GL;
|
||||
|
||||
internal Shader(IglFunc gl, glHandleSet handle, ShaderType usage)
|
||||
{
|
||||
@@ -40,17 +72,6 @@ namespace StgSharp.Graphics
|
||||
type = usage;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Compile(int index, string code)
|
||||
{
|
||||
if (code == null)
|
||||
{
|
||||
throw new ArgumentNullException("Must provide enough code");
|
||||
}
|
||||
GL.LoadShaderSource(handle[index], code + '\0');
|
||||
GL.CompileShader(handle[index]);
|
||||
}
|
||||
|
||||
//TODO:写链接着色器的文档注释
|
||||
/// <summary>
|
||||
///
|
||||
@@ -63,16 +84,29 @@ namespace StgSharp.Graphics
|
||||
GL.AttachShader(target.handle, handle[index]);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Compile(int index, string code)
|
||||
{
|
||||
if (code == null)
|
||||
{
|
||||
throw new ArgumentNullException("Must provide enough code");
|
||||
}
|
||||
GL.LoadShaderSource(handle[index], $"{code}\0");
|
||||
GL.CompileShader(handle[index]);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"This is a {type} shader, its shader handles are {handle.ToString()}.";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ShaderProgram
|
||||
{
|
||||
internal readonly glHandle handle;
|
||||
|
||||
private readonly IglFunc GL;
|
||||
internal readonly glHandle handle;
|
||||
|
||||
/// <summary>
|
||||
/// Inti a shader with no program attached.
|
||||
@@ -83,29 +117,6 @@ namespace StgSharp.Graphics
|
||||
handle = GL.CreateProgram();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Link the shader to current context.
|
||||
/// </summary>
|
||||
public unsafe void Link()
|
||||
{
|
||||
if (InternalIO.InternalLinkShaderProgram(GL.Context, 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", "");
|
||||
InternalIO.InternalWriteLog(log, LogType.Error);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
@@ -145,6 +156,29 @@ namespace StgSharp.Graphics
|
||||
return new Uniform<T, U, V>(this, name, GL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Link the shader to current context.
|
||||
/// </summary>
|
||||
public unsafe void Link()
|
||||
{
|
||||
if (InternalIO.InternalLinkShaderProgram(GL.Context, 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>
|
||||
@@ -153,5 +187,6 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
GL.UseProgram(handle);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user