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
@@ -86,6 +86,22 @@ class ShaderLoader : IProcessedAssetLoader
return effect;
}
private static mixin ReadScopedSizedString<Tint>(Stream stream)
where Tint : IInteger
where int : operator explicit Tint
where Tint : operator explicit int
where Tint : struct
{
int bufferNameLength = (int)Try!(stream.Read<Tint>());
String string = scope:mixin String(bufferNameLength);
if (bufferNameLength > 0)
stream.ReadStrSized32(bufferNameLength, string);
string
}
private static Result<void> LoadBuffer(Stream stream, Effect effect)
{
int64 bufferSize = Try!(stream.Read<int64>());
@@ -93,24 +109,10 @@ class ShaderLoader : IProcessedAssetLoader
int32 vertexShaderBindPoint = Try!(stream.Read<int32>());
int32 pixelShaderBindPoint = Try!(stream.Read<int32>());
int16 bufferNameLength = Try!(stream.Read<int16>());
String bufferName = scope String(bufferNameLength);
stream.ReadStrSized32(bufferNameLength, bufferName);
String bufferName = ReadScopedSizedString!<uint16>(stream);
String engineBufferName = ReadScopedSizedString!<uint16>(stream);
int16 engineBufferNameLength = Try!(stream.Read<int16>());
String engineBufferName = null;
if (engineBufferNameLength > 0)
{
scope String(engineBufferNameLength);
stream.ReadStrSized32(engineBufferNameLength, engineBufferName);
// TODO: Engine buffers currently do nothing. The bind points for each engine buffer are hardcoded.
// It only marks the buffer as engine buffer, preventing the variables from becomming accessible.
//effect.[Friend]_engineBuffers.Add()
}
using (ConstantBuffer buffer = new ConstantBuffer(bufferName, bufferSize))
using (ConstantBuffer buffer = new ConstantBuffer(bufferName, bufferSize, engineBufferName))
{
uint16 variableCount = Try!(stream.Read<uint16>());
@@ -124,26 +126,25 @@ class ShaderLoader : IProcessedAssetLoader
uint8 columns = Try!(stream.Read<uint8>());
uint64 arraySize = Try!(stream.Read<uint64>());
int16 variableNameLength = Try!(stream.Read<int16>());
String variableName = scope String(variableNameLength);
stream.ReadStrSized32(variableNameLength, variableName);
String variableName = ReadScopedSizedString!<uint16>(stream);
String previewName = ReadScopedSizedString!<uint16>(stream);
String editorTypeName = ReadScopedSizedString!<uint16>(stream);
if (engineBufferName == null)
buffer.AddVariable(variableName, variableOffset, sizeInBytes, isUsed, type, rows, columns, arraySize);
buffer.AddVariable(variableName, previewName, editorTypeName, variableOffset, sizeInBytes, isUsed, type, rows, columns, arraySize);
}
Try!(stream.TryRead(buffer.RawData));
Try!(buffer.Apply());
if (vertexShaderBindPoint != -1)
effect.VertexShader.Buffers.Add(vertexShaderBindPoint, buffer.Name, buffer);
effect.VertexShader.Buffers.Add(vertexShaderBindPoint, buffer.Name, engineBufferName, buffer);
if (pixelShaderBindPoint != -1)
effect.PixelShader.Buffers.Add(pixelShaderBindPoint, buffer.Name, buffer);
effect.PixelShader.Buffers.Add(pixelShaderBindPoint, buffer.Name, engineBufferName, buffer);
// TODO: Allow binding buffers to different indices? Does this theoretically work with textures?
let tempBindPoint = (vertexShaderBindPoint != -1) ? vertexShaderBindPoint : pixelShaderBindPoint;
effect.Buffers.Add(tempBindPoint, buffer.Name, buffer);
effect.Buffers.Add(tempBindPoint, buffer.Name, engineBufferName, buffer);
for (var variable in buffer.Variables)
{
+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))
{