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
+6 -10
View File
@@ -20,13 +20,13 @@ namespace GlitchyEngine
private bool _running = true;
private bool _isMinimized = false;
private LayerStack _layerStack;
private append LayerStack _layerStack = .() ~ delete:append _;
#if IMGUI
private ImGuiLayer _imGuiLayer;
#endif
private GameTime _gameTime;
private append GameTime _gameTime = .() ~ delete:append _;
private IContentManager _contentManager;
@@ -56,8 +56,7 @@ namespace GlitchyEngine
Log.EngineLogger.Assert(s_Instance == null, "Tried to create a second application.");
s_Instance = this;
_layerStack = new LayerStack();
_gameTime = new GameTime(true);
_contentManager = InitContentManager();
WindowDescription windowDesc = .Default;
windowDesc.Icon = "Resources/Textures/GlitchyEngineIcon.ico";
@@ -67,8 +66,6 @@ namespace GlitchyEngine
Input.Init();
_contentManager = InitContentManager();
// TODO: RenderAPI in RenderCommand initialisieren?
_rendererApi = new RendererAPI();
_rendererApi.Context = _window.Context;
@@ -106,13 +103,12 @@ namespace GlitchyEngine
Renderer.Deinit();
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 _window;
delete _gameTime;
ScriptEngine.Shutdown();
}
+6 -1
View File
@@ -22,7 +22,7 @@ abstract class Asset : RefCounter
public StringView 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).
@@ -40,6 +40,11 @@ abstract class Asset : RefCounter
((.)new => AssetSerialize, new => AssetDeserialize));
}
protected this()
{
Content.ManageAsset(this);
}
protected ~this()
{
// TODO: crash when _contentManager is deleted first...
+2 -2
View File
@@ -99,7 +99,7 @@ namespace GlitchyEngine.Content
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
contentManager = Application.Instance.ContentManager;
Asset asset = contentManager.GetAsset(typeof(T), handle, blocking);
@@ -124,7 +124,7 @@ namespace GlitchyEngine.Content
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
contentManager = Application.Instance.ContentManager;
return contentManager.ManageAsset(asset);
}
@@ -18,7 +18,7 @@ namespace GlitchyEngine.Renderer
protected internal ID3D11Texture2D* _nativeTexture ~ _?.Release();
protected internal ID3D11RenderTargetView* _nativeRenderTargetView ~ _?.Release();
private void ReleaseAndNullify()
internal void ReleaseAndNullifyD3DObjects()
{
Debug.Profiler.ProfileResourceFunction!();
@@ -29,15 +29,25 @@ namespace GlitchyEngine.Renderer
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!();
ReleaseAndNullify();
ReleaseAndNullifyD3DObjects();
_description.Width = width;
_description.Height = height;
if (newPixelFormat != null)
{
_description.PixelFormat = newPixelFormat.Value;
}
if (newDepthStencilFormat != null)
{
_description.DepthStencilFormat = newDepthStencilFormat.Value;
}
ApplyChanges();
}
@@ -45,7 +55,7 @@ namespace GlitchyEngine.Renderer
{
Debug.Profiler.ProfileResourceFunction!();
ReleaseAndNullify();
ReleaseAndNullifyD3DObjects();
if(_description.IsSwapchainTarget)
{
@@ -123,7 +133,7 @@ namespace GlitchyEngine.Renderer
protected override TextureViewBinding PlatformGetViewBinding()
{
return new .(_nativeResourceView, _samplerState.nativeSamplerState);
return new .(_nativeResourceView, _samplerState?.nativeSamplerState);
}
protected override void PlatformSneakySwappyTexture(RenderTarget2D otherTexture)
@@ -72,14 +72,16 @@ namespace GlitchyEngine.Renderer
if(nativeSwapChain != null)
{
//nativeBackBufferTarget.Release();
_backBuffer.ReleaseRef();
// It's important to not delete the _backBuffer-Object, because we want others to be able to rely on this backbuffer-reference.
_backBuffer.ReleaseAndNullifyD3DObjects();
var resizeResult = nativeSwapChain.ResizeBuffers(backBufferCount, _width, _height, backBufferFormat, .None);
if(resizeResult.Failed)
{
Log.EngineLogger.Error($"Failed to resize swap chain. Message({(int)resizeResult}):{resizeResult}");
}
_backBuffer.Resize(_width, _height, backBufferFormat, _depthStencilFormat);
}
else
{
@@ -108,15 +110,16 @@ namespace GlitchyEngine.Renderer
}
factory.Release();
RenderTarget2DDescription desc = .(backBufferFormat, _width, _height);
desc.DepthStencilFormat = _depthStencilFormat;
desc.IsSwapchainTarget = true;
_backBuffer = new RenderTarget2D(desc);
_backBuffer.Identifier = "Backbuffer";
_backBuffer.SamplerState = SamplerStateManager.LinearClamp;
}
RenderTarget2DDescription desc = .(backBufferFormat, _width, _height);
desc.DepthStencilFormat = _depthStencilFormat;
desc.IsSwapchainTarget = true;
// Todo: perhaps just update the render target
_backBuffer = new RenderTarget2D(desc);
_backBufferViewport = GlitchyEngine.Renderer.Viewport(0, 0, _width, _height, 0.0f, 1.0f);
}
@@ -12,7 +12,8 @@ namespace GlitchyEngine.Renderer
internal this(ID3D11ShaderResourceView* shaderResourceView, ID3D11SamplerState* samplerState)
{
_nativeShaderResourceView = shaderResourceView;
// TODO: What is the 200 check for?
if ((uint)(void*)_nativeShaderResourceView != 0 && (uint)(void*)_nativeShaderResourceView < 200)
{
@@ -23,6 +24,12 @@ namespace GlitchyEngine.Renderer
_nativeSamplerState = samplerState;
_nativeSamplerState?.AddRef();
}
protected ~this()
{
_nativeShaderResourceView?.Release();
_nativeSamplerState?.Release();
}
public static override TextureViewBinding CreateDefault() => new .(null, null);
}
+2 -1
View File
@@ -72,7 +72,8 @@ namespace GlitchyEngine.Renderer
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();
+1 -1
View File
@@ -69,7 +69,7 @@ namespace GlitchyEngine.Renderer
Target = new RenderTargetGroup(targetDesc);
// TODO: there needs to be a proper way to do it
Target.[Friend]Identifier = "GBuffer";
Content.ManageAsset(Target, null);
//Content.ManageAsset(Target, null);
}
_width = width;
@@ -569,11 +569,13 @@ namespace GlitchyEngine.Renderer.Text
if (text.Glyphs.Count == 0)
return;
//Renderer2D.Flush();
// 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);
_msdfMaterial.SetVariable("UnitRange", unitRange);
List<Texture2D> atlasses = scope .();
for (PreparedGlyph glyph in text.Glyphs)
+2 -2
View File
@@ -1062,7 +1062,7 @@ static class ScriptGlue
using (material = new Material(material, true))
{
material.Identifier = scope $"(Instance) {material.Identifier}";
Content.ManageAsset(material);
//Content.ManageAsset(material);
spriteRenderer.Material = material.Handle;
}
}
@@ -1183,7 +1183,7 @@ static class ScriptGlue
using (material = new Material(material, true))
{
material.Identifier = scope $"(Instance) {material.Identifier}";
Content.ManageAsset(material);
//Content.ManageAsset(material);
meshRenderer.Material = material.Handle;
}
}