| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using Ryujinx.Common.Logging;
- using Ryujinx.Graphics.Gpu.Image;
- using Ryujinx.Graphics.Shader;
- using Ryujinx.Graphics.Shader.Translation;
- using System;
- using System.Runtime.InteropServices;
- namespace Ryujinx.Graphics.Gpu.Shader
- {
- /// <summary>
- /// Represents a GPU state and memory accessor.
- /// </summary>
- class GpuAccessor : GpuAccessorBase, IGpuAccessor
- {
- private readonly GpuChannel _channel;
- private readonly GpuAccessorState _state;
- private readonly int _stageIndex;
- private readonly bool _compute;
- private readonly bool _isOpenGL;
- private readonly bool _hasGeometryShader;
- private readonly bool _supportsQuads;
- /// <summary>
- /// Creates a new instance of the GPU state accessor for graphics shader translation.
- /// </summary>
- /// <param name="context">GPU context</param>
- /// <param name="channel">GPU channel</param>
- /// <param name="state">Current GPU state</param>
- /// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param>
- /// <param name="hasGeometryShader">Indicates if a geometry shader is present</param>
- public GpuAccessor(
- GpuContext context,
- GpuChannel channel,
- GpuAccessorState state,
- int stageIndex,
- bool hasGeometryShader) : base(context, state.ResourceCounts, stageIndex)
- {
- _channel = channel;
- _state = state;
- _stageIndex = stageIndex;
- _isOpenGL = context.Capabilities.Api == TargetApi.OpenGL;
- _hasGeometryShader = hasGeometryShader;
- _supportsQuads = context.Capabilities.SupportsQuads;
- if (stageIndex == (int)ShaderStage.Geometry - 1)
- {
- // Only geometry shaders require the primitive topology.
- _state.SpecializationState.RecordPrimitiveTopology();
- }
- }
- /// <summary>
- /// Creates a new instance of the GPU state accessor for compute shader translation.
- /// </summary>
- /// <param name="context">GPU context</param>
- /// <param name="channel">GPU channel</param>
- /// <param name="state">Current GPU state</param>
- public GpuAccessor(GpuContext context, GpuChannel channel, GpuAccessorState state) : base(context, state.ResourceCounts, 0)
- {
- _channel = channel;
- _state = state;
- _compute = true;
- }
- /// <inheritdoc/>
- public uint ConstantBuffer1Read(int offset)
- {
- ulong baseAddress = _compute
- ? _channel.BufferManager.GetComputeUniformBufferAddress(1)
- : _channel.BufferManager.GetGraphicsUniformBufferAddress(_stageIndex, 1);
- return _channel.MemoryManager.Physical.Read<uint>(baseAddress + (ulong)offset);
- }
- /// <inheritdoc/>
- public void Log(string message)
- {
- Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
- }
- /// <inheritdoc/>
- public ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
- {
- int size = Math.Max(minimumSize, 0x1000 - (int)(address & 0xfff));
- return MemoryMarshal.Cast<byte, ulong>(_channel.MemoryManager.GetSpan(address, size));
- }
- /// <inheritdoc/>
- public int QueryComputeLocalSizeX() => _state.ComputeState.LocalSizeX;
- /// <inheritdoc/>
- public int QueryComputeLocalSizeY() => _state.ComputeState.LocalSizeY;
- /// <inheritdoc/>
- public int QueryComputeLocalSizeZ() => _state.ComputeState.LocalSizeZ;
- /// <inheritdoc/>
- public int QueryComputeLocalMemorySize() => _state.ComputeState.LocalMemorySize;
- /// <inheritdoc/>
- public int QueryComputeSharedMemorySize() => _state.ComputeState.SharedMemorySize;
- /// <inheritdoc/>
- public uint QueryConstantBufferUse()
- {
- uint useMask = _compute
- ? _channel.BufferManager.GetComputeUniformBufferUseMask()
- : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
- _state.SpecializationState?.RecordConstantBufferUse(_stageIndex, useMask);
- return useMask;
- }
- /// <inheritdoc/>
- public GpuGraphicsState QueryGraphicsState()
- {
- return _state.GraphicsState.CreateShaderGraphicsState(
- _isOpenGL,
- _supportsQuads,
- _hasGeometryShader,
- !_isOpenGL || _state.GraphicsState.YNegateEnabled);
- }
- /// <inheritdoc/>
- public bool QueryHasConstantBufferDrawParameters()
- {
- return _state.GraphicsState.HasConstantBufferDrawParameters;
- }
- /// <inheritdoc/>
- public bool QueryHasUnalignedStorageBuffer()
- {
- return _state.GraphicsState.HasUnalignedStorageBuffer || _state.ComputeState.HasUnalignedStorageBuffer;
- }
- /// <inheritdoc/>
- public int QuerySamplerArrayLengthFromPool()
- {
- int length = _state.SamplerPoolMaximumId + 1;
- _state.SpecializationState?.RegisterTextureArrayLengthFromPool(isSampler: true, length);
- return length;
- }
- /// <inheritdoc/>
- public SamplerType QuerySamplerType(int handle, int cbufSlot)
- {
- _state.SpecializationState?.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
- return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget().ConvertSamplerType();
- }
- /// <inheritdoc/>
- public int QueryTextureArrayLengthFromBuffer(int slot)
- {
- int size = _compute
- ? _channel.BufferManager.GetComputeUniformBufferSize(slot)
- : _channel.BufferManager.GetGraphicsUniformBufferSize(_stageIndex, slot);
- int arrayLength = size / Constants.TextureHandleSizeInBytes;
- _state.SpecializationState?.RegisterTextureArrayLengthFromBuffer(_stageIndex, 0, slot, arrayLength);
- return arrayLength;
- }
- /// <inheritdoc/>
- public int QueryTextureArrayLengthFromPool()
- {
- int length = _state.PoolState.TexturePoolMaximumId + 1;
- _state.SpecializationState?.RegisterTextureArrayLengthFromPool(isSampler: false, length);
- return length;
- }
- //// <inheritdoc/>
- public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
- {
- _state.SpecializationState?.RecordTextureFormat(_stageIndex, handle, cbufSlot);
- var descriptor = GetTextureDescriptor(handle, cbufSlot);
- return ConvertToTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb());
- }
- /// <inheritdoc/>
- public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
- {
- _state.SpecializationState?.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
- return GetTextureDescriptor(handle, cbufSlot).UnpackTextureCoordNormalized();
- }
- /// <summary>
- /// 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>
- /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
- /// <returns>Texture descriptor</returns>
- private Image.TextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
- {
- if (_compute)
- {
- return _channel.TextureManager.GetComputeTextureDescriptor(
- _state.PoolState.TexturePoolGpuVa,
- _state.PoolState.TextureBufferIndex,
- _state.PoolState.TexturePoolMaximumId,
- handle,
- cbufSlot);
- }
- else
- {
- return _channel.TextureManager.GetGraphicsTextureDescriptor(
- _state.PoolState.TexturePoolGpuVa,
- _state.PoolState.TextureBufferIndex,
- _state.PoolState.TexturePoolMaximumId,
- _stageIndex,
- handle,
- cbufSlot);
- }
- }
- /// <inheritdoc/>
- public bool QueryTransformFeedbackEnabled()
- {
- return _state.TransformFeedbackDescriptors != null;
- }
- /// <inheritdoc/>
- public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
- {
- return _state.TransformFeedbackDescriptors[bufferIndex].AsSpan();
- }
- /// <inheritdoc/>
- public int QueryTransformFeedbackStride(int bufferIndex)
- {
- return _state.TransformFeedbackDescriptors[bufferIndex].Stride;
- }
- /// <inheritdoc/>
- public void RegisterTexture(int handle, int cbufSlot)
- {
- _state.SpecializationState?.RegisterTexture(_stageIndex, handle, cbufSlot, GetTextureDescriptor(handle, cbufSlot));
- }
- }
- }
|