Separate logical ownership, render publication, and GPU retirement across live entities, landblocks, particles, textures, mesh arenas, portal/UI teardown, and per-frame scratch storage. Add bounded DAT/texture caches, upload budgets, three-frame fence retirement, exact-incarnation appearance reconciliation, frame pacing, and extensive lifetime conformance coverage.\n\nThe seven-destination connected route now cuts peak working/private memory roughly in half, returns Caul to 125-153 FPS locally, and produces no WER or AMD reset.\n\nCo-authored-by: OpenAI Codex <codex@openai.com>
96 lines
2.8 KiB
C#
96 lines
2.8 KiB
C#
using System.Numerics;
|
|
using Silk.NET.OpenGL;
|
|
|
|
namespace AcDream.App.Rendering;
|
|
|
|
public sealed class Shader : IDisposable
|
|
{
|
|
private readonly GL _gl;
|
|
private readonly Dictionary<string, int> _uniformLocations = new(StringComparer.Ordinal);
|
|
public uint Program { get; }
|
|
|
|
public Shader(GL gl, string vertexPath, string fragmentPath)
|
|
{
|
|
_gl = gl;
|
|
uint vert = Compile(File.ReadAllText(vertexPath), ShaderType.VertexShader);
|
|
uint frag = Compile(File.ReadAllText(fragmentPath), ShaderType.FragmentShader);
|
|
|
|
Program = _gl.CreateProgram();
|
|
_gl.AttachShader(Program, vert);
|
|
_gl.AttachShader(Program, frag);
|
|
_gl.LinkProgram(Program);
|
|
_gl.GetProgram(Program, ProgramPropertyARB.LinkStatus, out int linked);
|
|
if (linked == 0)
|
|
throw new Exception("program link failed: " + _gl.GetProgramInfoLog(Program));
|
|
_gl.DetachShader(Program, vert);
|
|
_gl.DetachShader(Program, frag);
|
|
_gl.DeleteShader(vert);
|
|
_gl.DeleteShader(frag);
|
|
}
|
|
|
|
private uint Compile(string source, ShaderType type)
|
|
{
|
|
uint id = _gl.CreateShader(type);
|
|
_gl.ShaderSource(id, source);
|
|
_gl.CompileShader(id);
|
|
_gl.GetShader(id, ShaderParameterName.CompileStatus, out int ok);
|
|
if (ok == 0)
|
|
throw new Exception($"{type} compile failed: " + _gl.GetShaderInfoLog(id));
|
|
return id;
|
|
}
|
|
|
|
public void Use() => _gl.UseProgram(Program);
|
|
|
|
public unsafe void SetMatrix4(string name, Matrix4x4 m)
|
|
{
|
|
int loc = GetUniformLocation(name);
|
|
_gl.UniformMatrix4(loc, 1, false, (float*)&m);
|
|
}
|
|
|
|
public void SetInt(string name, int value)
|
|
{
|
|
int loc = GetUniformLocation(name);
|
|
_gl.Uniform1(loc, value);
|
|
}
|
|
|
|
public void SetFloat(string name, float value)
|
|
{
|
|
int loc = GetUniformLocation(name);
|
|
_gl.Uniform1(loc, value);
|
|
}
|
|
|
|
public void SetVec3(string name, Vector3 v)
|
|
{
|
|
int loc = GetUniformLocation(name);
|
|
_gl.Uniform3(loc, v.X, v.Y, v.Z);
|
|
}
|
|
|
|
public void SetVec2(string name, Vector2 v)
|
|
{
|
|
int loc = GetUniformLocation(name);
|
|
_gl.Uniform2(loc, v.X, v.Y);
|
|
}
|
|
|
|
public void SetVec4(string name, Vector4 v)
|
|
{
|
|
int loc = GetUniformLocation(name);
|
|
_gl.Uniform4(loc, v.X, v.Y, v.Z, v.W);
|
|
}
|
|
|
|
private int GetUniformLocation(string name)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(name);
|
|
if (_uniformLocations.TryGetValue(name, out int location))
|
|
return location;
|
|
|
|
location = _gl.GetUniformLocation(Program, name);
|
|
_uniformLocations.Add(name, location);
|
|
return location;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_uniformLocations.Clear();
|
|
_gl.DeleteProgram(Program);
|
|
}
|
|
}
|