diff --git a/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs b/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs index d4d9b48a0b..62ab0e475b 100644 --- a/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs +++ b/Ryujinx.Graphics.Gpu/Memory/MemoryManager.cs @@ -44,7 +44,7 @@ namespace Ryujinx.Graphics.Gpu.Memory /// CPU virtual address to map into /// GPU virtual address to be mapped /// Size in bytes of the mapping - /// The GPU virtual address of the mapping + /// GPU virtual address of the mapping public ulong Map(ulong pa, ulong va, ulong size) { lock (_pageTable) @@ -117,7 +117,7 @@ namespace Ryujinx.Graphics.Gpu.Memory /// Reserves memory at a fixed GPU memory location. /// This prevents the reserved region from being used for memory allocation for map. /// - /// CPU virtual address to reserve + /// GPU virtual address to reserve /// Reservation size in bytes /// GPU virtual address of the reservation, or an all ones mask in case of failure public ulong ReserveFixed(ulong va, ulong size) diff --git a/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs b/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs index 7a6b096347..71384df237 100644 --- a/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs +++ b/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs @@ -6,7 +6,7 @@ namespace Ryujinx.Graphics.Gpu.Memory /// /// Represents physical memory, accessible from the GPU. - /// This is actually working CPU virtual addresses, of memory mapped on the game process. + /// This is actually working CPU virtual addresses, of memory mapped on the application process. /// class PhysicalMemory { diff --git a/Ryujinx.Graphics.Gpu/Memory/RangeList.cs b/Ryujinx.Graphics.Gpu/Memory/RangeList.cs index 52bcf9b46f..1d185e2165 100644 --- a/Ryujinx.Graphics.Gpu/Memory/RangeList.cs +++ b/Ryujinx.Graphics.Gpu/Memory/RangeList.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace Ryujinx.Graphics.Gpu.Memory { /// - /// Lists of GPU resources with data on guest memory. + /// List of GPU resources with data on guest memory. /// /// Type of the GPU resource class RangeList where T : IRange diff --git a/Ryujinx.Graphics.Gpu/Shader/CachedShader.cs b/Ryujinx.Graphics.Gpu/Shader/CachedShader.cs index 210d0720cf..362d149a64 100644 --- a/Ryujinx.Graphics.Gpu/Shader/CachedShader.cs +++ b/Ryujinx.Graphics.Gpu/Shader/CachedShader.cs @@ -3,13 +3,31 @@ using Ryujinx.Graphics.Shader; namespace Ryujinx.Graphics.Gpu.Shader { + /// + /// Cached shader code for a single shader stage. + /// class CachedShader { + /// + /// Shader program containing translated code. + /// public ShaderProgram Program { get; } - public IShader Shader { get; set; } + /// + /// Host shader object. + /// + public IShader Shader { get; set; } + + /// + /// Maxwell binary shader code. + /// public int[] Code { get; } + /// + /// Creates a new instace of the cached shader. + /// + /// Shader program + /// Maxwell binary shader code public CachedShader(ShaderProgram program, int[] code) { Program = program; diff --git a/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs b/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs index 908b04b9df..d7a701ea64 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ComputeShader.cs @@ -2,12 +2,26 @@ using Ryujinx.Graphics.GAL; namespace Ryujinx.Graphics.Gpu.Shader { + /// + /// Cached compute shader code. + /// class ComputeShader { + /// + /// Host shader program object. + /// public IProgram HostProgram { get; set; } + /// + /// Cached shader. + /// public CachedShader Shader { get; } + /// + /// Creates a new instance of the compute shader. + /// + /// Host shader program + /// Cached shader public ComputeShader(IProgram hostProgram, CachedShader shader) { HostProgram = hostProgram; diff --git a/Ryujinx.Graphics.Gpu/Shader/GraphicsShader.cs b/Ryujinx.Graphics.Gpu/Shader/GraphicsShader.cs index 7bdf68f7e7..14c8e5c21a 100644 --- a/Ryujinx.Graphics.Gpu/Shader/GraphicsShader.cs +++ b/Ryujinx.Graphics.Gpu/Shader/GraphicsShader.cs @@ -2,12 +2,24 @@ using Ryujinx.Graphics.GAL; namespace Ryujinx.Graphics.Gpu.Shader { + /// + /// Cached graphics shader code for all stages. + /// class GraphicsShader { + /// + /// Host shader program object. + /// public IProgram HostProgram { get; set; } + /// + /// Compiled shader for each shader stage. + /// public CachedShader[] Shader { get; } + /// + /// Creates a new instance of cached graphics shader. + /// public GraphicsShader() { Shader = new CachedShader[5]; diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderAddresses.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderAddresses.cs index c0a9162a65..76ea32482d 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderAddresses.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderAddresses.cs @@ -2,6 +2,9 @@ using System; namespace Ryujinx.Graphics.Gpu.Shader { + /// + /// Shader code addresses in memory for each shader stage. + /// struct ShaderAddresses : IEquatable { public ulong VertexA; @@ -11,11 +14,21 @@ namespace Ryujinx.Graphics.Gpu.Shader public ulong Geometry; public ulong Fragment; + /// + /// Check if the addresses are equal. + /// + /// Shader addresses structure to compare with + /// True if they are equal, false otherwise public override bool Equals(object other) { return other is ShaderAddresses addresses && Equals(addresses); } + /// + /// Check if the addresses are equal. + /// + /// Shader addresses structure to compare with + /// True if they are equal, false otherwise public bool Equals(ShaderAddresses other) { return VertexA == other.VertexA && @@ -26,6 +39,10 @@ namespace Ryujinx.Graphics.Gpu.Shader Fragment == other.Fragment; } + /// + /// Computes hash code from the addresses. + /// + /// Hash code public override int GetHashCode() { return HashCode.Combine(VertexA, Vertex, TessControl, TessEvaluation, Geometry, Fragment); diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs index bd3229bd25..b299da1a7d 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs @@ -11,6 +11,9 @@ namespace Ryujinx.Graphics.Gpu.Shader { using TextureDescriptor = Image.TextureDescriptor; + /// + /// Memory cache of shader code. + /// class ShaderCache { private const int MaxProgramSize = 0x100000; @@ -25,17 +28,31 @@ namespace Ryujinx.Graphics.Gpu.Shader private Dictionary> _gpPrograms; + /// + /// Creates a new instance of the shader cache. + /// + /// GPU context that the shader cache belongs to public ShaderCache(GpuContext context) { _context = context; - _dumper = new ShaderDumper(context); + _dumper = new ShaderDumper(); _cpPrograms = new Dictionary>(); _gpPrograms = new Dictionary>(); } + /// + /// Gets a compute shader from the cache. + /// This automatically translates, compiles and adds the code to the cache if not present. + /// + /// GPU virtual address of the binary shader code + /// Shared memory size of the compute shader + /// Local group size X of the computer shader + /// Local group size Y of the computer shader + /// Local group size Z of the computer shader + /// Compiled compute shader code public ComputeShader GetComputeShader(ulong gpuVa, int sharedMemorySize, int localSizeX, int localSizeY, int localSizeZ) { bool isCached = _cpPrograms.TryGetValue(gpuVa, out List list); @@ -73,6 +90,14 @@ namespace Ryujinx.Graphics.Gpu.Shader return cpShader; } + /// + /// Gets a graphics shader program from the shader cache. + /// This includes all the specified shader stages. + /// This automatically translates, compiles and adds the code to the cache if not present. + /// + /// Current GPU state + /// Addresses of the shaders for each stage + /// Compiled graphics shader code public GraphicsShader GetGraphicsShader(GpuState state, ShaderAddresses addresses) { bool isCached = _gpPrograms.TryGetValue(addresses, out List list); @@ -138,11 +163,23 @@ namespace Ryujinx.Graphics.Gpu.Shader return gpShaders; } + /// + /// Checks if compute shader code in memory is different from the cached shader. + /// + /// Cached compute shader + /// GPU virtual address of the shader code in memory + /// True if the code is different, false otherwise private bool IsShaderDifferent(ComputeShader cpShader, ulong gpuVa) { return IsShaderDifferent(cpShader.Shader, gpuVa); } + /// + /// Checks if graphics shader code from all stages in memory is different from the cached shaders. + /// + /// Cached graphics shaders + /// GPU virtual addresses of all enabled shader stages + /// True if the code is different, false otherwise private bool IsShaderDifferent(GraphicsShader gpShaders, ShaderAddresses addresses) { for (int stage = 0; stage < gpShaders.Shader.Length; stage++) @@ -174,6 +211,12 @@ namespace Ryujinx.Graphics.Gpu.Shader return false; } + /// + /// Checks if the code of the specified cached shader is different from the code in memory. + /// + /// Cached shader to compare with + /// GPU virtual address of the binary shader code + /// True if the code is different, false otherwise private bool IsShaderDifferent(CachedShader shader, ulong gpuVa) { for (int index = 0; index < shader.Code.Length; index++) @@ -187,6 +230,15 @@ namespace Ryujinx.Graphics.Gpu.Shader return false; } + /// + /// Translates the binary Maxwell shader code to something that the host API accepts. + /// + /// GPU virtual address of the binary shader code + /// Shared memory size of the compute shader + /// Local group size X of the computer shader + /// Local group size Y of the computer shader + /// Local group size Z of the computer shader + /// Compiled compute shader code private CachedShader TranslateComputeShader(ulong gpuVa, int sharedMemorySize, int localSizeX, int localSizeY, int localSizeZ) { if (gpuVa == 0) @@ -230,6 +282,15 @@ namespace Ryujinx.Graphics.Gpu.Shader return new CachedShader(program, codeCached); } + /// + /// Translates the binary Maxwell shader code to something that the host API accepts. + /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader. + /// + /// Current GPU state + /// Shader stage + /// GPU virtual address of the shader code + /// Optional GPU virtual address of the "Vertex A" shader code + /// private CachedShader TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0) { if (gpuVa == 0) @@ -301,6 +362,12 @@ namespace Ryujinx.Graphics.Gpu.Shader return new CachedShader(program, codeCached); } + /// + /// Performs backwards propagation of interpolation qualifiers or later shader stages input, + /// to ealier shader stages output. + /// This is required by older versions of OpenGL (pre-4.3). + /// + /// Graphics shader cached code private void BackpropQualifiers(GraphicsShader program) { ShaderProgram fragmentShader = program.Shader[4].Program; @@ -334,6 +401,11 @@ namespace Ryujinx.Graphics.Gpu.Shader } } + /// + /// Gets the primitive topology for the current draw. + /// This is required by geometry shaders. + /// + /// Primitive topology private InputTopology GetPrimitiveTopology() { switch (_context.Methods.PrimitiveType) @@ -359,11 +431,29 @@ namespace Ryujinx.Graphics.Gpu.Shader return InputTopology.Points; } + /// + /// Check if the target of a given texture is texture buffer. + /// This is required as 1D textures and buffer textures shares the same sampler type on binary shader code, + /// but not on GLSL. + /// + /// Current GPU state + /// Index of the shader stage + /// Index of the texture (this is the shader "fake" handle) + /// True if the texture is a buffer texture, false otherwise private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int index) { return GetTextureDescriptor(state, stageIndex, index).UnpackTextureTarget() == TextureTarget.TextureBuffer; } + /// + /// Check if the target of a given texture is texture rectangle. + /// This is required as 2D textures and rectangle textures shares the same sampler type on binary shader code, + /// but not on GLSL. + /// + /// Current GPU state + /// Index of the shader stage + /// Index of the texture (this is the shader "fake" handle) + /// True if the texture is a rectangle texture, false otherwise private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int index) { var descriptor = GetTextureDescriptor(state, stageIndex, index); @@ -376,11 +466,23 @@ namespace Ryujinx.Graphics.Gpu.Shader return !descriptor.UnpackTextureCoordNormalized() && is2DTexture; } + /// + /// Gets the texture descriptor for a given texture on the pool. + /// + /// Current GPU state + /// Index of the shader stage + /// Index of the texture (this is the shader "fake" handle) + /// Texture descriptor private TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int index) { return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, index); } + /// + /// Returns information required by both compute and graphics shader compilation. + /// + /// Information queried + /// Requested information private int QueryInfoCommon(QueryInfoName info) { switch (info) diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderDumper.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderDumper.cs index 04ad645b94..3be75564bf 100644 --- a/Ryujinx.Graphics.Gpu/Shader/ShaderDumper.cs +++ b/Ryujinx.Graphics.Gpu/Shader/ShaderDumper.cs @@ -4,23 +4,29 @@ using System.IO; namespace Ryujinx.Graphics.Gpu.Shader { + /// + /// Shader dumper, writes binary shader code to disk. + /// class ShaderDumper { - private GpuContext _context; - private string _runtimeDir; private string _dumpPath; private int _dumpIndex; public int CurrentDumpIndex => _dumpIndex; - public ShaderDumper(GpuContext context) + public ShaderDumper() { - _context = context; - _dumpIndex = 1; } + /// + /// Dumps shader code to disk. + /// + /// Code to be dumped + /// True for compute shader code, false for graphics shader code + /// Output path for the shader code with header included + /// Output path for the shader code without header public void Dump(Span code, bool compute, out string fullPath, out string codePath) { _dumpPath = GraphicsConfig.ShadersDumpPath; @@ -68,16 +74,28 @@ namespace Ryujinx.Graphics.Gpu.Shader } } + /// + /// Returns the output directory for shader code with header. + /// + /// Directory path private string FullDir() { return CreateAndReturn(Path.Combine(DumpDir(), "Full")); } + /// + /// Returns the output directory for shader code without header. + /// + /// Directory path private string CodeDir() { return CreateAndReturn(Path.Combine(DumpDir(), "Code")); } + /// + /// Returns the full output directory for the current shader dump. + /// + /// Directory path private string DumpDir() { if (string.IsNullOrEmpty(_runtimeDir)) @@ -98,6 +116,11 @@ namespace Ryujinx.Graphics.Gpu.Shader return _runtimeDir; } + /// + /// Creates a new specified directory if needed. + /// + /// The directory to create + /// The same directory passed to the method private static string CreateAndReturn(string dir) { Directory.CreateDirectory(dir);