New material loading pipeline almost working

This commit is contained in:
Simon Lübeß
2025-01-31 13:18:49 +01:00
parent 2f0505241f
commit acfb052137
11 changed files with 419 additions and 81 deletions
@@ -0,0 +1,53 @@
using System;
using System.Collections;
using System.IO;
using GlitchyEngine.Renderer;
namespace GlitchyEngine.Content.Loaders;
class MaterialLoader : IProcessedAssetLoader
{
public Result<Asset> Load(Stream stream)
{
Compiler.Assert(sizeof(AssetHandle) == 8);
AssetHandle<Effect> effectHandle = Try!(stream.Read<AssetHandle>());
Effect effect = Content.GetAsset<Effect>(effectHandle, blocking: true);
Material material = new Material(effect);
uint16 textureCount = Try!(stream.Read<uint16>());
for (int i < textureCount)
{
AssetHandle<Texture> textureHandle = Try!(stream.Read<AssetHandle>());
int16 textureNameLength = Try!(stream.Read<int16>());
String textureName = scope String(textureNameLength);
stream.ReadStrSized32(textureNameLength, textureName);
material.SetTexture(textureName, textureHandle);
}
uint16 bufferCount = Try!(stream.Read<uint16>());
for (int i < bufferCount)
{
int64 bufferDataSize = Try!(stream.Read<int64>());
int16 textureNameLength = Try!(stream.Read<int16>());
String textureName = scope String(textureNameLength);
stream.ReadStrSized32(textureNameLength, textureName);
uint8[] data = new:ScopedAlloc! uint8[bufferDataSize];
Try!(stream.TryRead(data));
// TODO: Somehow get buffer data into material
}
material.SetVariable("AlbedoColor", GlitchyEngine.Math.float4(1.0f, 0, 0, 1.0f));
return material;
}
}
+14 -11
View File
@@ -10,20 +10,20 @@ namespace GlitchyEngine.Renderer
private String _name ~ delete _;
private ShaderVariableType _type;
private ShaderVariableType _elementType;
internal uint32 _columns;
internal uint32 _rows;
private uint32 _offset;
internal uint32 _sizeInBytes;
// Number of elements in the array
internal uint32 _elements;
internal uint32 _arrayElements;
private bool _isUsed;
public ConstantBuffer ConstantBuffer => _constantBuffer;
public ShaderVariableType Type => _type;
public ShaderVariableType ElementType => _elementType;
public String Name => _name;
@@ -31,6 +31,9 @@ namespace GlitchyEngine.Renderer
public uint32 Columns => _columns;
public uint32 Rows => _rows;
public uint32 ArrayElements => _arrayElements;
public uint32 Offset => _offset;
/**
* Gets a pointer to the start of the variable in the constant buffers backing data.
@@ -38,16 +41,16 @@ 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 elements, bool isUsed)
public this(StringView name, ConstantBuffer constantBuffer, ShaderVariableType type, uint32 columns, uint32 rows, uint32 offset, uint32 sizeInBytes, uint32 arrayElements, bool isUsed)
{
_name = new String(name);
_constantBuffer = constantBuffer; // Only hold a weak reference. This variable has to die with the buffer
_type = type;
_elementType = type;
_columns = columns;
_rows = rows;
_offset = offset;
_sizeInBytes = sizeInBytes;
_elements = elements;
_arrayElements = arrayElements;
_isUsed = isUsed;
}
@@ -64,8 +67,8 @@ namespace GlitchyEngine.Renderer
#endif
#if GE_SHADER_VAR_TYPE_MISMATCH_IS_ERROR
if (type != _type)
Log.EngineLogger.Assert(false, scope $"The types do not match: Expected \"{_type}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
if (type != _elementType)
Log.EngineLogger.Assert(false, scope $"The types do not match: Expected \"{_elementType}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
#elif GE_SHADER_VAR_TYPE_MISMATCH_IS_WARNING
if (type != _type)
Log.EngineLogger.Warning($"The types do not match: Expected \"{_type}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
@@ -174,7 +177,7 @@ namespace GlitchyEngine.Renderer
// TODO: assert length
Internal.MemCpy(firstByte, value.Ptr, sizeof(Matrix4x3) * Math.Min(value.Count, _elements));
Internal.MemCpy(firstByte, value.Ptr, sizeof(Matrix4x3) * Math.Min(value.Count, _arrayElements));
}
public void SetData(Matrix3x3 value)
@@ -192,7 +195,7 @@ namespace GlitchyEngine.Renderer
// TODO: assert length
for(int i < Math.Min(value.Count, _elements))
for(int i < Math.Min(value.Count, _arrayElements))
{
((Matrix4x3*)firstByte)[i] = Matrix4x3(value[i]);
}
@@ -206,7 +209,7 @@ namespace GlitchyEngine.Renderer
// TODO: assert length
Internal.MemCpy(firstByte, value.Ptr, sizeof(Matrix) * Math.Min(value.Count, _elements));
Internal.MemCpy(firstByte, value.Ptr, sizeof(Matrix) * Math.Min(value.Count, _arrayElements));
}
// Todo: add all the other SetData-Methods
@@ -78,6 +78,7 @@ namespace GlitchyEngine.Renderer
switch (this)
{
case .Bool:
// TODO: I'm pretty sure this is wrong and should be 4 as well!
return 1;
case .Float:
return 4;
+4 -4
View File
@@ -93,9 +93,9 @@ public class Material : Asset
{
if (_variables.TryGetValue(newKey, let oldEntry))
{
if (oldEntry.Variable.Type == newValue.Variable.Type)
if (oldEntry.Variable.ElementType == newValue.Variable.ElementType)
{
int elementSize = newValue.Variable.Type.ElementSizeInBytes();
int elementSize = newValue.Variable.ElementType.ElementSizeInBytes();
for (int r = 0; r < Math.Min(oldEntry.Variable.Rows, newValue.Variable.Rows); r++)
for (int c = 0; c < Math.Min(oldEntry.Variable.Columns, newValue.Variable.Columns); c++)
@@ -259,7 +259,7 @@ public class Material : Asset
{
entry.Variable.EnsureTypeMatch<Matrix3x3>();
int count = Math.Min(values.Count, entry.Variable._elements);
int count = Math.Min(values.Count, entry.Variable._arrayElements);
for(int i < count)
{
@@ -281,7 +281,7 @@ public class Material : Asset
{
entry.Variable.EnsureTypeMatch<Matrix>();
Internal.MemCpy(RawPointer!<Matrix>(entry.Offset), values.Ptr, sizeof(Matrix) * Math.Min(values.Count, entry.Variable._elements));
Internal.MemCpy(RawPointer!<Matrix>(entry.Offset), values.Ptr, sizeof(Matrix) * Math.Min(values.Count, entry.Variable._arrayElements));
}
else
{