Support res scale on images, correctly blacklist for SUST, move logic out of backend. (#1657)

* Support res scale on images, correctly blacklist for SUST, move logic
out of backend.

* Fix Typo
This commit is contained in:
riperiperi 2020-11-02 19:53:23 +00:00 committed by GitHub
parent 11a7c99764
commit e1da7df207
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 199 additions and 78 deletions

View file

@ -92,6 +92,6 @@ namespace Ryujinx.Graphics.GAL
bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual); bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual);
void EndHostConditionalRendering(); void EndHostConditionalRendering();
void UpdateRenderScale(ShaderStage stage, int textureCount); void UpdateRenderScale(ShaderStage stage, float[] scales, int textureCount, int imageCount);
} }
} }

View file

@ -41,6 +41,9 @@ namespace Ryujinx.Graphics.Gpu.Image
private bool _rebind; private bool _rebind;
private float[] _scales;
private bool _scaleChanged;
/// <summary> /// <summary>
/// Constructs a new instance of the texture bindings manager. /// Constructs a new instance of the texture bindings manager.
/// </summary> /// </summary>
@ -60,6 +63,13 @@ namespace Ryujinx.Graphics.Gpu.Image
_textureState = new TextureStatePerStage[stages][]; _textureState = new TextureStatePerStage[stages][];
_imageState = new TextureStatePerStage[stages][]; _imageState = new TextureStatePerStage[stages][];
_scales = new float[64];
for (int i = 0; i < 64; i++)
{
_scales[i] = 1f;
}
} }
/// <summary> /// <summary>
@ -131,6 +141,81 @@ namespace Ryujinx.Graphics.Gpu.Image
_texturePoolMaximumId = maximumId; _texturePoolMaximumId = maximumId;
} }
/// <summary>
/// Updates the texture scale for a given texture or image.
/// </summary>
/// <param name="texture">Start GPU virtual address of the pool</param>
/// <param name="binding">The related texture binding</param>
/// <param name="index">The texture/image binding index</param>
/// <param name="stage">The active shader stage</param>
/// <returns>True if the given texture has become blacklisted, indicating that its host texture may have changed.</returns>
private bool UpdateScale(Texture texture, TextureBindingInfo binding, int index, ShaderStage stage)
{
float result = 1f;
bool changed = false;
if ((binding.Flags & TextureUsageFlags.NeedsScaleValue) != 0 && texture != null)
{
_scaleChanged |= true;
switch (stage)
{
case ShaderStage.Fragment:
if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0)
{
changed |= texture.ScaleMode != TextureScaleMode.Blacklisted;
texture.BlacklistScale();
break;
}
float scale = texture.ScaleFactor;
TextureManager manager = _context.Methods.TextureManager;
if (scale != 1)
{
Texture activeTarget = manager.GetAnyRenderTarget();
if (activeTarget != null && activeTarget.Info.Width / (float)texture.Info.Width == activeTarget.Info.Height / (float)texture.Info.Height)
{
// If the texture's size is a multiple of the sampler size, enable interpolation using gl_FragCoord. (helps "invent" new integer values between scaled pixels)
result = -scale;
break;
}
}
result = scale;
break;
case ShaderStage.Compute:
if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0)
{
changed |= texture.ScaleMode != TextureScaleMode.Blacklisted;
texture.BlacklistScale();
}
result = texture.ScaleFactor;
break;
}
}
_scales[index] = result;
return changed;
}
/// <summary>
/// Uploads texture and image scales to the backend when they are used.
/// </summary>
/// <param name="stage">Current shader stage</param>
/// <param name="stageIndex">Shader stage index</param>
private void CommitRenderScale(ShaderStage stage, int stageIndex)
{
if (_scaleChanged)
{
_context.Renderer.Pipeline.UpdateRenderScale(stage, _scales, _textureBindings[stageIndex]?.Length ?? 0, _imageBindings[stageIndex]?.Length ?? 0);
}
}
/// <summary> /// <summary>
/// Ensures that the bindings are visible to the host GPU. /// Ensures that the bindings are visible to the host GPU.
/// Note: this actually performs the binding using the host graphics API. /// Note: this actually performs the binding using the host graphics API.
@ -145,6 +230,8 @@ namespace Ryujinx.Graphics.Gpu.Image
{ {
CommitTextureBindings(texturePool, ShaderStage.Compute, 0); CommitTextureBindings(texturePool, ShaderStage.Compute, 0);
CommitImageBindings (texturePool, ShaderStage.Compute, 0); CommitImageBindings (texturePool, ShaderStage.Compute, 0);
CommitRenderScale(ShaderStage.Compute, 0);
} }
else else
{ {
@ -154,6 +241,8 @@ namespace Ryujinx.Graphics.Gpu.Image
CommitTextureBindings(texturePool, stage, stageIndex); CommitTextureBindings(texturePool, stage, stageIndex);
CommitImageBindings (texturePool, stage, stageIndex); CommitImageBindings (texturePool, stage, stageIndex);
CommitRenderScale(stage, stageIndex);
} }
} }
@ -174,8 +263,6 @@ namespace Ryujinx.Graphics.Gpu.Image
return; return;
} }
bool changed = false;
for (int index = 0; index < _textureBindings[stageIndex].Length; index++) for (int index = 0; index < _textureBindings[stageIndex].Length; index++)
{ {
TextureBindingInfo binding = _textureBindings[stageIndex][index]; TextureBindingInfo binding = _textureBindings[stageIndex][index];
@ -218,20 +305,18 @@ namespace Ryujinx.Graphics.Gpu.Image
Texture texture = pool.Get(textureId); Texture texture = pool.Get(textureId);
if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0)
{
texture?.BlacklistScale();
}
ITexture hostTexture = texture?.GetTargetTexture(binding.Target); ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
if (_textureState[stageIndex][index].Texture != hostTexture || _rebind) if (_textureState[stageIndex][index].Texture != hostTexture || _rebind)
{ {
if (UpdateScale(texture, binding, index, stage))
{
hostTexture = texture?.GetTargetTexture(binding.Target);
}
_textureState[stageIndex][index].Texture = hostTexture; _textureState[stageIndex][index].Texture = hostTexture;
_context.Renderer.Pipeline.SetTexture(index, stage, hostTexture); _context.Renderer.Pipeline.SetTexture(index, stage, hostTexture);
changed = true;
} }
if (hostTexture != null && texture.Info.Target == Target.TextureBuffer) if (hostTexture != null && texture.Info.Target == Target.TextureBuffer)
@ -253,11 +338,6 @@ namespace Ryujinx.Graphics.Gpu.Image
_context.Renderer.Pipeline.SetSampler(index, stage, hostSampler); _context.Renderer.Pipeline.SetSampler(index, stage, hostSampler);
} }
} }
if (changed)
{
_context.Renderer.Pipeline.UpdateRenderScale(stage, _textureBindings[stageIndex].Length);
}
} }
/// <summary> /// <summary>
@ -274,6 +354,9 @@ namespace Ryujinx.Graphics.Gpu.Image
return; return;
} }
// Scales for images appear after the texture ones.
int baseScaleIndex = _textureBindings[stageIndex]?.Length ?? 0;
for (int index = 0; index < _imageBindings[stageIndex].Length; index++) for (int index = 0; index < _imageBindings[stageIndex].Length; index++)
{ {
TextureBindingInfo binding = _imageBindings[stageIndex][index]; TextureBindingInfo binding = _imageBindings[stageIndex][index];
@ -283,11 +366,6 @@ namespace Ryujinx.Graphics.Gpu.Image
Texture texture = pool.Get(textureId); Texture texture = pool.Get(textureId);
if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0)
{
texture?.BlacklistScale();
}
ITexture hostTexture = texture?.GetTargetTexture(binding.Target); ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
if (hostTexture != null && texture.Info.Target == Target.TextureBuffer) if (hostTexture != null && texture.Info.Target == Target.TextureBuffer)
@ -300,6 +378,11 @@ namespace Ryujinx.Graphics.Gpu.Image
if (_imageState[stageIndex][index].Texture != hostTexture || _rebind) if (_imageState[stageIndex][index].Texture != hostTexture || _rebind)
{ {
if (UpdateScale(texture, binding, baseScaleIndex + index, stage))
{
hostTexture = texture?.GetTargetTexture(binding.Target);
}
_imageState[stageIndex][index].Texture = hostTexture; _imageState[stageIndex][index].Texture = hostTexture;
Format format = binding.Format; Format format = binding.Format;

View file

@ -194,6 +194,15 @@ namespace Ryujinx.Graphics.Gpu.Image
return changesScale || (hasValue && color.ScaleMode != TextureScaleMode.Blacklisted && color.ScaleFactor != GraphicsConfig.ResScale); return changesScale || (hasValue && color.ScaleMode != TextureScaleMode.Blacklisted && color.ScaleFactor != GraphicsConfig.ResScale);
} }
/// <summary>
/// Gets the first available bound colour target, or the depth stencil target if not present.
/// </summary>
/// <returns>The first bound colour target, otherwise the depth stencil target</returns>
public Texture GetAnyRenderTarget()
{
return _rtColors[0] ?? _rtDepthStencil;
}
/// <summary> /// <summary>
/// Updates the Render Target scale, given the currently bound render targets. /// Updates the Render Target scale, given the currently bound render targets.
/// This will update scale to match the configured scale, scale textures that are eligible but not scaled, /// This will update scale to match the configured scale, scale textures that are eligible but not scaled,

View file

@ -32,12 +32,10 @@ namespace Ryujinx.Graphics.OpenGL
private int _boundReadFramebuffer; private int _boundReadFramebuffer;
private int[] _fpIsBgra = new int[8]; private int[] _fpIsBgra = new int[8];
private float[] _fpRenderScale = new float[33]; private float[] _fpRenderScale = new float[65];
private float[] _cpRenderScale = new float[32]; private float[] _cpRenderScale = new float[64];
private TextureBase _unit0Texture; private TextureBase _unit0Texture;
private TextureBase _rtColor0Texture;
private TextureBase _rtDepthTexture;
private FrontFaceDirection _frontFace; private FrontFaceDirection _frontFace;
private ClipOrigin _clipOrigin; private ClipOrigin _clipOrigin;
@ -847,9 +845,6 @@ namespace Ryujinx.Graphics.OpenGL
{ {
EnsureFramebuffer(); EnsureFramebuffer();
_rtColor0Texture = (TextureBase)colors[0];
_rtDepthTexture = (TextureBase)depthStencil;
for (int index = 0; index < colors.Length; index++) for (int index = 0; index < colors.Length; index++)
{ {
TextureView color = (TextureView)colors[index]; TextureView color = (TextureView)colors[index];
@ -963,39 +958,6 @@ namespace Ryujinx.Graphics.OpenGL
{ {
((TextureBase)texture).Bind(unit); ((TextureBase)texture).Bind(unit);
} }
// Update scale factor for bound textures.
switch (stage)
{
case ShaderStage.Fragment:
if (_program.FragmentRenderScaleUniform != -1)
{
// Only update and send sampled texture scales if the shader uses them.
bool interpolate = false;
float scale = texture.ScaleFactor;
if (scale != 1)
{
TextureBase activeTarget = _rtColor0Texture ?? _rtDepthTexture;
if (activeTarget != null && activeTarget.Width / (float)texture.Width == activeTarget.Height / (float)texture.Height)
{
// If the texture's size is a multiple of the sampler size,
// enable interpolation using gl_FragCoord.
// (helps "invent" new integer values between scaled pixels)
interpolate = true;
}
}
_fpRenderScale[index + 1] = interpolate ? -scale : scale;
}
break;
case ShaderStage.Compute:
_cpRenderScale[index] = texture.ScaleFactor;
break;
}
} }
} }
@ -1232,7 +1194,7 @@ namespace Ryujinx.Graphics.OpenGL
} }
} }
public void UpdateRenderScale(ShaderStage stage, int textureCount) public void UpdateRenderScale(ShaderStage stage, float[] scales, int textureCount, int imageCount)
{ {
if (_program != null) if (_program != null)
{ {
@ -1241,14 +1203,16 @@ namespace Ryujinx.Graphics.OpenGL
case ShaderStage.Fragment: case ShaderStage.Fragment:
if (_program.FragmentRenderScaleUniform != -1) if (_program.FragmentRenderScaleUniform != -1)
{ {
GL.Uniform1(_program.FragmentRenderScaleUniform, textureCount + 1, _fpRenderScale); Array.Copy(scales, 0, _fpRenderScale, 1, textureCount + imageCount);
GL.Uniform1(_program.FragmentRenderScaleUniform, 1 + textureCount + imageCount, _fpRenderScale);
} }
break; break;
case ShaderStage.Compute: case ShaderStage.Compute:
if (_program.ComputeRenderScaleUniform != -1) if (_program.ComputeRenderScaleUniform != -1)
{ {
GL.Uniform1(_program.ComputeRenderScaleUniform, textureCount, _cpRenderScale); Array.Copy(scales, 0, _cpRenderScale, 0, textureCount + imageCount);
GL.Uniform1(_program.ComputeRenderScaleUniform, textureCount + imageCount, _cpRenderScale);
} }
break; break;
} }

View file

@ -84,7 +84,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
AppendLine("}" + suffix); AppendLine("}" + suffix);
} }
public int FindTextureDescriptorIndex(AstTextureOperation texOp) private int FindDescriptorIndex(List<TextureDescriptor> list, AstTextureOperation texOp)
{ {
AstOperand operand = texOp.GetSource(0) as AstOperand; AstOperand operand = texOp.GetSource(0) as AstOperand;
bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0; bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0;
@ -92,13 +92,24 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
int cBufSlot = bindless ? operand.CbufSlot : 0; int cBufSlot = bindless ? operand.CbufSlot : 0;
int cBufOffset = bindless ? operand.CbufOffset : 0; int cBufOffset = bindless ? operand.CbufOffset : 0;
return TextureDescriptors.FindIndex(descriptor => return list.FindIndex(descriptor =>
descriptor.Type == texOp.Type && descriptor.Type == texOp.Type &&
descriptor.HandleIndex == texOp.Handle && descriptor.HandleIndex == texOp.Handle &&
descriptor.Format == texOp.Format &&
descriptor.CbufSlot == cBufSlot && descriptor.CbufSlot == cBufSlot &&
descriptor.CbufOffset == cBufOffset); descriptor.CbufOffset == cBufOffset);
} }
public int FindTextureDescriptorIndex(AstTextureOperation texOp)
{
return FindDescriptorIndex(TextureDescriptors, texOp);
}
public int FindImageDescriptorIndex(AstTextureOperation texOp)
{
return FindDescriptorIndex(ImageDescriptors, texOp);
}
public StructuredFunction GetFunction(int id) public StructuredFunction GetFunction(int id)
{ {
return _info.Functions[id]; return _info.Functions[id];

View file

@ -509,7 +509,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{ {
string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage); string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage);
int scaleElements = context.TextureDescriptors.Count; int scaleElements = context.TextureDescriptors.Count + context.ImageDescriptors.Count;
if (context.Config.Stage == ShaderStage.Fragment) if (context.Config.Stage == ShaderStage.Fragment)
{ {

View file

@ -47,6 +47,43 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
texCall += ", " + str; texCall += ", " + str;
} }
string ApplyScaling(string vector)
{
int index = context.FindImageDescriptorIndex(texOp);
TextureUsageFlags flags = TextureUsageFlags.NeedsScaleValue;
if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
texOp.Inst == Instruction.ImageLoad &&
!isBindless &&
!isIndexed)
{
// Image scales start after texture ones.
int scaleIndex = context.TextureDescriptors.Count + index;
if (pCount == 3 && isArray)
{
// The array index is not scaled, just x and y.
vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + scaleIndex + "), (" + vector + ").z)";
}
else if (pCount == 2 && !isArray)
{
vector = "Helper_TexelFetchScale(" + vector + ", " + scaleIndex + ")";
}
else
{
flags |= TextureUsageFlags.ResScaleUnsupported;
}
}
else
{
flags |= TextureUsageFlags.ResScaleUnsupported;
}
context.ImageDescriptors[index] = context.ImageDescriptors[index].SetFlag(flags);
return vector;
}
if (pCount > 1) if (pCount > 1)
{ {
string[] elems = new string[pCount]; string[] elems = new string[pCount];
@ -56,7 +93,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
elems[index] = Src(VariableType.S32); elems[index] = Src(VariableType.S32);
} }
Append("ivec" + pCount + "(" + string.Join(", ", elems) + ")"); Append(ApplyScaling("ivec" + pCount + "(" + string.Join(", ", elems) + ")"));
} }
else else
{ {
@ -404,28 +441,35 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
if (intCoords) if (intCoords)
{ {
int index = context.FindTextureDescriptorIndex(texOp); int index = context.FindTextureDescriptorIndex(texOp);
TextureUsageFlags flags = TextureUsageFlags.NeedsScaleValue;
if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) && if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) &&
(texOp.Flags & TextureFlags.Bindless) == 0 && !isBindless &&
texOp.Type != SamplerType.Indexed) !isIndexed)
{ {
if (pCount == 3 && isArray) if (pCount == 3 && isArray)
{ {
// The array index is not scaled, just x and y. // The array index is not scaled, just x and y.
return "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)"; vector = "ivec3(Helper_TexelFetchScale((" + vector + ").xy, " + index + "), (" + vector + ").z)";
} }
else if (pCount == 2 && !isArray) else if (pCount == 2 && !isArray)
{ {
return "Helper_TexelFetchScale(" + vector + ", " + index + ")"; vector = "Helper_TexelFetchScale(" + vector + ", " + index + ")";
} }
else
{
flags |= TextureUsageFlags.ResScaleUnsupported;
}
}
else
{
// Resolution scaling cannot be applied to this texture right now.
// Flag so that we know to blacklist scaling on related textures when binding them.
flags |= TextureUsageFlags.ResScaleUnsupported;
} }
// Resolution scaling cannot be applied to this texture right now. context.TextureDescriptors[index] = context.TextureDescriptors[index].SetFlag(flags);
// Flag so that we know to blacklist scaling on related textures when binding them.
TextureDescriptor descriptor = context.TextureDescriptors[index];
descriptor.Flags |= TextureUsageFlags.ResScaleUnsupported;
context.TextureDescriptors[index] = descriptor;
} }
return vector; return vector;

View file

@ -15,6 +15,8 @@ namespace Ryujinx.Graphics.Shader.Instructions
public static void Suld(EmitterContext context) public static void Suld(EmitterContext context)
{ {
context.Config.SetUsedFeature(FeatureFlags.IntegerSampling);
OpCodeImage op = (OpCodeImage)context.CurrOp; OpCodeImage op = (OpCodeImage)context.CurrOp;
SamplerType type = ConvertSamplerType(op.Dimensions); SamplerType type = ConvertSamplerType(op.Dimensions);

View file

@ -46,5 +46,12 @@ namespace Ryujinx.Graphics.Shader
Flags = TextureUsageFlags.None; Flags = TextureUsageFlags.None;
} }
public TextureDescriptor SetFlag(TextureUsageFlags flag)
{
Flags |= flag;
return this;
}
} }
} }

View file

@ -11,6 +11,7 @@ namespace Ryujinx.Graphics.Shader
None = 0, None = 0,
// Integer sampled textures must be noted for resolution scaling. // Integer sampled textures must be noted for resolution scaling.
ResScaleUnsupported = 1 << 0 ResScaleUnsupported = 1 << 0,
NeedsScaleValue = 1 << 1
} }
} }