1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-19 04:12:02 +00:00

Update for new Shader IR format

This commit is contained in:
Isaac Marovitz 2023-08-12 15:12:35 +01:00 committed by Isaac Marovitz
parent 1f8ae7e5b1
commit 163be0a159
5 changed files with 62 additions and 43 deletions

View file

@ -54,6 +54,8 @@ namespace Ryujinx.Graphics.Metal
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetColorSpacePassthrough(bool colorSpacePassThroughEnabled) { }
public void Dispose()
{

View file

@ -12,7 +12,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
public StructuredProgramInfo Info { get; }
public ShaderConfig Config { get; }
public AttributeUsage AttributeUsage { get; }
public ShaderDefinitions Definitions { get; }
public ShaderProperties Properties { get; }
public HostCapabilities HostCapabilities { get; }
public ILogger Logger { get; }
public TargetApi TargetApi { get; }
public OperandManager OperandManager { get; }
@ -22,10 +27,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
private string _indentation;
public CodeGenContext(StructuredProgramInfo info, ShaderConfig config)
public CodeGenContext(StructuredProgramInfo info, CodeGenParameters parameters)
{
Info = info;
Config = config;
AttributeUsage = parameters.AttributeUsage;
Definitions = parameters.Definitions;
Properties = parameters.Properties;
HostCapabilities = parameters.HostCapabilities;
Logger = parameters.Logger;
TargetApi = parameters.TargetApi;
OperandManager = new OperandManager();

View file

@ -1,8 +1,9 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.CodeGen.Msl
@ -22,7 +23,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
}
DeclareInputAttributes(context, info);
// DeclareInputAttributes(context, info.IoDefinitions.Where(x => IsUserDefined(x, StorageKind.Input)));
}
static bool IsUserDefined(IoDefinition ioDefinition, StorageKind storageKind)
{
return ioDefinition.StorageKind == storageKind && ioDefinition.IoVariable == IoVariable.UserDefined;
}
public static void DeclareLocals(CodeGenContext context, StructuredFunction function)
@ -60,27 +66,28 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
};
}
private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
{
if (context.Config.UsedInputAttributes != 0)
{
context.AppendLine("struct VertexIn");
context.EnterScope();
int usedAttributes = context.Config.UsedInputAttributes | context.Config.PassthroughAttributes;
while (usedAttributes != 0)
{
int index = BitOperations.TrailingZeroCount(usedAttributes);
string name = $"{DefaultNames.IAttributePrefix}{index}";
var type = context.Config.GpuAccessor.QueryAttributeType(index).ToVec4Type(TargetLanguage.Msl);
context.AppendLine($"{type} {name} [[attribute({index})]];");
usedAttributes &= ~(1 << index);
}
context.LeaveScope(";");
}
}
// TODO: Redo for new Shader IR rep
// private static void DeclareInputAttributes(CodeGenContext context, IEnumerable<IoDefinition> inputs)
// {
// if (context.AttributeUsage.UsedInputAttributes != 0)
// {
// context.AppendLine("struct VertexIn");
// context.EnterScope();
//
// int usedAttributes = context.AttributeUsage.UsedInputAttributes | context.AttributeUsage.PassthroughAttributes;
// while (usedAttributes != 0)
// {
// int index = BitOperations.TrailingZeroCount(usedAttributes);
//
// string name = $"{DefaultNames.IAttributePrefix}{index}";
// var type = context.AttributeUsage.get .QueryAttributeType(index).ToVec4Type(TargetLanguage.Msl);
// context.AppendLine($"{type} {name} [[attribute({index})]];");
//
// usedAttributes &= ~(1 << index);
// }
//
// context.LeaveScope(";");
// }
// }
}
}

View file

