Manage all Assets with ContentManager

This commit is contained in:
Simon Lübeß
2025-02-16 01:02:41 +01:00
parent 25e1c53a08
commit 49c3e93e08
10 changed files with 57 additions and 33 deletions
+5 -9
View File
@@ -20,13 +20,13 @@ namespace GlitchyEngine
private bool _running = true; private bool _running = true;
private bool _isMinimized = false; private bool _isMinimized = false;
private LayerStack _layerStack; private append LayerStack _layerStack = .() ~ delete:append _;
#if IMGUI #if IMGUI
private ImGuiLayer _imGuiLayer; private ImGuiLayer _imGuiLayer;
#endif #endif
private GameTime _gameTime; private append GameTime _gameTime = .() ~ delete:append _;
private IContentManager _contentManager; private IContentManager _contentManager;
@@ -56,8 +56,7 @@ namespace GlitchyEngine
Log.EngineLogger.Assert(s_Instance == null, "Tried to create a second application."); Log.EngineLogger.Assert(s_Instance == null, "Tried to create a second application.");
s_Instance = this; s_Instance = this;
_layerStack = new LayerStack(); _contentManager = InitContentManager();
_gameTime = new GameTime(true);
WindowDescription windowDesc = .Default; WindowDescription windowDesc = .Default;
windowDesc.Icon = "Resources/Textures/GlitchyEngineIcon.ico"; windowDesc.Icon = "Resources/Textures/GlitchyEngineIcon.ico";
@@ -67,8 +66,6 @@ namespace GlitchyEngine
Input.Init(); Input.Init();
_contentManager = InitContentManager();
// TODO: RenderAPI in RenderCommand initialisieren? // TODO: RenderAPI in RenderCommand initialisieren?
_rendererApi = new RendererAPI(); _rendererApi = new RendererAPI();
_rendererApi.Context = _window.Context; _rendererApi.Context = _window.Context;
@@ -107,13 +104,12 @@ namespace GlitchyEngine
delete _contentManager; delete _contentManager;
delete _layerStack; // TODO: Was there a reason, that layerstack gets deleted before _rendererApi and _window but was created before it?
//delete _layerStack;
delete _rendererApi; delete _rendererApi;
delete _window; delete _window;
delete _gameTime;
ScriptEngine.Shutdown(); ScriptEngine.Shutdown();
} }
+6 -1
View File
@@ -22,7 +22,7 @@ abstract class Asset : RefCounter
public StringView Identifier public StringView Identifier
{ {
get => _identifier; get => _identifier;
internal set => _identifier.Set(value); set => _identifier.Set(value);
} }
/// If true the asset is completely loaded. If false it is only partially loaded (if at all). /// If true the asset is completely loaded. If false it is only partially loaded (if at all).
@@ -40,6 +40,11 @@ abstract class Asset : RefCounter
((.)new => AssetSerialize, new => AssetDeserialize)); ((.)new => AssetSerialize, new => AssetDeserialize));
} }
protected this()
{
Content.ManageAsset(this);
}
protected ~this() protected ~this()
{ {
// TODO: crash when _contentManager is deleted first... // TODO: crash when _contentManager is deleted first...
+2 -2
View File
@@ -99,7 +99,7 @@ namespace GlitchyEngine.Content
var contentManager; var contentManager;
if (contentManager == null) if (contentManager == null)
contentManager = Application.Get().ContentManager; contentManager = Application.Instance.ContentManager;
Asset asset = contentManager.GetAsset(typeof(T), handle, blocking); Asset asset = contentManager.GetAsset(typeof(T), handle, blocking);
@@ -124,7 +124,7 @@ namespace GlitchyEngine.Content
var contentManager; var contentManager;
if (contentManager == null) if (contentManager == null)
contentManager = Application.Get().ContentManager; contentManager = Application.Instance.ContentManager;
return contentManager.ManageAsset(asset); return contentManager.ManageAsset(asset);
} }
@@ -18,7 +18,7 @@ namespace GlitchyEngine.Renderer
protected internal ID3D11Texture2D* _nativeTexture ~ _?.Release(); protected internal ID3D11Texture2D* _nativeTexture ~ _?.Release();
protected internal ID3D11RenderTargetView* _nativeRenderTargetView ~ _?.Release(); protected internal ID3D11RenderTargetView* _nativeRenderTargetView ~ _?.Release();
private void ReleaseAndNullify() internal void ReleaseAndNullifyD3DObjects()
{ {
Debug.Profiler.ProfileResourceFunction!(); Debug.Profiler.ProfileResourceFunction!();
@@ -29,15 +29,25 @@ namespace GlitchyEngine.Renderer
ReleaseRefAndNullify!(_depthStenilTarget); ReleaseRefAndNullify!(_depthStenilTarget);
} }
public override void Resize(uint32 width, uint32 height) public override void Resize(uint32 width, uint32 height, Format? newPixelFormat, DepthStencilFormat? newDepthStencilFormat)
{ {
Debug.Profiler.ProfileResourceFunction!(); Debug.Profiler.ProfileResourceFunction!();
ReleaseAndNullify(); ReleaseAndNullifyD3DObjects();
_description.Width = width; _description.Width = width;
_description.Height = height; _description.Height = height;
if (newPixelFormat != null)
{
_description.PixelFormat = newPixelFormat.Value;
}
if (newDepthStencilFormat != null)
{
_description.DepthStencilFormat = newDepthStencilFormat.Value;
}
ApplyChanges(); ApplyChanges();
} }
@@ -45,7 +55,7 @@ namespace GlitchyEngine.Renderer
{ {
Debug.Profiler.ProfileResourceFunction!(); Debug.Profiler.ProfileResourceFunction!();
ReleaseAndNullify(); ReleaseAndNullifyD3DObjects();
if(_description.IsSwapchainTarget) if(_description.IsSwapchainTarget)
{ {
@@ -123,7 +133,7 @@ namespace GlitchyEngine.Renderer
protected override TextureViewBinding PlatformGetViewBinding() protected override TextureViewBinding PlatformGetViewBinding()
{ {
return new .(_nativeResourceView, _samplerState.nativeSamplerState); return new .(_nativeResourceView, _samplerState?.nativeSamplerState);
} }
protected override void PlatformSneakySwappyTexture(RenderTarget2D otherTexture) protected override void PlatformSneakySwappyTexture(RenderTarget2D otherTexture)
@@ -72,14 +72,16 @@ namespace GlitchyEngine.Renderer
if(nativeSwapChain != null) if(nativeSwapChain != null)
{ {
//nativeBackBufferTarget.Release(); // It's important to not delete the _backBuffer-Object, because we want others to be able to rely on this backbuffer-reference.
_backBuffer.ReleaseRef(); _backBuffer.ReleaseAndNullifyD3DObjects();
var resizeResult = nativeSwapChain.ResizeBuffers(backBufferCount, _width, _height, backBufferFormat, .None); var resizeResult = nativeSwapChain.ResizeBuffers(backBufferCount, _width, _height, backBufferFormat, .None);
if(resizeResult.Failed) if(resizeResult.Failed)
{ {
Log.EngineLogger.Error($"Failed to resize swap chain. Message({(int)resizeResult}):{resizeResult}"); Log.EngineLogger.Error($"Failed to resize swap chain. Message({(int)resizeResult}):{resizeResult}");
} }
_backBuffer.Resize(_width, _height, backBufferFormat, _depthStencilFormat);
} }
else else
{ {
@@ -108,14 +110,15 @@ namespace GlitchyEngine.Renderer
} }
factory.Release(); factory.Release();
}
RenderTarget2DDescription desc = .(backBufferFormat, _width, _height); RenderTarget2DDescription desc = .(backBufferFormat, _width, _height);
desc.DepthStencilFormat = _depthStencilFormat; desc.DepthStencilFormat = _depthStencilFormat;
desc.IsSwapchainTarget = true; desc.IsSwapchainTarget = true;
// Todo: perhaps just update the render target
_backBuffer = new RenderTarget2D(desc); _backBuffer = new RenderTarget2D(desc);
_backBuffer.Identifier = "Backbuffer";
_backBuffer.SamplerState = SamplerStateManager.LinearClamp;
}
_backBufferViewport = GlitchyEngine.Renderer.Viewport(0, 0, _width, _height, 0.0f, 1.0f); _backBufferViewport = GlitchyEngine.Renderer.Viewport(0, 0, _width, _height, 0.0f, 1.0f);
} }
@@ -13,6 +13,7 @@ namespace GlitchyEngine.Renderer
{ {
_nativeShaderResourceView = shaderResourceView; _nativeShaderResourceView = shaderResourceView;
// TODO: What is the 200 check for?
if ((uint)(void*)_nativeShaderResourceView != 0 && (uint)(void*)_nativeShaderResourceView < 200) if ((uint)(void*)_nativeShaderResourceView != 0 && (uint)(void*)_nativeShaderResourceView < 200)
{ {
@@ -24,6 +25,12 @@ namespace GlitchyEngine.Renderer
_nativeSamplerState?.AddRef(); _nativeSamplerState?.AddRef();
} }
protected ~this()
{
_nativeShaderResourceView?.Release();
_nativeSamplerState?.Release();
}
public static override TextureViewBinding CreateDefault() => new .(null, null); public static override TextureViewBinding CreateDefault() => new .(null, null);
} }
} }
+2 -1
View File
@@ -72,7 +72,8 @@ namespace GlitchyEngine.Renderer
PlatformApplyChanges(); PlatformApplyChanges();
} }
public extern void Resize(uint32 width, uint32 height); /// Resizes the RenderTarget and optionally changes the pixel format.
public extern void Resize(uint32 width, uint32 height, Format? newPixelFormat = null, DepthStencilFormat? newDepthStencilFormat = null);
protected extern void PlatformApplyChanges(); protected extern void PlatformApplyChanges();
+1 -1
View File
@@ -69,7 +69,7 @@ namespace GlitchyEngine.Renderer
Target = new RenderTargetGroup(targetDesc); Target = new RenderTargetGroup(targetDesc);
// TODO: there needs to be a proper way to do it // TODO: there needs to be a proper way to do it
Target.[Friend]Identifier = "GBuffer"; Target.[Friend]Identifier = "GBuffer";
Content.ManageAsset(Target, null); //Content.ManageAsset(Target, null);
} }
_width = width; _width = width;
@@ -570,6 +570,8 @@ namespace GlitchyEngine.Renderer.Text
if (text.Glyphs.Count == 0) if (text.Glyphs.Count == 0)
return; return;
//Renderer2D.Flush();
// TODO: this doesn't really work with fallback fonts unless we use the same settings for all fonts // TODO: this doesn't really work with fallback fonts unless we use the same settings for all fonts
float2 unitRange = ((float)text.Font._range) / float2(text.Font._atlas.Width, text.Font._atlas.Height); float2 unitRange = ((float)text.Font._range) / float2(text.Font._atlas.Width, text.Font._atlas.Height);
_msdfMaterial.SetVariable("UnitRange", unitRange); _msdfMaterial.SetVariable("UnitRange", unitRange);
+2 -2
View File
@@ -1062,7 +1062,7 @@ static class ScriptGlue
using (material = new Material(material, true)) using (material = new Material(material, true))
{ {
material.Identifier = scope $"(Instance) {material.Identifier}"; material.Identifier = scope $"(Instance) {material.Identifier}";
Content.ManageAsset(material); //Content.ManageAsset(material);
spriteRenderer.Material = material.Handle; spriteRenderer.Material = material.Handle;
} }
} }
@@ -1183,7 +1183,7 @@ static class ScriptGlue
using (material = new Material(material, true)) using (material = new Material(material, true))
{ {
material.Identifier = scope $"(Instance) {material.Identifier}"; material.Identifier = scope $"(Instance) {material.Identifier}";
Content.ManageAsset(material); //Content.ManageAsset(material);
meshRenderer.Material = material.Handle; meshRenderer.Material = material.Handle;
} }
} }