From 28e0edb8ad656f626b9673ea1d459c9707ff6017 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Tue, 5 Nov 2019 17:32:39 -0500 Subject: [PATCH] Prevent failed images from loading in opengl --- .../FileFormats/ASTC/ASTCDecoder.cs | 16 ++++---- .../Rendering/RenderableTex.cs | 39 ++++++++++++------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Switch_Toolbox_Library/FileFormats/ASTC/ASTCDecoder.cs b/Switch_Toolbox_Library/FileFormats/ASTC/ASTCDecoder.cs index fddf5db5..f3ff96c9 100644 --- a/Switch_Toolbox_Library/FileFormats/ASTC/ASTCDecoder.cs +++ b/Switch_Toolbox_Library/FileFormats/ASTC/ASTCDecoder.cs @@ -8,7 +8,7 @@ namespace Ryujinx.Graphics.Gal.Texture { public class ASTCDecoderException : Exception { - public ASTCDecoderException(string ExMsg) : base(ExMsg) { Toolbox.Library.Forms.STErrorDialog.Show(ExMsg, "", ExMsg); } + public ASTCDecoderException(string ExMsg) : base(ExMsg) {/* Toolbox.Library.Forms.STErrorDialog.Show(ExMsg, "", ExMsg);*/ } } //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp @@ -67,12 +67,12 @@ namespace Ryujinx.Graphics.Gal.Texture if (BlockX > 12 || BlockY > 12) { - throw new ASTCDecoderException("Block size unsupported!"); + throw new Exception("Block size unsupported!"); } if (BlockZ != 1 || Z != 1) { - throw new ASTCDecoderException("3D compressed textures unsupported!"); + throw new Exception("3D compressed textures unsupported!"); } using (MemoryStream OutputStream = new MemoryStream()) @@ -121,7 +121,7 @@ namespace Ryujinx.Graphics.Gal.Texture if (TexelParams.Error) { - throw new ASTCDecoderException("Invalid block mode"); + throw new Exception("Invalid block mode"); } // Console.WriteLine($"BlockWidth {BlockWidth} {BlockHeight} BlockHeight"); @@ -136,17 +136,17 @@ namespace Ryujinx.Graphics.Gal.Texture if (TexelParams.VoidExtentHDR) { - throw new ASTCDecoderException("HDR void extent blocks are unsupported!"); + throw new Exception("HDR void extent blocks are unsupported!"); } if (TexelParams.Width > BlockWidth) { - throw new ASTCDecoderException("Texel weight grid width should be smaller than block width"); + throw new Exception("Texel weight grid width should be smaller than block width"); } if (TexelParams.Height > BlockHeight) { - throw new ASTCDecoderException("Texel weight grid height should be smaller than block height"); + throw new Exception("Texel weight grid height should be smaller than block height"); } // Read num partitions @@ -155,7 +155,7 @@ namespace Ryujinx.Graphics.Gal.Texture if (NumberPartitions == 4 && TexelParams.DualPlane) { - throw new ASTCDecoderException("Dual plane mode is incompatible with four partition blocks"); + throw new Exception("Dual plane mode is incompatible with four partition blocks"); } // Based on the number of partitions, read the color endpoint mode for diff --git a/Switch_Toolbox_Library/Rendering/RenderableTex.cs b/Switch_Toolbox_Library/Rendering/RenderableTex.cs index e1fb98fa..3f46ccc6 100644 --- a/Switch_Toolbox_Library/Rendering/RenderableTex.cs +++ b/Switch_Toolbox_Library/Rendering/RenderableTex.cs @@ -114,11 +114,12 @@ namespace Toolbox.Library.Rendering TexID = GenerateOpenGLTexture(this, bitmap); } + private bool IsFailedState = false; private bool UseMipmaps = false; private bool UseOpenGLDecoder = true; public void LoadOpenGLTexture(STGenericTexture GenericTexture, int ArrayStartIndex = 0, bool LoadArrayLevels = false) { - if (!Runtime.OpenTKInitialized || GLInitialized || Runtime.UseLegacyGL) + if (!Runtime.OpenTKInitialized || GLInitialized || Runtime.UseLegacyGL || IsFailedState) return; width = (int)GenericTexture.Width; @@ -162,25 +163,27 @@ namespace Toolbox.Library.Rendering GenericTexture.ArrayCount = 1; List Surfaces = new List(); - if (UseMipmaps && GenericTexture.ArrayCount <= 1) + try { - //Load surfaces with mip maps - Surfaces = GenericTexture.GetSurfaces(ArrayStartIndex, false, 6); - } - else - { - //Only load first mip level. Will be generated after - for (int i = 0; i < GenericTexture.ArrayCount; i++) + if (UseMipmaps && GenericTexture.ArrayCount <= 1) { - if (i >= ArrayStartIndex && i <= ArrayStartIndex + 6) //Only load up to 6 faces + //Load surfaces with mip maps + Surfaces = GenericTexture.GetSurfaces(ArrayStartIndex, false, 6); + } + else + { + //Only load first mip level. Will be generated after + for (int i = 0; i < GenericTexture.ArrayCount; i++) { - Surfaces.Add(new STGenericTexture.Surface() + if (i >= ArrayStartIndex && i <= ArrayStartIndex + 6) //Only load up to 6 faces { - mipmaps = new List() { GenericTexture.GetImageData(i, 0) } - }); + Surfaces.Add(new STGenericTexture.Surface() + { + mipmaps = new List() { GenericTexture.GetImageData(i, 0) } + }); + } } } - } if (Surfaces.Count == 0 || Surfaces[0].mipmaps[0].Length == 0) return; @@ -238,6 +241,14 @@ namespace Toolbox.Library.Rendering } Surfaces.Clear(); + + } + catch + { + IsFailedState = true; + GLInitialized = false; + return; + } } static bool IsPow2(int Value)