@ -10,15 +10,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
{
static class MslGenerator
{
public static string Generate(StructuredProgramInfo info, ShaderConfig config)
public static string Generate(StructuredProgramInfo info, CodeGenParameters parameters)
{
if (config.Stage is not (ShaderStage.Vertex or ShaderStage.Fragment or ShaderStage.Compute))
if (parameters.Definitions.Stage is not (ShaderStage.Vertex or ShaderStage.Fragment or ShaderStage.Compute))
{
Logger.Warning?.Print(LogClass.Gpu, $"Attempted to generate unsupported shader type {config.Stage}!");
Logger.Warning?.Print(LogClass.Gpu, $"Attempted to generate unsupported shader type {parameters.Definitions.Stage}!");
return "";
}
CodeGenContext context = new(info, config);
CodeGenContext context = new(info, parameters);
Declarations.Declare(context, info);
@ -26,20 +26,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
{
for (int i = 1; i < info.Functions.Count; i++)
{
context.AppendLine($"{GetFunctionSignature(context, info.Functions[i], config.Stage)};");
context.AppendLine($"{GetFunctionSignature(context, info.Functions[i], parameters.Definitions.Stage)};");
}
context.AppendLine();
for (int i = 1; i < info.Functions.Count; i++)
{
PrintFunction(context, info.Functions[i], config.Stage);
PrintFunction(context, info.Functions[i], parameters.Definitions.Stage);
context.AppendLine();
}
}
PrintFunction(context, info.Functions[0], config.Stage, true);
PrintFunction(context, info.Functions[0], parameters.Definitions.Stage, true);
return context.GetCode();
}
@ -91,7 +91,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
funcName = "fragmentMain";
}
if (context.Config.UsedInputAttributes != 0)
if (context.AttributeUsage.UsedInputAttributes != 0)
{
args = args.Prepend("VertexIn in [[stage_in]]").ToArray();
}
@ -141,7 +141,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
}
};
bool supportsBarrierDivergence = context.Config.GpuAccessor.QueryHostSupportsShaderBarrierDivergence();
bool supportsBarrierDivergence = context.HostCapabilities.SupportsShaderBarrierDivergence;
bool mayHaveReturned = false;
foreach (IAstNode node in visitor.Visit())
@ -156,7 +156,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
// so skip emitting the barrier for those cases.
if (visitor.Block.Type != AstBlockType.Main || mayHaveReturned || !isMainFunction)
{
context.Config.GpuAccessor.Log($"Shader has barrier on potentially divergent block, the barrier will be removed.");
context.Logger.Log($"Shader has barrier on potentially divergent block, the barrier will be removed.");
continue;
}

View file

@ -68,8 +68,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
}
BufferDefinition buffer = operation.StorageKind == StorageKind.ConstantBuffer
? context.Config.Properties.ConstantBuffers[bindingIndex.Value]
: context.Config.Properties.StorageBuffers[bindingIndex.Value];
? context.Properties.ConstantBuffers[bindingIndex.Value]
: context.Properties.StorageBuffers[bindingIndex.Value];
StructureField field = buffer.Type.Fields[fieldIndex.Value];
return field.Type & AggregateType.ElementTypeMask;
@ -82,8 +82,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
}
MemoryDefinition memory = operation.StorageKind == StorageKind.LocalMemory
? context.Config.Properties.LocalMemories[bindingId.Value]
: context.Config.Properties.SharedMemories[bindingId.Value];
? context.Properties.LocalMemories[bindingId.Value]
: context.Properties.SharedMemories[bindingId.Value];
return memory.Type & AggregateType.ElementTypeMask;
@ -100,7 +100,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
bool isOutput = operation.StorageKind == StorageKind.Output || operation.StorageKind == StorageKind.OutputPerPatch;
bool isPerPatch = operation.StorageKind == StorageKind.InputPerPatch || operation.StorageKind == StorageKind.OutputPerPatch;
if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput))
if (context.Definitions.HasPerLocationInputOrOutput(ioVariable, isOutput))
{
if (operation.GetSource(1) is not AstOperand vecIndex || vecIndex.Type != OperandType.Constant)
{
@ -112,7 +112,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl
if (operation.SourcesCount > 2 &&
operation.GetSource(2) is AstOperand elemIndex &&
elemIndex.Type == OperandType.Constant &&
context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput))
context.Definitions.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput))
{
int component = elemIndex.Value;
}