Handles barely work now, mostly no crashes

This commit is contained in:
Simon Lübeß
2023-03-16 17:57:23 +01:00
parent c3fb9bbd5d
commit b22f73a156
20 changed files with 780 additions and 627 deletions
+4 -7
View File
@@ -10,7 +10,7 @@ namespace GlitchyEngine.Content;
[BonTarget]
class Asset : RefCounter
{
internal AssetHandle _handle;
internal AssetHandle _handle = .Invalid;
private append String _identifier;
@@ -60,16 +60,13 @@ class Asset : RefCounter
static Result<void> AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment, DeserializeValueState state)
{
// TODO!!!
return .Err;
/*Log.EngineLogger.Assert(value.type == typeof(Asset));
Log.EngineLogger.Assert(value.type == typeof(Asset));
String identifier = scope .();
Deserialize.String!(reader, ref identifier, environment);
Asset asset = Application.Get().ContentManager.LoadAsset(identifier);
Asset asset = Content.GetAsset<Asset>(Content.LoadAsset(identifier));
if (asset != null)
{
@@ -82,7 +79,7 @@ class Asset : RefCounter
else
{
Deserialize.Error!("Invalid resource path", reader, value.type);
}*/
}
}
//gBonEnv.typeHandlers.Add(typeof(Resource<>),
+29 -13
View File
@@ -3,16 +3,25 @@ using xxHash;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using GlitchyEngine.Core;
namespace GlitchyEngine.Content;
struct AssetHandle : uint64
struct AssetHandle : IHashable
{
/// Defines an asset that is invalid. E.g. because it couldn't be loaded.
public const AssetHandle Invalid = (.)0;
private UUID _uuid;
public this(StringView name)
/// Defines an asset that is invalid.
public const AssetHandle Invalid = .(UUID(0xAAAA'AAAA'AAAA'AAAA));
/// Create a new random AssetHandle
public this()
{
this = (uint64)xxHash.ComputeHash(name);
_uuid = UUID();
}
private this(UUID uuid)
{
_uuid = uuid;
}
[Inline]
@@ -20,6 +29,8 @@ struct AssetHandle : uint64
{
return Content.GetAsset<T>(this, contentManager);
}
public int GetHashCode() => _uuid.GetHashCode();
}
struct AssetHandle<T> where T : Asset
@@ -30,25 +41,27 @@ struct AssetHandle<T> where T : Asset
* We don't increment/decrement the reference counter since we guarantee that we query for the asset every frame.
*/
private T _asset;
private uint8 _currentFrame;
//private uint8 _currentFrame;
//private uint64 _actualCurrentFrame = 0;
public const Self Invalid = .();
public this(AssetHandle handle, IContentManager contentManager = null)
{
_handle = handle;
_contentManager = contentManager;
_asset = handle.Get<T>(contentManager);
_contentManager = _asset.ContentManager;
_currentFrame = (uint8)Application.Get().GameTime.FrameCount;
_contentManager = _asset?.ContentManager;
if (_contentManager == null)
_contentManager = contentManager;
//_currentFrame = (uint8)Application.Get().GameTime.FrameCount;
}
// Creates a new invalid asset handle
private this()
{
_handle = .Invalid;
_currentFrame = 0;
//_currentFrame = 0;
_contentManager = null;
_asset = null;
}
@@ -71,11 +84,14 @@ struct AssetHandle<T> where T : Asset
public T Get(IContentManager contentManager = null) mut
{
// We only care whether we are in a different frame -> we only compare the lower 8 bits.
uint8 actualFrame = (uint8)Application.Get().GameTime.FrameCount;
//uint8 actualFrame = (uint8)Application.Get().GameTime.FrameCount;
//var actualActualFrame = Application.Get().GameTime.FrameCount;
if (actualFrame != _currentFrame)
//if (actualFrame != _currentFrame)
{
_asset = Content.GetAsset<T>(_handle, contentManager == null ? _contentManager : contentManager);
//_currentFrame = actualFrame;
//_actualCurrentFrame = Application.Get().GameTime.FrameCount;
}
return _asset;
@@ -234,4 +250,4 @@ struct AssetHandle<T> where T : Asset
Compiler.EmitTypeBody(typeof(Self), code);
}
}
}
}
@@ -86,6 +86,26 @@ namespace GlitchyEngine.Content
return (T)asset;
}
public static AssetHandle ManageAsset(Asset asset, IContentManager contentManager = null)
{
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
return contentManager.ManageAsset(asset);
}
/*public static AssetHandle<T> ManageAsset<T>(T asset, IContentManager contentManager = null) where T : Asset
{
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
contentManager.ManageAsset(asset);
}*/
}
interface IContentManager
+13 -9
View File
@@ -229,7 +229,7 @@ namespace GlitchyEngine.Content
return .Success;
}
public static EcsEntity LoadModel(String filename, Material material, EcsWorld world,
/*public static EcsEntity LoadModel(String filename, Material material, EcsWorld world,
List<AnimationClip> outClips, StringView entityName = StringView())
{
CGLTF.Options options = .();
@@ -252,7 +252,7 @@ namespace GlitchyEngine.Content
CGLTF.Free(data);
return entity;
}
}*/
private static (EcsEntity Entity, TransformComponent* Transform) CreateEntity(EcsWorld world, StringView? name, EcsEntity parent)
{
@@ -280,7 +280,7 @@ namespace GlitchyEngine.Content
return (entity, childTransform);
}
private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, EcsEntity parentEntity, EcsWorld world, Material material, List<AnimationClip> clips)
/*private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, EcsEntity parentEntity, EcsWorld world, Material material, List<AnimationClip> clips)
{
(EcsEntity entity, TransformComponent* childTransform) = CreateEntity(world, node.Name == null ? null : StringView(node.Name), parentEntity);
@@ -328,13 +328,13 @@ namespace GlitchyEngine.Content
using (var geo = PrimitiveToGeoBinding(node.Mesh.Primitives[0]))
{
mesh.Mesh = geo;
mesh.Mesh = Content.ManageAsset(geo);
}
if(skeleton == null)
{
var meshRenderer = world.AssignComponent<MeshRendererComponent>(entity);
meshRenderer.Material = material;
meshRenderer.Material = material.Handle;
}
else
{
@@ -352,14 +352,18 @@ namespace GlitchyEngine.Content
var meshParent = world.AssignComponent<ParentComponent>(meshEntity);
meshParent.Entity = entity;
var mesh = world.AssignComponent<MeshComponent>(meshEntity);
mesh.Mesh = PrimitiveToGeoBinding(primitive);
using (var geo = PrimitiveToGeoBinding(primitive))
{
mesh.Mesh = Content.ManageAsset(geo);
}
if(skeleton == null)
{
var meshRenderer = world.AssignComponent<MeshRendererComponent>(meshEntity);
meshRenderer.Material = material;
meshRenderer.Material = material.Handle;
}
else
{
@@ -377,7 +381,7 @@ namespace GlitchyEngine.Content
{
NodesToEntities(data, child, entity, world, material, clips);
}
}
}*/
public static GeometryBinding PrimitiveToGeoBinding(CGLTF.Primitive primitive)
{