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 GpuContext _context; private readonly ReadOnlyMemory _data; private readonly GuestGpuAccessorHeader _header; private readonly Dictionary _textureDescriptors; /// /// Creates a new instance of the cached GPU state accessor for shader translation. /// /// GPU context /// The data of the shader /// The cache of the GPU accessor /// The cache of the texture descriptors public CachedGpuAccessor(GpuContext context, ReadOnlyMemory data, GuestGpuAccessorHeader header, Dictionary guestTextureDescriptors) { _context = context; _data = data; _header = header; _textureDescriptors = new Dictionary(); foreach (KeyValuePair guestTextureDescriptor in guestTextureDescriptors) { _textureDescriptors.Add(guestTextureDescriptor.Key, guestTextureDescriptor.Value); } } /// /// Prints a log message. /// /// Message to print public void Log(string message) { Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}"); } /// /// Reads data from GPU memory. /// /// Type of the data to be read /// GPU virtual address of the data /// Data at the memory location public override T MemoryRead(ulong address) { return MemoryMarshal.Cast(_data.Span.Slice((int)address))[0]; } /// /// Checks if a given memory address is mapped. /// /// GPU virtual address to be checked /// True if the address is mapped, false otherwise public bool MemoryMapped(ulong address) { return address < (ulong)_data.Length; } /// /// Queries Local Size X for compute shaders. /// /// Local Size X public int QueryComputeLocalSizeX() { return _header.ComputeLocalSizeX; } /// /// Queries Local Size Y for compute shaders. /// /// Local Size Y public int QueryComputeLocalSizeY() { return _header.ComputeLocalSizeY; } /// /// Queries Local Size Z for compute shaders. /// /// Local Size Z public int QueryComputeLocalSizeZ() { return _header.ComputeLocalSizeZ; } /// /// Queries Local Memory size in bytes for compute shaders. /// /// Local Memory size in bytes public int QueryComputeLocalMemorySize() { return _header.ComputeLocalMemorySize; } /// /// Queries Shared Memory size in bytes for compute shaders. /// /// Shared Memory size in bytes public int QueryComputeSharedMemorySize() { return _header.ComputeSharedMemorySize; } /// /// Queries current primitive topology for geometry shaders. /// /// Current primitive topology public InputTopology QueryPrimitiveTopology() { return _header.PrimitiveTopology; } /// /// Queries host storage buffer alignment required. /// /// Host storage buffer alignment in bytes public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment; /// /// Queries host support for readable images without a explicit format declaration on the shader. /// /// True if formatted image load is supported, false otherwise public bool QuerySupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted; /// /// Queries host GPU non-constant texture offset support. /// /// True if the GPU and driver supports non-constant texture offsets, false otherwise public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset; /// /// Gets the texture descriptor for a given texture on the pool. /// /// Index of the texture (this is the word offset of the handle in the constant buffer) /// Constant buffer slot for the texture handle /// Texture descriptor public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot) { if (!_textureDescriptors.TryGetValue(handle, out GuestTextureDescriptor textureDescriptor)) { throw new ArgumentException(); } return textureDescriptor; } /// /// Queries if host state forces early depth testing. /// /// True if early depth testing is forced public bool QueryEarlyZForce() { return (_header.StateFlags & GuestGpuStateFlags.EarlyZForce) != 0; } } }