From 383452f5cf2a81d50ef1a5e630fba45c6fb97647 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 5 Jan 2020 13:40:21 -0300 Subject: [PATCH] Fix some shader disposal issues --- Ryujinx.Graphics.Gpu/Engine/Methods.cs | 4 +-- Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs | 2 +- Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs | 30 +++++++++----------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Ryujinx.Graphics.Gpu/Engine/Methods.cs b/Ryujinx.Graphics.Gpu/Engine/Methods.cs index d832c62e..90935b34 100644 --- a/Ryujinx.Graphics.Gpu/Engine/Methods.cs +++ b/Ryujinx.Graphics.Gpu/Engine/Methods.cs @@ -737,11 +737,11 @@ namespace Ryujinx.Graphics.Gpu.Engine GraphicsShader gs = ShaderCache.GetGraphicsShader(state, addresses); - _vsUsesInstanceId = gs.Shaders[0].Program.Info.UsesInstanceId; + _vsUsesInstanceId = gs.Shaders[0]?.Program.Info.UsesInstanceId ?? false; for (int stage = 0; stage < Constants.ShaderStages; stage++) { - ShaderProgramInfo info = gs.Shaders[stage].Program?.Info; + ShaderProgramInfo info = gs.Shaders[stage]?.Program.Info; _currentProgramInfo[stage] = info; diff --git a/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs b/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs index d7a701ea..fcc38d04 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs @@ -10,7 +10,7 @@ namespace Ryujinx.Graphics.Gpu.Shader /// /// Host shader program object. /// - public IProgram HostProgram { get; set; } + public IProgram HostProgram { get; } /// /// Cached shader. diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs index 9f0c1c13..548a7e07 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs @@ -73,11 +73,9 @@ namespace Ryujinx.Graphics.Gpu.Shader CachedShader shader = TranslateComputeShader(gpuVa, sharedMemorySize, localSizeX, localSizeY, localSizeZ); - IShader hostShader = _context.Renderer.CompileShader(shader.Program); + shader.HostShader = _context.Renderer.CompileShader(shader.Program); - IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { hostShader }); - - ulong address = _context.MemoryManager.Translate(gpuVa); + IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader }); ComputeShader cpShader = new ComputeShader(hostProgram, shader); @@ -140,7 +138,7 @@ namespace Ryujinx.Graphics.Gpu.Shader for (int stage = 0; stage < gpShaders.Shaders.Length; stage++) { - ShaderProgram program = gpShaders.Shaders[stage].Program; + ShaderProgram program = gpShaders.Shaders[stage]?.Program; if (program == null) { @@ -191,11 +189,6 @@ namespace Ryujinx.Graphics.Gpu.Shader { CachedShader shader = gpShaders.Shaders[stage]; - if (shader.Code == null) - { - continue; - } - ulong gpuVa = 0; switch (stage) @@ -224,6 +217,11 @@ namespace Ryujinx.Graphics.Gpu.Shader /// True if the code is different, false otherwise private bool IsShaderDifferent(CachedShader shader, ulong gpuVa) { + if (shader == null) + { + return false; + } + for (int index = 0; index < shader.Code.Length; index++) { if (_context.MemoryAccessor.ReadInt32(gpuVa + (ulong)index * 4) != shader.Code[index]) @@ -299,7 +297,7 @@ namespace Ryujinx.Graphics.Gpu.Shader { if (gpuVa == 0) { - return new CachedShader(null, null); + return null; } int QueryInfo(QueryInfoName info, int index) @@ -370,13 +368,13 @@ namespace Ryujinx.Graphics.Gpu.Shader /// Graphics shader cached code private void BackpropQualifiers(GraphicsShader program) { - ShaderProgram fragmentShader = program.Shaders[4].Program; + ShaderProgram fragmentShader = program.Shaders[4]?.Program; bool isFirst = true; for (int stage = 3; stage >= 0; stage--) { - if (program.Shaders[stage].Program == null) + if (program.Shaders[stage] == null) { continue; } @@ -387,7 +385,7 @@ namespace Ryujinx.Graphics.Gpu.Shader { string iq = fragmentShader?.Info.InterpolationQualifiers[attr].ToGlslQualifier() ?? string.Empty; - if (isFirst && iq != string.Empty) + if (isFirst && !string.IsNullOrEmpty(iq)) { program.Shaders[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr}", iq); } @@ -513,7 +511,7 @@ namespace Ryujinx.Graphics.Gpu.Shader foreach (ComputeShader shader in list) { shader.HostProgram.Dispose(); - shader.Shader.HostShader.Dispose(); + shader.Shader?.HostShader.Dispose(); } } @@ -525,7 +523,7 @@ namespace Ryujinx.Graphics.Gpu.Shader foreach (CachedShader cachedShader in shader.Shaders) { - cachedShader.HostShader?.Dispose(); + cachedShader?.HostShader.Dispose(); } } }