2020-05-06 03:02:28 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
|
using Ryujinx.Graphics.Shader;
|
2021-10-12 22:35:31 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2020-05-06 03:02:28 +02:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Shader
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a GPU state and memory accessor.
|
|
|
|
|
/// </summary>
|
2021-08-11 23:56:59 +02:00
|
|
|
|
class GpuAccessor : TextureDescriptorCapableGpuAccessor, IGpuAccessor
|
2020-05-06 03:02:28 +02:00
|
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
|
private readonly GpuChannel _channel;
|
|
|
|
|
private readonly GpuAccessorState _state;
|
2020-05-06 03:02:28 +02:00
|
|
|
|
private readonly int _stageIndex;
|
|
|
|
|
private readonly bool _compute;
|
|
|
|
|
private readonly int _localSizeX;
|
|
|
|
|
private readonly int _localSizeY;
|
|
|
|
|
private readonly int _localSizeZ;
|
|
|
|
|
private readonly int _localMemorySize;
|
|
|
|
|
private readonly int _sharedMemorySize;
|
|
|
|
|
|
2021-08-11 20:59:42 +02:00
|
|
|
|
public int Cb1DataSize { get; private set; }
|
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of the GPU state accessor for graphics shader translation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">GPU context</param>
|
2021-07-08 01:56:06 +02:00
|
|
|
|
/// <param name="channel">GPU channel</param>
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
|
/// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param>
|
2021-08-11 23:01:06 +02:00
|
|
|
|
public GpuAccessor(GpuContext context, GpuChannel channel, GpuAccessorState state, int stageIndex) : base(context)
|
2020-05-06 03:02:28 +02:00
|
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
|
_channel = channel;
|
2020-05-06 03:02:28 +02:00
|
|
|
|
_state = state;
|
|
|
|
|
_stageIndex = stageIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of the GPU state accessor for compute shader translation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">GPU context</param>
|
2021-07-08 01:56:06 +02:00
|
|
|
|
/// <param name="channel">GPU channel</param>
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
|
/// <param name="localSizeX">Local group size X of the compute shader</param>
|
|
|
|
|
/// <param name="localSizeY">Local group size Y of the compute shader</param>
|
|
|
|
|
/// <param name="localSizeZ">Local group size Z of the compute shader</param>
|
|
|
|
|
/// <param name="localMemorySize">Local memory size of the compute shader</param>
|
|
|
|
|
/// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
|
|
|
|
|
public GpuAccessor(
|
|
|
|
|
GpuContext context,
|
2021-07-08 01:56:06 +02:00
|
|
|
|
GpuChannel channel,
|
|
|
|
|
GpuAccessorState state,
|
2020-05-06 03:02:28 +02:00
|
|
|
|
int localSizeX,
|
|
|
|
|
int localSizeY,
|
|
|
|
|
int localSizeZ,
|
|
|
|
|
int localMemorySize,
|
2021-08-11 23:01:06 +02:00
|
|
|
|
int sharedMemorySize) : base(context)
|
2020-05-06 03:02:28 +02:00
|
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
|
_channel = channel;
|
2020-05-06 03:02:28 +02:00
|
|
|
|
_state = state;
|
|
|
|
|
_compute = true;
|
|
|
|
|
_localSizeX = localSizeX;
|
|
|
|
|
_localSizeY = localSizeY;
|
|
|
|
|
_localSizeZ = localSizeZ;
|
|
|
|
|
_localMemorySize = localMemorySize;
|
|
|
|
|
_sharedMemorySize = sharedMemorySize;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 20:59:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads data from the constant buffer 1.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="offset">Offset in bytes to read from</param>
|
|
|
|
|
/// <returns>Value at the given offset</returns>
|
|
|
|
|
public uint ConstantBuffer1Read(int offset)
|
|
|
|
|
{
|
|
|
|
|
if (Cb1DataSize < offset + 4)
|
|
|
|
|
{
|
|
|
|
|
Cb1DataSize = offset + 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ulong baseAddress = _compute
|
|
|
|
|
? _channel.BufferManager.GetComputeUniformBufferAddress(1)
|
|
|
|
|
: _channel.BufferManager.GetGraphicsUniformBufferAddress(_stageIndex, 1);
|
|
|
|
|
|
|
|
|
|
return _channel.MemoryManager.Physical.Read<uint>(baseAddress + (ulong)offset);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints a log message.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">Message to print</param>
|
|
|
|
|
public void Log(string message)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
|
2020-05-06 03:02:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-12 22:35:31 +02:00
|
|
|
|
/// Gets a span of the specified memory location, containing shader code.
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">GPU virtual address of the data</param>
|
2021-10-12 22:35:31 +02:00
|
|
|
|
/// <param name="minimumSize">Minimum size that the returned span may have</param>
|
|
|
|
|
/// <returns>Span of the memory location</returns>
|
|
|
|
|
public override ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
|
2020-05-06 03:02:28 +02:00
|
|
|
|
{
|
2021-10-12 22:35:31 +02:00
|
|
|
|
int size = Math.Max(minimumSize, 0x1000 - (int)(address & 0xfff));
|
|
|
|
|
return MemoryMarshal.Cast<byte, ulong>(_channel.MemoryManager.GetSpan(address, size));
|
2020-10-03 07:43:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Size X for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Size X</returns>
|
|
|
|
|
public int QueryComputeLocalSizeX() => _localSizeX;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Size Y for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Size Y</returns>
|
|
|
|
|
public int QueryComputeLocalSizeY() => _localSizeY;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Size Z for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Size Z</returns>
|
|
|
|
|
public int QueryComputeLocalSizeZ() => _localSizeZ;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Memory size in bytes for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Memory size in bytes</returns>
|
|
|
|
|
public int QueryComputeLocalMemorySize() => _localMemorySize;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Shared Memory size in bytes for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Shared Memory size in bytes</returns>
|
|
|
|
|
public int QueryComputeSharedMemorySize() => _sharedMemorySize;
|
|
|
|
|
|
2020-10-13 02:40:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Constant Buffer usage information.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A mask where each bit set indicates a bound constant buffer</returns>
|
|
|
|
|
public uint QueryConstantBufferUse()
|
|
|
|
|
{
|
|
|
|
|
return _compute
|
2021-07-08 01:56:06 +02:00
|
|
|
|
? _channel.BufferManager.GetComputeUniformBufferUseMask()
|
|
|
|
|
: _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
|
2020-10-13 02:40:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries current primitive topology for geometry shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Current primitive topology</returns>
|
|
|
|
|
public InputTopology QueryPrimitiveTopology()
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return _state.Topology switch
|
2020-05-06 03:02:28 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
PrimitiveTopology.Points => InputTopology.Points,
|
|
|
|
|
PrimitiveTopology.Lines or
|
|
|
|
|
PrimitiveTopology.LineLoop or
|
|
|
|
|
PrimitiveTopology.LineStrip => InputTopology.Lines,
|
|
|
|
|
PrimitiveTopology.LinesAdjacency or
|
|
|
|
|
PrimitiveTopology.LineStripAdjacency => InputTopology.LinesAdjacency,
|
|
|
|
|
PrimitiveTopology.Triangles or
|
|
|
|
|
PrimitiveTopology.TriangleStrip or
|
|
|
|
|
PrimitiveTopology.TriangleFan => InputTopology.Triangles,
|
|
|
|
|
PrimitiveTopology.TrianglesAdjacency or
|
|
|
|
|
PrimitiveTopology.TriangleStripAdjacency => InputTopology.TrianglesAdjacency,
|
2021-10-18 23:38:04 +02:00
|
|
|
|
PrimitiveTopology.Patches => _state.TessellationMode.UnpackPatchType() == TessPatchType.Isolines
|
|
|
|
|
? InputTopology.Lines
|
|
|
|
|
: InputTopology.Triangles,
|
|
|
|
|
_ => InputTopology.Points
|
2021-07-11 22:20:40 +02:00
|
|
|
|
};
|
2020-05-06 03:02:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 23:38:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries the tessellation evaluation shader primitive winding order.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if the primitive winding order is clockwise, false if counter-clockwise</returns>
|
|
|
|
|
public bool QueryTessCw() => _state.TessellationMode.UnpackCw();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries the tessellation evaluation shader abstract patch type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Abstract patch type</returns>
|
|
|
|
|
public TessPatchType QueryTessPatchType() => _state.TessellationMode.UnpackPatchType();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries the tessellation evaluation shader spacing between tessellated vertices of the patch.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Spacing between tessellated vertices of the patch</returns>
|
|
|
|
|
public TessSpacing QueryTessSpacing() => _state.TessellationMode.UnpackSpacing();
|
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the texture descriptor for a given texture on the pool.
|
|
|
|
|
/// </summary>
|
2020-11-13 00:15:34 +01:00
|
|
|
|
/// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
|
2021-05-19 20:05:43 +02:00
|
|
|
|
/// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
|
2020-05-06 03:02:28 +02:00
|
|
|
|
/// <returns>Texture descriptor</returns>
|
2021-05-19 20:05:43 +02:00
|
|
|
|
public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
|
2020-05-06 03:02:28 +02:00
|
|
|
|
{
|
|
|
|
|
if (_compute)
|
|
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
|
return _channel.TextureManager.GetComputeTextureDescriptor(
|
|
|
|
|
_state.TexturePoolGpuVa,
|
|
|
|
|
_state.TextureBufferIndex,
|
|
|
|
|
_state.TexturePoolMaximumId,
|
|
|
|
|
handle,
|
|
|
|
|
cbufSlot);
|
2020-05-06 03:02:28 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
|
return _channel.TextureManager.GetGraphicsTextureDescriptor(
|
|
|
|
|
_state.TexturePoolGpuVa,
|
|
|
|
|
_state.TextureBufferIndex,
|
|
|
|
|
_state.TexturePoolMaximumId,
|
|
|
|
|
_stageIndex,
|
|
|
|
|
handle,
|
|
|
|
|
cbufSlot);
|
2020-05-06 03:02:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-02 00:13:27 +01:00
|
|
|
|
|
2022-01-21 16:35:21 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries transform feedback enable state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if the shader uses transform feedback, false otherwise</returns>
|
|
|
|
|
public bool QueryTransformFeedbackEnabled()
|
|
|
|
|
{
|
|
|
|
|
return _state.TransformFeedbackDescriptors != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries the varying locations that should be written to the transform feedback buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bufferIndex">Index of the transform feedback buffer</param>
|
|
|
|
|
/// <returns>Varying locations for the specified buffer</returns>
|
|
|
|
|
public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
|
|
|
|
|
{
|
|
|
|
|
return _state.TransformFeedbackDescriptors[bufferIndex].VaryingLocations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries the stride (in bytes) of the per vertex data written into the transform feedback buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bufferIndex">Index of the transform feedback buffer</param>
|
|
|
|
|
/// <returns>Stride for the specified buffer</returns>
|
|
|
|
|
public int QueryTransformFeedbackStride(int bufferIndex)
|
|
|
|
|
{
|
|
|
|
|
return _state.TransformFeedbackDescriptors[bufferIndex].Stride;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 00:13:27 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries if host state forces early depth testing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if early depth testing is forced</returns>
|
|
|
|
|
public bool QueryEarlyZForce()
|
|
|
|
|
{
|
2021-07-08 01:56:06 +02:00
|
|
|
|
return _state.EarlyZForce;
|
2020-12-02 00:13:27 +01:00
|
|
|
|
}
|
2020-05-06 03:02:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|