Merge pull request #8 from SamoZ256/metal

More shader fixes
This commit is contained in:
Isaac Marovitz 2024-05-22 15:47:19 -04:00 committed by GitHub
commit 1e7709dd66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 101 additions and 39 deletions

View file

@ -51,7 +51,7 @@ namespace Ryujinx.Graphics.Metal
public void BackgroundContextAction(Action action, bool alwaysBackground = false)
{
throw new NotImplementedException();
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public BufferHandle CreateBuffer(int size, BufferAccess access, BufferHandle storageHint)

View file

@ -68,11 +68,7 @@ namespace Ryujinx.Graphics.Metal
slices.location = (ulong)firstLayer;
slices.length = 1;
if (info.Target == Target.Texture3D)
{
slices.length = (ulong)Info.Depth;
}
else if (info.Target != Target.Cubemap)
if (info.Target != Target.Texture3D && info.Target != Target.Cubemap)
{
slices.length = (ulong)Info.Depth;
}

View file

@ -63,6 +63,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
public static void DeclareLocals(CodeGenContext context, StructuredFunction function, ShaderStage stage)
{
DeclareMemories(context, context.Properties.LocalMemories.Values, isShared: false);
switch (stage)
{
case ShaderStage.Vertex:
@ -106,6 +107,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
};
}
private static void DeclareMemories(CodeGenContext context, IEnumerable<MemoryDefinition> memories, bool isShared)
{
foreach (var memory in memories)
{
var typeName = GetVarTypeName(context, memory.Type & ~AggregateType.Array);
context.AppendLine($"{typeName} {memory.Name}[{memory.ArrayLength}];");
}
}
private static void DeclareInputAttributes(CodeGenContext context, IEnumerable<IoDefinition> inputs)
{
if (context.Definitions.IaIndexing)
@ -141,16 +151,34 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
foreach (var ioDefinition in inputs.OrderBy(x => x.Location))
{
string type = GetVarTypeName(context, context.Definitions.GetUserDefinedType(ioDefinition.Location, isOutput: false));
string name = $"{DefaultNames.IAttributePrefix}{ioDefinition.Location}";
string suffix = context.Definitions.Stage switch
string type = ioDefinition.IoVariable switch
{
ShaderStage.Vertex => $" [[attribute({ioDefinition.Location})]]",
ShaderStage.Fragment => $" [[user(loc{ioDefinition.Location})]]",
// IoVariable.Position => "float4",
IoVariable.GlobalId => "uint3",
IoVariable.VertexId => "uint",
IoVariable.VertexIndex => "uint",
_ => GetVarTypeName(context, context.Definitions.GetUserDefinedType(ioDefinition.Location, isOutput: false))
};
string name = ioDefinition.IoVariable switch
{
// IoVariable.Position => "position",
IoVariable.GlobalId => "global_id",
IoVariable.VertexId => "vertex_id",
IoVariable.VertexIndex => "vertex_index",
_ => $"{DefaultNames.IAttributePrefix}{ioDefinition.Location}"
};
string suffix = ioDefinition.IoVariable switch
{
// IoVariable.Position => "[[position]]",
IoVariable.GlobalId => "[[thread_position_in_grid]]",
IoVariable.VertexId => "[[vertex_id]]",
// TODO: Avoid potential redeclaration
IoVariable.VertexIndex => "[[vertex_id]]",
IoVariable.UserDefined => context.Definitions.Stage == ShaderStage.Fragment ? $"[[user(loc{ioDefinition.Location})]]" : $"[[attribute({ioDefinition.Location})]]",
_ => ""
};
context.AppendLine($"{type} {name}{suffix};");
context.AppendLine($"{type} {name} {suffix};");
}
context.LeaveScope(";");
@ -197,19 +225,19 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
{
IoVariable.Position => "position",
IoVariable.PointSize => "point_size",
IoVariable.FragmentOutputColor => "color",
IoVariable.FragmentOutputColor => $"color{ioDefinition.Location}",
_ => $"{DefaultNames.OAttributePrefix}{ioDefinition.Location}"
};
string suffix = ioDefinition.IoVariable switch
{
IoVariable.Position => " [[position]]",
IoVariable.PointSize => " [[point_size]]",
IoVariable.UserDefined => $" [[user(loc{ioDefinition.Location})]]",
IoVariable.FragmentOutputColor => $" [[color({ioDefinition.Location})]]",
IoVariable.Position => "[[position]]",
IoVariable.PointSize => "[[point_size]]",
IoVariable.UserDefined => $"[[user(loc{ioDefinition.Location})]]",
IoVariable.FragmentOutputColor => $"[[color({ioDefinition.Location})]]",
_ => ""
};
context.AppendLine($"{type} {name}{suffix};");
context.AppendLine($"{type} {name} {suffix};");
}
context.LeaveScope(";");

View file

@ -175,20 +175,25 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
}
else
{
texCall += "sample(";
texCall += "sample";
texCall += $"samp_{samplerName}";
if (isGather)
{
texCall += "_gather";
}
if (isShadow)
{
texCall += "_compare";
}
texCall += $"(samp_{samplerName}";
}
int coordsCount = texOp.Type.GetDimensions();
int pCount = coordsCount;
if (isShadow && !isGather)
{
pCount++;
}
void Append(string str)
{
texCall += ", " + str;
@ -224,6 +229,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
texCall += ", " + Src(AggregateType.S32);
}
if (isShadow)
{
texCall += ", " + Src(AggregateType.S32);
}
// TODO: Support offsets
texCall += ")" + (colorIsVector ? GetMaskMultiDest(texOp.Index) : "");
return texCall;
@ -267,7 +279,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
return NumberFormatter.FormatInt(0);
}
string textureName = "texture";
string samplerName = GetSamplerName(context.Properties, texOp);
string textureName = $"tex_{samplerName}";
string texCall = textureName + ".";
texCall += $"get_num_samples()";
@ -278,7 +291,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
{
AstTextureOperation texOp = (AstTextureOperation)operation;
string textureName = "texture";
string samplerName = GetSamplerName(context.Properties, texOp);
string textureName = $"tex_{samplerName}";
string texCall = textureName + ".";
if (texOp.Index == 3)
@ -314,7 +328,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
texCall += $"{lodExpr}";
}
texCall += $"){GetMask(texOp.Index)}";
texCall += $")";
}
return texCall;

