2019-12-29 18:41:50 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2019-11-14 19:26:40 +01:00
|
|
|
using Ryujinx.Graphics.Gpu.Shader;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using Ryujinx.Graphics.Shader;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
using Texture = Image.Texture;
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU method implementations.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
partial class Methods
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
private readonly GpuContext _context;
|
|
|
|
private readonly ShaderProgramInfo[] _currentProgramInfo;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-31 23:09:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// In-memory shader cache.
|
|
|
|
/// </summary>
|
|
|
|
public ShaderCache ShaderCache { get; }
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU buffer manager.
|
|
|
|
/// </summary>
|
|
|
|
public BufferManager BufferManager { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// GPU texture manager.
|
|
|
|
/// </summary>
|
2019-12-29 18:41:50 +01:00
|
|
|
public TextureManager TextureManager { get; }
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
private bool _isAnyVbInstanced;
|
|
|
|
private bool _vsUsesInstanceId;
|
|
|
|
|
2020-07-04 00:30:41 +02:00
|
|
|
private bool _forceShaderUpdate;
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU methods class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU context</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public Methods(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
2019-12-31 23:09:49 +01:00
|
|
|
ShaderCache = new ShaderCache(_context);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
_currentProgramInfo = new ShaderProgramInfo[Constants.ShaderStages];
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
BufferManager = new BufferManager(context);
|
|
|
|
TextureManager = new TextureManager(context);
|
2020-04-22 08:00:11 +02:00
|
|
|
|
|
|
|
context.MemoryManager.MemoryUnmapped += _counterCache.MemoryUnmappedHandler;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Register callback for GPU method calls that triggers an action on the GPU.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">GPU state where the triggers will be registered</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
public void RegisterCallbacks(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.LaunchDma, LaunchDma);
|
|
|
|
state.RegisterCallback(MethodOffset.LoadInlineData, LoadInlineData);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.Dispatch, Dispatch);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-04-19 03:25:57 +02:00
|
|
|
state.RegisterCallback(MethodOffset.SyncpointAction, IncrementSyncpoint);
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.CopyBuffer, CopyBuffer);
|
|
|
|
state.RegisterCallback(MethodOffset.CopyTexture, CopyTexture);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.TextureBarrier, TextureBarrier);
|
|
|
|
state.RegisterCallback(MethodOffset.InvalidateTextures, InvalidateTextures);
|
|
|
|
state.RegisterCallback(MethodOffset.TextureBarrierTiled, TextureBarrierTiled);
|
2019-10-18 04:41:18 +02:00
|
|
|
|
2020-07-04 00:41:27 +02:00
|
|
|
state.RegisterCallback(MethodOffset.VbElementU8, VbElementU8);
|
|
|
|
state.RegisterCallback(MethodOffset.VbElementU16, VbElementU16);
|
|
|
|
state.RegisterCallback(MethodOffset.VbElementU32, VbElementU32);
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.ResetCounter, ResetCounter);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.DrawEnd, DrawEnd);
|
|
|
|
state.RegisterCallback(MethodOffset.DrawBegin, DrawBegin);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.IndexBufferCount, SetIndexBufferCount);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.Clear, Clear);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.Report, Report);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2020-01-22 02:11:43 +01:00
|
|
|
state.RegisterCallback(MethodOffset.FirmwareCall4, FirmwareCall4);
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferUpdateData, 16, UniformBufferUpdate);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferBindVertex, UniformBufferBindVertex);
|
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferBindTessControl, UniformBufferBindTessControl);
|
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferBindTessEvaluation, UniformBufferBindTessEvaluation);
|
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferBindGeometry, UniformBufferBindGeometry);
|
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferBindFragment, UniformBufferBindFragment);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 03:25:57 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Register callback for Fifo method calls that triggers an action on the GPFIFO.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">GPU state where the triggers will be registered</param>
|
|
|
|
public void RegisterCallbacksForFifo(GpuState state)
|
|
|
|
{
|
2020-05-29 10:51:10 +02:00
|
|
|
state.RegisterCallback(MethodOffset.Semaphore, Semaphore);
|
2020-04-19 03:25:57 +02:00
|
|
|
state.RegisterCallback(MethodOffset.FenceAction, FenceAction);
|
|
|
|
state.RegisterCallback(MethodOffset.WaitForIdle, WaitForIdle);
|
|
|
|
state.RegisterCallback(MethodOffset.SendMacroCodeData, SendMacroCodeData);
|
|
|
|
state.RegisterCallback(MethodOffset.BindMacro, BindMacro);
|
|
|
|
state.RegisterCallback(MethodOffset.SetMmeShadowRamControl, SetMmeShadowRamControl);
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host state based on the current guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Guest GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
// Shaders must be the first one to be updated if modified, because
|
|
|
|
// some of the other state depends on information from the currently
|
|
|
|
// bound shaders.
|
2020-07-04 00:30:41 +02:00
|
|
|
if (state.QueryModified(MethodOffset.ShaderBaseAddress, MethodOffset.ShaderState) || _forceShaderUpdate)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-07-04 00:30:41 +02:00
|
|
|
_forceShaderUpdate = false;
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateShaderState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 04:04:49 +02:00
|
|
|
if (state.QueryModified(MethodOffset.ClipDistanceEnable))
|
|
|
|
{
|
|
|
|
UpdateUserClipState(state);
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:19:45 +02:00
|
|
|
if (state.QueryModified(MethodOffset.RasterizeEnable))
|
|
|
|
{
|
|
|
|
UpdateRasterizerState(state);
|
|
|
|
}
|
|
|
|
|
2019-12-06 23:37:00 +01:00
|
|
|
if (state.QueryModified(MethodOffset.RtColorState,
|
|
|
|
MethodOffset.RtDepthStencilState,
|
|
|
|
MethodOffset.RtControl,
|
|
|
|
MethodOffset.RtDepthStencilSize,
|
|
|
|
MethodOffset.RtDepthStencilEnable))
|
|
|
|
{
|
|
|
|
UpdateRenderTargetState(state, useControl: true);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-03-29 05:02:58 +02:00
|
|
|
if (state.QueryModified(MethodOffset.ScissorState))
|
|
|
|
{
|
|
|
|
UpdateScissorState(state);
|
|
|
|
}
|
|
|
|
|
2020-04-17 03:16:49 +02:00
|
|
|
if (state.QueryModified(MethodOffset.ViewVolumeClipControl))
|
|
|
|
{
|
|
|
|
UpdateDepthClampState(state);
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.DepthTestEnable,
|
2019-12-05 21:34:47 +01:00
|
|
|
MethodOffset.DepthWriteEnable,
|
|
|
|
MethodOffset.DepthTestFunc))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateDepthTestState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 03:16:49 +02:00
|
|
|
if (state.QueryModified(MethodOffset.DepthMode,
|
|
|
|
MethodOffset.ViewportTransform,
|
|
|
|
MethodOffset.ViewportExtents))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateViewportTransform(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.DepthBiasState,
|
2019-12-05 21:34:47 +01:00
|
|
|
MethodOffset.DepthBiasFactor,
|
|
|
|
MethodOffset.DepthBiasUnits,
|
|
|
|
MethodOffset.DepthBiasClamp))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateDepthBiasState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.StencilBackMasks,
|
2019-12-05 21:34:47 +01:00
|
|
|
MethodOffset.StencilTestState,
|
|
|
|
MethodOffset.StencilBackTestState))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateStencilTestState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 19:50:52 +02:00
|
|
|
// Pools.
|
2019-12-07 00:19:12 +01:00
|
|
|
if (state.QueryModified(MethodOffset.SamplerPoolState, MethodOffset.SamplerIndex))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateSamplerPoolState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.TexturePoolState))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateTexturePoolState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 19:50:52 +02:00
|
|
|
// Input assembler state.
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.VertexAttribState))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateVertexAttribState(state);
|
2019-10-26 19:50:52 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 00:19:46 +01:00
|
|
|
if (state.QueryModified(MethodOffset.PointSize))
|
|
|
|
{
|
|
|
|
UpdatePointSizeState(state);
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.PrimitiveRestartState))
|
2019-10-26 19:50:52 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdatePrimitiveRestartState(state);
|
2019-10-26 19:50:52 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.IndexBufferState))
|
2019-10-26 19:50:52 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateIndexBufferState(state);
|
2019-10-26 19:50:52 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.VertexBufferDrawState,
|
2019-12-05 21:34:47 +01:00
|
|
|
MethodOffset.VertexBufferInstanced,
|
|
|
|
MethodOffset.VertexBufferState,
|
|
|
|
MethodOffset.VertexBufferEndAddress))
|
2019-10-26 19:50:52 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateVertexBufferState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
if (state.QueryModified(MethodOffset.FaceState))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateFaceState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 23:37:00 +01:00
|
|
|
if (state.QueryModified(MethodOffset.RtColorMaskShared, MethodOffset.RtColorMask))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateRtColorMask(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 21:34:47 +01:00
|
|
|
if (state.QueryModified(MethodOffset.BlendIndependent,
|
2020-04-25 15:00:43 +02:00
|
|
|
MethodOffset.BlendConstant,
|
2019-12-05 21:34:47 +01:00
|
|
|
MethodOffset.BlendStateCommon,
|
|
|
|
MethodOffset.BlendEnableCommon,
|
|
|
|
MethodOffset.BlendEnable,
|
|
|
|
MethodOffset.BlendState))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateBlendState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CommitBindings();
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:19:45 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates Rasterizer primitive discard state based on guest gpu state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateRasterizerState(GpuState state)
|
|
|
|
{
|
|
|
|
Boolean32 enable = state.Get<Boolean32>(MethodOffset.RasterizeEnable);
|
|
|
|
_context.Renderer.Pipeline.SetRasterizerDiscard(!enable);
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Ensures that the bindings are visible to the host GPU.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// Note: this actually performs the binding using the host graphics API.
|
2019-12-31 20:19:44 +01:00
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
private void CommitBindings()
|
|
|
|
{
|
2019-10-26 19:50:52 +02:00
|
|
|
UpdateStorageBuffers();
|
|
|
|
|
2020-04-25 15:02:18 +02:00
|
|
|
BufferManager.CommitGraphicsBindings();
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.CommitGraphicsBindings();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates storage buffer bindings.
|
|
|
|
/// </summary>
|
2019-10-26 19:50:52 +02:00
|
|
|
private void UpdateStorageBuffers()
|
|
|
|
{
|
|
|
|
for (int stage = 0; stage < _currentProgramInfo.Length; stage++)
|
|
|
|
{
|
|
|
|
ShaderProgramInfo info = _currentProgramInfo[stage];
|
|
|
|
|
|
|
|
if (info == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < info.SBuffers.Count; index++)
|
|
|
|
{
|
|
|
|
BufferDescriptor sb = info.SBuffers[index];
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
ulong sbDescAddress = BufferManager.GetGraphicsUniformBufferAddress(stage, 0);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
|
|
|
int sbDescOffset = 0x110 + stage * 0x100 + sb.Slot * 0x10;
|
|
|
|
|
|
|
|
sbDescAddress += (ulong)sbDescOffset;
|
|
|
|
|
2020-05-27 16:07:10 +02:00
|
|
|
SbDescriptor sbDescriptor = _context.PhysicalMemory.Read<SbDescriptor>(sbDescAddress);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
BufferManager.SetGraphicsStorageBuffer(stage, sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
|
2019-10-26 19:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates render targets (color and depth-stencil buffers) based on current render target state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="useControl">Use draw buffers information from render target control register</param>
|
2019-12-06 23:37:00 +01:00
|
|
|
private void UpdateRenderTargetState(GpuState state, bool useControl)
|
2019-10-26 19:50:52 +02:00
|
|
|
{
|
2019-12-06 23:37:00 +01:00
|
|
|
var rtControl = state.Get<RtControl>(MethodOffset.RtControl);
|
|
|
|
|
|
|
|
int count = useControl ? rtControl.UnpackCount() : Constants.TotalRenderTargets;
|
2019-10-26 19:50:52 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
var msaaMode = state.Get<TextureMsaaMode>(MethodOffset.RtMsaaMode);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
int samplesInX = msaaMode.SamplesInX();
|
|
|
|
int samplesInY = msaaMode.SamplesInY();
|
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
for (int index = 0; index < Constants.TotalRenderTargets; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-06 23:37:00 +01:00
|
|
|
int rtIndex = useControl ? rtControl.UnpackPermutationIndex(index) : index;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-06 23:37:00 +01:00
|
|
|
var colorState = state.Get<RtColorState>(MethodOffset.RtColorState, rtIndex);
|
|
|
|
|
|
|
|
if (index >= count || !IsRtEnabled(colorState))
|
2019-10-31 00:45:01 +01:00
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetRenderTargetColor(index, null);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
Texture color = TextureManager.FindOrCreateTexture(colorState, samplesInX, samplesInY);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetRenderTargetColor(index, color);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
if (color != null)
|
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
color.SignalModified();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
bool dsEnable = state.Get<Boolean32>(MethodOffset.RtDepthStencilEnable);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
Texture depthStencil = null;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (dsEnable)
|
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var dsState = state.Get<RtDepthStencilState>(MethodOffset.RtDepthStencilState);
|
|
|
|
var dsSize = state.Get<Size3D> (MethodOffset.RtDepthStencilSize);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
depthStencil = TextureManager.FindOrCreateTexture(dsState, dsSize, samplesInX, samplesInY);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetRenderTargetDepthStencil(depthStencil);
|
2019-10-26 19:50:52 +02:00
|
|
|
|
|
|
|
if (depthStencil != null)
|
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
depthStencil.SignalModified();
|
2019-10-26 19:50:52 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if a render target color buffer is used.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="colorState">Color buffer information</param>
|
|
|
|
/// <returns>True if the specified buffer is enabled/used, false otherwise</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
private static bool IsRtEnabled(RtColorState colorState)
|
|
|
|
{
|
|
|
|
// Colors are disabled by writing 0 to the format.
|
|
|
|
return colorState.Format != 0 && colorState.WidthOrStride != 0;
|
|
|
|
}
|
|
|
|
|
2020-03-29 05:02:58 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host scissor test state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateScissorState(GpuState state)
|
|
|
|
{
|
|
|
|
for (int index = 0; index < Constants.TotalViewports; index++)
|
|
|
|
{
|
|
|
|
ScissorState scissor = state.Get<ScissorState>(MethodOffset.ScissorState, index);
|
|
|
|
|
|
|
|
bool enable = scissor.Enable && (scissor.X1 != 0 || scissor.Y1 != 0 || scissor.X2 != 0xffff || scissor.Y2 != 0xffff);
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetScissorEnable(index, enable);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.SetScissor(index, scissor.X1, scissor.Y1, scissor.X2 - scissor.X1, scissor.Y2 - scissor.Y1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 03:16:49 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host depth clamp state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateDepthClampState(GpuState state)
|
|
|
|
{
|
|
|
|
ViewVolumeClipControl clip = state.Get<ViewVolumeClipControl>(MethodOffset.ViewVolumeClipControl);
|
2020-04-30 03:47:24 +02:00
|
|
|
_context.Renderer.Pipeline.SetDepthClamp((clip & ViewVolumeClipControl.DepthClampDisabled) == 0);
|
2020-04-17 03:16:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host depth test state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateDepthTestState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetDepthTest(new DepthTestDescriptor(
|
2019-11-22 03:46:14 +01:00
|
|
|
state.Get<Boolean32>(MethodOffset.DepthTestEnable),
|
|
|
|
state.Get<Boolean32>(MethodOffset.DepthWriteEnable),
|
|
|
|
state.Get<CompareOp>(MethodOffset.DepthTestFunc)));
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host viewport transform and clipping state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateViewportTransform(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-07 05:54:28 +01:00
|
|
|
DepthMode depthMode = state.Get<DepthMode>(MethodOffset.DepthMode);
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetDepthMode(depthMode);
|
|
|
|
|
2020-05-28 01:03:07 +02:00
|
|
|
YControl yControl = state.Get<YControl>(MethodOffset.YControl);
|
|
|
|
|
|
|
|
bool flipY = yControl.HasFlag(YControl.NegateY);
|
|
|
|
Origin origin = yControl.HasFlag(YControl.TriangleRastFlip) ? Origin.LowerLeft : Origin.UpperLeft;
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetOrigin(origin);
|
|
|
|
|
|
|
|
// The triangle rast flip flag only affects rasterization, the viewport is not flipped.
|
2020-05-29 10:51:10 +02:00
|
|
|
// Setting the origin mode to upper left on the host, however, not only affects rasterization,
|
2020-05-28 01:03:07 +02:00
|
|
|
// but also flips the viewport.
|
|
|
|
// We negate the effects of flipping the viewport by flipping it again using the viewport swizzle.
|
|
|
|
if (origin == Origin.UpperLeft)
|
|
|
|
{
|
|
|
|
flipY = !flipY;
|
|
|
|
}
|
2019-12-05 21:34:47 +01:00
|
|
|
|
2020-05-28 01:03:07 +02:00
|
|
|
Span<Viewport> viewports = stackalloc Viewport[Constants.TotalViewports];
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
for (int index = 0; index < Constants.TotalViewports; index++)
|
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var transform = state.Get<ViewportTransform>(MethodOffset.ViewportTransform, index);
|
|
|
|
var extents = state.Get<ViewportExtents> (MethodOffset.ViewportExtents, index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-31 23:22:45 +01:00
|
|
|
float x = transform.TranslateX - MathF.Abs(transform.ScaleX);
|
|
|
|
float y = transform.TranslateY - MathF.Abs(transform.ScaleY);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-28 01:03:07 +02:00
|
|
|
float width = MathF.Abs(transform.ScaleX) * 2;
|
|
|
|
float height = MathF.Abs(transform.ScaleY) * 2;
|
2019-12-06 23:37:00 +01:00
|
|
|
|
2019-12-31 23:22:45 +01:00
|
|
|
RectangleF region = new RectangleF(x, y, width, height);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-28 01:03:07 +02:00
|
|
|
ViewportSwizzle swizzleX = transform.UnpackSwizzleX();
|
|
|
|
ViewportSwizzle swizzleY = transform.UnpackSwizzleY();
|
|
|
|
ViewportSwizzle swizzleZ = transform.UnpackSwizzleZ();
|
|
|
|
ViewportSwizzle swizzleW = transform.UnpackSwizzleW();
|
|
|
|
|
|
|
|
if (transform.ScaleX < 0)
|
|
|
|
{
|
|
|
|
swizzleX ^= ViewportSwizzle.NegativeFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flipY)
|
|
|
|
{
|
|
|
|
swizzleY ^= ViewportSwizzle.NegativeFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transform.ScaleY < 0)
|
|
|
|
{
|
|
|
|
swizzleY ^= ViewportSwizzle.NegativeFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transform.ScaleZ < 0)
|
|
|
|
{
|
|
|
|
swizzleZ ^= ViewportSwizzle.NegativeFlag;
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
viewports[index] = new Viewport(
|
|
|
|
region,
|
2020-05-28 01:03:07 +02:00
|
|
|
swizzleX,
|
|
|
|
swizzleY,
|
|
|
|
swizzleZ,
|
|
|
|
swizzleW,
|
2019-10-13 08:02:07 +02:00
|
|
|
extents.DepthNear,
|
|
|
|
extents.DepthFar);
|
|
|
|
}
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetViewports(0, viewports);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host depth bias (also called polygon offset) state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateDepthBiasState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var depthBias = state.Get<DepthBiasState>(MethodOffset.DepthBiasState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
float factor = state.Get<float>(MethodOffset.DepthBiasFactor);
|
|
|
|
float units = state.Get<float>(MethodOffset.DepthBiasUnits);
|
|
|
|
float clamp = state.Get<float>(MethodOffset.DepthBiasClamp);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
PolygonModeMask enables;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-26 19:50:52 +02:00
|
|
|
enables = (depthBias.PointEnable ? PolygonModeMask.Point : 0);
|
|
|
|
enables |= (depthBias.LineEnable ? PolygonModeMask.Line : 0);
|
|
|
|
enables |= (depthBias.FillEnable ? PolygonModeMask.Fill : 0);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetDepthBias(enables, factor, units, clamp);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host stencil test state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateStencilTestState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var backMasks = state.Get<StencilBackMasks> (MethodOffset.StencilBackMasks);
|
|
|
|
var test = state.Get<StencilTestState> (MethodOffset.StencilTestState);
|
|
|
|
var backTest = state.Get<StencilBackTestState>(MethodOffset.StencilBackTestState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
CompareOp backFunc;
|
|
|
|
StencilOp backSFail;
|
|
|
|
StencilOp backDpPass;
|
|
|
|
StencilOp backDpFail;
|
|
|
|
int backFuncRef;
|
|
|
|
int backFuncMask;
|
|
|
|
int backMask;
|
|
|
|
|
2019-10-26 19:50:52 +02:00
|
|
|
if (backTest.TwoSided)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
backFunc = backTest.BackFunc;
|
|
|
|
backSFail = backTest.BackSFail;
|
|
|
|
backDpPass = backTest.BackDpPass;
|
|
|
|
backDpFail = backTest.BackDpFail;
|
|
|
|
backFuncRef = backMasks.FuncRef;
|
|
|
|
backFuncMask = backMasks.FuncMask;
|
|
|
|
backMask = backMasks.Mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
backFunc = test.FrontFunc;
|
|
|
|
backSFail = test.FrontSFail;
|
|
|
|
backDpPass = test.FrontDpPass;
|
|
|
|
backDpFail = test.FrontDpFail;
|
|
|
|
backFuncRef = test.FrontFuncRef;
|
|
|
|
backFuncMask = test.FrontFuncMask;
|
|
|
|
backMask = test.FrontMask;
|
|
|
|
}
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetStencilTest(new StencilTestDescriptor(
|
2019-10-26 19:50:52 +02:00
|
|
|
test.Enable,
|
2019-10-13 08:02:07 +02:00
|
|
|
test.FrontFunc,
|
|
|
|
test.FrontSFail,
|
|
|
|
test.FrontDpPass,
|
|
|
|
test.FrontDpFail,
|
|
|
|
test.FrontFuncRef,
|
|
|
|
test.FrontFuncMask,
|
|
|
|
test.FrontMask,
|
|
|
|
backFunc,
|
|
|
|
backSFail,
|
|
|
|
backDpPass,
|
|
|
|
backDpFail,
|
|
|
|
backFuncRef,
|
|
|
|
backFuncMask,
|
|
|
|
backMask));
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates current sampler pool address and size based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateSamplerPoolState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-07 00:19:12 +01:00
|
|
|
var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
|
2019-11-22 03:46:14 +01:00
|
|
|
var samplerPool = state.Get<PoolState>(MethodOffset.SamplerPoolState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-05 21:34:47 +01:00
|
|
|
var samplerIndex = state.Get<SamplerIndex>(MethodOffset.SamplerIndex);
|
|
|
|
|
2019-12-07 00:19:12 +01:00
|
|
|
int maximumId = samplerIndex == SamplerIndex.ViaHeaderIndex
|
|
|
|
? texturePool.MaximumId
|
|
|
|
: samplerPool.MaximumId;
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetGraphicsSamplerPool(samplerPool.Address.Pack(), maximumId, samplerIndex);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates current texture pool address and size based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateTexturePoolState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetGraphicsTexturePool(texturePool.Address.Pack(), texturePool.MaximumId);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetGraphicsTextureBufferIndex(state.Get<int>(MethodOffset.TextureBufferIndex));
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host vertex attributes based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateVertexAttribState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-05-23 11:46:09 +02:00
|
|
|
Span<VertexAttribDescriptor> vertexAttribs = stackalloc VertexAttribDescriptor[Constants.TotalVertexAttribs];
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-23 11:46:09 +02:00
|
|
|
for (int index = 0; index < Constants.TotalVertexAttribs; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var vertexAttrib = state.Get<VertexAttribState>(MethodOffset.VertexAttribState, index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (!FormatTable.TryGetAttribFormat(vertexAttrib.UnpackFormat(), out Format format))
|
|
|
|
{
|
2019-12-30 18:47:20 +01:00
|
|
|
Logger.PrintDebug(LogClass.Gpu, $"Invalid attribute format 0x{vertexAttrib.UnpackFormat():X}.");
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
format = Format.R32G32B32A32Float;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertexAttribs[index] = new VertexAttribDescriptor(
|
|
|
|
vertexAttrib.UnpackBufferIndex(),
|
|
|
|
vertexAttrib.UnpackOffset(),
|
2020-03-30 04:11:24 +02:00
|
|
|
vertexAttrib.UnpackIsConstant(),
|
2019-10-13 08:02:07 +02:00
|
|
|
format);
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
_context.Renderer.Pipeline.SetVertexAttribs(vertexAttribs);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 00:19:46 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host point size based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdatePointSizeState(GpuState state)
|
|
|
|
{
|
|
|
|
float size = state.Get<float>(MethodOffset.PointSize);
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetPointSize(size);
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host primitive restart based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdatePrimitiveRestartState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
PrimitiveRestartState primitiveRestart = state.Get<PrimitiveRestartState>(MethodOffset.PrimitiveRestartState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetPrimitiveRestart(
|
2019-10-13 08:02:07 +02:00
|
|
|
primitiveRestart.Enable,
|
|
|
|
primitiveRestart.Index);
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host index buffer binding based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateIndexBufferState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var indexBuffer = state.Get<IndexBufferState>(MethodOffset.IndexBufferState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
_firstIndex = indexBuffer.First;
|
|
|
|
_indexCount = indexBuffer.Count;
|
|
|
|
|
|
|
|
if (_indexCount == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong gpuVa = indexBuffer.Address.Pack();
|
|
|
|
|
|
|
|
// Do not use the end address to calculate the size, because
|
|
|
|
// the result may be much larger than the real size of the index buffer.
|
|
|
|
ulong size = (ulong)(_firstIndex + _indexCount);
|
|
|
|
|
|
|
|
switch (indexBuffer.Type)
|
|
|
|
{
|
|
|
|
case IndexType.UShort: size *= 2; break;
|
|
|
|
case IndexType.UInt: size *= 4; break;
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
BufferManager.SetIndexBuffer(gpuVa, size, indexBuffer.Type);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
// The index buffer affects the vertex buffer size calculation, we
|
|
|
|
// need to ensure that they are updated.
|
2019-11-22 03:46:14 +01:00
|
|
|
UpdateVertexBufferState(state);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host vertex buffer bindings based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateVertexBufferState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
_isAnyVbInstanced = false;
|
|
|
|
|
2020-05-23 11:46:09 +02:00
|
|
|
for (int index = 0; index < Constants.TotalVertexBuffers; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var vertexBuffer = state.Get<VertexBufferState>(MethodOffset.VertexBufferState, index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (!vertexBuffer.UnpackEnable())
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
BufferManager.SetVertexBuffer(index, 0, 0, 0, 0);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
GpuVa endAddress = state.Get<GpuVa>(MethodOffset.VertexBufferEndAddress, index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
ulong address = vertexBuffer.Address.Pack();
|
|
|
|
|
|
|
|
int stride = vertexBuffer.UnpackStride();
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
bool instanced = state.Get<Boolean32>(MethodOffset.VertexBufferInstanced + index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
int divisor = instanced ? vertexBuffer.Divisor : 0;
|
|
|
|
|
|
|
|
_isAnyVbInstanced |= divisor != 0;
|
|
|
|
|
|
|
|
ulong size;
|
|
|
|
|
2020-07-04 00:41:27 +02:00
|
|
|
if (_inlineIndexCount != 0 || _drawIndexed || stride == 0 || instanced)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
// This size may be (much) larger than the real vertex buffer size.
|
|
|
|
// Avoid calculating it this way, unless we don't have any other option.
|
|
|
|
size = endAddress.Pack() - address + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For non-indexed draws, we can guess the size from the vertex count
|
|
|
|
// and stride.
|
2019-11-22 03:46:14 +01:00
|
|
|
int firstInstance = state.Get<int>(MethodOffset.FirstInstance);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
size = (ulong)((firstInstance + drawState.First + drawState.Count) * stride);
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
BufferManager.SetVertexBuffer(index, address, size, stride, divisor);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host face culling and orientation based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateFaceState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var face = state.Get<FaceState>(MethodOffset.FaceState);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-26 19:50:52 +02:00
|
|
|
_context.Renderer.Pipeline.SetFaceCulling(face.CullEnable, face.CullFace);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetFrontFace(face.FrontFace);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host render target color masks, based on guest GPU state.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// This defines which color channels are written to each color buffer.
|
2019-12-31 20:19:44 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateRtColorMask(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-06 23:37:00 +01:00
|
|
|
bool rtColorMaskShared = state.Get<Boolean32>(MethodOffset.RtColorMaskShared);
|
|
|
|
|
2020-05-23 11:46:09 +02:00
|
|
|
Span<uint> componentMasks = stackalloc uint[Constants.TotalRenderTargets];
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
for (int index = 0; index < Constants.TotalRenderTargets; index++)
|
|
|
|
{
|
2019-12-06 23:37:00 +01:00
|
|
|
var colorMask = state.Get<RtColorMask>(MethodOffset.RtColorMask, rtColorMaskShared ? 0 : index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
uint componentMask;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
componentMask = (colorMask.UnpackRed() ? 1u : 0u);
|
|
|
|
componentMask |= (colorMask.UnpackGreen() ? 2u : 0u);
|
|
|
|
componentMask |= (colorMask.UnpackBlue() ? 4u : 0u);
|
|
|
|
componentMask |= (colorMask.UnpackAlpha() ? 8u : 0u);
|
|
|
|
|
|
|
|
componentMasks[index] = componentMask;
|
|
|
|
}
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
_context.Renderer.Pipeline.SetRenderTargetColorMasks(componentMasks);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host render target color buffer blending state, based on guest state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateBlendState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-05 21:34:47 +01:00
|
|
|
bool blendIndependent = state.Get<Boolean32>(MethodOffset.BlendIndependent);
|
2020-04-25 15:00:43 +02:00
|
|
|
ColorF blendConstant = state.Get<ColorF>(MethodOffset.BlendConstant);
|
2019-12-05 21:34:47 +01:00
|
|
|
|
2020-04-25 15:00:43 +02:00
|
|
|
for (int index = 0; index < Constants.TotalRenderTargets; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-05 21:34:47 +01:00
|
|
|
BlendDescriptor descriptor;
|
|
|
|
|
|
|
|
if (blendIndependent)
|
|
|
|
{
|
|
|
|
bool enable = state.Get<Boolean32> (MethodOffset.BlendEnable, index);
|
|
|
|
var blend = state.Get<BlendState>(MethodOffset.BlendState, index);
|
|
|
|
|
|
|
|
descriptor = new BlendDescriptor(
|
|
|
|
enable,
|
2020-04-25 15:00:43 +02:00
|
|
|
blendConstant,
|
2019-12-05 21:34:47 +01:00
|
|
|
blend.ColorOp,
|
|
|
|
blend.ColorSrcFactor,
|
|
|
|
blend.ColorDstFactor,
|
|
|
|
blend.AlphaOp,
|
|
|
|
blend.AlphaSrcFactor,
|
|
|
|
blend.AlphaDstFactor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool enable = state.Get<Boolean32> (MethodOffset.BlendEnable, 0);
|
|
|
|
var blend = state.Get<BlendStateCommon>(MethodOffset.BlendStateCommon);
|
|
|
|
|
|
|
|
descriptor = new BlendDescriptor(
|
|
|
|
enable,
|
2020-04-25 15:00:43 +02:00
|
|
|
blendConstant,
|
2019-12-05 21:34:47 +01:00
|
|
|
blend.ColorOp,
|
|
|
|
blend.ColorSrcFactor,
|
|
|
|
blend.ColorDstFactor,
|
|
|
|
blend.AlphaOp,
|
|
|
|
blend.AlphaSrcFactor,
|
|
|
|
blend.AlphaDstFactor);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
_context.Renderer.Pipeline.SetBlendState(index, descriptor);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Storage buffer address and size information.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
private struct SbDescriptor
|
|
|
|
{
|
2020-04-20 23:59:59 +02:00
|
|
|
#pragma warning disable CS0649
|
2019-10-13 08:02:07 +02:00
|
|
|
public uint AddressLow;
|
|
|
|
public uint AddressHigh;
|
|
|
|
public int Size;
|
|
|
|
public int Padding;
|
2020-04-20 23:59:59 +02:00
|
|
|
#pragma warning restore CS0649
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
public ulong PackAddress()
|
|
|
|
{
|
|
|
|
return AddressLow | ((ulong)AddressHigh << 32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host shaders based on the guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void UpdateShaderState(GpuState state)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
ShaderAddresses addresses = new ShaderAddresses();
|
|
|
|
|
|
|
|
Span<ShaderAddresses> addressesSpan = MemoryMarshal.CreateSpan(ref addresses, 1);
|
|
|
|
|
|
|
|
Span<ulong> addressesArray = MemoryMarshal.Cast<ShaderAddresses, ulong>(addressesSpan);
|
|
|
|
|
2019-11-22 03:46:14 +01:00
|
|
|
ulong baseAddress = state.Get<GpuVa>(MethodOffset.ShaderBaseAddress).Pack();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
for (int index = 0; index < 6; index++)
|
|
|
|
{
|
2019-11-22 03:46:14 +01:00
|
|
|
var shader = state.Get<ShaderState>(MethodOffset.ShaderState, index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (!shader.UnpackEnable() && index != 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addressesArray[index] = baseAddress + shader.Offset;
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
ShaderBundle gs = ShaderCache.GetGraphicsShader(state, addresses);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-01-05 17:40:21 +01:00
|
|
|
_vsUsesInstanceId = gs.Shaders[0]?.Program.Info.UsesInstanceId ?? false;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
for (int stage = 0; stage < Constants.ShaderStages; stage++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-01-05 17:40:21 +01:00
|
|
|
ShaderProgramInfo info = gs.Shaders[stage]?.Program.Info;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-26 19:50:52 +02:00
|
|
|
_currentProgramInfo[stage] = info;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (info == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var textureBindings = new TextureBindingInfo[info.Textures.Count];
|
|
|
|
|
|
|
|
for (int index = 0; index < info.Textures.Count; index++)
|
|
|
|
{
|
|
|
|
var descriptor = info.Textures[index];
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
Target target = GetTarget(descriptor.Type);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-28 02:16:14 +01:00
|
|
|
if (descriptor.IsBindless)
|
|
|
|
{
|
|
|
|
textureBindings[index] = new TextureBindingInfo(target, descriptor.CbufSlot, descriptor.CbufOffset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetGraphicsTextures(stage, textureBindings);
|
2019-10-18 04:41:18 +02:00
|
|
|
|
|
|
|
var imageBindings = new TextureBindingInfo[info.Images.Count];
|
|
|
|
|
|
|
|
for (int index = 0; index < info.Images.Count; index++)
|
|
|
|
{
|
|
|
|
var descriptor = info.Images[index];
|
|
|
|
|
|
|
|
Target target = GetTarget(descriptor.Type);
|
|
|
|
|
|
|
|
imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.SetGraphicsImages(stage, imageBindings);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
uint sbEnableMask = 0;
|
|
|
|
uint ubEnableMask = 0;
|
|
|
|
|
|
|
|
for (int index = 0; index < info.SBuffers.Count; index++)
|
|
|
|
{
|
2019-10-26 19:50:52 +02:00
|
|
|
sbEnableMask |= 1u << info.SBuffers[index].Slot;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < info.CBuffers.Count; index++)
|
|
|
|
{
|
|
|
|
ubEnableMask |= 1u << info.CBuffers[index].Slot;
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
BufferManager.SetGraphicsStorageBufferEnableMask(stage, sbEnableMask);
|
|
|
|
BufferManager.SetGraphicsUniformBufferEnableMask(stage, ubEnableMask);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
_context.Renderer.Pipeline.SetProgram(gs.HostProgram);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 04:04:49 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates user-defined clipping based on the guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateUserClipState(GpuState state)
|
|
|
|
{
|
|
|
|
int clipMask = state.Get<int>(MethodOffset.ClipDistanceEnable);
|
|
|
|
|
|
|
|
for (int i = 0; i < Constants.TotalClipDistances; ++i)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.SetUserClipDistance(i, (clipMask & (1 << i)) != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets texture target from a sampler type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">Sampler type</param>
|
|
|
|
/// <returns>Texture target value</returns>
|
2019-10-18 04:41:18 +02:00
|
|
|
private static Target GetTarget(SamplerType type)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-03 03:07:21 +01:00
|
|
|
type &= ~(SamplerType.Indexed | SamplerType.Shadow);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
switch (type)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.Texture1D:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture1D;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.TextureBuffer:
|
|
|
|
return Target.TextureBuffer;
|
|
|
|
|
|
|
|
case SamplerType.Texture1D | SamplerType.Array:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture1DArray;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.Texture2D:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture2D;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.Texture2D | SamplerType.Array:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture2DArray;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.Texture2D | SamplerType.Multisample:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture2DMultisample;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.Texture2D | SamplerType.Multisample | SamplerType.Array:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture2DMultisampleArray;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.Texture3D:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Texture3D;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.TextureCube:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.Cubemap;
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
case SamplerType.TextureCube | SamplerType.Array:
|
2019-10-13 08:02:07 +02:00
|
|
|
return Target.CubemapArray;
|
|
|
|
}
|
|
|
|
|
2020-01-01 16:39:09 +01:00
|
|
|
Logger.PrintWarning(LogClass.Gpu, $"Invalid sampler type \"{type}\".");
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
return Target.Texture2D;
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Issues a texture barrier.
|
|
|
|
/// This waits until previous texture writes from the GPU to finish, before
|
|
|
|
/// performing new operations with said textures.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <param name="state">Current GPU state (unused)</param>
|
|
|
|
/// <param name="argument">Method call argument (unused)</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void TextureBarrier(GpuState state, int argument)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.TextureBarrier();
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Invalidates all modified textures on the cache.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <param name="state">Current GPU state (unused)</param>
|
|
|
|
/// <param name="argument">Method call argument (unused)</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void InvalidateTextures(GpuState state, int argument)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
TextureManager.Flush();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2019-10-18 04:41:18 +02:00
|
|
|
|
2019-12-31 20:19:44 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Issues a texture barrier.
|
|
|
|
/// This waits until previous texture writes from the GPU to finish, before
|
|
|
|
/// performing new operations with said textures.
|
|
|
|
/// This performs a per-tile wait, it is only valid if both the previous write
|
|
|
|
/// and current access has the same access patterns.
|
|
|
|
/// This may be faster than the regular barrier on tile-based rasterizers.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <param name="state">Current GPU state (unused)</param>
|
|
|
|
/// <param name="argument">Method call argument (unused)</param>
|
2019-11-22 03:46:14 +01:00
|
|
|
private void TextureBarrierTiled(GpuState state, int argument)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.TextureBarrierTiled();
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|