Fix out-of-bounds texture size query extracts in the SPIR-V translator

The SPIR-V generator emitted OpCompositeExtract with the guest's component index even when it exceeded the image's size vector. A 2D texture's OpImageQuerySizeLod returns a v2int, so a guest request for the third component (depth) produced an extract of index 2, which is invalid SPIR-V. RADV crashed while compiling those variants during shader preparation.

Return the constant 1 for a component past the texture's dimensions, the depth of a 1D/2D/cube texture, and apply the same guard in the GLSL generator. build_xlate.py now expects the added patch.
This commit is contained in:
DieMitchell
2026-11-09 15:28:14 +01:00
committed by tangerineturtle1
parent 4223e0aa5c
commit 21eb3de1c2
2 changed files with 42 additions and 2 deletions

View File

@@ -99,8 +99,8 @@ def prepare_sources() -> Path:
for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")):
shutil.rmtree(junk)
patches = sorted((XLATE / "patches").glob("*.patch"))
if len(patches) != 36:
sys.exit(f"expected 36 patches, found {len(patches)}")
if len(patches) != 37:
sys.exit(f"expected 37 patches, found {len(patches)}")
# build/xlate is inside this checkout, and git inside a repository reads a "diff --git" patch's paths from the
# repository's top: those hunks fall outside the directory handed to --directory, git skips every file of the patch
# and still exits 0. 0021-prmt-byte-permute.patch is the only one written in that form, so it used to be skipped in

View File

@@ -0,0 +1,40 @@
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
@@ -1677,7 +1677,13 @@
result = context.ImageQuerySize(resultType, image);
}
- if (dimensions != 1)
+ if (texOp.Index >= dimensions)
+ {
+ // A 1D/2D/cube texture has no size component beyond its dimensions; the
+ // missing component (depth for 1D/2D/cube) is 1.
+ result = context.Constant(context.TypeS32(), 1);
+ }
+ else if (dimensions != 1)
{
result = context.CompositeExtract(context.TypeS32(), result, texOp.Index);
}
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
@@ -466,7 +466,19 @@
bool hasLod = !definition.Type.HasFlag(SamplerType.Multisample) && (definition.Type & SamplerType.Mask) != SamplerType.TextureBuffer;
string texCall;
- if (hasLod)
+ int dimensions = (definition.Type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : definition.Type.Dimensions;
+
+ if (definition.Type.HasFlag(SamplerType.Array))
+ {
+ dimensions++;
+ }
+
+ if (texOp.Index >= dimensions)
+ {
+ // A 1D/2D/cube texture has no size component beyond its dimensions.
+ texCall = "1";
+ }
+ else if (hasLod)
{
IAstNode lod = operation.GetSource(srcIndex);
string lodExpr = GetSourceExpr(context, lod, GetSrcVarType(operation.Inst, srcIndex));