read ShaderModules from stream

main
cosmonaut 2022-06-06 10:42:05 -07:00
parent 436affe5de
commit 6ab7a2f722
2 changed files with 19 additions and 37 deletions

View File

@ -1,5 +1,6 @@
using RefreshCS; using RefreshCS;
using System; using System;
using System.IO;
namespace MoonWorks.Graphics namespace MoonWorks.Graphics
{ {
@ -12,15 +13,29 @@ namespace MoonWorks.Graphics
public unsafe ShaderModule(GraphicsDevice device, string filePath) : base(device) public unsafe ShaderModule(GraphicsDevice device, string filePath) : base(device)
{ {
var bytecode = Bytecode.ReadBytecodeAsUInt32(filePath); using (FileStream stream = new FileStream(filePath, FileMode.Open))
{
Handle = CreateFromStream(device, stream);
}
}
fixed (uint* ptr = bytecode) public unsafe ShaderModule(GraphicsDevice device, Stream stream) : base(device)
{
Handle = CreateFromStream(device, stream);
}
private unsafe static IntPtr CreateFromStream(GraphicsDevice device, Stream stream)
{
var bytecode = new byte[stream.Length];
stream.Read(bytecode, 0, (int) stream.Length);
fixed (byte* ptr = bytecode)
{ {
Refresh.ShaderModuleCreateInfo shaderModuleCreateInfo; Refresh.ShaderModuleCreateInfo shaderModuleCreateInfo;
shaderModuleCreateInfo.codeSize = (UIntPtr) (bytecode.Length * sizeof(uint)); shaderModuleCreateInfo.codeSize = (UIntPtr) bytecode.Length;
shaderModuleCreateInfo.byteCode = (IntPtr) ptr; shaderModuleCreateInfo.byteCode = (IntPtr) ptr;
Handle = Refresh.Refresh_CreateShaderModule(device.Handle, shaderModuleCreateInfo); return Refresh.Refresh_CreateShaderModule(device.Handle, shaderModuleCreateInfo);
} }
} }
} }

View File

@ -1,33 +0,0 @@
using System.IO;
namespace MoonWorks.Graphics
{
public static class Bytecode
{
public static uint[] ReadBytecodeAsUInt32(string filePath)
{
byte[] data;
int size;
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
size = (int) stream.Length;
data = new byte[size];
stream.Read(data, 0, size);
}
uint[] uintData = new uint[size / 4];
using (var memoryStream = new MemoryStream(data))
{
using (var reader = new BinaryReader(memoryStream))
{
for (int i = 0; i < size / 4; i++)
{
uintData[i] = reader.ReadUInt32();
}
}
}
return uintData;
}
}
}