View file

@ -1,3 +1,4 @@
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation;
using System.Globalization;
@ -14,12 +15,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
bool isOutput,
bool isPerPatch)
{
return ioVariable switch
var returnValue = ioVariable switch
{
IoVariable.BaseInstance => ("base_instance", AggregateType.S32),
IoVariable.BaseVertex => ("base_vertex", AggregateType.S32),
IoVariable.ClipDistance => ("clip_distance", AggregateType.Array | AggregateType.FP32),
IoVariable.FragmentOutputColor => ("out.color", AggregateType.Vector4 | AggregateType.FP32),
IoVariable.FragmentOutputColor => ($"out.color{location}", AggregateType.Vector4 | AggregateType.FP32),
IoVariable.FragmentOutputDepth => ("depth", AggregateType.FP32),
IoVariable.FrontFacing => ("front_facing", AggregateType.Bool),
IoVariable.InstanceId => ("instance_id", AggregateType.S32),
@ -29,10 +30,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
IoVariable.PrimitiveId => ("primitive_id", AggregateType.S32),
IoVariable.UserDefined => GetUserDefinedVariableName(definitions, location, component, isOutput, isPerPatch),
IoVariable.VertexId => ("vertex_id", AggregateType.S32),
IoVariable.GlobalId => ("global_id", AggregateType.Vector3 | AggregateType.U32),
// gl_VertexIndex does not have a direct equivalent in MSL
IoVariable.VertexIndex => ("vertex_index", AggregateType.U32),
IoVariable.ViewportIndex => ("viewport_array_index", AggregateType.S32),
IoVariable.FragmentCoord => ("in.position", AggregateType.Vector4 | AggregateType.FP32),
_ => (null, AggregateType.Invalid),
};
if (returnValue.Item2 == AggregateType.Invalid)
{
Logger.Warning?.PrintMsg(LogClass.Gpu, $"Unable to find type for IoVariable {ioVariable}!");
}
return returnValue;
}
private static (string, AggregateType) GetUserDefinedVariableName(ShaderDefinitions definitions, int location, int component, bool isOutput, bool isPerPatch)

View file

@ -158,16 +158,29 @@ namespace Ryujinx.Graphics.Shader
public static string ToMslTextureType(this SamplerType type)
{
string typeName = (type & SamplerType.Mask) switch
string typeName;
if ((type & SamplerType.Shadow) != 0)
{
SamplerType.None => "texture",
SamplerType.Texture1D => "texture1d",
SamplerType.TextureBuffer => "texturebuffer",
SamplerType.Texture2D => "texture2d",
SamplerType.Texture3D => "texture3d",
SamplerType.TextureCube => "texturecube",
_ => throw new ArgumentException($"Invalid sampler type \"{type}\"."),
};
typeName = (type & SamplerType.Mask) switch
{
SamplerType.Texture2D => "depth2d",
SamplerType.TextureCube => "depthcube",
_ => throw new ArgumentException($"Invalid shadow texture type \"{type}\"."),
};
}
else
{
typeName = (type & SamplerType.Mask) switch
{
SamplerType.Texture1D => "texture1d",
SamplerType.TextureBuffer => "texturebuffer",
SamplerType.Texture2D => "texture2d",
SamplerType.Texture3D => "texture3d",
SamplerType.TextureCube => "texturecube",
_ => throw new ArgumentException($"Invalid texture type \"{type}\"."),
};
}
if ((type & SamplerType.Multisample) != 0)
{