From 416dc8fde49f8eb42d47b1ab606028a5cabe8f90 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Mon, 30 Aug 2021 14:02:40 -0300 Subject: [PATCH] Fix out-of-bounds shader thread shuffle (#2605) * Fix out-of-bounds shader thread shuffle * Shader cache version bump --- Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs | 2 +- .../CodeGen/Glsl/HelperFunctions/Shuffle.glsl | 3 ++- .../CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl | 3 ++- .../CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl | 6 +++--- .../CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl | 3 ++- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs index 51ae5aa4d..eb8847cd6 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs @@ -40,7 +40,7 @@ namespace Ryujinx.Graphics.Gpu.Shader /// /// Version of the codegen (to be changed when codegen or guest format change). /// - private const ulong ShaderCodeGenVersion = 2546; + private const ulong ShaderCodeGenVersion = 2605; // Progress reporting helpers private volatile int _shaderCount; diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl index 356bdd793..cb7c8d435 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl @@ -6,5 +6,6 @@ float Helper_Shuffle(float x, uint index, uint mask, out bool valid) uint maxThreadId = minThreadId | (clamp & ~segMask); uint srcThreadId = (index & ~segMask) | minThreadId; valid = srcThreadId <= maxThreadId; - return valid ? readInvocationARB(x, srcThreadId) : x; + float v = readInvocationARB(x, srcThreadId); + return valid ? v : x; } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl index a79b90e20..450125501 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl @@ -6,5 +6,6 @@ float Helper_ShuffleDown(float x, uint index, uint mask, out bool valid) uint maxThreadId = minThreadId | (clamp & ~segMask); uint srcThreadId = gl_SubGroupInvocationARB + index; valid = srcThreadId <= maxThreadId; - return valid ? readInvocationARB(x, srcThreadId) : x; + float v = readInvocationARB(x, srcThreadId); + return valid ? v : x; } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl index 4e74f217f..0781678a9 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl @@ -1,9 +1,9 @@ float Helper_ShuffleUp(float x, uint index, uint mask, out bool valid) { - uint clamp = mask & 0x1fu; uint segMask = (mask >> 8) & 0x1fu; uint minThreadId = gl_SubGroupInvocationARB & segMask; uint srcThreadId = gl_SubGroupInvocationARB - index; - valid = srcThreadId >= minThreadId; - return valid ? readInvocationARB(x, srcThreadId) : x; + valid = int(srcThreadId) >= int(minThreadId); + float v = readInvocationARB(x, srcThreadId); + return valid ? v : x; } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl index 0631472be..59db54441 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl @@ -6,5 +6,6 @@ float Helper_ShuffleXor(float x, uint index, uint mask, out bool valid) uint maxThreadId = minThreadId | (clamp & ~segMask); uint srcThreadId = gl_SubGroupInvocationARB ^ index; valid = srcThreadId <= maxThreadId; - return valid ? readInvocationARB(x, srcThreadId) : x; + float v = readInvocationARB(x, srcThreadId); + return valid ? v : x; } \ No newline at end of file