Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Load/Store Local/Shared and Atomic shared using new instructions #5241

Merged
merged 7 commits into from Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,7 @@ class DiskCacheHostStorage
private const ushort FileFormatVersionMajor = 1;
private const ushort FileFormatVersionMinor = 2;
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
private const uint CodeGenVersion = 5080;
private const uint CodeGenVersion = 5241;

private const string SharedTocFileName = "shared.toc";
private const string SharedDataFileName = "shared.data";
Expand Down
75 changes: 31 additions & 44 deletions src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
Expand Up @@ -71,40 +71,10 @@ public static void Declare(CodeGenContext context, StructuredProgramInfo info)
context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;");
context.AppendLine();

if (context.Config.Stage == ShaderStage.Compute)
{
int localMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeLocalMemorySize(), 4);

if (localMemorySize != 0)
{
string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize);

context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];");
context.AppendLine();
}

int sharedMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeSharedMemorySize(), 4);

if (sharedMemorySize != 0)
{
string sharedMemorySizeStr = NumberFormatter.FormatInt(sharedMemorySize);

context.AppendLine($"shared uint {DefaultNames.SharedMemoryName}[{sharedMemorySizeStr}];");
context.AppendLine();
}
}
else if (context.Config.LocalMemorySize != 0)
{
int localMemorySize = BitUtils.DivRoundUp(context.Config.LocalMemorySize, 4);

string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize);

context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];");
context.AppendLine();
}

DeclareConstantBuffers(context, context.Config.Properties.ConstantBuffers.Values);
DeclareStorageBuffers(context, context.Config.Properties.StorageBuffers.Values);
DeclareMemories(context, context.Config.Properties.LocalMemories.Values, isShared: false);
DeclareMemories(context, context.Config.Properties.SharedMemories.Values, isShared: true);

var textureDescriptors = context.Config.GetTextureDescriptors();
if (textureDescriptors.Length != 0)
Expand Down Expand Up @@ -238,11 +208,6 @@ public static void Declare(CodeGenContext context, StructuredProgramInfo info)
context.AppendLine();
}

if ((info.HelperFunctionsMask & HelperFunctionsMask.AtomicMinMaxS32Shared) != 0)
{
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/AtomicMinMaxS32Shared.glsl");
}

if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighS32) != 0)
{
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighS32.glsl");
Expand Down Expand Up @@ -273,11 +238,6 @@ public static void Declare(CodeGenContext context, StructuredProgramInfo info)
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl");
}

if ((info.HelperFunctionsMask & HelperFunctionsMask.StoreSharedSmallInt) != 0)
{
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/StoreSharedSmallInt.glsl");
}

if ((info.HelperFunctionsMask & HelperFunctionsMask.SwizzleAdd) != 0)
{
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/SwizzleAdd.glsl");
Expand Down Expand Up @@ -358,7 +318,14 @@ private static void DeclareBuffers(CodeGenContext context, IEnumerable<BufferDef
_ => "std430"
};

context.AppendLine($"layout (binding = {buffer.Binding}, {layout}) {declType} _{buffer.Name}");
string set = string.Empty;

if (context.Config.Options.TargetApi == TargetApi.Vulkan)
{
set = $"set = {buffer.Set}, ";
}

context.AppendLine($"layout ({set}binding = {buffer.Binding}, {layout}) {declType} _{buffer.Name}");
context.EnterScope();

foreach (StructureField field in buffer.Type.Fields)
Expand Down Expand Up @@ -391,6 +358,27 @@ private static void DeclareBuffers(CodeGenContext context, IEnumerable<BufferDef
}
}

private static void DeclareMemories(CodeGenContext context, IEnumerable<MemoryDefinition> memories, bool isShared)
{
string prefix = isShared ? "shared " : string.Empty;

foreach (MemoryDefinition memory in memories)
{
string typeName = GetVarTypeName(context, memory.Type & ~AggregateType.Array);

if (memory.ArrayLength > 0)
{
string arraySize = memory.ArrayLength.ToString(CultureInfo.InvariantCulture);

context.AppendLine($"{prefix}{typeName} {memory.Name}[{arraySize}];");
}
else
{
context.AppendLine($"{prefix}{typeName} {memory.Name}[];");
}
}
}

