From 4ad5be6f1879836b4cfd4c7f9ecb068497cfdb98 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Tue, 15 Aug 2023 15:00:39 +0100 Subject: [PATCH] Start TextureSample --- .../CodeGen/Msl/Instructions/InstGen.cs | 2 +- .../CodeGen/Msl/Instructions/InstGenMemory.cs | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs index ca2c4bedf7..058f4a6c63 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs @@ -135,7 +135,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions case Instruction.Store: return Store(context, operation); case Instruction.TextureSample: - return "|| TEXTURE SAMPLE ||"; + return TextureSample(context, operation); case Instruction.TextureSize: return "|| TEXTURE SIZE ||"; case Instruction.VectorExtract: diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs index 863956c0c7..83beec9c84 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGenMemory.cs @@ -49,5 +49,45 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions { return GenerateLoadOrStore(context, operation, isStore: true); } + + public static string TextureSample(CodeGenContext context, AstOperation operation) + { + AstTextureOperation texOp = (AstTextureOperation)operation; + + bool isGather = (texOp.Flags & TextureFlags.Gather) != 0; + bool isShadow = (texOp.Type & SamplerType.Shadow) != 0; + + bool colorIsVector = isGather || !isShadow; + + string texCall = "texture.sample("; + + string samplerName = GetSamplerName(context.Properties, texOp); + + texCall += samplerName; + + texCall += ")" + (colorIsVector ? GetMaskMultiDest(texOp.Index) : ""); + + return texCall; + } + + private static string GetSamplerName(ShaderProperties resourceDefinitions, AstTextureOperation textOp) + { + return resourceDefinitions.Textures[textOp.Binding].Name; + } + + private static string GetMaskMultiDest(int mask) + { + string swizzle = "."; + + for (int i = 0; i < 4; i++) + { + if ((mask & (1 << i)) != 0) + { + swizzle += "xyzw"[i]; + } + } + + return swizzle; + } } }