diff options
Diffstat (limited to 'src/Ryujinx.Graphics.Gpu')
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Engine/Threed/SemaphoreUpdater.cs | 8 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs | 17 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/GpuContext.cs | 9 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Image/Texture.cs | 6 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs | 2 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs | 4 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Memory/SupportBufferUpdater.cs | 232 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Shader/ShaderInfoBuilder.cs | 1 | ||||
-rw-r--r-- | src/Ryujinx.Graphics.Gpu/Window.cs | 11 |
9 files changed, 279 insertions, 11 deletions
diff --git a/src/Ryujinx.Graphics.Gpu/Engine/Threed/SemaphoreUpdater.cs b/src/Ryujinx.Graphics.Gpu/Engine/Threed/SemaphoreUpdater.cs index 0a813ee5..247752ca 100644 --- a/src/Ryujinx.Graphics.Gpu/Engine/Threed/SemaphoreUpdater.cs +++ b/src/Ryujinx.Graphics.Gpu/Engine/Threed/SemaphoreUpdater.cs @@ -177,13 +177,15 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed resultHandler(null, (ulong)_state.State.SemaphorePayload); break; case ReportCounterType.SamplesPassed: - counter = _context.Renderer.ReportCounter(CounterType.SamplesPassed, resultHandler, false); + float scale = _channel.TextureManager.RenderTargetScale; + float divisor = scale * scale; + counter = _context.Renderer.ReportCounter(CounterType.SamplesPassed, resultHandler, divisor, false); break; case ReportCounterType.PrimitivesGenerated: - counter = _context.Renderer.ReportCounter(CounterType.PrimitivesGenerated, resultHandler, false); + counter = _context.Renderer.ReportCounter(CounterType.PrimitivesGenerated, resultHandler, 1f, false); break; case ReportCounterType.TransformFeedbackPrimitivesWritten: - counter = _context.Renderer.ReportCounter(CounterType.TransformFeedbackPrimitivesWritten, resultHandler, false); + counter = _context.Renderer.ReportCounter(CounterType.TransformFeedbackPrimitivesWritten, resultHandler, 1f, false); break; } diff --git a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs index 34439657..b4f56245 100644 --- a/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs +++ b/src/Ryujinx.Graphics.Gpu/Engine/Threed/StateUpdater.cs @@ -495,6 +495,11 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed { clipRegionHeight = color.Height / samplesInY; } + + if (!_context.Capabilities.SupportsBgraFormat) + { + _context.SupportBufferUpdater.SetRenderTargetIsBgra(index, color.Format.IsBgr()); + } } } @@ -539,7 +544,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed if (oldScale != _channel.TextureManager.RenderTargetScale) { - _context.Renderer.Pipeline.SetRenderTargetScale(_channel.TextureManager.RenderTargetScale); + _context.SupportBufferUpdater.SetRenderTargetScale(_channel.TextureManager.RenderTargetScale); UpdateViewportTransform(); UpdateScissorState(); @@ -758,9 +763,15 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed } _context.Renderer.Pipeline.SetDepthMode(GetDepthMode()); - _context.Renderer.Pipeline.SetViewports(viewports, disableTransform); + _context.Renderer.Pipeline.SetViewports(viewports); + + _context.SupportBufferUpdater.SetViewportTransformDisable( + viewports[0].Region.Width, + viewports[0].Region.Height, + _channel.TextureManager.RenderTargetScale, + disableTransform); - _currentSpecState.SetViewportTransformDisable(_state.State.ViewportTransformEnable == 0); + _currentSpecState.SetViewportTransformDisable(disableTransform); _currentSpecState.SetDepthMode(GetDepthMode() == DepthMode.MinusOneToOne); } diff --git a/src/Ryujinx.Graphics.Gpu/GpuContext.cs b/src/Ryujinx.Graphics.Gpu/GpuContext.cs index a5fe8f4c..a50852b0 100644 --- a/src/Ryujinx.Graphics.Gpu/GpuContext.cs +++ b/src/Ryujinx.Graphics.Gpu/GpuContext.cs @@ -86,6 +86,11 @@ namespace Ryujinx.Graphics.Gpu internal ConcurrentDictionary<ulong, PhysicalMemory> PhysicalMemoryRegistry { get; } /// <summary> + /// Support buffer updater. + /// </summary> + internal SupportBufferUpdater SupportBufferUpdater { get; } + + /// <summary> /// Host hardware capabilities. /// </summary> internal Capabilities Capabilities; @@ -125,6 +130,8 @@ namespace Ryujinx.Graphics.Gpu PhysicalMemoryRegistry = new ConcurrentDictionary<ulong, PhysicalMemory>(); + SupportBufferUpdater = new SupportBufferUpdater(renderer); + _firstTimestamp = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds); } @@ -399,6 +406,8 @@ namespace Ryujinx.Graphics.Gpu physicalMemory.Dispose(); } + SupportBufferUpdater.Dispose(); + PhysicalMemoryRegistry.Clear(); RunDeferredActions(); diff --git a/src/Ryujinx.Graphics.Gpu/Image/Texture.cs b/src/Ryujinx.Graphics.Gpu/Image/Texture.cs index c0d45cbd..0fce4deb 100644 --- a/src/Ryujinx.Graphics.Gpu/Image/Texture.cs +++ b/src/Ryujinx.Graphics.Gpu/Image/Texture.cs @@ -278,7 +278,7 @@ namespace Ryujinx.Graphics.Gpu.Image Debug.Assert(!isView); TextureCreateInfo createInfo = TextureCache.GetCreateInfo(Info, _context.Capabilities, ScaleFactor); - HostTexture = _context.Renderer.CreateTexture(createInfo, ScaleFactor); + HostTexture = _context.Renderer.CreateTexture(createInfo); SynchronizeMemory(); // Load the data. if (ScaleMode == TextureScaleMode.Scaled) @@ -302,7 +302,7 @@ namespace Ryujinx.Graphics.Gpu.Image } TextureCreateInfo createInfo = TextureCache.GetCreateInfo(Info, _context.Capabilities, ScaleFactor); - HostTexture = _context.Renderer.CreateTexture(createInfo, ScaleFactor); + HostTexture = _context.Renderer.CreateTexture(createInfo); } } } @@ -490,7 +490,7 @@ namespace Ryujinx.Graphics.Gpu.Image if (storage == null) { TextureCreateInfo createInfo = TextureCache.GetCreateInfo(Info, _context.Capabilities, scale); - storage = _context.Renderer.CreateTexture(createInfo, scale); + storage = _context.Renderer.CreateTexture(createInfo); } if (copy) diff --git a/src/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs b/src/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs index e5df1776..d1454fc9 100644 --- a/src/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs +++ b/src/Ryujinx.Graphics.Gpu/Image/TextureBindingsManager.cs @@ -302,7 +302,7 @@ namespace Ryujinx.Graphics.Gpu.Image _lastFragmentTotal = fragmentTotal; - _context.Renderer.Pipeline.UpdateRenderScale(_scales, total, fragmentTotal); + _context.SupportBufferUpdater.UpdateRenderScale(_scales, total, fragmentTotal); _scaleChanged = false; } diff --git a/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs b/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs index 4cd3710b..10224a6d 100644 --- a/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs +++ b/src/Ryujinx.Graphics.Gpu/Memory/BufferManager.cs @@ -478,6 +478,8 @@ namespace Ryujinx.Graphics.Gpu.Memory // Force rebind after doing compute work. Rebind(); + + _context.SupportBufferUpdater.Commit(); } /// <summary> @@ -686,6 +688,8 @@ namespace Ryujinx.Graphics.Gpu.Memory CommitBufferTextureBindings(); _rebind = false; + + _context.SupportBufferUpdater.Commit(); } /// <summary> diff --git a/src/Ryujinx.Graphics.Gpu/Memory/SupportBufferUpdater.cs b/src/Ryujinx.Graphics.Gpu/Memory/SupportBufferUpdater.cs new file mode 100644 index 00000000..3ea37c55 --- /dev/null +++ b/src/Ryujinx.Graphics.Gpu/Memory/SupportBufferUpdater.cs @@ -0,0 +1,232 @@ +using Ryujinx.Graphics.GAL; +using Ryujinx.Graphics.Shader; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace Ryujinx.Graphics.Gpu.Memory +{ + /// <summary> + /// Support buffer data updater. + /// </summary> + class SupportBufferUpdater : IDisposable + { + private SupportBuffer _data; + private BufferHandle _handle; + + private readonly IRenderer _renderer; + private int _startOffset = -1; + private int _endOffset = -1; + + /// <summary> + /// Creates a new instance of the support buffer updater. + /// </summary> + /// <param name="renderer">Renderer that the support buffer will be used with</param> + public SupportBufferUpdater(IRenderer renderer) + { + _renderer = renderer; + + var defaultScale = new Vector4<float> { X = 1f, Y = 0f, Z = 0f, W = 0f }; + _data.RenderScale.AsSpan().Fill(defaultScale); + DirtyRenderScale(0, SupportBuffer.RenderScaleMaxCount); + } + + /// <summary> + /// Mark a region of the support buffer as modified and needing to be sent to the GPU. + /// </summary> + /// <param name="startOffset">Start offset of the region in bytes</param> + /// <param name="byteSize">Size of the region in bytes</param> + private void MarkDirty(int startOffset, int byteSize) + { + int endOffset = startOffset + byteSize; + + if (_startOffset == -1) + { + _startOffset = startOffset; + _endOffset = endOffset; + } + else + { + if (startOffset < _startOffset) + { + _startOffset = startOffset; + } + + if (endOffset > _endOffset) + { + _endOffset = endOffset; + } + } + } + + /// <summary> + /// Marks the fragment render scale count as being modified. + /// </summary> + private void DirtyFragmentRenderScaleCount() + { + MarkDirty(SupportBuffer.FragmentRenderScaleCountOffset, sizeof(int)); + } + + /// <summary> + /// Marks data of a given type as being modified. + /// </summary> + /// <typeparam name="T">Type of the data</typeparam> + /// <param name="baseOffset">Base offset of the data in bytes</param> + /// <param name="offset">Index of the data, in elements</param> + /// <param name="count">Number of elements</param> + private void DirtyGenericField<T>(int baseOffset, int offset, int count) where T : unmanaged + { + int elemSize = Unsafe.SizeOf<T>(); + + MarkDirty(baseOffset + offset * elemSize, count * elemSize); + } + + /// <summary> + /// Marks render scales as being modified. + /// </summary> + /// <param name="offset">Index of the first scale that was modified</param> + /// <param name="count">Number of modified scales</param> + private void DirtyRenderScale(int offset, int count) + { + DirtyGenericField<Vector4<float>>(SupportBuffer.GraphicsRenderScaleOffset, offset, count); + } + + /// <summary> + /// Marks render target BGRA format state as modified. + /// </summary> + /// <param name="offset">Index of the first render target that had its BGRA format modified</param> + /// <param name="count">Number of render targets</param> + private void DirtyFragmentIsBgra(int offset, int count) + { + DirtyGenericField<Vector4<int>>(SupportBuffer.FragmentIsBgraOffset, offset, count); + } + + /// <summary> + /// Updates the inverse viewport vector. + /// </summary> + /// <param name="data">Inverse viewport vector</param> + private void UpdateViewportInverse(Vector4<float> data) + { + _data.ViewportInverse = data; + + MarkDirty(SupportBuffer.ViewportInverseOffset, SupportBuffer.FieldSize); + } + + /// <summary> + /// Sets the scale of all output render targets (they should all have the same scale). + /// </summary> + /// <param name="scale">Scale value</param> + public void SetRenderTargetScale(float scale) + { + _data.RenderScale[0].X = scale; + DirtyRenderScale(0, 1); // Just the first element. + } + + /// <summary> + /// Updates the render scales for shader input textures or images. + /// </summary> + /// <param name="scales">Scale values</param> + /// <param name="totalCount">Total number of scales across all stages</param> + /// <param name="fragmentCount">Total number of scales on the fragment shader stage</param> + public void UpdateRenderScale(ReadOnlySpan<float> scales, int totalCount, int fragmentCount) + { + bool changed = false; + + for (int index = 0; index < totalCount; index++) + { + if (_data.RenderScale[1 + index].X != scales[index]) + { + _data.RenderScale[1 + index].X = scales[index]; + changed = true; + } + } + + // Only update fragment count if there are scales after it for the vertex stage. + if (fragmentCount != totalCount && fragmentCount != _data.FragmentRenderScaleCount.X) + { + _data.FragmentRenderScaleCount.X = fragmentCount; + DirtyFragmentRenderScaleCount(); + } + + if (changed) + { + DirtyRenderScale(0, 1 + totalCount); + } + } + + /// <summary> + /// Sets whether the format of a given render target is a BGRA format. + /// </summary> + /// <param name="index">Render target index</param> + /// <param name="isBgra">True if the format is BGRA< false otherwise</param> + public void SetRenderTargetIsBgra(int index, bool isBgra) + { + bool isBgraChanged = (_data.FragmentIsBgra[index].X != 0) != isBgra; + + if (isBgraChanged) + { + _data.FragmentIsBgra[index].X = isBgra ? 1 : 0; + DirtyFragmentIsBgra(index, 1); + } + } + + /// <summary> + /// Sets whether a viewport has transform disabled. + /// </summary> + /// <param name="viewportWidth">Value used as viewport width</param> + /// <param name="viewportHeight">Value used as viewport height</param> + /// <param name="scale">Render target scale</param> + /// <param name="disableTransform">True if transform is disabled, false otherwise</param> + public void SetViewportTransformDisable(float viewportWidth, float viewportHeight, float scale, bool disableTransform) + { + float disableTransformF = disableTransform ? 1.0f : 0.0f; + if (_data.ViewportInverse.W != disableTransformF || disableTransform) + { + UpdateViewportInverse(new Vector4<float> + { + X = scale * 2f / viewportWidth, + Y = scale * 2f / viewportHeight, + Z = 1, + W = disableTransformF + }); + } + } + + /// <summary> + /// Submits all pending buffer updates to the GPU. + /// </summary> + public void Commit() + { + if (_startOffset != -1) + { + if (_handle == BufferHandle.Null) + { + _handle = _renderer.CreateBuffer(SupportBuffer.RequiredSize); + _renderer.Pipeline.ClearBuffer(_handle, 0, SupportBuffer.RequiredSize, 0); + + var range = new BufferRange(_handle, 0, SupportBuffer.RequiredSize); + _renderer.Pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(0, range) }); + } + + ReadOnlySpan<byte> data = MemoryMarshal.Cast<SupportBuffer, byte>(MemoryMarshal.CreateSpan(ref _data, 1)); + + _renderer.SetBufferData(_handle, _startOffset, data.Slice(_startOffset, _endOffset - _startOffset)); + + _startOffset = -1; + _endOffset = -1; + } + } + + /// <summary> + /// Destroys the support buffer. + /// </summary> + public void Dispose() + { + if (_handle != BufferHandle.Null) + { + _renderer.DeleteBuffer(_handle); + _handle = BufferHandle.Null; + } + } + } +} diff --git a/src/Ryujinx.Graphics.Gpu/Shader/ShaderInfoBuilder.cs b/src/Ryujinx.Graphics.Gpu/Shader/ShaderInfoBuilder.cs index 7356410c..af1e1ee3 100644 --- a/src/Ryujinx.Graphics.Gpu/Shader/ShaderInfoBuilder.cs +++ b/src/Ryujinx.Graphics.Gpu/Shader/ShaderInfoBuilder.cs @@ -58,6 +58,7 @@ namespace Ryujinx.Graphics.Gpu.Shader } AddDescriptor(SupportBufferStages, ResourceType.UniformBuffer, UniformSetIndex, 0, 1); + AddUsage(SupportBufferStages, ResourceType.UniformBuffer, ResourceAccess.Read, UniformSetIndex, 0, 1); _reservedConstantBuffers = 1; // For the support buffer. diff --git a/src/Ryujinx.Graphics.Gpu/Window.cs b/src/Ryujinx.Graphics.Gpu/Window.cs index 5da472b3..89addc8a 100644 --- a/src/Ryujinx.Graphics.Gpu/Window.cs +++ b/src/Ryujinx.Graphics.Gpu/Window.cs @@ -208,7 +208,16 @@ namespace Ryujinx.Graphics.Gpu texture.SynchronizeMemory(); - ImageCrop crop = pt.Crop; + ImageCrop crop = new ImageCrop( + (int)(pt.Crop.Left * texture.ScaleFactor), + (int)MathF.Ceiling(pt.Crop.Right * texture.ScaleFactor), + (int)(pt.Crop.Top * texture.ScaleFactor), + (int)MathF.Ceiling(pt.Crop.Bottom * texture.ScaleFactor), + pt.Crop.FlipX, + pt.Crop.FlipY, + pt.Crop.IsStretched, + pt.Crop.AspectRatioX, + pt.Crop.AspectRatioY); if (texture.Info.Width > pt.Info.Width || texture.Info.Height > pt.Info.Height) { |