MoonWorks/src/Graphics/Resources/ShaderModule.cs

43 lines
1.2 KiB
C#
Raw Normal View History

using RefreshCS;
using System;
2022-06-06 17:42:05 +00:00
using System.IO;
namespace MoonWorks.Graphics
{
2022-02-23 05:14:32 +00:00
/// <summary>
/// Shader modules expect input in SPIR-V bytecode format.
/// </summary>
public class ShaderModule : GraphicsResource
{
2022-03-04 01:16:39 +00:00
protected override Action<IntPtr, IntPtr> QueueDestroyFunction => Refresh.Refresh_QueueDestroyShaderModule;
2022-02-23 05:14:32 +00:00
public unsafe ShaderModule(GraphicsDevice device, string filePath) : base(device)
{
2022-06-06 17:42:05 +00:00
using (FileStream stream = new FileStream(filePath, FileMode.Open))
{
Handle = CreateFromStream(device, stream);
}
}
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);
2021-01-29 02:07:48 +00:00
2022-06-06 17:42:05 +00:00
fixed (byte* ptr = bytecode)
2022-02-23 05:14:32 +00:00
{
Refresh.ShaderModuleCreateInfo shaderModuleCreateInfo;
2022-06-06 17:42:05 +00:00
shaderModuleCreateInfo.codeSize = (UIntPtr) bytecode.Length;
2022-02-23 05:14:32 +00:00
shaderModuleCreateInfo.byteCode = (IntPtr) ptr;
2022-06-06 17:42:05 +00:00
return Refresh.Refresh_CreateShaderModule(device.Handle, shaderModuleCreateInfo);
2022-02-23 05:14:32 +00:00
}
}
}
}