make new opengl source generator

This commit is contained in:
Youmu Liu
2026-08-26 18:12:08 +08:00
parent 65041ff8bc
commit 75bf7fc139
126 changed files with 8934 additions and 26139 deletions
+232 -98
View File
@@ -14,6 +14,7 @@ using StgSharp.Timing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -23,12 +24,10 @@ namespace StgSharp
public class ParticleRenderStream : glRender
{
private AutoTextureGL autoT;
private ElementBuffer ebo;
private ElementBuffer f_ebo;
private FrameBuffer fbo;
private Image i;
private Image<Rgba8> i;
private int temp = 0;
private PlainInstancingBuffer<ParticleOnPlain> buffer;
@@ -37,6 +36,7 @@ namespace StgSharp
private ShaderProgram s;
private TextureGL f_t;
private TextureGL t;
private Uniform<GraphicsMatrix> cameraUniform;
private Uniform<int> texUniform;
private Uniform<float> time;
private VertexArray f_vao;
@@ -64,16 +64,21 @@ namespace StgSharp
GL.ClearColor(0.4f, TimePassed % 1.0f, 0f, 1.0f); /*GL.Assert(true);/**/
GL.Clear(MaskBufferBit.COLOR /**/ | MaskBufferBit.DEPTH/**/); /*GL.Assert(true);/**/
f_vao.Bind(0); //GL.Assert(true);
GL.BindVertexArray(f_vao[0]); //GL.Assert(true);
foreach (PlainGeometryMesh mesh in items)
{
// autoT.SelectTextureAndBind(mesh.TextureImage);
f_t.LoadTexture(0, mesh.TextureImage);
GL.ActiveTextureUnit((uint)TextureUnit.Unit1);
GL.BindTexture(glConst.TEXTURE_2D, f_t[0]);
GL.TextureImage2d(Texture2DTarget.Texture2D, 0, glConst.RGBA8,
mesh.TextureImage.Width, mesh.TextureImage.Height,
FrameBufferChannel.RGBA, PixelChannelLayout.UByte,
mesh.TextureImage.ReadOnlyPixels);
GL.GenerateMipmap(glConst.TEXTURE_2D);
// GL.Assert(true);
frameMainShader.Use(); //GL.Assert(true);
GL.UseProgram(frameMainShader.Handle); //GL.Assert(true);
GL.DrawElements(GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero);
}
@@ -94,17 +99,17 @@ namespace StgSharp
// Console.WriteLine(GL.CheckFrameBufferStatus(FrameBufferTarget.All));
fbo.Bind(0); //GL.Assert(true);
vao.Bind(0); //GL.Assert(true);
t.Bind2D(0); //GL.Assert(false);
GL.BindFrameBuffer(FrameBufferTarget.All, fbo[0]); //GL.Assert(true);
GL.BindVertexArray(vao[0]); //GL.Assert(true);
GL.ActiveTextureUnit((uint)TextureUnit.Unit0);
GL.BindTexture(glConst.TEXTURE_2D, t[0]); //GL.Assert(false);
GL.Viewport(0, 0, Width, Height);
/**/
GL.TextureImage2d<byte>(Texture2DTarget.Texture2D, 0, ImageChannel.WithAlphaChannel,
Width, Height, ImageChannel.WithAlphaChannel,
PixelChannelLayout.Byte, null);
GL.TextureImage2d(Texture2DTarget.Texture2D, 0, glConst.RGBA8, Width, Height,
FrameBufferChannel.RGBA, PixelChannelLayout.UByte);
/**/
// GL.Assert(false);
@@ -120,12 +125,13 @@ namespace StgSharp
// GL.Assert(false);
vbo.Bind(1);
vbo.WriteVectorData<Vec4>(1, buffer.CoordAndRotationSpan, BufferUsage.StreamDraw);
GL.BindBuffer(BufferType.ArrayBuffer, vbo[1]);
GL.SetBufferData<Vec4>(BufferType.ArrayBuffer, buffer.CoordAndRotationSpan,
BufferUsage.StreamDraw);
s.Use();
NativeCamera.SetAllUniforms();
GL.UseProgram(s.Handle);
GL.SetUniformValue(cameraUniform, NativeCamera.CameraMatrix());
GL.SetUniformValue(time, TimePassed);
GL.DrawElementsInstanced(GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero, 100);
@@ -139,22 +145,26 @@ namespace StgSharp
// GL.Assert(true);
ImageHelper.Resize(i, Width, Height, ImageMovement.Discard);
if (i.Size != (Width, Height))
{
i = new Image<Rgba8>(Width, Height);
ButtonTextureProvider.Image = i;
}
// GL.ClearError();
// Console.Write($"Size set to {Height},{Width}");
// Console.WriteLine($"Image resized to {i.Height} {i.Width}");
GL.GetTextureImage(Texture2DTarget.Texture2D, 0, ImageChannel.WithAlphaChannel,
PixelChannelLayout.Byte, i.PixelBuffer);
GL.GetTextureImage(Texture2DTarget.Texture2D, 0, FrameBufferChannel.RGBA,
PixelChannelLayout.UByte, i.Bytes);
/*
for (int num = 0; num < i.PixelBuffer.Length; num+=10000)
for (int num = 0; num < i.Bytes.Length; num+=10000)
{
Console.Write($"{i.PixelBuffer[num]}\t");
Console.Write($"{i.Bytes[num]}\t");
}
/**/
// GL.Assert(true);
// Array.Fill<byte>(i.PixelBuffer, 24);
// i.Bytes.Fill(24);
GL.BindFrameBuffer(FrameBufferTarget.All, GlHandle.Zero);
@@ -192,7 +202,114 @@ namespace StgSharp
0)) * rotation;
}
protected override void CustomizeDeinit() { }
private Shader CreateShaderSet(
ShaderType type,
int count
)
{
GlHandle[] handles = new GlHandle[count];
for (int index = 0; index < handles.Length; index++) {
handles[index] = GL.CreateShader((uint)type);
}
return new Shader(handles, type);
}
private void CompileShaderFromFile(
GlHandle shader,
string path
)
{
byte[] source = File.ReadAllBytes(path);
if (source.Length == 0) {
throw new InvalidOperationException($"Shader source is empty: {path}");
}
GL.ShaderSource(shader, source);
GL.CompileShader(shader);
Span<int> status = stackalloc int[1];
GL.GetShaderiv(shader, glConst.COMPILE_STATUS, status);
if (status[0] != 0) {
return;
}
throw new InvalidOperationException(
$"Failed to compile shader '{path}'.{Environment.NewLine}{GetShaderInfoLog(shader)}");
}
private void LinkShaderProgram(
GlHandle program
)
{
GL.LinkProgram(program);
Span<int> status = stackalloc int[1];
GL.GetProgramiv(program, glConst.LINK_STATUS, status);
if (status[0] != 0) {
return;
}
throw new InvalidOperationException(
$"Failed to link shader program.{Environment.NewLine}{GetProgramInfoLog(program)}");
}
private string GetShaderInfoLog(
GlHandle shader
)
{
Span<int> requiredLength = stackalloc int[1];
GL.GetShaderiv(shader, glConst.INFO_LOG_LENGTH, requiredLength);
if (requiredLength[0] <= 1) {
return "OpenGL did not provide a shader info log.";
}
byte[] bytes = new byte[requiredLength[0]];
GL.GetShaderInfoLog(shader, out int written, bytes);
return Encoding.UTF8.GetString(bytes, 0, Math.Clamp(written, 0, bytes.Length))
.TrimEnd('\0');
}
private string GetProgramInfoLog(
GlHandle program
)
{
Span<int> requiredLength = stackalloc int[1];
GL.GetProgramiv(program, glConst.INFO_LOG_LENGTH, requiredLength);
if (requiredLength[0] <= 1) {
return "OpenGL did not provide a program info log.";
}
byte[] bytes = new byte[requiredLength[0]];
GL.GetProgramInfoLog(program, out int written, bytes);
return Encoding.UTF8.GetString(bytes, 0, Math.Clamp(written, 0, bytes.Length))
.TrimEnd('\0');
}
private void DeleteShaderSet(
Shader shaderSet
)
{
for (int index = 0; index < shaderSet.Count; index++) {
GL.DeleteShader(shaderSet[index]);
}
}
protected override void CustomizeDeinit()
{
GL.DeleteProgram(s.Handle);
GL.DeleteProgram(frameMainShader.Handle);
GL.DeleteBuffers(vbo.Handles);
GL.DeleteBuffers(ebo.Handles);
GL.DeleteBuffers(f_vbo.Handles);
GL.DeleteBuffers(f_ebo.Handles);
GL.DeleteVertexArrays(vao.Handles);
GL.DeleteVertexArrays(f_vao.Handles);
GL.DeleteFramebuffers(fbo.Handles);
GL.DeleteRenderbuffers(rbo.Handles);
GL.DeleteTextures(t.Handles);
GL.DeleteTextures(f_t.Handles);
}
protected override void CustomizeInit()
{
@@ -221,59 +338,69 @@ namespace StgSharp
// SetSizeLimit(800, 800, 800, 800);
s = CreateShaderProgram();
frameMainShader = CreateShaderProgram();
Shader pvs = CreateShaderSegment(ShaderType.Vertex, 2);
Shader pfs = CreateShaderSegment(ShaderType.Fragment, 2);
s = new ShaderProgram(GL.CreateProgram());
frameMainShader = new ShaderProgram(GL.CreateProgram());
Shader pvs = CreateShaderSet(ShaderType.Vertex, 2);
Shader pfs = CreateShaderSet(ShaderType.Fragment, 2);
// GL.Assert(true);
pvs.Compile(0, Shader.FromCodeFile("Shader\\particle.vert"));
CompileShaderFromFile(pvs[0], Path.Combine("Shader", "particle.vert"));
// GL.Assert(true);
pvs.AttachTo(0, s);
GL.AttachShader(s.Handle, pvs[0]);
// GL.Assert(true);
pfs.Compile(0, Shader.FromCodeFile("Shader\\particle.frag"));
pfs.AttachTo(0, s);
s.Link();
CompileShaderFromFile(pfs[0], Path.Combine("Shader", "particle.frag"));
GL.AttachShader(s.Handle, pfs[0]);
LinkShaderProgram(s.Handle);
// GL.Assert(true);
pvs.Compile(1, Shader.FromCodeFile("Shader\\fbo.vert"));
pvs.AttachTo(1, frameMainShader);
pfs.Compile(1, Shader.FromCodeFile("Shader\\fbo.frag"));
pfs.AttachTo(1, frameMainShader);
frameMainShader.Link();
CompileShaderFromFile(pvs[1], Path.Combine("Shader", "fbo.vert"));
GL.AttachShader(frameMainShader.Handle, pvs[1]);
CompileShaderFromFile(pfs[1], Path.Combine("Shader", "fbo.frag"));
GL.AttachShader(frameMainShader.Handle, pfs[1]);
LinkShaderProgram(frameMainShader.Handle);
DeleteShaderSet(pvs);
DeleteShaderSet(pfs);
// GL.Assert(true);
vbo = CreateVertexBuffer(3);
vao = CreateVertexArray(1);
ebo = CreateElementBuffer(1);
vbo.Bind(0);
vbo = new VertexBuffer(GL.GenBuffers(3));
vao = new VertexArray(GL.GenVertexArrays(1));
ebo = new ElementBuffer(GL.GenBuffers(1));
GL.BindBuffer(BufferType.ArrayBuffer, vbo[0]);
Rectangle rec = new Rectangle(0.05f, 0.05f, 0.05f, -0.05f, -0.05f, -0.05f, -0.05f,
0.05f);
// GL.Assert(true);
vao.Bind(0);
vbo.WriteVectorData(0, rec.VertexStream, BufferUsage.StaticDraw);
ebo.SetValue(0, rec.VertexIndices, BufferUsage.StaticDraw);
vao.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
GL.BindVertexArray(vao[0]);
GL.BindBuffer(BufferType.ArrayBuffer, vbo[0]);
GL.SetBufferData(BufferType.ArrayBuffer, rec.VertexStream, BufferUsage.StaticDraw);
GL.BindBuffer(BufferType.ElementArrayBuffer, ebo[0]);
GL.SetBufferData(BufferType.ElementArrayBuffer, rec.VertexIndices,
BufferUsage.StaticDraw);
GL.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
vbo.WriteVectorData<Vec4>(1, buffer.CoordAndRotationSpan, BufferUsage.StreamDraw);
vao.SetVertexAttribute(1, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
vao.SetVertexAttribute(2, 1, TypeCode.Single, false, 4 * sizeof(float),
3 * sizeof(float));
GL.BindBuffer(BufferType.ArrayBuffer, vbo[1]);
GL.SetBufferData<Vec4>(BufferType.ArrayBuffer, buffer.CoordAndRotationSpan,
BufferUsage.StreamDraw);
GL.SetVertexAttribute(1, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
GL.SetVertexAttribute(2, 1, TypeCode.Single, false, 4 * sizeof(float),
3 * sizeof(float));
GL.VertexAttributeDivisor(1, 1);
GL.VertexAttributeDivisor(2, 1);
vbo.WriteScalerData<float>(2, buffer.ScalingSpan, BufferUsage.StaticDraw);
vao.SetVertexAttribute(3, 1, TypeCode.Single, false, sizeof(float), 0);
GL.BindBuffer(BufferType.ArrayBuffer, vbo[2]);
GL.SetBufferData<float>(BufferType.ArrayBuffer, buffer.ScalingSpan,
BufferUsage.StaticDraw);
GL.SetVertexAttribute(3, 1, TypeCode.Single, false, sizeof(float), 0);
GL.VertexAttributeDivisor(3, 1);
vbo.Unbind();
GL.BindBuffer(BufferType.ArrayBuffer, GlHandle.Zero);
GL.BindVertexArray(GlHandle.Zero);
NativeCameraViewTarget([
@@ -290,23 +417,31 @@ namespace StgSharp
0f
]);
NativeCameraViewRange(new Degree(1.5f), Vec2.Zero, (50, 50));
NativeCameraUniform(s, "Camera");
cameraUniform = GL.GetUniform<GraphicsMatrix>(s.Handle, "Camera");
s.Use();
GL.UseProgram(s.Handle);
fbo = CreateFrameBuffer(1);
rbo = CreateRenderBuffer(1);
fbo.Bind(0);
rbo.Bind(0);
GlHandle[] frameBufferHandles = new GlHandle[1];
GL.GenFrameBuffers(frameBufferHandles);
fbo = new FrameBuffer(frameBufferHandles);
rbo = new RenderBuffer(GL.GenRenderBuffer(1));
GL.BindFrameBuffer(FrameBufferTarget.All, fbo[0]);
GL.BindRenderBuffer(rbo[0]);
// GL.Assert(true);
t = CreateTexture(1);
t.Bind2D(0);
GL.TextureImage2d<byte>(Texture2DTarget.Texture2D, 0, ImageChannel.WithAlphaChannel,
Width, Height, ImageChannel.WithAlphaChannel,
PixelChannelLayout.Byte, null!);
t.Set2dWrapProperty(0, TextureWrap.Repeat, TextureWrap.Repeat);
t.Set2dFilterProperty(0, TextureFilter.Linear, TextureFilter.Linear);
t = new TextureGL(GL.GenTextures(1));
GL.ActiveTextureUnit((uint)TextureUnit.Unit0);
GL.BindTexture(glConst.TEXTURE_2D, t[0]);
GL.TextureImage2d(Texture2DTarget.Texture2D, 0, glConst.RGBA8, Width, Height,
FrameBufferChannel.RGBA, PixelChannelLayout.UByte);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_WRAP_S,
(int)TextureWrap.Repeat);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_WRAP_T,
(int)TextureWrap.Repeat);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_MIN_FILTER,
(int)TextureFilter.Linear);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_MAG_FILTER,
(int)TextureFilter.Linear);
// GL.Assert(true);
GL.FrameBufferTexture2d(FrameBufferTarget.All, glAttachment.Color(0),
@@ -317,15 +452,13 @@ namespace StgSharp
Console.WriteLine(GL.CheckFrameBufferStatus(FrameBufferTarget.All));
FrameBuffer.BindNull();
GL.BindFrameBuffer(FrameBufferTarget.All, GlHandle.Zero);
// GL.Assert(true);
int streamLength = this.Width * this.Height * 4;
byte[] imageStream = new byte[streamLength];
i = Image.FromMemory(this.Size, ImageChannel.WithAlphaChannel, imageStream);
i = new Image<Rgba8>(this.Size);
time = s.GetUniform<float>("time");
time = GL.GetUniform<float>(s.Handle, "time");
Vec2[] buttonTextureCoordArray = {
(0f, 1f),
@@ -338,45 +471,46 @@ namespace StgSharp
// GL.Assert(true);
ButtonTextureProvider = new TextureProvider(i, buttonTextureCoordArray);
f_vao = CreateVertexArray(1);
f_vbo = CreateVertexBuffer(2);
f_ebo = CreateElementBuffer(1);
f_vao = new VertexArray(GL.GenVertexArrays(1));
f_vbo = new VertexBuffer(GL.GenBuffers(2));
f_ebo = new ElementBuffer(GL.GenBuffers(1));
// GL.Assert(true);
f_vao.Bind(0);
GL.BindVertexArray(f_vao[0]);
f_ebo.Bind(0);
f_ebo.SetValue(0, ButtonBounding.VertexIndices, BufferUsage.StaticDraw);
GL.BindBuffer(BufferType.ElementArrayBuffer, f_ebo[0]);
GL.SetBufferData(BufferType.ElementArrayBuffer, ButtonBounding.VertexIndices,
BufferUsage.StaticDraw);
// GL.Assert(true);
f_vbo.Bind(0);
f_vbo.WriteVectorData(0, ButtonBounding.VertexStream, BufferUsage.StaticDraw);
f_vao.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
GL.BindBuffer(BufferType.ArrayBuffer, f_vbo[0]);
GL.SetBufferData(BufferType.ArrayBuffer, ButtonBounding.VertexStream,
BufferUsage.StaticDraw);
GL.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
// GL.Assert(true);
f_vbo.Bind(1);
f_vbo.WriteVectorData(1, buttonTextureCoordArray, BufferUsage.StaticDraw);
f_vao.SetVertexAttribute(1, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
GL.BindBuffer(BufferType.ArrayBuffer, f_vbo[1]);
GL.SetBufferData(BufferType.ArrayBuffer, buttonTextureCoordArray,
BufferUsage.StaticDraw);
GL.SetVertexAttribute(1, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
// GL.Assert(true);
autoT = CreateAutoTexture(4);
autoT.DefaultTextureProperty = new TextureProperty(TextureFilter.Nearest,
TextureFilter.Nearest,
TextureWrap.Repeat,
TextureWrap.Repeat);
f_t = CreateTexture(1);
f_t.Bind2D(0);
f_t.Set2dWrapProperty(0, TextureWrap.Repeat, TextureWrap.Repeat);
f_t.Set2dFilterProperty(0, TextureFilter.Nearest, TextureFilter.Nearest);
f_t = new TextureGL(GL.GenTextures(1));
GL.ActiveTextureUnit((uint)TextureUnit.Unit1);
GL.BindTexture(glConst.TEXTURE_2D, f_t[0]);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_WRAP_S,
(int)TextureWrap.Repeat);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_WRAP_T,
(int)TextureWrap.Repeat);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_MIN_FILTER,
(int)TextureFilter.Nearest);
GL.TextureParameter(glConst.TEXTURE_2D, glConst.TEXTURE_MAG_FILTER,
(int)TextureFilter.Nearest);
// GL.Assert(true);
f_t.ActivateAs(TextureUnit.Unit1);
// GL.Assert(true);
frameMainShader.Use();
texUniform = frameMainShader.GetUniform<int>("tex");
GL.UseProgram(frameMainShader.Handle);
texUniform = GL.GetUniform<int>(frameMainShader.Handle, "tex");
// GL.Assert(true);
GL.SetUniformValue(texUniform, 1);