private static void DeclareSamplers(CodeGenContext context, TextureDescriptor[] descriptors)
{
int arraySize = 0;
Expand Down Expand Up @@ -717,7 +705,6 @@ private static void AppendHelperFunction(CodeGenContext context, string filename
string code = EmbeddedResources.ReadAllText(filename);

code = code.Replace("\t", CodeGenContext.Tab);
code = code.Replace("$SHARED_MEM$", DefaultNames.SharedMemoryName);

if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot())
{
Expand Down
3 changes: 0 additions & 3 deletions src/Ryujinx.Graphics.Shader/CodeGen/Glsl/DefaultNames.cs
Expand Up @@ -11,9 +11,6 @@ static class DefaultNames
public const string IAttributePrefix = "in_attr";
public const string OAttributePrefix = "out_attr";

public const string LocalMemoryName = "local_mem";
public const string SharedMemoryName = "shared_mem";

public const string ArgumentNamePrefix = "a";

public const string UndefinedName = "undef";
Expand Down

This file was deleted.

Expand Up @@ -2,9 +2,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{
static class HelperFunctionNames
{
public static string AtomicMaxS32 = "Helper_AtomicMaxS32";
public static string AtomicMinS32 = "Helper_AtomicMinS32";

public static string MultiplyHighS32 = "Helper_MultiplyHighS32";
public static string MultiplyHighU32 = "Helper_MultiplyHighU32";

Expand All @@ -13,10 +10,5 @@ static class HelperFunctionNames
public static string ShuffleUp = "Helper_ShuffleUp";
public static string ShuffleXor = "Helper_ShuffleXor";
public static string SwizzleAdd = "Helper_SwizzleAdd";

public static string StoreShared16 = "Helper_StoreShared16";
public static string StoreShared8 = "Helper_StoreShared8";
public static string StoreStorage16 = "Helper_StoreStorage16";
public static string StoreStorage8 = "Helper_StoreStorage8";
}
}

This file was deleted.

37 changes: 1 addition & 36 deletions src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs
Expand Up @@ -68,7 +68,7 @@ private static string GetExpression(CodeGenContext context, AstOperation operati

string args = string.Empty;

if (atomic && operation.StorageKind == StorageKind.StorageBuffer)
if (atomic && (operation.StorageKind == StorageKind.StorageBuffer || operation.StorageKind == StorageKind.SharedMemory))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: might be more readable with is and or if it works in this context

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of is, or, and etc expressions. It is easier to understand like this for me.

{
args = GenerateLoadOrStore(context, operation, isStore: false);

Expand All @@ -81,23 +81,6 @@ private static string GetExpression(CodeGenContext context, AstOperation operati
args += ", " + GetSoureExpr(context, operation.GetSource(argIndex), dstType);
}
}
else if (atomic && operation.StorageKind == StorageKind.SharedMemory)
{
args = LoadShared(context, operation);

// For shared memory access, the second argument is unused and should be ignored.
// It is there to make both storage and shared access have the same number of arguments.
// For storage, both inputs are consumed when the argument index is 0, so we should skip it here.

for (int argIndex = 2; argIndex < arity; argIndex++)
{
args += ", ";

AggregateType dstType = GetSrcVarType(inst, argIndex);

args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
}
}
else
{
for (int argIndex = 0; argIndex < arity; argIndex++)
Expand Down Expand Up @@ -179,12 +162,6 @@ private static string GetExpression(CodeGenContext context, AstOperation operati
case Instruction.Load:
return Load(context, operation);

case Instruction.LoadLocal:
return LoadLocal(context, operation);

case Instruction.LoadShared:
return LoadShared(context, operation);

case Instruction.Lod:
return Lod(context, operation);

Expand All @@ -200,18 +177,6 @@ private static string GetExpression(CodeGenContext context, AstOperation operati
case Instruction.Store:
return Store(context, operation);

case Instruction.StoreLocal:
return StoreLocal(context, operation);

case Instruction.StoreShared:
return StoreShared(context, operation);

case Instruction.StoreShared16:
return StoreShared16(context, operation);

case Instruction.StoreShared8:
return StoreShared8(context, operation);

case Instruction.TextureSample:
return TextureSample(context, operation);

Expand Down
Expand Up @@ -17,9 +17,7 @@ static InstGenHelper()
Add(Instruction.AtomicAdd, InstType.AtomicBinary, "atomicAdd");
Add(Instruction.AtomicAnd, InstType.AtomicBinary, "atomicAnd");
Add(Instruction.AtomicCompareAndSwap, InstType.AtomicTernary, "atomicCompSwap");
Add(Instruction.AtomicMaxS32, InstType.CallTernary, HelperFunctionNames.AtomicMaxS32);
Add(Instruction.AtomicMaxU32, InstType.AtomicBinary, "atomicMax");
Add(Instruction.AtomicMinS32, InstType.CallTernary, HelperFunctionNames.AtomicMinS32);
Add(Instruction.AtomicMinU32, InstType.AtomicBinary, "atomicMin");
Add(Instruction.AtomicOr, InstType.AtomicBinary, "atomicOr");
Add(Instruction.AtomicSwap, InstType.AtomicBinary, "atomicExchange");
Expand Down Expand Up @@ -83,8 +81,6 @@ static InstGenHelper()
Add(Instruction.ImageAtomic, InstType.Special);
Add(Instruction.IsNan, InstType.CallUnary, "isnan");
Add(Instruction.Load, InstType.Special);
Add(Instruction.LoadLocal, InstType.Special);
Add(Instruction.LoadShared, InstType.Special);
Add(Instruction.Lod, InstType.Special);
Add(Instruction.LogarithmB2, InstType.CallUnary, "log2");
Add(Instruction.LogicalAnd, InstType.OpBinaryCom, "&&", 9);
Expand Down Expand Up @@ -118,10 +114,6 @@ static InstGenHelper()
Add(Instruction.Sine, InstType.CallUnary, "sin");
Add(Instruction.SquareRoot, InstType.CallUnary, "sqrt");
Add(Instruction.Store, InstType.Special);
Add(Instruction.StoreLocal, InstType.Special);
Add(Instruction.StoreShared, InstType.Special);
Add(Instruction.StoreShared16, InstType.Special);
Add(Instruction.StoreShared8, InstType.Special);
Add(Instruction.Subtract, InstType.OpBinary, "-", 2);
Add(Instruction.SwizzleAdd, InstType.CallTernary, HelperFunctionNames.SwizzleAdd);
Add(Instruction.TextureSample, InstType.Special);
Expand Down