Correct non-constant offset rewrite for texelFetch

This commit is contained in:
gdkchan 2019-12-11 14:41:07 -03:00 committed by Thog
parent f2c85c5d58
commit 171c3d54c6
2 changed files with 78 additions and 53 deletions

View file

@ -140,8 +140,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
{
// TODO: Return correct type depending on source index,
// that can improve the decompiler output.
if (
inst == Instruction.ImageLoad ||
if (inst == Instruction.ImageLoad ||
inst == Instruction.ImageStore ||
inst == Instruction.Lod ||
inst == Instruction.TextureSample)

View file

@ -126,6 +126,15 @@ namespace Ryujinx.Graphics.Shader.Translation
private static LinkedListNode<INode> RewriteTextureSample(LinkedListNode<INode> node)
{
// Technically, non-constant texture offsets are not allowed (according to the spec),
// however some GPUs does support that.
// For GPUs where it is not supported, we can replace the instruction with the following:
// For texture*Offset, we replace it by texture*, and add the offset to the P coords.
// The offset can be calculated as offset / textureSize(lod), where lod = textureQueryLod(coords).
// For texelFetchOffset, we replace it by texelFetch and add the offset to the P coords directly.
// For textureGatherOffset, we take advantage of the fact that the operation is already broken down
// to read the 4 pixels separately, and just replace it with 4 textureGather with a different offset
// for each pixel.
TextureOperation texOp = (TextureOperation)node.Value;
bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
@ -139,6 +148,7 @@ namespace Ryujinx.Graphics.Shader.Translation
bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
@ -228,6 +238,34 @@ namespace Ryujinx.Graphics.Shader.Translation
sources[dstIndex++] = texOp.GetSource(srcIndex++);
}
int coordsIndex = isBindless || isIndexed ? 1 : 0;
if (intCoords)
{
for (int index = 0; index < coordsCount; index++)
{
Operand source = sources[coordsIndex + index];
Operand coordPlusOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, coordPlusOffset, source, offsets[index]));
sources[coordsIndex + index] = coordPlusOffset;
}
}
else
{
Operand lod = Local();
node.List.AddBefore(node, new TextureOperation(
Instruction.Lod,
texOp.Type,
texOp.Flags,
texOp.Handle,
1,
lod,
lodSources));
Operand Int(Operand value)
{
Operand res = Local();
@ -246,19 +284,6 @@ namespace Ryujinx.Graphics.Shader.Translation
return res;
}
Operand lod = Local();
node.List.AddBefore(node, new TextureOperation(
Instruction.Lod,
texOp.Type,
texOp.Flags,
texOp.Handle,
1,
lod,
lodSources));
int coordsIndex = isBindless || isIndexed ? 1 : 0;
for (int index = 0; index < coordsCount; index++)
{
Operand coordSize = Local();
@ -297,6 +322,7 @@ namespace Ryujinx.Graphics.Shader.Translation
sources[coordsIndex + index] = coordPlusOffset;
}
}
int componentIndex;