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 : BufferUpdater
{
private SupportBuffer _data;
///
/// Creates a new instance of the support buffer updater.
///
/// Renderer that the support buffer will be used with
public SupportBufferUpdater(IRenderer renderer) : base(renderer)
{
var defaultScale = new Vector4 { X = 1f, Y = 0f, Z = 0f, W = 0f };
_data.RenderScale.AsSpan().Fill(defaultScale);
DirtyRenderScale(0, SupportBuffer.RenderScaleMaxCount);
}
///
/// 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.
///
/// Index of the scale
/// Scale value
public void UpdateRenderScale(int index, float scale)
{
if (_data.RenderScale[1 + index].X != scale)
{
_data.RenderScale[1 + index].X = scale;
DirtyRenderScale(1 + index, 1);
}
}
///
/// Updates the render scales for shader input textures or images.
///
/// Total number of scales across all stages
/// Total number of scales on the fragment shader stage
public void UpdateRenderScaleFragmentCount(int totalCount, int fragmentCount)
{
// 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();
}
}
///
/// 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
});
}
}
///
/// Sets offset for the misaligned portion of a transform feedback buffer, and the buffer size, for transform feedback emulation.
///
/// Index of the transform feedback buffer
/// Misaligned offset of the buffer
public void SetTfeOffset(int bufferIndex, int offset)
{
ref int currentOffset = ref GetElementRef(ref _data.TfeOffset, bufferIndex);
if (currentOffset != offset)
{
currentOffset = offset;
MarkDirty(SupportBuffer.TfeOffsetOffset + bufferIndex * sizeof(int), sizeof(int));
}
}
///
/// Sets the vertex count used for transform feedback emulation with instanced draws.
///
/// Vertex count of the instanced draw
public void SetTfeVertexCount(int vertexCount)
{
if (_data.TfeVertexCount.X != vertexCount)
{
_data.TfeVertexCount.X = vertexCount;
MarkDirty(SupportBuffer.TfeVertexCountOffset, sizeof(int));
}
}
///
/// Submits all pending buffer updates to the GPU.
///
public void Commit()
{
Commit(MemoryMarshal.Cast(MemoryMarshal.CreateSpan(ref _data, 1)), SupportBuffer.Binding);
}
}
}