- Added resource manager
- Shader building now inspects the spir-v for descriptor sets and writes the info to the output binary
This commit is contained in:
9
Nerfed.Runtime/Resource/Resource.cs
Normal file
9
Nerfed.Runtime/Resource/Resource.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public abstract class Resource
|
||||
{
|
||||
public string Path { get; internal set; }
|
||||
|
||||
internal abstract void Load(Stream stream);
|
||||
internal abstract void Unload();
|
||||
}
|
43
Nerfed.Runtime/Resource/ResourceManager.cs
Normal file
43
Nerfed.Runtime/Resource/ResourceManager.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public static class ResourceManager
|
||||
{
|
||||
private const string rootName = "Resources";
|
||||
private static readonly Dictionary<string, Resource> loadedResources = new Dictionary<string, Resource>();
|
||||
|
||||
public static T Load<T>(string resourcePath) where T : Resource
|
||||
{
|
||||
if (loadedResources.TryGetValue(resourcePath, out Resource resource))
|
||||
{
|
||||
return (T)resource;
|
||||
}
|
||||
|
||||
if (typeof(T) == typeof(Shader))
|
||||
{
|
||||
resource = new Shader();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Failed to create resource");
|
||||
}
|
||||
|
||||
Assert.Always(resource != null);
|
||||
resource.Path = resourcePath;
|
||||
resource.Load(StorageContainer.OpenStream(Path.Combine(AppContext.BaseDirectory, rootName, resourcePath) + ".bin"));
|
||||
|
||||
loadedResources.Add(resourcePath, resource);
|
||||
return (T)resource;
|
||||
}
|
||||
|
||||
public static void Unload(Resource resource)
|
||||
{
|
||||
if (!loadedResources.ContainsKey(resource.Path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
resource.Unload();
|
||||
resource.Path = string.Empty;
|
||||
loadedResources.Remove(resource.Path);
|
||||
}
|
||||
}
|
58
Nerfed.Runtime/Resource/Shader.cs
Normal file
58
Nerfed.Runtime/Resource/Shader.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using RefreshCS;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public class Shader : Resource
|
||||
{
|
||||
public IntPtr Handle { get; private set; }
|
||||
|
||||
public uint SamplerCount { get; private set; }
|
||||
public uint StorageTextureCount { get; private set; }
|
||||
public uint StorageBufferCount { get; private set; }
|
||||
public uint UniformBufferCount { get; private set; }
|
||||
|
||||
internal Shader() { }
|
||||
|
||||
internal override unsafe void Load(Stream stream)
|
||||
{
|
||||
using (BinaryReader reader = new BinaryReader(stream))
|
||||
{
|
||||
Refresh.ShaderCreateInfo createInfo;
|
||||
createInfo.Format = (Refresh.ShaderFormat)reader.ReadInt32();
|
||||
createInfo.Stage = (Refresh.ShaderStage)reader.ReadInt32();
|
||||
createInfo.UniformBufferCount = (uint)reader.ReadInt32();
|
||||
createInfo.StorageBufferCount = (uint)reader.ReadInt32();
|
||||
createInfo.StorageTextureCount = (uint)reader.ReadInt32();
|
||||
createInfo.SamplerCount = (uint)reader.ReadInt32();
|
||||
|
||||
int byteCodeSize = reader.ReadInt32();
|
||||
void* byteCodeBuffer = NativeMemory.Alloc((nuint)byteCodeSize);
|
||||
Span<byte> byteCodeSpan = new Span<byte>(byteCodeBuffer, byteCodeSize);
|
||||
|
||||
int bytesRead = 0;
|
||||
while (bytesRead < byteCodeSize)
|
||||
{
|
||||
bytesRead += reader.Read(byteCodeSpan.Slice(bytesRead));
|
||||
}
|
||||
|
||||
createInfo.CodeSize = (nuint)byteCodeSize;
|
||||
createInfo.Code = (byte*)byteCodeBuffer;
|
||||
createInfo.EntryPointName = "main";
|
||||
|
||||
Handle = Refresh.Refresh_CreateShader(Engine.GraphicsDevice.Handle, createInfo);
|
||||
NativeMemory.Free(byteCodeBuffer);
|
||||
|
||||
SamplerCount = createInfo.SamplerCount;
|
||||
StorageTextureCount = createInfo.StorageTextureCount;
|
||||
StorageBufferCount = createInfo.StorageBufferCount;
|
||||
UniformBufferCount = createInfo.UniformBufferCount;
|
||||
}
|
||||
}
|
||||
|
||||
internal override void Unload()
|
||||
{
|
||||
Refresh.Refresh_ReleaseShader(Engine.GraphicsDevice.Handle, Handle);
|
||||
Handle = IntPtr.Zero;
|
||||
}
|
||||
}
|
9
Nerfed.Runtime/Resource/StorageContainer.cs
Normal file
9
Nerfed.Runtime/Resource/StorageContainer.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public static class StorageContainer
|
||||
{
|
||||
public static Stream OpenStream(string file)
|
||||
{
|
||||
return File.Open(file, FileMode.Open);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user