daily update on 20240419 by nitload youmu, SSD merged into main stream

This commit is contained in:
YoumuLiu
2024-04-20 02:36:36 +08:00
parent efeb4ba058
commit 7b67eb2f4d
81 changed files with 4568 additions and 33 deletions
@@ -28,6 +28,7 @@
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Data;
using StgSharp.Math;
using System;
@@ -29,6 +29,7 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp;
using StgSharp.Data;
using System;
using System.Collections.Generic;
@@ -56,13 +57,15 @@ namespace StgSharp.Graphics
}
}
public class Shader
public class Shader : SSDSerializable
{
private readonly IntPtr context;
internal readonly glHandleSet handle;
public readonly ShaderType type;
public override byte SSDTypeCode => SerializableTypeCode.Shader;
internal unsafe Shader(glHandleSet handle, ShaderType usage)
{
this.handle = handle;
@@ -97,6 +100,21 @@ namespace StgSharp.Graphics
return $"This is a {type} shader, its shader handles are {handle.ToString()}.";
}
//TODO shader 的序列化究极麻烦,原因是shader的架构问题
public override byte[] GetBytes()
{
throw new NotImplementedException();
}
public override byte[] GetBytes(out int length)
{
throw new NotImplementedException();
}
internal override void BuildFromByteStream(byte[] stream)
{
throw new NotImplementedException();
}
}
public class ShaderProgram
+77 -3
View File
@@ -28,6 +28,7 @@
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Data;
using StgSharp.Math;
using System;
@@ -40,11 +41,11 @@ using System.Threading.Tasks;
namespace StgSharp.Graphics
{
public class Image
public class Image : SSDSerializable
{
internal ImageInfo info;
internal byte[] data;
internal glHandle pixelArrayHandle;
internal Image(ImageInfo information)
@@ -64,12 +65,85 @@ namespace StgSharp.Graphics
public int Width => info.width;
public override byte SSDTypeCode => SerializableTypeCode.Image;
~Image()
{
Marshal.FreeHGlobal(info.pixelPtr);
if (info.pixelPtr!= IntPtr.Zero)
{
Marshal.FreeHGlobal(info.pixelPtr);
}
}
public unsafe override byte[] GetBytes()
{
if (data == null)
{
int size = Width * Height * Channel + 1 + 8;
data = new byte[size];
int i = 0;
fixed (byte* bptr = data)
{
*(int*)bptr = Width;
*(int*)(bptr + 4) = Height;
*(bptr + 8) = (byte)Channel;
byte* pptr = bptr;
for (; i < size; i += 16)
{
*(M128*)(pptr + i) = *(M128*)(info.pixelPtr + i);
}
for (; i < size; i++)
{
*(pptr + i) = *(byte*)(info.pixelPtr + i);
}
}
}
Marshal.FreeHGlobal(info.pixelPtr);
info.pixelPtr = IntPtr.Zero;
return data;
}
public unsafe override byte[] GetBytes(out int length)
{
if (data == null)
{
int size = Width * Height * Channel + 1 + 8;
data = new byte[size];
int i = 0;
fixed (byte* bptr = data)
{
*(int*)bptr = Width;
*(int*)(bptr + 4) = Height;
*(bptr + 8) = (byte)Channel;
byte* pptr = bptr;
for (; i < size; i += 16)
{
*(M128*)(pptr + i) = *(M128*)(info.pixelPtr + i);
}
for (; i < size; i++)
{
*(pptr + i) = *(byte*)(info.pixelPtr + i);
}
}
}
length = data.Length;
Marshal.FreeHGlobal(info.pixelPtr);
info.pixelPtr = IntPtr.Zero;
return data;
}
internal unsafe override void BuildFromByteStream(byte[] stream)
{
info = new ImageInfo();
fixed (byte* bptr = stream)
{
info.width = *(int*)bptr;
info.height = *(int*)(bptr + 4);
info.channel = *(bptr + 8);
info.pixelPtr = IntPtr.Zero;
}
data = stream;
}
}
[StructLayout(LayoutKind.Sequential)]