diff --git a/Ryujinx.Graphics.GAL/IPipeline.cs b/Ryujinx.Graphics.GAL/IPipeline.cs index 818712c32..3baf272f9 100644 --- a/Ryujinx.Graphics.GAL/IPipeline.cs +++ b/Ryujinx.Graphics.GAL/IPipeline.cs @@ -92,6 +92,6 @@ namespace Ryujinx.Graphics.GAL bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual); void EndHostConditionalRendering(); - void UpdateRenderScale(ShaderStage stage, int textureCount); + void UpdateRenderScale(ShaderStage stage, float[] scales, int textureCount, int imageCount); } } diff --git a/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs b/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs index e70b09337..08c4082ef 100644 --- a/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs +++ b/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs @@ -41,6 +41,9 @@ namespace Ryujinx.Graphics.Gpu.Image private bool _rebind; + private float[] _scales; + private bool _scaleChanged; + /// /// Constructs a new instance of the texture bindings manager. /// @@ -60,6 +63,13 @@ namespace Ryujinx.Graphics.Gpu.Image _textureState = new TextureStatePerStage[stages][]; _imageState = new TextureStatePerStage[stages][]; + + _scales = new float[64]; + + for (int i = 0; i < 64; i++) + { + _scales[i] = 1f; + } } /// @@ -131,6 +141,81 @@ namespace Ryujinx.Graphics.Gpu.Image _texturePoolMaximumId = maximumId; } + /// + /// Updates the texture scale for a given texture or image. + /// + /// Start GPU virtual address of the pool + /// The related texture binding + /// The texture/image binding index + /// The active shader stage + /// True if the given texture has become blacklisted, indicating that its host texture may have changed. + 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; + } + + /// + /// Uploads texture and image scales to the backend when they are used. + /// + /// Current shader stage + /// Shader stage index + private void CommitRenderScale(ShaderStage stage, int stageIndex) + { + if (_scaleChanged) + { + _context.Renderer.Pipeline.UpdateRenderScale(stage, _scales, _textureBindings[stageIndex]?.Length ?? 0, _imageBindings[stageIndex]?.Length ?? 0); + } + } + /// /// Ensures that the bindings are visible to the host GPU. /// 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); CommitImageBindings (texturePool, ShaderStage.Compute, 0); + + CommitRenderScale(ShaderStage.Compute, 0); } else { @@ -154,6 +241,8 @@ namespace Ryujinx.Graphics.Gpu.Image CommitTextureBindings(texturePool, stage, stageIndex); CommitImageBindings (texturePool, stage, stageIndex); + + CommitRenderScale(stage, stageIndex); } } @@ -174,8 +263,6 @@ namespace Ryujinx.Graphics.Gpu.Image return; } - bool changed = false; - for (int index = 0; index < _textureBindings[stageIndex].Length; index++) { TextureBindingInfo binding = _textureBindings[stageIndex][index]; @@ -218,20 +305,18 @@ namespace Ryujinx.Graphics.Gpu.Image Texture texture = pool.Get(textureId); - if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0) - { - texture?.BlacklistScale(); - } - ITexture hostTexture = texture?.GetTargetTexture(binding.Target); if (_textureState[stageIndex][index].Texture != hostTexture || _rebind) { + if (UpdateScale(texture, binding, index, stage)) + { + hostTexture = texture?.GetTargetTexture(binding.Target); + } + _textureState[stageIndex][index].Texture = hostTexture; _context.Renderer.Pipeline.SetTexture(index, stage, hostTexture); - - changed = true; } if (hostTexture != null && texture.Info.Target == Target.TextureBuffer) @@ -253,11 +338,6 @@ namespace Ryujinx.Graphics.Gpu.Image _context.Renderer.Pipeline.SetSampler(index, stage, hostSampler); } } - - if (changed) - { - _context.Renderer.Pipeline.UpdateRenderScale(stage, _textureBindings[stageIndex].Length); - } } /// @@ -274,6 +354,9 @@ namespace Ryujinx.Graphics.Gpu.Image return; } + // Scales for images appear after the texture ones. + int baseScaleIndex = _textureBindings[stageIndex]?.Length ?? 0; + for (int index = 0; index < _imageBindings[stageIndex].Length; index++) { TextureBindingInfo binding = _imageBindings[stageIndex][index]; @@ -283,11 +366,6 @@ namespace Ryujinx.Graphics.Gpu.Image Texture texture = pool.Get(textureId); - if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0) - { - texture?.BlacklistScale(); - } - ITexture hostTexture = texture?.GetTargetTexture(binding.Target); 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 (UpdateScale(texture, binding, baseScaleIndex + index, stage)) + { + hostTexture = texture?.GetTargetTexture(binding.Target); + } + _imageState[stageIndex][index].Texture = hostTexture; Format format = binding.Format; diff --git a/Ryujinx.Graphics.Gpu/Image/TextureManager.cs b/Ryujinx.Graphics.Gpu/Image/TextureManager.cs index 7588258f0..08398cb67 100644 --- a/Ryujinx.Graphics.Gpu/Image/TextureManager.cs +++ b/Ryujinx.Graphics.Gpu/Image/TextureManager.cs @@ -194,6 +194,15 @@ namespace Ryujinx.Graphics.Gpu.Image return changesScale || (hasValue && color.ScaleMode != TextureScaleMode.Blacklisted && color.ScaleFactor != GraphicsConfig.ResScale); } + /// + /// Gets the first available bound colour target, or the depth stencil target if not present. + /// + /// The first bound colour target, otherwise the depth stencil target + public Texture GetAnyRenderTarget() + { + return _rtColors[0] ?? _rtDepthStencil; + } + /// /// 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, diff --git a/Ryujinx.Graphics.OpenGL/Pipeline.cs b/Ryujinx.Graphics.OpenGL/Pipeline.cs index 6277fe168..06cf5ef42 100644 --- a/Ryujinx.Graphics.OpenGL/Pipeline.cs +++ b/Ryujinx.Graphics.OpenGL/Pipeline.cs @@ -32,12 +32,10 @@ namespace Ryujinx.Graphics.OpenGL private int _boundReadFramebuffer; private int[] _fpIsBgra = new int[8]; - private float[] _fpRenderScale = new float[33]; - private float[] _cpRenderScale = new float[32]; + private float[] _fpRenderScale = new float[65]; + private float[] _cpRenderScale = new float[64]; private TextureBase _unit0Texture; - private TextureBase _rtColor0Texture; - private TextureBase _rtDepthTexture; private FrontFaceDirection _frontFace; private ClipOrigin _clipOrigin; @@ -847,9 +845,6 @@ namespace Ryujinx.Graphics.OpenGL { EnsureFramebuffer(); - _rtColor0Texture = (TextureBase)colors[0]; - _rtDepthTexture = (TextureBase)depthStencil; - for (int index = 0; index < colors.Length; index++) { TextureView color = (TextureView)colors[index]; @@ -963,39 +958,6 @@ namespace Ryujinx.Graphics.OpenGL { ((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) { @@ -1241,14 +1203,16 @@ namespace Ryujinx.Graphics.OpenGL case ShaderStage.Fragment: 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; case ShaderStage.Compute: 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; } diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs index 85347dfdf..f86e82da2 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs @@ -84,7 +84,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl AppendLine("}" + suffix); } - public int FindTextureDescriptorIndex(AstTextureOperation texOp) + private int FindDescriptorIndex(List list, AstTextureOperation texOp) { AstOperand operand = texOp.GetSource(0) as AstOperand; bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0; @@ -92,13 +92,24 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl int cBufSlot = bindless ? operand.CbufSlot : 0; int cBufOffset = bindless ? operand.CbufOffset : 0; - return TextureDescriptors.FindIndex(descriptor => + return list.FindIndex(descriptor => descriptor.Type == texOp.Type && descriptor.HandleIndex == texOp.Handle && + descriptor.Format == texOp.Format && descriptor.CbufSlot == cBufSlot && 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) { return _info.Functions[id]; diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs index 0250d8520..734546a0e 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs @@ -509,7 +509,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { 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) { diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs index 456bfc4eb..f10eb101b 100644 --- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs +++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs @@ -47,6 +47,43 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions 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) { string[] elems = new string[pCount]; @@ -56,7 +93,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions elems[index] = Src(VariableType.S32); } - Append("ivec" + pCount + "(" + string.Join(", ", elems) + ")"); + Append(ApplyScaling("ivec" + pCount + "(" + string.Join(", ", elems) + ")")); } else { @@ -404,28 +441,35 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions if (intCoords) { int index = context.FindTextureDescriptorIndex(texOp); + TextureUsageFlags flags = TextureUsageFlags.NeedsScaleValue; if ((context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute) && - (texOp.Flags & TextureFlags.Bindless) == 0 && - texOp.Type != SamplerType.Indexed) + !isBindless && + !isIndexed) { if (pCount == 3 && isArray) { // 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) { - 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. - // 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; + context.TextureDescriptors[index] = context.TextureDescriptors[index].SetFlag(flags); } return vector; diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs index 49e6cf5d3..6b439901e 100644 --- a/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs +++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitTexture.cs @@ -15,6 +15,8 @@ namespace Ryujinx.Graphics.Shader.Instructions public static void Suld(EmitterContext context) { + context.Config.SetUsedFeature(FeatureFlags.IntegerSampling); + OpCodeImage op = (OpCodeImage)context.CurrOp; SamplerType type = ConvertSamplerType(op.Dimensions); diff --git a/Ryujinx.Graphics.Shader/TextureDescriptor.cs b/Ryujinx.Graphics.Shader/TextureDescriptor.cs index 7c2bd7141..7cf868efa 100644 --- a/Ryujinx.Graphics.Shader/TextureDescriptor.cs +++ b/Ryujinx.Graphics.Shader/TextureDescriptor.cs @@ -46,5 +46,12 @@ namespace Ryujinx.Graphics.Shader Flags = TextureUsageFlags.None; } + + public TextureDescriptor SetFlag(TextureUsageFlags flag) + { + Flags |= flag; + + return this; + } } } \ No newline at end of file diff --git a/Ryujinx.Graphics.Shader/TextureUsageFlags.cs b/Ryujinx.Graphics.Shader/TextureUsageFlags.cs index d9fa1a504..1f0243098 100644 --- a/Ryujinx.Graphics.Shader/TextureUsageFlags.cs +++ b/Ryujinx.Graphics.Shader/TextureUsageFlags.cs @@ -11,6 +11,7 @@ namespace Ryujinx.Graphics.Shader None = 0, // Integer sampled textures must be noted for resolution scaling. - ResScaleUnsupported = 1 << 0 + ResScaleUnsupported = 1 << 0, + NeedsScaleValue = 1 << 1 } }