diff --git a/tools/build_xlate.py b/tools/build_xlate.py index 35b28361..3c755609 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -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) != 37: - sys.exit(f"expected 37 patches, found {len(patches)}") + if len(patches) != 38: + sys.exit(f"expected 38 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. GIT_CEILING_DIRECTORIES stops git from walking up to the checkout, so it reads each patch as it diff --git a/tools/xlate/patches/0005-declare-storage-image-write-without-format.patch b/tools/xlate/patches/0005-declare-storage-image-write-without-format.patch index 268a6458..49de91f9 100644 --- a/tools/xlate/patches/0005-declare-storage-image-write-without-format.patch +++ b/tools/xlate/patches/0005-declare-storage-image-write-without-format.patch @@ -16,7 +16,7 @@ + // NX2NAVI (2026-10-20): an image declared without a format (QueryHostSupportsImageLoadFormatted) that the + // shader stores to needs StorageImageWriteWithoutFormat; upstream never declares it, so spirv-val rejects + // 22 agl_technique_pfx compute shaders with formatted SUST. Vulkan's shaderStorageImageWriteWithoutFormat -+ // feature backs it. Reads without a format need no capability under Vulkan 1.3 (format feature flags 2). ++ // feature backs it. A read of such an image needs StorageImageReadWithoutFormat, which GenerateImageLoad declares. + writeWithoutFormat |= image.Format == TextureFormat.Unknown && image.Flags.HasFlag(TextureUsageFlags.ImageStore); + SpvInstruction imagePointerType = context.TypePointer(StorageClass.UniformConstant, imageType); diff --git a/tools/xlate/patches/0039-image-read-without-format.patch b/tools/xlate/patches/0039-image-read-without-format.patch new file mode 100644 index 00000000..8521d1ff --- /dev/null +++ b/tools/xlate/patches/0039-image-read-without-format.patch @@ -0,0 +1,28 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs +@@ -83,6 +83,7 @@ + public bool IsMainFunction { get; private set; } + public bool MayHaveReturned { get; set; } + public bool WasNonUniformAccessDeclared { get; set; } ++ public bool WasReadWithoutFormatDeclared { get; set; } + + public CodeGenContext( + StructuredProgramInfo info, +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs +@@ -743,6 +743,15 @@ + SpvInstruction imageComponentType = context.GetType(componentType); + AggregateType swizzledResultType = texOp.GetVectorType(componentType); + ++ // NX2NAVI: an image declared without a format (ShaderProperties.GetTextureFormat) is read with OpImageRead, ++ // which SPIR-V allows only when the module declares StorageImageReadWithoutFormat; Vulkan 1.3, which the ++ // runtime requires, allows that capability on every device. ++ if (texOp.Format == TextureFormat.Unknown && !context.WasReadWithoutFormatDeclared) ++ { ++ context.AddCapability(Capability.StorageImageReadWithoutFormat); ++ context.WasReadWithoutFormatDeclared = true; ++ } ++ + SpvInstruction texel = context.ImageRead(context.TypeVector(imageComponentType, 4), image, pCoords, ImageOperandsMask.MaskNone); + SpvInstruction result = GetSwizzledResult(context, texel, swizzledResultType, texOp.Index); + diff --git a/tools/xlate_smoke.py b/tools/xlate_smoke.py index d3734bda..0a407f35 100644 --- a/tools/xlate_smoke.py +++ b/tools/xlate_smoke.py @@ -1,8 +1,10 @@ """Smoke test for build/xlate/nx2navi_xlate(.exe): no game files needed. Sends one hand-assembled Maxwell fragment shader (writes the constant colour 1.0, 0.5, 0.25, 1.0 to render target 0) -through --serve, checks the reply, validates the SPIR-V with spirv-val, then sends a malformed line and checks the -service survives and still answers. +through --serve, checks the reply, then sends a malformed line and checks the service survives and still answers. A +hand-assembled compute shader that loads a texel from a float image and stores it back follows: the translator +declares such an image without a format, and its read needs StorageImageReadWithoutFormat. Every module is validated +with spirv-val. python tools/xlate_smoke.py """ @@ -31,6 +33,8 @@ def mov32i(dest: int, value: float) -> int: EXIT = 0xE30000000000000F | PT SCHED = 0x001F8000FC0007E0 # scheduling control word; the decoder skips it +RZ = 0xFF +IMAGE0 = 0x120 // 4 # first NVN image handle in c[2] (tools/xlate/Program.cs ImgSlot): image slot 0 def fragment_code() -> bytes: @@ -42,6 +46,21 @@ def fragment_code() -> bytes: return struct.pack("<20I", *header) + struct.pack(f"<{len(words)}Q", *words) +SULD_P, SUST_P = 0x1D61, 0x1D65 # opcode bits 51-63 of SULD.P and SUST.P + + +def surface_p(opcode: int, data: int, coord: int, handle: int) -> int: + """SULD.P / SUST.P 1D RGBA: the data registers from `data` on, coordinate register, image handle (TidB).""" + no_pred_out = 7 << 30 if opcode == SULD_P else 0 # SULD's second destination predicate: PT, none written + return (opcode << 51) | (handle << 36) | no_pred_out | (0xF << 20) | PT | (coord << 8) | data + + +def image_read_code() -> bytes: + """Compute code: R0-R3 = image 0 at x 0 (SULD.P), stored back there (SUST.P).""" + words = [SCHED, surface_p(SULD_P, 0, RZ, IMAGE0), surface_p(SUST_P, 0, RZ, IMAGE0), EXIT] + return struct.pack(f"<{len(words)}Q", *words) + + def main() -> int: if not EXE.is_file(): print(f"missing {EXE}; run tools/build_xlate.py", file=sys.stderr) @@ -50,27 +69,35 @@ def main() -> int: tmp = Path(tmp) code = tmp / "ps.bin" code.write_bytes(fragment_code()) + image_code = tmp / "cs.bin" + image_code.write_bytes(image_read_code()) out = tmp / "out" job = {"key": "const_ps", "stage": "ps", "code": str(code), "debug": False, "fout": {"0": "float"}} + image_job = {"key": "image_read_cs", "stage": "cs", "code": str(image_code), "debug": False, "local": [1, 1, 1]} lines = [ json.dumps({"job": job, "out": str(out)}), "this is not json", json.dumps({"job": dict(job, key="again"), "out": str(out)}), + json.dumps({"job": image_job, "out": str(out)}), ] r = subprocess.run([str(EXE), "--serve"], input="\n".join(lines) + "\n", capture_output=True, text=True, timeout=60) replies = r.stdout.splitlines() print("replies:", replies) - if len(replies) != 3 or replies[0] != "ok const_ps" or not replies[1].startswith("fail ") or replies[2] != "ok again": + if (len(replies) != 4 or replies[0] != "ok const_ps" or not replies[1].startswith("fail ") or replies[2] != "ok again" + or replies[3] != "ok image_read_cs"): print("unexpected replies; stderr:", r.stderr, file=sys.stderr) return 1 - spv = out / "const_ps.spv" - data = spv.read_bytes() - if len(data) < 20 or struct.unpack("