Handle EngineBuffers, Preview Names and variable editor types for materials

This commit is contained in:
Simon Lübeß
2025-03-22 23:40:43 +01:00
parent 77a7a04637
commit 5e50d75eb9
13 changed files with 696 additions and 431 deletions
+15 -3
View File
@@ -4,15 +4,16 @@ using GlitchyEngine.Core;
namespace GlitchyEngine.Renderer
{
public class BufferCollection : RefCounter, IEnumerable<(String Name, Buffer Buffer)>
public class BufferCollection : RefCounter, IEnumerable<(String Name, String EngineBufferName, Buffer Buffer)>
{
public static extern int MaxBufferSlotCount { get; }
public typealias BufferEntry = (String Name, Buffer Buffer);
public typealias BufferEntry = (String Name, String EngineBufferName, Buffer Buffer);
BufferEntry[] _buffers ~ DeleteBufferEntries!(_);
Dictionary<StringView, BufferEntry*> _strToBuf ~ delete _;
Dictionary<StringView, BufferEntry*> _engineBuffers ~ delete _;
[AllowAppend]
public this()
@@ -22,9 +23,11 @@ namespace GlitchyEngine.Renderer
// Todo: append allocate as soon as it's fixed
let buffers = new BufferEntry[MaxBufferSlotCount];
let strToBuf = new Dictionary<StringView, BufferEntry*>();
let engineBuffers = new Dictionary<StringView, BufferEntry*>();
_buffers = buffers;
_strToBuf = strToBuf;
_engineBuffers = engineBuffers;
}
public ~this()
@@ -40,6 +43,7 @@ namespace GlitchyEngine.Renderer
for(let entry in entries)
{
delete entry.Name;
delete entry.EngineBufferName;
entry.Buffer?.ReleaseRef();
}
@@ -120,13 +124,21 @@ namespace GlitchyEngine.Renderer
}
}
public void Add(int slot, StringView name, Buffer buffer)
public void Add(int slot, StringView name, StringView engineBufferName, Buffer buffer)
{
ref BufferEntry bufferEntry = ref _buffers[slot];
SetReference!(bufferEntry.Buffer, buffer);
String.NewOrSet!(bufferEntry.Name, name);
if (engineBufferName.IsEmpty)
DeleteAndNullify!(bufferEntry.EngineBufferName);
else
{
String.NewOrSet!(bufferEntry.EngineBufferName, engineBufferName);
_engineBuffers.Add(bufferEntry.EngineBufferName, &bufferEntry);
}
_strToBuf.Add(bufferEntry.Name, &bufferEntry);
}
+7 -1
View File
@@ -24,6 +24,8 @@ namespace GlitchyEngine.Renderer
private ConstantBuffer _constantBuffer;
private String _name ~ delete _;
protected String _previewName ~ delete _;
protected String _editorTypeName ~ delete _;
private ShaderVariableType _elementType;
@@ -41,6 +43,8 @@ namespace GlitchyEngine.Renderer
public ShaderVariableType ElementType => _elementType;
public String Name => _name;
public StringView PreviewName => _previewName;
public StringView EditorTypeName => _editorTypeName;
public bool IsUsed => _flags.HasFlag(.Used);
@@ -70,9 +74,11 @@ namespace GlitchyEngine.Renderer
[Inline]
internal uint8* firstByte => _constantBuffer.rawData.CArray() + _offset;
public this(StringView name, ConstantBuffer constantBuffer, ShaderVariableType type, uint32 columns, uint32 rows, uint32 offset, uint32 sizeInBytes, uint32 arrayElements, bool isUsed)
public this(StringView name, ConstantBuffer constantBuffer, ShaderVariableType type, uint32 columns, uint32 rows, uint32 offset, uint32 sizeInBytes, uint32 arrayElements, bool isUsed, StringView previewName, StringView editorTypeName)
{
_name = new String(name);
_previewName = new String(previewName);
_editorTypeName = new String(editorTypeName);
_constantBuffer = constantBuffer; // Only hold a weak reference. This variable has to die with the buffer
_elementType = type;
_columns = columns;
+11 -3
View File
@@ -9,6 +9,7 @@ namespace GlitchyEngine.Renderer
public class ConstantBuffer : Buffer
{
protected String _name ~ delete _;
protected String _engineBufferName ~ delete _;
/**
* The buffer that contains the buffer data on the CPU.
@@ -23,6 +24,7 @@ namespace GlitchyEngine.Renderer
/// Gets the name of the constant buffer.
public StringView Name => _name;
public StringView EngineBufferName => _engineBufferName;
public BufferVariableCollection Variables => _variables;
@@ -31,9 +33,15 @@ namespace GlitchyEngine.Renderer
protected this() {}
public this(StringView name, int64 size)
public this(StringView name, int64 size, StringView engineBufferName = "")
{
_name = new String(name);
if (!engineBufferName.IsEmpty)
{
_engineBufferName = new String(engineBufferName);
}
rawData = new uint8[size];
ConstructBuffer();
}
@@ -43,9 +51,9 @@ namespace GlitchyEngine.Renderer
_variables.Add(ownVariable);
}
public void AddVariable(StringView name, uint64 offset, uint64 sizeInBytes, bool isUsed, ShaderVariableType type, uint8 rows, uint8 columns, uint64 arraySize)
public void AddVariable(StringView name, StringView previewName, StringView editorTypeName, uint64 offset, uint64 sizeInBytes, bool isUsed, ShaderVariableType type, uint8 rows, uint8 columns, uint64 arraySize)
{
_variables.Add(new BufferVariable(name, this, type, columns, rows, (uint32)offset, (uint32)sizeInBytes, (uint32)arraySize, isUsed));
_variables.Add(new BufferVariable(name, this, type, columns, rows, (uint32)offset, (uint32)sizeInBytes, (uint32)arraySize, isUsed, previewName, editorTypeName));
}
/**
+3 -3
View File
@@ -95,7 +95,7 @@ public class Material : Asset
BufferCollection parentBuffers = _parent?._bufferCollection ?? _effect.Buffers;
for (let (bufferName, buffer) in parentBuffers)
for (let (bufferName, engineBufferName, buffer) in parentBuffers)
{
if (buffer == null)
continue;
@@ -104,7 +104,7 @@ public class Material : Asset
{
using (OverridingConstantBuffer childConstBuffer = new OverridingConstantBuffer(parentConstBuffer))
{
_bufferCollection.Add(@bufferName.Index, childConstBuffer.Name, childConstBuffer);
_bufferCollection.Add(@bufferName.Index, childConstBuffer.Name, engineBufferName, childConstBuffer);
InitVariables(childConstBuffer);
}
}
@@ -177,7 +177,7 @@ public class Material : Asset
{
Debug.Profiler.ProfileRendererFunction!();
for (let (bufferName, buffer) in _bufferCollection)
for (let (bufferName, engineBufferName, buffer) in _bufferCollection)
{
if (let cbuffer = buffer as ConstantBuffer)
{
@@ -12,7 +12,7 @@ class OverridingConstantBuffer : ConstantBuffer
public ConstantBuffer Parent => _parent;
public this(ConstantBuffer parent) : base(parent.Name, parent.RawData.Length)
public this(ConstantBuffer parent) : base(parent.Name, parent.RawData.Length, parent.EngineBufferName)
{
Log.EngineLogger.AssertDebug(parent != null);
_parent = parent;
@@ -25,7 +25,7 @@ class OverridingConstantBuffer : ConstantBuffer
for (BufferVariable parentVariable in _parent.Variables)
{
BufferVariable newVariable = new BufferVariable(parentVariable.Name, this, parentVariable.ElementType, parentVariable.Columns,
parentVariable.Rows, parentVariable.Offset, parentVariable._sizeInBytes, parentVariable.ArrayElements, parentVariable.IsUsed);
parentVariable.Rows, parentVariable.Offset, parentVariable._sizeInBytes, parentVariable.ArrayElements, parentVariable.IsUsed, parentVariable.PreviewName, parentVariable.EditorTypeName);
if (parentVariable.Flags.HasFlag(.Locked))
{