2020-11-13 00:15:34 +01:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
|
|
|
|
|
using Ryujinx.Graphics.Shader;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Shader
|
|
|
|
|
{
|
|
|
|
|
class CachedGpuAccessor : TextureDescriptorCapableGpuAccessor, IGpuAccessor
|
|
|
|
|
{
|
|
|
|
|
private readonly ReadOnlyMemory<byte> _data;
|
2021-08-11 20:59:42 +02:00
|
|
|
|
private readonly ReadOnlyMemory<byte> _cb1Data;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
private readonly GuestGpuAccessorHeader _header;
|
2020-11-17 22:20:17 +01:00
|
|
|
|
private readonly Dictionary<int, GuestTextureDescriptor> _textureDescriptors;
|
2022-01-21 16:35:21 +01:00
|
|
|
|
private readonly TransformFeedbackDescriptor[] _tfd;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of the cached GPU state accessor for shader translation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">GPU context</param>
|
|
|
|
|
/// <param name="data">The data of the shader</param>
|
2021-08-11 20:59:42 +02:00
|
|
|
|
/// <param name="cb1Data">The constant buffer 1 data of the shader</param>
|
2020-11-13 00:15:34 +01:00
|
|
|
|
/// <param name="header">The cache of the GPU accessor</param>
|
|
|
|
|
/// <param name="guestTextureDescriptors">The cache of the texture descriptors</param>
|
2021-08-11 20:59:42 +02:00
|
|
|
|
public CachedGpuAccessor(
|
|
|
|
|
GpuContext context,
|
|
|
|
|
ReadOnlyMemory<byte> data,
|
|
|
|
|
ReadOnlyMemory<byte> cb1Data,
|
|
|
|
|
GuestGpuAccessorHeader header,
|
2022-01-21 16:35:21 +01:00
|
|
|
|
IReadOnlyDictionary<int, GuestTextureDescriptor> guestTextureDescriptors,
|
|
|
|
|
TransformFeedbackDescriptor[] tfd) : base(context)
|
2020-11-13 00:15:34 +01:00
|
|
|
|
{
|
|
|
|
|
_data = data;
|
2021-08-11 20:59:42 +02:00
|
|
|
|
_cb1Data = cb1Data;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
_header = header;
|
2020-11-17 22:20:17 +01:00
|
|
|
|
_textureDescriptors = new Dictionary<int, GuestTextureDescriptor>();
|
2020-11-13 00:15:34 +01:00
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<int, GuestTextureDescriptor> guestTextureDescriptor in guestTextureDescriptors)
|
|
|
|
|
{
|
2020-11-17 22:20:17 +01:00
|
|
|
|
_textureDescriptors.Add(guestTextureDescriptor.Key, guestTextureDescriptor.Value);
|
2020-11-13 00:15:34 +01:00
|
|
|
|
}
|
2022-01-21 16:35:21 +01:00
|
|
|
|
|
|
|
|
|
_tfd = tfd;
|
2020-11-13 00:15:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
return MemoryMarshal.Cast<byte, uint>(_cb1Data.Span.Slice(offset))[0];
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 00:15:34 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prints a log message.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">Message to print</param>
|
|
|
|
|
public void Log(string message)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-12 22:35:31 +02:00
|
|
|
|
/// Gets a span of the specified memory location, containing shader code.
|
2020-11-13 00:15:34 +01: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-11-13 00:15:34 +01:00
|
|
|
|
{
|
2021-10-12 22:35:31 +02:00
|
|
|
|
return MemoryMarshal.Cast<byte, ulong>(_data.Span.Slice((int)address));
|
2020-11-13 00:15:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if a given memory address is mapped.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">GPU virtual address to be checked</param>
|
|
|
|
|
/// <returns>True if the address is mapped, false otherwise</returns>
|
|
|
|
|
public bool MemoryMapped(ulong address)
|
|
|
|
|
{
|
|
|
|
|
return address < (ulong)_data.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Size X for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Size X</returns>
|
|
|
|
|
public int QueryComputeLocalSizeX()
|
|
|
|
|
{
|
|
|
|
|
return _header.ComputeLocalSizeX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Size Y for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Size Y</returns>
|
|
|
|
|
public int QueryComputeLocalSizeY()
|
|
|
|
|
{
|
|
|
|
|
return _header.ComputeLocalSizeY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Size Z for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Size Z</returns>
|
|
|
|
|
public int QueryComputeLocalSizeZ()
|
|
|
|
|
{
|
|
|
|
|
return _header.ComputeLocalSizeZ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Local Memory size in bytes for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Local Memory size in bytes</returns>
|
|
|
|
|
public int QueryComputeLocalMemorySize()
|
|
|
|
|
{
|
|
|
|
|
return _header.ComputeLocalMemorySize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries Shared Memory size in bytes for compute shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Shared Memory size in bytes</returns>
|
|
|
|
|
public int QueryComputeSharedMemorySize()
|
|
|
|
|
{
|
|
|
|
|
return _header.ComputeSharedMemorySize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries current primitive topology for geometry shaders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Current primitive topology</returns>
|
|
|
|
|
public InputTopology QueryPrimitiveTopology()
|
|
|
|
|
{
|
|
|
|
|
return _header.PrimitiveTopology;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-18 23:38:04 +02:00
|
|
|
|
/// 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()
|
|
|
|
|
{
|
|
|
|
|
return (_header.TessellationModePacked & 0x10) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queries the tessellation evaluation shader abstract patch type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Abstract patch type</returns>
|
|
|
|
|
public TessPatchType QueryTessPatchType()
|
|
|
|
|
{
|
|
|
|
|
return (TessPatchType)(_header.TessellationModePacked & 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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()
|
|
|
|
|
{
|
|
|
|
|
return (TessSpacing)((_header.TessellationModePacked >> 2) & 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 00:15:34 +01:00
|
|
|
|
/// Gets the texture descriptor for a given texture on the pool.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <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-11-13 00:15:34 +01:00
|
|
|
|
/// <returns>Texture descriptor</returns>
|
2021-05-19 20:05:43 +02:00
|
|
|
|
public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
|
2020-11-13 00:15:34 +01:00
|
|
|
|
{
|
2020-11-17 22:20:17 +01:00
|
|
|
|
if (!_textureDescriptors.TryGetValue(handle, out GuestTextureDescriptor textureDescriptor))
|
2020-11-13 00:15:34 +01:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return textureDescriptor;
|
|
|
|
|
}
|
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 _tfd != 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 _tfd[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 _tfd[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()
|
|
|
|
|
{
|
|
|
|
|
return (_header.StateFlags & GuestGpuStateFlags.EarlyZForce) != 0;
|
|
|
|
|
}
|
2020-11-13 00:15:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|