More asset handles

This commit is contained in:
Simon Lübeß
2023-03-13 20:35:36 +01:00
parent c22d122921
commit c3fb9bbd5d
11 changed files with 97 additions and 75 deletions
+1
View File
@@ -1,5 +1,6 @@
FileVersion = 1 FileVersion = 1
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}, msdfgen-beef = {Path = "GlitchyEngine/vendor/msdfgen/msdfgen-beef"}, ImGui = {Path = "GlitchyEngine/vendor/imgui/ImGui"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplDX11"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplWin32"}, ImGuizmo = {Path = "GlitchyEngine/vendor/imgui/ImGuizmo"}, GlitchyEngineHelper = {Path = "GlitchyEngineHelper"}, bon = {Path = "GlitchyEngine/vendor/bon"}, box2d-beef = {Path = "GlitchyEngine/vendor/box2D"}, "Beef.Linq" = {Path = "GlitchyEngine/vendor/Beef.Linq/src"}} Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}, msdfgen-beef = {Path = "GlitchyEngine/vendor/msdfgen/msdfgen-beef"}, ImGui = {Path = "GlitchyEngine/vendor/imgui/ImGui"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplDX11"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplWin32"}, ImGuizmo = {Path = "GlitchyEngine/vendor/imgui/ImGuizmo"}, GlitchyEngineHelper = {Path = "GlitchyEngineHelper"}, bon = {Path = "GlitchyEngine/vendor/bon"}, box2d-beef = {Path = "GlitchyEngine/vendor/box2D"}, "Beef.Linq" = {Path = "GlitchyEngine/vendor/Beef.Linq/src"}}
Unlocked = ["corlib"]
WorkspaceFolders = {GlitchyEngine = ["GlitchyEngine", "GlitchLog", "GlitchyEngineHelper"], "GlitchyEngine/Dependencies" = ["cgltf-beef", "DirectX", "FreeType", "ImGui", "ImGuiImplDX11", "ImGuiImplWin32", "ImGuizmo", "LodePng", "msdfgen-beef", "bon", "box2d-beef", "Beef.Linq"]} WorkspaceFolders = {GlitchyEngine = ["GlitchyEngine", "GlitchLog", "GlitchyEngineHelper"], "GlitchyEngine/Dependencies" = ["cgltf-beef", "DirectX", "FreeType", "ImGui", "ImGuiImplDX11", "ImGuiImplWin32", "ImGuizmo", "LodePng", "msdfgen-beef", "bon", "box2d-beef", "Beef.Linq"]}
[Workspace] [Workspace]
@@ -29,7 +29,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
mixin DropAssetTarget<T>() where T : Asset mixin DropAssetTarget<T>() where T : Asset
{ {
Asset asset = null; AssetHandle handle = .Invalid;
if (ImGui.BeginDragDropTarget()) if (ImGui.BeginDragDropTarget())
{ {
@@ -39,13 +39,13 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
{ {
StringView fullpath = .((char8*)payload.Data, (int)payload.DataSize); StringView fullpath = .((char8*)payload.Data, (int)payload.DataSize);
asset = Content.LoadAsset<Asset>(fullpath); handle = Content.LoadAsset(fullpath);
} }
ImGui.EndDragDropTarget(); ImGui.EndDragDropTarget();
} }
asset handle
} }
public this(AssetFile asset) : base(asset) public this(AssetFile asset) : base(asset)
@@ -84,11 +84,11 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
{ {
StringView path = .((char8*)payload.Data, (int)payload.DataSize); StringView path = .((char8*)payload.Data, (int)payload.DataSize);
using (Texture2D newTexture = Content.LoadAsset<Texture2D>(path))//new Texture2D(path, true)) AssetHandle<Texture2D> newTexture = Content.LoadAsset(path);//new Texture2D(path, true))
{
newTexture.SamplerState = SamplerStateManager.AnisotropicWrap; newTexture.Get().SamplerState = SamplerStateManager.AnisotropicWrap;
material.SetTexture(texture.key, newTexture); //material.SetTexture(texture.key, newTexture);
} // TODO!!!
} }
ImGui.EndDragDropTarget(); ImGui.EndDragDropTarget();
@@ -268,10 +268,7 @@ namespace GlitchyEditor.EditWindows
StringView path = .((char8*)payload.Data, (int)payload.DataSize); StringView path = .((char8*)payload.Data, (int)payload.DataSize);
using (Texture2D texture = Content.LoadAsset<Texture2D>(path)) spriteRendererComponent.Sprite = Content.LoadAsset(path);
{
spriteRendererComponent.Sprite = texture;
}
} }
ImGui.EndDragDropTarget(); ImGui.EndDragDropTarget();
@@ -301,10 +298,7 @@ namespace GlitchyEditor.EditWindows
{ {
StringView fullpath = .((char8*)payload.Data, (int)payload.DataSize); StringView fullpath = .((char8*)payload.Data, (int)payload.DataSize);
using (Material loadedMaterial = Content.LoadAsset<Material>(fullpath)) meshRendererComponent.Material = Content.LoadAsset(fullpath);
{
meshRendererComponent.Material = loadedMaterial;
}
} }
ImGui.EndDragDropTarget(); ImGui.EndDragDropTarget();
@@ -488,10 +482,7 @@ namespace GlitchyEditor.EditWindows
StringView filePath = fullpath.Substring(0, idx); StringView filePath = fullpath.Substring(0, idx);
StringView meshName = fullpath.Substring(idx + 1);*/ StringView meshName = fullpath.Substring(idx + 1);*/
using (GeometryBinding geometry = Content.LoadAsset<GeometryBinding>(fullpath)) meshComponent.Mesh = Content.LoadAsset(fullpath);
{
meshComponent.Mesh = geometry;
}
// TODO: support multiple primitives (treat every primitive as a single mesh? or: mesh can have multiple primitives) // TODO: support multiple primitives (treat every primitive as a single mesh? or: mesh can have multiple primitives)
/*using (GeometryBinding binding = ModelLoader.LoadMesh(filePath, meshName, 0)) /*using (GeometryBinding binding = ModelLoader.LoadMesh(filePath, meshName, 0))
@@ -18,7 +18,8 @@ class PropertiesWindow : EditorWindow
private append String _selectedFileName = .(); private append String _selectedFileName = .();
private Asset _currentAsset ~ _?.ReleaseRef(); private AssetHandle _currentAssetHandle;
private Asset _currentAsset;
public this(Editor editor) public this(Editor editor)
{ {
@@ -78,7 +79,8 @@ class PropertiesWindow : EditorWindow
if (_currentAsset?.Identifier != assetFile.Identifier) if (_currentAsset?.Identifier != assetFile.Identifier)
{ {
_currentAsset?.ReleaseRef(); _currentAsset?.ReleaseRef();
_currentAsset = _editor.ContentManager.LoadAsset(assetFile.Identifier); _currentAssetHandle = _editor.ContentManager.LoadAsset(assetFile.Identifier);
_currentAsset = _editor.ContentManager.GetAsset(null, _currentAssetHandle);
} }
// TODO: allow changing AssetLoader // TODO: allow changing AssetLoader
+5 -4
View File
@@ -2,12 +2,13 @@ using System;
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using GlitchyEngine.Math; using GlitchyEngine.Math;
using GlitchyEngine; using GlitchyEngine;
using GlitchyEngine.Content;
namespace GlitchyEditor namespace GlitchyEditor
{ {
class EditorIcons : RefCounted class EditorIcons : RefCounted
{ {
Texture2D _texture ~ _.ReleaseRef(); AssetHandle<Texture2D> _texture;
public SubTexture2D DirectionalLight ~ _.ReleaseRef(); public SubTexture2D DirectionalLight ~ _.ReleaseRef();
public SubTexture2D Camera ~ _.ReleaseRef(); public SubTexture2D Camera ~ _.ReleaseRef();
@@ -18,13 +19,13 @@ namespace GlitchyEditor
public SamplerState SamplerState public SamplerState SamplerState
{ {
get => _texture.SamplerState; get => _texture.Get().SamplerState;
set => _texture.SamplerState = value; set => _texture.Get().SamplerState = value;
} }
public this(String texturePath, Vector2 iconSize) public this(String texturePath, Vector2 iconSize)
{ {
_texture = Content.LoadAsset<Texture2D>(texturePath);//new Texture2D(texturePath); _texture = Content.LoadAsset(texturePath);//new Texture2D(texturePath);
Vector2 pen = .(); Vector2 pen = .();
+31 -5
View File
@@ -32,6 +32,8 @@ struct AssetHandle<T> where T : Asset
private T _asset; private T _asset;
private uint8 _currentFrame; private uint8 _currentFrame;
public const Self Invalid = .();
public this(AssetHandle handle, IContentManager contentManager = null) public this(AssetHandle handle, IContentManager contentManager = null)
{ {
_handle = handle; _handle = handle;
@@ -42,6 +44,30 @@ struct AssetHandle<T> where T : Asset
_currentFrame = (uint8)Application.Get().GameTime.FrameCount; _currentFrame = (uint8)Application.Get().GameTime.FrameCount;
} }
// Creates a new invalid asset handle
private this()
{
_handle = .Invalid;
_currentFrame = 0;
_contentManager = null;
_asset = null;
}
public static implicit operator Self(AssetHandle handle)
{
return Self(handle);
}
public static implicit operator AssetHandle(Self handle)
{
return handle._handle;
}
public static implicit operator T(ref Self handle)
{
return handle.Get();
}
public T Get(IContentManager contentManager = null) mut public T Get(IContentManager contentManager = null) mut
{ {
// We only care whether we are in a different frame -> we only compare the lower 8 bits. // We only care whether we are in a different frame -> we only compare the lower 8 bits.
@@ -58,7 +84,7 @@ struct AssetHandle<T> where T : Asset
[Comptime, OnCompile(.TypeInit)] [Comptime, OnCompile(.TypeInit)]
static void Init() static void Init()
{ {
for (var field in typeof(T).GetFields()) for (var field in typeof(T).GetFields(BindingFlags.FlattenHierarchy))
{ {
if (field.IsStatic || !field.IsPublic) if (field.IsStatic || !field.IsPublic)
continue; continue;
@@ -86,7 +112,7 @@ struct AssetHandle<T> where T : Asset
Dictionary<StringView, (MethodInfo? Getter, MethodInfo? Setter)> properties = scope .(); Dictionary<StringView, (MethodInfo? Getter, MethodInfo? Setter)> properties = scope .();
for (var method in typeof(T).GetMethods()) for (var method in typeof(T).GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public))
{ {
if (method.IsStatic || !method.IsPublic || method.IsConstructor || method.IsDestructor) if (method.IsStatic || !method.IsPublic || method.IsConstructor || method.IsDestructor)
continue; continue;
@@ -137,8 +163,8 @@ struct AssetHandle<T> where T : Asset
{ {
Type paramType = method.GetParamType(param); Type paramType = method.GetParamType(param);
StringView paramName = method.GetParamName(param); StringView paramName = method.GetParamName(param);
//String buffer = scope .(); /*String buffer = scope .();
//method.GetParamsDecl(buffer); method.GetParamsDecl(buffer);*/
if (param != 0) if (param != 0)
{ {
@@ -151,7 +177,7 @@ struct AssetHandle<T> where T : Asset
/*if (!buffer.IsEmpty) /*if (!buffer.IsEmpty)
{ {
parameters.AppendF($" = {buffer}"); parameters.AppendF($"/*{buffer}*/");
}*/ }*/
arguments.AppendF($" {paramName}"); arguments.AppendF($" {paramName}");
+6 -3
View File
@@ -1,12 +1,15 @@
using System; using System;
using GlitchyEngine.World; using GlitchyEngine.World;
using GlitchyEngine.Content;
namespace GlitchyEngine.Renderer namespace GlitchyEngine.Renderer
{ {
public struct MeshComponent : IDisposableComponent public struct MeshComponent// : IDisposableComponent
{ {
public AssetHandle<GeometryBinding> Mesh {get; set mut;}
/*
private GeometryBinding _mesh; private GeometryBinding _mesh;
public GeometryBinding Mesh public AssetHandle<GeometryBinding> Mesh
{ {
[Inline] [Inline]
get => _mesh; get => _mesh;
@@ -22,6 +25,6 @@ namespace GlitchyEngine.Renderer
public void Dispose() mut public void Dispose() mut
{ {
ReleaseRefAndNullify!(_mesh); ReleaseRefAndNullify!(_mesh);
} }*/
} }
} }
+23 -25
View File
@@ -80,13 +80,13 @@ namespace GlitchyEngine.Renderer
static SceneConstants _sceneConstants; static SceneConstants _sceneConstants;
static Effect LineEffect; static AssetHandle<Effect> LineEffect;
static VertexBuffer LineVertices; static VertexBuffer LineVertices;
static GeometryBinding LineGeometry; static GeometryBinding LineGeometry;
static GBuffer _gBuffer; static GBuffer _gBuffer;
static Effect TestFullscreenEffect; static AssetHandle<Effect> TestFullscreenEffect;
static Effect s_tonemappingEffect; static AssetHandle<Effect> s_tonemappingEffect;
static BlendState _gBufferBlend; static BlendState _gBufferBlend;
static BlendState _lightBlend; static BlendState _lightBlend;
@@ -133,7 +133,7 @@ namespace GlitchyEngine.Renderer
{ {
Debug.Profiler.ProfileFunction!(); Debug.Profiler.ProfileFunction!();
LineEffect = Content.LoadAsset<Effect>("Shaders\\lineShader.hlsl"); LineEffect = Content.LoadAsset("Shaders\\lineShader.hlsl");
LineGeometry = new GeometryBinding(); LineGeometry = new GeometryBinding();
LineGeometry.SetPrimitiveTopology(.LineList); LineGeometry.SetPrimitiveTopology(.LineList);
@@ -159,13 +159,12 @@ namespace GlitchyEngine.Renderer
{ {
LineVertices.ReleaseRef(); LineVertices.ReleaseRef();
LineGeometry.ReleaseRef(); LineGeometry.ReleaseRef();
LineEffect.ReleaseRef();
} }
static void InitDeferredRenderer() static void InitDeferredRenderer()
{ {
TestFullscreenEffect = Content.LoadAsset<Effect>("Shaders\\simpleLight.hlsl"); TestFullscreenEffect = Content.LoadAsset("Shaders\\simpleLight.hlsl");
s_tonemappingEffect = Content.LoadAsset<Effect>("Shaders\\SimpleTonemapping.hlsl"); s_tonemappingEffect = Content.LoadAsset("Shaders\\SimpleTonemapping.hlsl");
_gBuffer = new GBuffer(); _gBuffer = new GBuffer();
BlendStateDescription gBufferBlendDesc = .Default; BlendStateDescription gBufferBlendDesc = .Default;
@@ -204,9 +203,6 @@ namespace GlitchyEngine.Renderer
_lightBlend.ReleaseRef(); _lightBlend.ReleaseRef();
_gBufferBlend.ReleaseRef(); _gBufferBlend.ReleaseRef();
delete _gBuffer; delete _gBuffer;
s_tonemappingEffect.ReleaseRef();
TestFullscreenEffect.ReleaseRef();
} }
// [Obsolete("", false)] // [Obsolete("", false)]
@@ -347,22 +343,23 @@ namespace GlitchyEngine.Renderer
Vector3 lightDir = -light.Transform.Forward; Vector3 lightDir = -light.Transform.Forward;
TestFullscreenEffect.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0); Effect fsEffect = TestFullscreenEffect.Get();
TestFullscreenEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1); fsEffect.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0);
TestFullscreenEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2); fsEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1);
TestFullscreenEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3); fsEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2);
TestFullscreenEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 4); fsEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3);
fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 4);
TestFullscreenEffect.Variables["LightColor"].SetData(light.Light.Color); fsEffect.Variables["LightColor"].SetData(light.Light.Color);
TestFullscreenEffect.Variables["Illuminance"].SetData(light.Light.Illuminance); fsEffect.Variables["Illuminance"].SetData(light.Light.Illuminance);
TestFullscreenEffect.Variables["LightDir"].SetData(lightDir); fsEffect.Variables["LightDir"].SetData(lightDir);
TestFullscreenEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition); fsEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition);
TestFullscreenEffect.Variables["Scaling"].SetData(scaling); fsEffect.Variables["Scaling"].SetData(scaling);
TestFullscreenEffect.ApplyChanges(); fsEffect.ApplyChanges();
TestFullscreenEffect.Bind(); fsEffect.Bind();
//RenderCommand.BindEffect(TestFullscreenEffect); //RenderCommand.BindEffect(TestFullscreenEffect);
@@ -380,10 +377,11 @@ namespace GlitchyEngine.Renderer
RenderCommand.BindRenderTargets(); RenderCommand.BindRenderTargets();
RenderCommand.SetRenderTargetGroup(_sceneConstants.CompositionTarget, true); RenderCommand.SetRenderTargetGroup(_sceneConstants.CompositionTarget, true);
Effect toneMappingFx = s_tonemappingEffect.Get();
// TODO: Postprocessing effects // TODO: Postprocessing effects
s_tonemappingEffect.SetTexture("CameraTarget", _sceneConstants.CameraTarget, 0); toneMappingFx.SetTexture("CameraTarget", _sceneConstants.CameraTarget, 0);
s_tonemappingEffect.ApplyChanges(); toneMappingFx.ApplyChanges();
s_tonemappingEffect.Bind(); toneMappingFx.Bind();
RenderCommand.BindRenderTargets(); RenderCommand.BindRenderTargets();
//RenderCommand.BindEffect(s_tonemappingEffect); //RenderCommand.BindEffect(s_tonemappingEffect);
+8 -7
View File
@@ -3,6 +3,7 @@ using GlitchyEngine.Math;
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using GlitchyEngine.Core; using GlitchyEngine.Core;
using Box2D; using Box2D;
using GlitchyEngine.Content;
namespace GlitchyEngine.World namespace GlitchyEngine.World
{ {
@@ -44,19 +45,19 @@ namespace GlitchyEngine.World
} }
[Component("Sprite Renderer")] [Component("Sprite Renderer")]
struct SpriterRendererComponent : IDisposableComponent struct SpriterRendererComponent// : IDisposableComponent
{ {
private Texture2D _sprite = null; private AssetHandle<Texture2D> _sprite = .Invalid;
public Texture2D Sprite public AssetHandle<Texture2D> Sprite
{ {
get => _sprite; get => _sprite;
set mut set mut
{ {
if (_sprite == value) if (_sprite == value)
return; return;
_sprite = value;
SetReference!(_sprite, value); //SetReference!(_sprite, value);
} }
} }
@@ -74,10 +75,10 @@ namespace GlitchyEngine.World
Color = color; Color = color;
} }
public void Dispose() /*public void Dispose()
{ {
_sprite?.ReleaseRef(); _sprite?.ReleaseRef();
} }*/
} }
struct SceneCamera : Camera struct SceneCamera : Camera
@@ -1,14 +1,15 @@
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using System; using System;
using GlitchyEngine.Content;
namespace GlitchyEngine.World namespace GlitchyEngine.World
{ {
/// A component that allows to render a mesh. /// A component that allows to render a mesh.
public struct MeshRendererComponent : IDisposableComponent public struct MeshRendererComponent// : IDisposableComponent
{ {
private Material _material; private AssetHandle<Material> _material;
public Material Material public AssetHandle<Material> Material
{ {
[Inline] [Inline]
get => _material; get => _material;
@@ -17,15 +18,13 @@ namespace GlitchyEngine.World
if(_material == value) if(_material == value)
return; return;
_material?.ReleaseRef();
_material = value; _material = value;
_material?.AddRef();
} }
} }
public void Dispose() mut /*public void Dispose() mut
{ {
_material?.ReleaseRef(); _material?.ReleaseRef();
} }*/
} }
} }
+1 -1
View File
@@ -88,7 +88,7 @@ namespace GlitchyEngine.World
{ {
Serialize.Value(writer, "Color", component.Color); Serialize.Value(writer, "Color", component.Color);
Serialize.Value(writer, "IsCircle", component.IsCircle); Serialize.Value(writer, "IsCircle", component.IsCircle);
Serialize.Value(writer, "Sprite", component.Sprite?.Identifier); Serialize.Value(writer, "Sprite", component.Sprite.Get().Identifier);
Serialize.Value(writer, "UvTransform", component.UvTransform); Serialize.Value(writer, "UvTransform", component.UvTransform);
}); });