From ce163db30b742528728d333d55118dd8ad570464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 22 Jul 2021 18:57:35 +0200 Subject: [PATCH] Unified texture loading, updated DirectX-beef --- .../src/Platform/DX11/Renderer/Dx11Texture.bf | 59 +++++++++---------- GlitchyEngine/vendor/directx | 2 +- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf index f96de74..db6679e 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf @@ -3,6 +3,7 @@ using DirectX.D3D11; using DirectX.Common; using DirectXTK; using GlitchyEngine.Math; +using System.Collections; using internal GlitchyEngine.Renderer; @@ -19,6 +20,32 @@ namespace GlitchyEngine.Renderer _context.nativeContext.VertexShader.SetShaderResources(slot, 1, &nativeView); _context.nativeContext.PixelShader.SetShaderResources(slot, 1, &nativeView); } + + /** \brief Loads the texture from the specified path. + * @param path The path of the texture to load. + * @param texture The reference to the pointer that will hold the texture. + * @returns true if the texture was loaded successfully; false otherwise. + */ + protected bool LoadResourcePlatform(StringView path, ref T* texture) where T : ID3D11Resource + { + ((ID3D11Resource*)texture)?.Release(); + nativeView?.Release(); + + HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, path.ToScopedNativeWChar!(), + (.)&texture, &nativeView); + + if(loadResult.Failed) + { + Log.EngineLogger.Error($"Failed to load texture \"{path}\". Error({(int)loadResult}): {loadResult}"); + + ReleaseAndNullify!(texture); + ReleaseAndNullify!(nativeView); + + return false; + } + + return true; + } } extension Texture2DDesc @@ -58,22 +85,8 @@ namespace GlitchyEngine.Renderer protected override void LoadTexturePlatform() { - nativeTexture?.Release(); - nativeView?.Release(); + LoadResourcePlatform(_path, ref nativeTexture); - HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, _path.ToScopedNativeWChar!(), - (.)&nativeTexture, &nativeView); - - if(loadResult.Failed) - { - Log.EngineLogger.Error($"Failed to load texture \"{_path}\". Error({(int)loadResult}): {loadResult}"); - - nativeTexture?.Release(); - nativeView?.Release(); - - // TODO: load fallback texture - } - let resType = nativeTexture.GetResourceType(); Log.EngineLogger.Assert(resType == .Texture2D, scope $"The texture \"{_path}\" is not a 2D texture (it is {resType})."); @@ -239,21 +252,7 @@ namespace GlitchyEngine.Renderer protected override void LoadTexturePlatform() { - nativeTexture?.Release(); - nativeView?.Release(); - - HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, _path.ToScopedNativeWChar!(), - (.)&nativeTexture, &nativeView); - - if(loadResult.Failed) - { - Log.EngineLogger.Error($"Failed to load texture \"{_path}\". Error({(int)loadResult}): {loadResult}"); - - nativeTexture?.Release(); - nativeView?.Release(); - - // TODO: load fallback texture - } + LoadResourcePlatform(_path, ref nativeTexture); let resType = nativeTexture.GetResourceType(); Log.EngineLogger.Assert(resType == .Texture2D, scope $"The texture \"{_path}\" is not a texture cube (it is {resType})."); diff --git a/GlitchyEngine/vendor/directx b/GlitchyEngine/vendor/directx index 0399d40..1bc6b78 160000 --- a/GlitchyEngine/vendor/directx +++ b/GlitchyEngine/vendor/directx @@ -1 +1 @@ -Subproject commit 0399d40bf807ea6c0dddf2d5274cfbf37cc7b37e +Subproject commit 1bc6b78ee4b5892b051cd7b2c42dadc80f6826c1