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) 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) public BufferHandle CreateBuffer(int size, BufferAccess access, BufferHandle storageHint)

View file

@ -68,11 +68,7 @@ namespace Ryujinx.Graphics.Metal
slices.location = (ulong)firstLayer; slices.location = (ulong)firstLayer;
slices.length = 1; slices.length = 1;
if (info.Target == Target.Texture3D) if (info.Target != Target.Texture3D && info.Target != Target.Cubemap)
{
slices.length = (ulong)Info.Depth;
}
else if (info.Target != Target.Cubemap)
{ {
slices.length = (ulong)Info.Depth; 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) public static void DeclareLocals(CodeGenContext context, StructuredFunction function, ShaderStage stage)
{ {
DeclareMemories(context, context.Properties.LocalMemories.Values, isShared: false);
switch (stage) switch (stage)
{ {
case ShaderStage.Vertex: 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) private static void DeclareInputAttributes(CodeGenContext context, IEnumerable<IoDefinition> inputs)
{ {
if (context.Definitions.IaIndexing) if (context.Definitions.IaIndexing)
@ -141,12 +151,30 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
foreach (var ioDefinition in inputs.OrderBy(x => x.Location)) foreach (var ioDefinition in inputs.OrderBy(x => x.Location))
{ {
string type = GetVarTypeName(context, context.Definitions.GetUserDefinedType(ioDefinition.Location, isOutput: false)); string type = ioDefinition.IoVariable switch
string name = $"{DefaultNames.IAttributePrefix}{ioDefinition.Location}";
string suffix = context.Definitions.Stage switch
{ {
ShaderStage.Vertex => $" [[attribute({ioDefinition.Location})]]", // IoVariable.Position => "float4",
ShaderStage.Fragment => $" [[user(loc{ioDefinition.Location})]]", 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})]]",
_ => "" _ => ""
}; };
@ -197,7 +225,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
{ {
IoVariable.Position => "position", IoVariable.Position => "position",
IoVariable.PointSize => "point_size", IoVariable.PointSize => "point_size",
IoVariable.FragmentOutputColor => "color", IoVariable.FragmentOutputColor => $"color{ioDefinition.Location}",
_ => $"{DefaultNames.OAttributePrefix}{ioDefinition.Location}" _ => $"{DefaultNames.OAttributePrefix}{ioDefinition.Location}"
}; };
string suffix = ioDefinition.IoVariable switch string suffix = ioDefinition.IoVariable switch

View file

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

View file

@ -1,3 +1,4 @@
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Shader.IntermediateRepresentation; using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation; using Ryujinx.Graphics.Shader.Translation;
using System.Globalization; using System.Globalization;
@ -14,12 +15,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
bool isOutput, bool isOutput,
bool isPerPatch) bool isPerPatch)
{ {
return ioVariable switch var returnValue = ioVariable switch
{ {
IoVariable.BaseInstance => ("base_instance", AggregateType.S32), IoVariable.BaseInstance => ("base_instance", AggregateType.S32),
IoVariable.BaseVertex => ("base_vertex", AggregateType.S32), IoVariable.BaseVertex => ("base_vertex", AggregateType.S32),
IoVariable.ClipDistance => ("clip_distance", AggregateType.Array | AggregateType.FP32), 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.FragmentOutputDepth => ("depth", AggregateType.FP32),
IoVariable.FrontFacing => ("front_facing", AggregateType.Bool), IoVariable.FrontFacing => ("front_facing", AggregateType.Bool),
IoVariable.InstanceId => ("instance_id", AggregateType.S32), IoVariable.InstanceId => ("instance_id", AggregateType.S32),
@ -29,10 +30,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
IoVariable.PrimitiveId => ("primitive_id", AggregateType.S32), IoVariable.PrimitiveId => ("primitive_id", AggregateType.S32),
IoVariable.UserDefined => GetUserDefinedVariableName(definitions, location, component, isOutput, isPerPatch), IoVariable.UserDefined => GetUserDefinedVariableName(definitions, location, component, isOutput, isPerPatch),
IoVariable.VertexId => ("vertex_id", AggregateType.S32), 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.ViewportIndex => ("viewport_array_index", AggregateType.S32),
IoVariable.FragmentCoord => ("in.position", AggregateType.Vector4 | AggregateType.FP32), IoVariable.FragmentCoord => ("in.position", AggregateType.Vector4 | AggregateType.FP32),
_ => (null, AggregateType.Invalid), _ => (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) 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) public static string ToMslTextureType(this SamplerType type)
{ {
string typeName = (type & SamplerType.Mask) switch string typeName;
if ((type & SamplerType.Shadow) != 0)
{
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.None => "texture",
SamplerType.Texture1D => "texture1d", SamplerType.Texture1D => "texture1d",
SamplerType.TextureBuffer => "texturebuffer", SamplerType.TextureBuffer => "texturebuffer",
SamplerType.Texture2D => "texture2d", SamplerType.Texture2D => "texture2d",
SamplerType.Texture3D => "texture3d", SamplerType.Texture3D => "texture3d",
SamplerType.TextureCube => "texturecube", SamplerType.TextureCube => "texturecube",
_ => throw new ArgumentException($"Invalid sampler type \"{type}\"."), _ => throw new ArgumentException($"Invalid texture type \"{type}\"."),
}; };
}
if ((type & SamplerType.Multisample) != 0) if ((type & SamplerType.Multisample) != 0)
{ {