mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Handles barely work now, mostly no crashes
This commit is contained in:
@@ -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<>),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -212,6 +212,9 @@ public class Effect : Asset
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
if (texture == null)
|
||||
return;
|
||||
|
||||
[Inline]InternalSetTexture(name, texture.GetViewBinding());
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Material : Asset
|
||||
|
||||
private uint8[] _rawVariables ~ delete _;
|
||||
|
||||
private Dictionary<String, TextureViewBinding> _textures = new .();
|
||||
private Dictionary<String, Texture> _textures = new .();
|
||||
|
||||
private Dictionary<String, (uint32 Offset, BufferVariable Variable)> _variables = new .() ~ delete _;
|
||||
|
||||
@@ -26,12 +26,14 @@ public class Material : Asset
|
||||
|
||||
// TODO: get variables from effect
|
||||
|
||||
// Get texture slots from effect
|
||||
for(let (name, entry) in _effect.Textures)
|
||||
{
|
||||
var texture = entry.BoundTexture;
|
||||
texture.AddRef();
|
||||
// TODO: Do we want to be able to define textures in the shader?
|
||||
/*var texture = entry.BoundTexture;
|
||||
texture.AddRef();*/
|
||||
|
||||
_textures.Add(name, texture);
|
||||
_textures.Add(name, null);
|
||||
}
|
||||
|
||||
InitRawData();
|
||||
@@ -41,7 +43,7 @@ public class Material : Asset
|
||||
{
|
||||
for(let (name, texture) in _textures)
|
||||
{
|
||||
texture.Release();
|
||||
texture?.ReleaseRef();
|
||||
}
|
||||
|
||||
delete _textures;
|
||||
@@ -92,9 +94,9 @@ public class Material : Asset
|
||||
{
|
||||
if(_textures.TryGetValue(name, var entry))
|
||||
{
|
||||
entry.Release();
|
||||
_textures[name] = texture.GetViewBinding();
|
||||
//texture?.AddRef();
|
||||
entry?.ReleaseRef();
|
||||
_textures[name] = texture;
|
||||
texture?.AddRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public struct MeshComponent// : IDisposableComponent
|
||||
{
|
||||
public AssetHandle<GeometryBinding> Mesh {get; set mut;}
|
||||
public AssetHandle<GeometryBinding> Mesh {get; set mut;} = .Invalid;
|
||||
/*
|
||||
private GeometryBinding _mesh;
|
||||
public AssetHandle<GeometryBinding> Mesh
|
||||
|
||||
@@ -469,6 +469,9 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
if (geometry == null || material == null)
|
||||
return;
|
||||
|
||||
_queue.Add(SubmittedMesh(geometry, material, transform, entity.[Friend]Index));
|
||||
}
|
||||
|
||||
|
||||
@@ -876,9 +876,9 @@ namespace GlitchyEngine.Renderer
|
||||
public static void DrawSprite(Matrix transform, SpriterRendererComponent* spriteRenderer, uint32 entityId)
|
||||
{
|
||||
if (spriteRenderer.IsCircle)
|
||||
DrawCircle(transform, spriteRenderer.Sprite ?? s_whiteTexture, spriteRenderer.Color, 1.0f, spriteRenderer.UvTransform, entityId);
|
||||
DrawCircle(transform, spriteRenderer.Sprite.Get() ?? s_whiteTexture, spriteRenderer.Color, 1.0f, spriteRenderer.UvTransform, entityId);
|
||||
else
|
||||
DrawQuad(transform, spriteRenderer.Sprite ?? s_whiteTexture, spriteRenderer.Color, spriteRenderer.UvTransform, entityId);
|
||||
DrawQuad(transform, spriteRenderer.Sprite.Get() ?? s_whiteTexture, spriteRenderer.Color, spriteRenderer.UvTransform, entityId);
|
||||
}
|
||||
|
||||
// Textured quad pivot
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace GlitchyEngine.World
|
||||
/// A component that allows to render a mesh.
|
||||
public struct MeshRendererComponent// : IDisposableComponent
|
||||
{
|
||||
private AssetHandle<Material> _material;
|
||||
private AssetHandle<Material> _material = .Invalid;
|
||||
|
||||
public AssetHandle<Material> Material
|
||||
{
|
||||
|
||||
@@ -445,7 +445,7 @@ namespace GlitchyEngine.World
|
||||
|
||||
for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate<TransformComponent, MeshComponent, MeshRendererComponent>())
|
||||
{
|
||||
if (mesh.Mesh == null || meshRenderer.Material == null)
|
||||
if (mesh.Mesh == .Invalid || meshRenderer.Material == .Invalid)
|
||||
continue;
|
||||
|
||||
Renderer.Submit(mesh.Mesh, meshRenderer.Material, entity, transform.WorldTransform);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user