using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.Shader; using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Ryujinx.Graphics.Gpu.Memory { /// /// Support buffer data updater. /// class SupportBufferUpdater : IDisposable { private SupportBuffer _data; private BufferHandle _handle; private readonly IRenderer _renderer; private int _startOffset = -1; private int _endOffset = -1; /// /// Creates a new instance of the support buffer updater. /// /// Renderer that the support buffer will be used with public SupportBufferUpdater(IRenderer renderer) { _renderer = renderer; var defaultScale = new Vector4 { X = 1f, Y = 0f, Z = 0f, W = 0f }; _data.RenderScale.AsSpan().Fill(defaultScale); DirtyRenderScale(0, SupportBuffer.RenderScaleMaxCount); } /// /// Mark a region of the support buffer as modified and needing to be sent to the GPU. /// /// Start offset of the region in bytes /// Size of the region in bytes 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; } } } /// /// Marks the fragment render scale count as being modified. /// private void DirtyFragmentRenderScaleCount() { MarkDirty(SupportBuffer.FragmentRenderScaleCountOffset, sizeof(int)); } /// /// Marks data of a given type as being modified. /// /// Type of the data /// Base offset of the data in bytes /// Index of the data, in elements /// Number of elements private void DirtyGenericField(int baseOffset, int offset, int count) where T : unmanaged { int elemSize = Unsafe.SizeOf(); MarkDirty(baseOffset + offset * elemSize, count * elemSize); } /// /// Marks render scales as being modified. /// /// Index of the first scale that was modified /// Number of modified scales private void DirtyRenderScale(int offset, int count) { DirtyGenericField>(SupportBuffer.GraphicsRenderScaleOffset, offset, count); } /// /// Marks render target BGRA format state as modified. /// /// Index of the first render target that had its BGRA format modified /// Number of render targets private void DirtyFragmentIsBgra(int offset, int count) { DirtyGenericField>(SupportBuffer.FragmentIsBgraOffset, offset, count); } /// /// Updates the inverse viewport vector. /// /// Inverse viewport vector private void UpdateViewportInverse(Vector4 data) { _data.ViewportInverse = data; MarkDirty(SupportBuffer.ViewportInverseOffset, SupportBuffer.FieldSize); } /// /// Updates the viewport size vector. /// /// Viewport size vector private void UpdateViewportSize(Vector4 data) { _data.ViewportSize = data; MarkDirty(SupportBuffer.ViewportSizeOffset, SupportBuffer.FieldSize); } /// /// Sets the scale of all output render targets (they should all have the same scale). /// /// Scale value public void SetRenderTargetScale(float scale) { _data.RenderScale[0].X = scale; DirtyRenderScale(0, 1); // Just the first element. } /// /// Updates the render scales for shader input textures or images. /// /// Scale values /// Total number of scales across all stages /// Total number of scales on the fragment shader stage public void UpdateRenderScale(ReadOnlySpan 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); } } /// /// Sets whether the format of a given render target is a BGRA format. /// /// Render target index /// True if the format is BGRA< false otherwise 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); } } /// /// Sets whether a viewport has transform disabled. /// /// Value used as viewport width /// Value used as viewport height /// Render target scale /// True if transform is disabled, false otherwise 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 { X = scale * 2f / viewportWidth, Y = scale * 2f / viewportHeight, Z = 1, W = disableTransformF, }); } } /// /// Sets the viewport size, used to invert the fragment coordinates Y value. /// /// Value used as viewport width /// Value used as viewport height public void SetViewportSize(float viewportWidth, float viewportHeight) { if (_data.ViewportSize.X != viewportWidth || _data.ViewportSize.Y != viewportHeight) { UpdateViewportSize(new Vector4 { X = viewportWidth, Y = viewportHeight, Z = 1, W = 0 }); } } /// /// Submits all pending buffer updates to the GPU. /// 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 data = MemoryMarshal.Cast(MemoryMarshal.CreateSpan(ref _data, 1)); _renderer.SetBufferData(_handle, _startOffset, data[_startOffset.._endOffset]); _startOffset = -1; _endOffset = -1; } } /// /// Destroys the support buffer. /// public void Dispose() { if (_handle != BufferHandle.Null) { _renderer.DeleteBuffer(_handle); _handle = BufferHandle.Null; } } } }