2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2019-11-30 21:53:09 -05:00
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
using static Ryujinx.Graphics.Shader.Translation.GlobalMemory;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
|
|
|
|
{
|
|
|
|
static class GlobalToStorage
|
|
|
|
{
|
2022-12-05 14:11:32 -05:00
|
|
|
public static void RunPass(BasicBlock block, ShaderConfig config, ref int sbUseMask)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-30 21:53:09 -05:00
|
|
|
int sbStart = GetStorageBaseCbOffset(config.Stage);
|
2019-10-13 02:02:07 -04:00
|
|
|
int sbEnd = sbStart + StorageDescsSize;
|
|
|
|
|
|
|
|
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
|
|
|
{
|
2022-12-05 14:11:32 -05:00
|
|
|
for (int index = 0; index < node.Value.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
Operand src = node.Value.GetSource(index);
|
|
|
|
|
|
|
|
int storageIndex = GetStorageIndex(src, sbStart, sbEnd);
|
|
|
|
|
|
|
|
if (storageIndex >= 0)
|
|
|
|
{
|
|
|
|
sbUseMask |= 1 << storageIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
if (!(node.Value is Operation operation))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-30 21:53:09 -05:00
|
|
|
if (UsesGlobalMemory(operation.Inst))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
Operand source = operation.GetSource(0);
|
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
int storageIndex = SearchForStorageBase(block, source, sbStart, sbEnd);
|
|
|
|
|
|
|
|
if (storageIndex >= 0)
|
|
|
|
{
|
|
|
|
// Storage buffers are implemented using global memory access.
|
|
|
|
// If we know from where the base address of the access is loaded,
|
|
|
|
// we can guess which storage buffer it is accessing.
|
|
|
|
// We can then replace the global memory access with a storage
|
|
|
|
// buffer access.
|
2022-11-17 12:47:41 -05:00
|
|
|
node = ReplaceGlobalWithStorage(block, node, config, storageIndex);
|
2021-04-18 06:31:39 -04:00
|
|
|
}
|
|
|
|
else if (config.Stage == ShaderStage.Compute && operation.Inst == Instruction.LoadGlobal)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2021-04-18 06:31:39 -04:00
|
|
|
// Here we effectively try to replace a LDG instruction with LDC.
|
|
|
|
// The hardware only supports a limited amount of constant buffers
|
|
|
|
// so NVN "emulates" more constant buffers using global memory access.
|
|
|
|
// Here we try to replace the global access back to a constant buffer
|
|
|
|
// load.
|
|
|
|
storageIndex = SearchForStorageBase(block, source, UbeBaseOffset, UbeBaseOffset + UbeDescsSize);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (storageIndex >= 0)
|
|
|
|
{
|
2021-04-18 06:31:39 -04:00
|
|
|
node = ReplaceLdgWithLdc(node, config, storageIndex);
|
2020-02-10 19:10:05 -05:00
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 14:11:32 -05:00
|
|
|
|
|
|
|
config.SetAccessibleStorageBuffersMask(sbUseMask);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-17 12:47:41 -05:00
|
|
|
private static LinkedListNode<INode> ReplaceGlobalWithStorage(BasicBlock block, LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
Operation operation = (Operation)node.Value;
|
|
|
|
|
2021-05-19 17:15:26 -04:00
|
|
|
bool isAtomic = operation.Inst.IsAtomic();
|
2021-10-18 19:24:15 -04:00
|
|
|
bool isStg16Or8 = operation.Inst == Instruction.StoreGlobal16 || operation.Inst == Instruction.StoreGlobal8;
|
|
|
|
bool isWrite = isAtomic || operation.Inst == Instruction.StoreGlobal || isStg16Or8;
|
2021-05-19 17:15:26 -04:00
|
|
|
|
|
|
|
config.SetUsedStorageBuffer(storageIndex, isWrite);
|
|
|
|
|
2019-11-30 21:53:09 -05:00
|
|
|
Operand[] sources = new Operand[operation.SourcesCount];
|
2019-11-08 15:29:41 -05:00
|
|
|
|
2019-11-30 21:53:09 -05:00
|
|
|
sources[0] = Const(storageIndex);
|
2022-11-17 12:47:41 -05:00
|
|
|
sources[1] = GetStorageOffset(block, node, config, storageIndex, operation.GetSource(0), isStg16Or8);
|
2019-11-30 21:53:09 -05:00
|
|
|
|
|
|
|
for (int index = 2; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
sources[index] = operation.GetSource(index);
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:10:05 -05:00
|
|
|
Operation storageOp;
|
|
|
|
|
2021-05-19 17:15:26 -04:00
|
|
|
if (isAtomic)
|
2019-11-30 21:53:09 -05:00
|
|
|
{
|
2019-11-08 15:29:41 -05:00
|
|
|
Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
|
|
|
|
|
2019-11-30 21:53:09 -05:00
|
|
|
storageOp = new Operation(inst, operation.Dest, sources);
|
2019-11-08 15:29:41 -05:00
|
|
|
}
|
|
|
|
else if (operation.Inst == Instruction.LoadGlobal)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-30 21:53:09 -05:00
|
|
|
storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-18 19:24:15 -04:00
|
|
|
Instruction storeInst = operation.Inst switch
|
|
|
|
{
|
|
|
|
Instruction.StoreGlobal16 => Instruction.StoreStorage16,
|
|
|
|
Instruction.StoreGlobal8 => Instruction.StoreStorage8,
|
|
|
|
_ => Instruction.StoreStorage
|
|
|
|
};
|
|
|
|
|
|
|
|
storageOp = new Operation(storeInst, null, sources);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
operation.SetSource(index, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListNode<INode> oldNode = node;
|
|
|
|
|
2019-11-30 21:53:09 -05:00
|
|
|
node = node.List.AddBefore(node, storageOp);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
node.List.Remove(oldNode);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2022-11-17 12:47:41 -05:00
|
|
|
private static Operand GetStorageOffset(
|
|
|
|
BasicBlock block,
|
|
|
|
LinkedListNode<INode> node,
|
|
|
|
ShaderConfig config,
|
|
|
|
int storageIndex,
|
|
|
|
Operand addrLow,
|
|
|
|
bool isStg16Or8)
|
|
|
|
{
|
|
|
|
int baseAddressCbOffset = GetStorageCbOffset(config.Stage, storageIndex);
|
|
|
|
|
|
|
|
bool storageAligned = !(config.GpuAccessor.QueryHasUnalignedStorageBuffer() || config.GpuAccessor.QueryHostStorageBufferOffsetAlignment() > Constants.StorageAlignment);
|
|
|
|
|
|
|
|
(Operand byteOffset, int constantOffset) = storageAligned ?
|
|
|
|
GetStorageOffset(block, Utils.FindLastOperation(addrLow, block), baseAddressCbOffset) :
|
|
|
|
(null, 0);
|
|
|
|
|
2022-11-25 09:39:03 -05:00
|
|
|
if (byteOffset != null)
|
|
|
|
{
|
|
|
|
ReplaceAddressAlignment(node.List, addrLow, byteOffset, constantOffset);
|
|
|
|
}
|
|
|
|
|
2022-11-17 12:47:41 -05:00
|
|
|
if (byteOffset == null)
|
|
|
|
{
|
|
|
|
Operand baseAddrLow = Cbuf(0, baseAddressCbOffset);
|
|
|
|
Operand baseAddrTrunc = Local();
|
|
|
|
|
|
|
|
Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
|
|
|
|
|
|
|
|
Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
|
|
|
|
|
|
|
|
node.List.AddBefore(node, andOp);
|
|
|
|
|
|
|
|
Operand offset = Local();
|
|
|
|
Operation subOp = new Operation(Instruction.Subtract, offset, addrLow, baseAddrTrunc);
|
|
|
|
|
|
|
|
node.List.AddBefore(node, subOp);
|
|
|
|
|
|
|
|
byteOffset = offset;
|
|
|
|
}
|
|
|
|
else if (constantOffset != 0)
|
|
|
|
{
|
|
|
|
Operand offset = Local();
|
|
|
|
Operation addOp = new Operation(Instruction.Add, offset, byteOffset, Const(constantOffset));
|
|
|
|
|
|
|
|
node.List.AddBefore(node, addOp);
|
|
|
|
|
|
|
|
byteOffset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isStg16Or8)
|
|
|
|
{
|
|
|
|
return byteOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand wordOffset = Local();
|
|
|
|
Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
|
|
|
|
|
|
|
|
node.List.AddBefore(node, shrOp);
|
|
|
|
|
|
|
|
return wordOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsCb0Offset(Operand operand, int offset)
|
|
|
|
{
|
|
|
|
return operand.Type == OperandType.ConstantBuffer && operand.GetCbufSlot() == 0 && operand.GetCbufOffset() == offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ReplaceAddressAlignment(LinkedList<INode> list, Operand address, Operand byteOffset, int constantOffset)
|
|
|
|
{
|
|
|
|
// When we emit 16/8-bit LDG, we add extra code to determine the address alignment.
|
|
|
|
// Eliminate the storage buffer base address from this too, leaving only the byte offset.
|
|
|
|
|
|
|
|
foreach (INode useNode in address.UseOps)
|
|
|
|
{
|
|
|
|
if (useNode is Operation op && op.Inst == Instruction.BitwiseAnd)
|
|
|
|
{
|
|
|
|
Operand src1 = op.GetSource(0);
|
|
|
|
Operand src2 = op.GetSource(1);
|
|
|
|
|
|
|
|
int addressIndex = -1;
|
|
|
|
|
|
|
|
if (src1 == address && src2.Type == OperandType.Constant && src2.Value == 3)
|
|
|
|
{
|
|
|
|
addressIndex = 0;
|
|
|
|
}
|
|
|
|
else if (src2 == address && src1.Type == OperandType.Constant && src1.Value == 3)
|
|
|
|
{
|
|
|
|
addressIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addressIndex != -1)
|
|
|
|
{
|
|
|
|
LinkedListNode<INode> node = list.Find(op);
|
|
|
|
|
|
|
|
// Add offset calculation before the use. Needs to be on the same block.
|
|
|
|
if (node != null)
|
|
|
|
{
|
|
|
|
Operand offset = Local();
|
|
|
|
Operation addOp = new Operation(Instruction.Add, offset, byteOffset, Const(constantOffset));
|
|
|
|
list.AddBefore(node, addOp);
|
|
|
|
|
|
|
|
op.SetSource(addressIndex, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static (Operand, int) GetStorageOffset(BasicBlock block, Operand address, int baseAddressCbOffset)
|
|
|
|
{
|
|
|
|
if (IsCb0Offset(address, baseAddressCbOffset))
|
|
|
|
{
|
|
|
|
// Direct offset: zero.
|
|
|
|
return (Const(0), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
(address, int constantOffset) = GetStorageConstantOffset(block, address);
|
|
|
|
|
|
|
|
address = Utils.FindLastOperation(address, block);
|
|
|
|
|
|
|
|
if (IsCb0Offset(address, baseAddressCbOffset))
|
|
|
|
{
|
|
|
|
// Only constant offset
|
|
|
|
return (Const(0), constantOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(address.AsgOp is Operation offsetAdd) || offsetAdd.Inst != Instruction.Add)
|
|
|
|
{
|
|
|
|
return (null, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand src1 = offsetAdd.GetSource(0);
|
|
|
|
Operand src2 = Utils.FindLastOperation(offsetAdd.GetSource(1), block);
|
|
|
|
|
|
|
|
if (IsCb0Offset(src2, baseAddressCbOffset))
|
|
|
|
{
|
|
|
|
return (src1, constantOffset);
|
|
|
|
}
|
|
|
|
else if (IsCb0Offset(src1, baseAddressCbOffset))
|
|
|
|
{
|
|
|
|
return (src2, constantOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (null, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static (Operand, int) GetStorageConstantOffset(BasicBlock block, Operand address)
|
|
|
|
{
|
|
|
|
if (!(address.AsgOp is Operation offsetAdd) || offsetAdd.Inst != Instruction.Add)
|
|
|
|
{
|
|
|
|
return (address, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand src1 = offsetAdd.GetSource(0);
|
|
|
|
Operand src2 = offsetAdd.GetSource(1);
|
|
|
|
|
|
|
|
if (src2.Type != OperandType.Constant)
|
|
|
|
{
|
|
|
|
return (address, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (src1, src2.Value);
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:10:05 -05:00
|
|
|
private static LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
|
|
|
|
{
|
|
|
|
Operation operation = (Operation)node.Value;
|
|
|
|
|
|
|
|
Operand GetCbufOffset()
|
|
|
|
{
|
|
|
|
Operand addrLow = operation.GetSource(0);
|
|
|
|
|
2022-07-23 10:15:58 -04:00
|
|
|
Operand baseAddrLow = Cbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
|
2020-02-10 19:10:05 -05:00
|
|
|
|
|
|
|
Operand baseAddrTrunc = Local();
|
|
|
|
|
2021-08-11 17:01:06 -04:00
|
|
|
Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
|
2020-02-10 19:10:05 -05:00
|
|
|
|
|
|
|
Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
|
|
|
|
|
|
|
|
node.List.AddBefore(node, andOp);
|
|
|
|
|
|
|
|
Operand byteOffset = Local();
|
|
|
|
Operand wordOffset = Local();
|
|
|
|
|
2022-11-17 12:47:41 -05:00
|
|
|
Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
|
2020-02-10 19:10:05 -05:00
|
|
|
Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
|
|
|
|
|
|
|
|
node.List.AddBefore(node, subOp);
|
|
|
|
node.List.AddBefore(node, shrOp);
|
|
|
|
|
|
|
|
return wordOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand[] sources = new Operand[operation.SourcesCount];
|
|
|
|
|
2021-05-19 17:15:26 -04:00
|
|
|
int cbSlot = UbeFirstCbuf + storageIndex;
|
|
|
|
|
|
|
|
sources[0] = Const(cbSlot);
|
2020-02-10 19:10:05 -05:00
|
|
|
sources[1] = GetCbufOffset();
|
|
|
|
|
2021-05-19 17:15:26 -04:00
|
|
|
config.SetUsedConstantBuffer(cbSlot);
|
|
|
|
|
2020-02-10 19:10:05 -05:00
|
|
|
for (int index = 2; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
sources[index] = operation.GetSource(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
|
|
|
|
|
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
operation.SetSource(index, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListNode<INode> oldNode = node;
|
|
|
|
|
|
|
|
node = node.List.AddBefore(node, ldcOp);
|
|
|
|
|
|
|
|
node.List.Remove(oldNode);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2021-04-18 06:31:39 -04:00
|
|
|
globalAddress = Utils.FindLastOperation(globalAddress, block);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
if (globalAddress.Type == OperandType.ConstantBuffer)
|
|
|
|
{
|
|
|
|
return GetStorageIndex(globalAddress, sbStart, sbEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation operation = globalAddress.AsgOp as Operation;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
if (operation == null || operation.Inst != Instruction.Add)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2021-04-18 06:31:39 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand src1 = operation.GetSource(0);
|
|
|
|
Operand src2 = operation.GetSource(1);
|
|
|
|
|
|
|
|
if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
|
|
|
|
(src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
|
|
|
|
{
|
|
|
|
if (src1.Type == OperandType.LocalVariable)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2021-04-18 06:31:39 -04:00
|
|
|
operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
if (operation == null || operation.Inst != Instruction.Add)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
Operand source = operation.GetSource(index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-18 06:31:39 -04:00
|
|
|
if (storageIndex != -1)
|
|
|
|
{
|
|
|
|
return storageIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
|
|
|
|
{
|
|
|
|
if (operand.Type == OperandType.ConstantBuffer)
|
|
|
|
{
|
2022-11-17 12:47:41 -05:00
|
|
|
int slot = operand.GetCbufSlot();
|
2021-04-18 06:31:39 -04:00
|
|
|
int offset = operand.GetCbufOffset();
|
|
|
|
|
|
|
|
if (slot == 0 && offset >= sbStart && offset < sbEnd)
|
|
|
|
{
|
|
|
|
int storageIndex = (offset - sbStart) / StorageDescSize;
|
|
|
|
|
|
|
|
return storageIndex;
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|