2019-12-29 12:41:50 -05:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2019-11-14 13:26:40 -05:00
|
|
|
using Ryujinx.Graphics.Gpu.Shader;
|
2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using Ryujinx.Graphics.Shader;
|
2020-11-09 19:41:13 -05:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2019-10-13 02:02:07 -04:00
|
|
|
using System;
|
2020-11-08 06:10:00 -05:00
|
|
|
using System.Linq;
|
2019-10-13 02:02:07 -04:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
2019-12-29 12:41:50 -05:00
|
|
|
using Texture = Image.Texture;
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// GPU method implementations.
|
|
|
|
/// </summary>
|
2019-10-13 02:02:07 -04:00
|
|
|
partial class Methods
|
|
|
|
{
|
2019-12-29 12:41:50 -05:00
|
|
|
private readonly GpuContext _context;
|
|
|
|
private readonly ShaderProgramInfo[] _currentProgramInfo;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
private bool _isAnyVbInstanced;
|
|
|
|
private bool _vsUsesInstanceId;
|
2021-04-20 06:33:54 -04:00
|
|
|
private byte _vsClipDistancesWritten;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-07-03 18:30:41 -04:00
|
|
|
private bool _forceShaderUpdate;
|
|
|
|
|
2020-07-14 23:01:10 -04:00
|
|
|
private bool _prevTfEnable;
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU methods class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU context</param>
|
2019-10-13 02:02:07 -04:00
|
|
|
public Methods(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
2020-01-01 10:39:09 -05:00
|
|
|
_currentProgramInfo = new ShaderProgramInfo[Constants.ShaderStages];
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05: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-21 21:46:14 -05:00
|
|
|
public void RegisterCallbacks(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.LaunchDma, LaunchDma);
|
|
|
|
state.RegisterCallback(MethodOffset.LoadInlineData, LoadInlineData);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-18 21:25:57 -04:00
|
|
|
state.RegisterCallback(MethodOffset.SyncpointAction, IncrementSyncpoint);
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.TextureBarrier, TextureBarrier);
|
|
|
|
state.RegisterCallback(MethodOffset.TextureBarrierTiled, TextureBarrierTiled);
|
2019-10-17 22:41:18 -04:00
|
|
|
|
2020-07-03 18:41:27 -04:00
|
|
|
state.RegisterCallback(MethodOffset.VbElementU8, VbElementU8);
|
|
|
|
state.RegisterCallback(MethodOffset.VbElementU16, VbElementU16);
|
|
|
|
state.RegisterCallback(MethodOffset.VbElementU32, VbElementU32);
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.ResetCounter, ResetCounter);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
state.RegisterCallback(MethodOffset.DrawEnd, DrawEnd);
|
|
|
|
state.RegisterCallback(MethodOffset.DrawBegin, DrawBegin);
|
|
|
|
state.RegisterCallback(MethodOffset.DrawIndexedSmall, DrawIndexedSmall);
|
|
|
|
state.RegisterCallback(MethodOffset.DrawIndexedSmall2, DrawIndexedSmall2);
|
|
|
|
state.RegisterCallback(MethodOffset.DrawIndexedSmallIncInstance, DrawIndexedSmallIncInstance);
|
|
|
|
state.RegisterCallback(MethodOffset.DrawIndexedSmallIncInstance2, DrawIndexedSmallIncInstance2);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.IndexBufferCount, SetIndexBufferCount);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.Clear, Clear);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.Report, Report);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2020-01-21 20:11:43 -05:00
|
|
|
state.RegisterCallback(MethodOffset.FirmwareCall4, FirmwareCall4);
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.RegisterCallback(MethodOffset.UniformBufferUpdateData, 16, UniformBufferUpdate);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2019-11-21 21:46:14 -05: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 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host state based on the current guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Guest GPU state</param>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
|
|
|
|
/// <param name="indexCount">Number of index buffer elements used on the draw</param>
|
|
|
|
private void UpdateState(GpuState state, int firstIndex, int indexCount)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-07-14 23:01:10 -04:00
|
|
|
bool tfEnable = state.Get<Boolean32>(MethodOffset.TfEnable);
|
|
|
|
|
|
|
|
if (!tfEnable && _prevTfEnable)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndTransformFeedback();
|
|
|
|
_prevTfEnable = false;
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
FlushUboDirty(state.Channel.MemoryManager);
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 16:52:44 -04:00
|
|
|
|
2019-10-13 02:02:07 -04: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-03 18:30:41 -04:00
|
|
|
if (state.QueryModified(MethodOffset.ShaderBaseAddress, MethodOffset.ShaderState) || _forceShaderUpdate)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-07-03 18:30:41 -04:00
|
|
|
_forceShaderUpdate = false;
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateShaderState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-07-14 23:01:10 -04:00
|
|
|
if (state.QueryModified(MethodOffset.TfBufferState))
|
|
|
|
{
|
|
|
|
UpdateTfBufferState(state);
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:04:49 -04:00
|
|
|
if (state.QueryModified(MethodOffset.ClipDistanceEnable))
|
|
|
|
{
|
|
|
|
UpdateUserClipState(state);
|
|
|
|
}
|
|
|
|
|
2020-04-07 05:19:45 -04:00
|
|
|
if (state.QueryModified(MethodOffset.RasterizeEnable))
|
|
|
|
{
|
|
|
|
UpdateRasterizerState(state);
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:37:00 -05:00
|
|
|
if (state.QueryModified(MethodOffset.RtColorState,
|
|
|
|
MethodOffset.RtDepthStencilState,
|
|
|
|
MethodOffset.RtControl,
|
|
|
|
MethodOffset.RtDepthStencilSize,
|
|
|
|
MethodOffset.RtDepthStencilEnable))
|
|
|
|
{
|
|
|
|
UpdateRenderTargetState(state, useControl: true);
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-03-28 23:02:58 -04:00
|
|
|
if (state.QueryModified(MethodOffset.ScissorState))
|
|
|
|
{
|
|
|
|
UpdateScissorState(state);
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:16:49 -04:00
|
|
|
if (state.QueryModified(MethodOffset.ViewVolumeClipControl))
|
|
|
|
{
|
|
|
|
UpdateDepthClampState(state);
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:30:08 -04:00
|
|
|
if (state.QueryModified(MethodOffset.AlphaTestEnable,
|
|
|
|
MethodOffset.AlphaTestRef,
|
|
|
|
MethodOffset.AlphaTestFunc))
|
|
|
|
{
|
|
|
|
UpdateAlphaTestState(state);
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.DepthTestEnable,
|
2019-12-05 15:34:47 -05:00
|
|
|
MethodOffset.DepthWriteEnable,
|
|
|
|
MethodOffset.DepthTestFunc))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateDepthTestState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:16:49 -04:00
|
|
|
if (state.QueryModified(MethodOffset.DepthMode,
|
|
|
|
MethodOffset.ViewportTransform,
|
|
|
|
MethodOffset.ViewportExtents))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateViewportTransform(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.DepthBiasState,
|
2019-12-05 15:34:47 -05:00
|
|
|
MethodOffset.DepthBiasFactor,
|
|
|
|
MethodOffset.DepthBiasUnits,
|
|
|
|
MethodOffset.DepthBiasClamp))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateDepthBiasState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.StencilBackMasks,
|
2019-12-05 15:34:47 -05:00
|
|
|
MethodOffset.StencilTestState,
|
|
|
|
MethodOffset.StencilBackTestState))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateStencilTestState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-10-26 13:50:52 -04:00
|
|
|
// Pools.
|
2019-12-06 18:19:12 -05:00
|
|
|
if (state.QueryModified(MethodOffset.SamplerPoolState, MethodOffset.SamplerIndex))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateSamplerPoolState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.TexturePoolState))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateTexturePoolState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:11:54 -04:00
|
|
|
// Rasterizer state.
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.VertexAttribState))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateVertexAttribState(state);
|
2019-10-26 13:50:52 -04:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:11:54 -04:00
|
|
|
if (state.QueryModified(MethodOffset.LineWidthSmooth, MethodOffset.LineSmoothEnable))
|
|
|
|
{
|
|
|
|
UpdateLineState(state);
|
|
|
|
}
|
|
|
|
|
2020-07-20 20:59:13 -04:00
|
|
|
if (state.QueryModified(MethodOffset.PointSize,
|
|
|
|
MethodOffset.VertexProgramPointSize,
|
|
|
|
MethodOffset.PointSpriteEnable,
|
|
|
|
MethodOffset.PointCoordReplace))
|
2020-02-01 18:19:46 -05:00
|
|
|
{
|
2020-07-20 20:59:13 -04:00
|
|
|
UpdatePointState(state);
|
2020-02-01 18:19:46 -05:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.PrimitiveRestartState))
|
2019-10-26 13:50:52 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdatePrimitiveRestartState(state);
|
2019-10-26 13:50:52 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.IndexBufferState))
|
2019-10-26 13:50:52 -04:00
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
UpdateIndexBufferState(state, firstIndex, indexCount);
|
2019-10-26 13:50:52 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.VertexBufferDrawState,
|
2019-12-05 15:34:47 -05:00
|
|
|
MethodOffset.VertexBufferInstanced,
|
|
|
|
MethodOffset.VertexBufferState,
|
|
|
|
MethodOffset.VertexBufferEndAddress))
|
2019-10-26 13:50:52 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateVertexBufferState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
if (state.QueryModified(MethodOffset.FaceState))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateFaceState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 17:37:00 -05:00
|
|
|
if (state.QueryModified(MethodOffset.RtColorMaskShared, MethodOffset.RtColorMask))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateRtColorMask(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-05 15:34:47 -05:00
|
|
|
if (state.QueryModified(MethodOffset.BlendIndependent,
|
2020-04-25 09:00:43 -04:00
|
|
|
MethodOffset.BlendConstant,
|
2019-12-05 15:34:47 -05:00
|
|
|
MethodOffset.BlendStateCommon,
|
|
|
|
MethodOffset.BlendEnableCommon,
|
|
|
|
MethodOffset.BlendEnable,
|
|
|
|
MethodOffset.BlendState))
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateBlendState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-07-10 13:23:15 -04:00
|
|
|
if (state.QueryModified(MethodOffset.LogicOpState))
|
|
|
|
{
|
|
|
|
UpdateLogicOpState(state);
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
CommitBindings(state);
|
2020-07-14 23:01:10 -04:00
|
|
|
|
|
|
|
if (tfEnable && !_prevTfEnable)
|
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
_context.Renderer.Pipeline.BeginTransformFeedback(Topology);
|
2020-07-14 23:01:10 -04:00
|
|
|
_prevTfEnable = true;
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 05:19:45 -04: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 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Ensures that the bindings are visible to the host GPU.
|
2020-01-01 10:39:09 -05:00
|
|
|
/// Note: this actually performs the binding using the host graphics API.
|
2019-12-31 14:19:44 -05:00
|
|
|
/// </summary>
|
2021-06-23 19:51:41 -04:00
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void CommitBindings(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2021-06-23 19:51:41 -04:00
|
|
|
UpdateStorageBuffers(state);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.CommitGraphicsBindings();
|
|
|
|
state.Channel.BufferManager.CommitGraphicsBindings();
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates storage buffer bindings.
|
|
|
|
/// </summary>
|
2021-06-23 19:51:41 -04:00
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateStorageBuffers(GpuState state)
|
2019-10-26 13:50:52 -04:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
ulong sbDescAddress = state.Channel.BufferManager.GetGraphicsUniformBufferAddress(stage, 0);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
|
|
|
int sbDescOffset = 0x110 + stage * 0x100 + sb.Slot * 0x10;
|
|
|
|
|
|
|
|
sbDescAddress += (ulong)sbDescOffset;
|
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
SbDescriptor sbDescriptor = state.Channel.MemoryManager.Physical.Read<SbDescriptor>(sbDescAddress);
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetGraphicsStorageBuffer(stage, sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size, sb.Flags);
|
2019-10-26 13:50:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05: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>
|
2020-07-14 23:01:10 -04:00
|
|
|
/// <param name="singleUse">If this is not -1, it indicates that only the given indexed target will be used.</param>
|
2020-07-06 22:41:07 -04:00
|
|
|
private void UpdateRenderTargetState(GpuState state, bool useControl, int singleUse = -1)
|
2019-10-26 13:50:52 -04:00
|
|
|
{
|
2021-06-29 13:32:02 -04:00
|
|
|
var memoryManager = state.Channel.MemoryManager;
|
2019-12-06 17:37:00 -05:00
|
|
|
var rtControl = state.Get<RtControl>(MethodOffset.RtControl);
|
|
|
|
|
|
|
|
int count = useControl ? rtControl.UnpackCount() : Constants.TotalRenderTargets;
|
2019-10-26 13:50:52 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
var msaaMode = state.Get<TextureMsaaMode>(MethodOffset.RtMsaaMode);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
int samplesInX = msaaMode.SamplesInX();
|
|
|
|
int samplesInY = msaaMode.SamplesInY();
|
|
|
|
|
2020-11-12 18:40:26 -05:00
|
|
|
var scissor = state.Get<ScreenScissorState>(MethodOffset.ScreenScissorState);
|
|
|
|
Size sizeHint = new Size(scissor.X + scissor.Width, scissor.Y + scissor.Height, 1);
|
2020-11-09 19:41:13 -05:00
|
|
|
|
2020-07-06 22:41:07 -04:00
|
|
|
bool changedScale = false;
|
|
|
|
|
2019-10-30 19:45:01 -04:00
|
|
|
for (int index = 0; index < Constants.TotalRenderTargets; index++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-06 17:37:00 -05:00
|
|
|
int rtIndex = useControl ? rtControl.UnpackPermutationIndex(index) : index;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-06 17:37:00 -05:00
|
|
|
var colorState = state.Get<RtColorState>(MethodOffset.RtColorState, rtIndex);
|
|
|
|
|
|
|
|
if (index >= count || !IsRtEnabled(colorState))
|
2019-10-30 19:45:01 -04:00
|
|
|
{
|
2021-06-23 19:51:41 -04:00
|
|
|
changedScale |= state.Channel.TextureManager.SetRenderTargetColor(index, null);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-30 19:45:01 -04:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
Texture color = memoryManager.Physical.TextureCache.FindOrCreateTexture(
|
|
|
|
memoryManager,
|
|
|
|
colorState,
|
|
|
|
samplesInX,
|
|
|
|
samplesInY,
|
|
|
|
sizeHint);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
changedScale |= state.Channel.TextureManager.SetRenderTargetColor(index, color);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
bool dsEnable = state.Get<Boolean32>(MethodOffset.RtDepthStencilEnable);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
Texture depthStencil = null;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (dsEnable)
|
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var dsState = state.Get<RtDepthStencilState>(MethodOffset.RtDepthStencilState);
|
2020-07-28 17:30:08 -04:00
|
|
|
var dsSize = state.Get<Size3D>(MethodOffset.RtDepthStencilSize);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
depthStencil = memoryManager.Physical.TextureCache.FindOrCreateTexture(
|
|
|
|
memoryManager,
|
|
|
|
dsState,
|
|
|
|
dsSize,
|
|
|
|
samplesInX,
|
|
|
|
samplesInY,
|
|
|
|
sizeHint);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
changedScale |= state.Channel.TextureManager.SetRenderTargetDepthStencil(depthStencil);
|
2020-07-06 22:41:07 -04:00
|
|
|
|
|
|
|
if (changedScale)
|
|
|
|
{
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.UpdateRenderTargetScale(singleUse);
|
|
|
|
_context.Renderer.Pipeline.SetRenderTargetScale(state.Channel.TextureManager.RenderTargetScale);
|
2020-07-06 22:41:07 -04:00
|
|
|
|
|
|
|
UpdateViewportTransform(state);
|
|
|
|
UpdateScissorState(state);
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05: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 02:02:07 -04: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-28 23:02:58 -04: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);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
2020-07-06 22:41:07 -04:00
|
|
|
int x = scissor.X1;
|
|
|
|
int y = scissor.Y1;
|
|
|
|
int width = scissor.X2 - x;
|
|
|
|
int height = scissor.Y2 - y;
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
float scale = state.Channel.TextureManager.RenderTargetScale;
|
2020-07-06 22:41:07 -04:00
|
|
|
if (scale != 1f)
|
|
|
|
{
|
|
|
|
x = (int)(x * scale);
|
|
|
|
y = (int)(y * scale);
|
|
|
|
width = (int)Math.Ceiling(width * scale);
|
|
|
|
height = (int)Math.Ceiling(height * scale);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:44:07 -05:00
|
|
|
_context.Renderer.Pipeline.SetScissor(index, true, x, y, width, height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.SetScissor(index, false, 0, 0, 0, 0);
|
2020-03-28 23:02:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:16:49 -04: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-29 21:47:24 -04:00
|
|
|
_context.Renderer.Pipeline.SetDepthClamp((clip & ViewVolumeClipControl.DepthClampDisabled) == 0);
|
2020-04-16 21:16:49 -04:00
|
|
|
}
|
|
|
|
|
2020-07-28 17:30:08 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host alpha test state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateAlphaTestState(GpuState state)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.SetAlphaTest(
|
|
|
|
state.Get<Boolean32>(MethodOffset.AlphaTestEnable),
|
|
|
|
state.Get<float>(MethodOffset.AlphaTestRef),
|
|
|
|
state.Get<CompareOp>(MethodOffset.AlphaTestFunc));
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host depth test state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateDepthTestState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.SetDepthTest(new DepthTestDescriptor(
|
2019-11-21 21:46:14 -05:00
|
|
|
state.Get<Boolean32>(MethodOffset.DepthTestEnable),
|
|
|
|
state.Get<Boolean32>(MethodOffset.DepthWriteEnable),
|
|
|
|
state.Get<CompareOp>(MethodOffset.DepthTestFunc)));
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host viewport transform and clipping state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateViewportTransform(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-09-19 18:46:49 -04:00
|
|
|
var yControl = state.Get<YControl> (MethodOffset.YControl);
|
|
|
|
var face = state.Get<FaceState>(MethodOffset.FaceState);
|
2019-12-06 23:54:28 -05:00
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
UpdateFrontFace(yControl, face.FrontFace);
|
2019-12-06 23:54:28 -05:00
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
bool flipY = yControl.HasFlag(YControl.NegateY);
|
2019-12-05 15:34:47 -05:00
|
|
|
|
2020-05-27 19:03:07 -04:00
|
|
|
Span<Viewport> viewports = stackalloc Viewport[Constants.TotalViewports];
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
for (int index = 0; index < Constants.TotalViewports; index++)
|
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var transform = state.Get<ViewportTransform>(MethodOffset.ViewportTransform, index);
|
|
|
|
var extents = state.Get<ViewportExtents> (MethodOffset.ViewportExtents, index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
float scaleX = MathF.Abs(transform.ScaleX);
|
|
|
|
float scaleY = transform.ScaleY;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
if (flipY)
|
|
|
|
{
|
|
|
|
scaleY = -scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_context.Capabilities.SupportsViewportSwizzle && transform.UnpackSwizzleY() == ViewportSwizzle.NegativeY)
|
|
|
|
{
|
|
|
|
scaleY = -scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
{
|
|
|
|
// Try to guess the depth mode being used on the high level API
|
|
|
|
// based on current transform.
|
|
|
|
// It is setup like so by said APIs:
|
|
|
|
// If depth mode is ZeroToOne:
|
|
|
|
// TranslateZ = Near
|
|
|
|
// ScaleZ = Far - Near
|
|
|
|
// If depth mode is MinusOneToOne:
|
|
|
|
// TranslateZ = (Near + Far) / 2
|
|
|
|
// ScaleZ = (Far - Near) / 2
|
|
|
|
// DepthNear/Far are sorted such as that Near is always less than Far.
|
|
|
|
DepthMode depthMode = extents.DepthNear != transform.TranslateZ &&
|
|
|
|
extents.DepthFar != transform.TranslateZ ? DepthMode.MinusOneToOne : DepthMode.ZeroToOne;
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetDepthMode(depthMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
float x = transform.TranslateX - scaleX;
|
|
|
|
float y = transform.TranslateY - scaleY;
|
|
|
|
|
|
|
|
float width = scaleX * 2;
|
|
|
|
float height = scaleY * 2;
|
2019-12-06 17:37:00 -05:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
float scale = state.Channel.TextureManager.RenderTargetScale;
|
2020-07-06 22:41:07 -04:00
|
|
|
if (scale != 1f)
|
|
|
|
{
|
|
|
|
x *= scale;
|
|
|
|
y *= scale;
|
|
|
|
width *= scale;
|
|
|
|
height *= scale;
|
|
|
|
}
|
|
|
|
|
2019-12-31 17:22:45 -05:00
|
|
|
RectangleF region = new RectangleF(x, y, width, height);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-05-27 19:03:07 -04:00
|
|
|
ViewportSwizzle swizzleX = transform.UnpackSwizzleX();
|
|
|
|
ViewportSwizzle swizzleY = transform.UnpackSwizzleY();
|
|
|
|
ViewportSwizzle swizzleZ = transform.UnpackSwizzleZ();
|
|
|
|
ViewportSwizzle swizzleW = transform.UnpackSwizzleW();
|
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
float depthNear = extents.DepthNear;
|
|
|
|
float depthFar = extents.DepthFar;
|
2020-05-27 19:03:07 -04:00
|
|
|
|
|
|
|
if (transform.ScaleZ < 0)
|
|
|
|
{
|
2020-09-19 18:46:49 -04:00
|
|
|
float temp = depthNear;
|
|
|
|
depthNear = depthFar;
|
|
|
|
depthFar = temp;
|
2020-05-27 19:03:07 -04:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
viewports[index] = new Viewport(region, swizzleX, swizzleY, swizzleZ, swizzleW, depthNear, depthFar);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.SetViewports(0, viewports);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05: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-21 21:46:14 -05:00
|
|
|
private void UpdateDepthBiasState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var depthBias = state.Get<DepthBiasState>(MethodOffset.DepthBiasState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-11-21 21:46:14 -05: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 02:02:07 -04:00
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
PolygonModeMask enables;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-26 13:50:52 -04:00
|
|
|
enables = (depthBias.PointEnable ? PolygonModeMask.Point : 0);
|
|
|
|
enables |= (depthBias.LineEnable ? PolygonModeMask.Line : 0);
|
|
|
|
enables |= (depthBias.FillEnable ? PolygonModeMask.Fill : 0);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-07-26 17:11:28 -04:00
|
|
|
_context.Renderer.Pipeline.SetDepthBias(enables, factor, units / 2f, clamp);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host stencil test state based on current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateStencilTestState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-07-28 17:30:08 -04:00
|
|
|
var backMasks = state.Get<StencilBackMasks>(MethodOffset.StencilBackMasks);
|
|
|
|
var test = state.Get<StencilTestState>(MethodOffset.StencilTestState);
|
2019-11-21 21:46:14 -05:00
|
|
|
var backTest = state.Get<StencilBackTestState>(MethodOffset.StencilBackTestState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
CompareOp backFunc;
|
|
|
|
StencilOp backSFail;
|
|
|
|
StencilOp backDpPass;
|
|
|
|
StencilOp backDpFail;
|
|
|
|
int backFuncRef;
|
|
|
|
int backFuncMask;
|
|
|
|
int backMask;
|
|
|
|
|
2019-10-26 13:50:52 -04:00
|
|
|
if (backTest.TwoSided)
|
2019-10-13 02:02:07 -04: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-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.SetStencilTest(new StencilTestDescriptor(
|
2019-10-26 13:50:52 -04:00
|
|
|
test.Enable,
|
2019-10-13 02:02:07 -04: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 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates current sampler pool address and size based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateSamplerPoolState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-06 18:19:12 -05:00
|
|
|
var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
|
2019-11-21 21:46:14 -05:00
|
|
|
var samplerPool = state.Get<PoolState>(MethodOffset.SamplerPoolState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-05 15:34:47 -05:00
|
|
|
var samplerIndex = state.Get<SamplerIndex>(MethodOffset.SamplerIndex);
|
|
|
|
|
2019-12-06 18:19:12 -05:00
|
|
|
int maximumId = samplerIndex == SamplerIndex.ViaHeaderIndex
|
|
|
|
? texturePool.MaximumId
|
|
|
|
: samplerPool.MaximumId;
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.SetGraphicsSamplerPool(samplerPool.Address.Pack(), maximumId, samplerIndex);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates current texture pool address and size based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateTexturePoolState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.SetGraphicsTexturePool(texturePool.Address.Pack(), texturePool.MaximumId);
|
|
|
|
state.Channel.TextureManager.SetGraphicsTextureBufferIndex(state.Get<int>(MethodOffset.TextureBufferIndex));
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host vertex attributes based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateVertexAttribState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-05-23 05:46:09 -04:00
|
|
|
Span<VertexAttribDescriptor> vertexAttribs = stackalloc VertexAttribDescriptor[Constants.TotalVertexAttribs];
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-05-23 05:46:09 -04:00
|
|
|
for (int index = 0; index < Constants.TotalVertexAttribs; index++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var vertexAttrib = state.Get<VertexAttribState>(MethodOffset.VertexAttribState, index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (!FormatTable.TryGetAttribFormat(vertexAttrib.UnpackFormat(), out Format format))
|
|
|
|
{
|
2020-08-03 19:32:53 -04:00
|
|
|
Logger.Debug?.Print(LogClass.Gpu, $"Invalid attribute format 0x{vertexAttrib.UnpackFormat():X}.");
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
format = Format.R32G32B32A32Float;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertexAttribs[index] = new VertexAttribDescriptor(
|
|
|
|
vertexAttrib.UnpackBufferIndex(),
|
|
|
|
vertexAttrib.UnpackOffset(),
|
2020-03-29 22:11:24 -04:00
|
|
|
vertexAttrib.UnpackIsConstant(),
|
2019-10-13 02:02:07 -04:00
|
|
|
format);
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
_context.Renderer.Pipeline.SetVertexAttribs(vertexAttribs);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:11:54 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host line width based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateLineState(GpuState state)
|
|
|
|
{
|
|
|
|
float width = state.Get<float>(MethodOffset.LineWidthSmooth);
|
|
|
|
bool smooth = state.Get<Boolean32>(MethodOffset.LineSmoothEnable);
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetLineParameters(width, smooth);
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:19:46 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host point size based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2020-07-20 20:59:13 -04:00
|
|
|
private void UpdatePointState(GpuState state)
|
2020-02-01 18:19:46 -05:00
|
|
|
{
|
|
|
|
float size = state.Get<float>(MethodOffset.PointSize);
|
2020-07-20 20:59:13 -04:00
|
|
|
bool isProgramPointSize = state.Get<Boolean32>(MethodOffset.VertexProgramPointSize);
|
|
|
|
bool enablePointSprite = state.Get<Boolean32>(MethodOffset.PointSpriteEnable);
|
2020-07-26 17:11:28 -04:00
|
|
|
|
2020-07-20 20:59:13 -04:00
|
|
|
// TODO: Need to figure out a way to map PointCoordReplace enable bit.
|
|
|
|
Origin origin = (state.Get<int>(MethodOffset.PointCoordReplace) & 4) == 0 ? Origin.LowerLeft : Origin.UpperLeft;
|
2020-02-01 18:19:46 -05:00
|
|
|
|
2020-07-20 20:59:13 -04:00
|
|
|
_context.Renderer.Pipeline.SetPointParameters(size, isProgramPointSize, enablePointSprite, origin);
|
2020-02-01 18:19:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host primitive restart based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdatePrimitiveRestartState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
PrimitiveRestartState primitiveRestart = state.Get<PrimitiveRestartState>(MethodOffset.PrimitiveRestartState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.SetPrimitiveRestart(
|
2019-10-13 02:02:07 -04:00
|
|
|
primitiveRestart.Enable,
|
|
|
|
primitiveRestart.Index);
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host index buffer binding based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
|
|
|
|
/// <param name="indexCount">Number of index buffer elements used on the draw</param>
|
|
|
|
private void UpdateIndexBufferState(GpuState state, int firstIndex, int indexCount)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var indexBuffer = state.Get<IndexBufferState>(MethodOffset.IndexBufferState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
if (indexCount == 0)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
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.
|
2020-09-23 19:48:34 -04:00
|
|
|
ulong size = (ulong)(firstIndex + indexCount);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
switch (indexBuffer.Type)
|
|
|
|
{
|
|
|
|
case IndexType.UShort: size *= 2; break;
|
|
|
|
case IndexType.UInt: size *= 4; break;
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetIndexBuffer(gpuVa, size, indexBuffer.Type);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
// The index buffer affects the vertex buffer size calculation, we
|
|
|
|
// need to ensure that they are updated.
|
2019-11-21 21:46:14 -05:00
|
|
|
UpdateVertexBufferState(state);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host vertex buffer bindings based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateVertexBufferState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
_isAnyVbInstanced = false;
|
|
|
|
|
2020-05-23 05:46:09 -04:00
|
|
|
for (int index = 0; index < Constants.TotalVertexBuffers; index++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var vertexBuffer = state.Get<VertexBufferState>(MethodOffset.VertexBufferState, index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (!vertexBuffer.UnpackEnable())
|
|
|
|
{
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetVertexBuffer(index, 0, 0, 0, 0);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
GpuVa endAddress = state.Get<GpuVa>(MethodOffset.VertexBufferEndAddress, index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
ulong address = vertexBuffer.Address.Pack();
|
|
|
|
|
|
|
|
int stride = vertexBuffer.UnpackStride();
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
bool instanced = state.Get<Boolean32>(MethodOffset.VertexBufferInstanced + index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
int divisor = instanced ? vertexBuffer.Divisor : 0;
|
|
|
|
|
|
|
|
_isAnyVbInstanced |= divisor != 0;
|
|
|
|
|
|
|
|
ulong size;
|
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
if (_ibStreamer.HasInlineIndexData || _drawIndexed || stride == 0 || instanced)
|
2019-10-13 02:02:07 -04: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-21 21:46:14 -05:00
|
|
|
int firstInstance = state.Get<int>(MethodOffset.FirstInstance);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
size = (ulong)((firstInstance + drawState.First + drawState.Count) * stride);
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetVertexBuffer(index, address, size, stride, divisor);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host face culling and orientation based on guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateFaceState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-09-19 18:46:49 -04:00
|
|
|
var yControl = state.Get<YControl> (MethodOffset.YControl);
|
|
|
|
var face = state.Get<FaceState>(MethodOffset.FaceState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-26 13:50:52 -04:00
|
|
|
_context.Renderer.Pipeline.SetFaceCulling(face.CullEnable, face.CullFace);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-19 18:46:49 -04:00
|
|
|
UpdateFrontFace(yControl, face.FrontFace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Updates the front face based on the current front face and the origin.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="yControl">Y control register value, where the origin is located</param>
|
|
|
|
/// <param name="frontFace">Front face</param>
|
|
|
|
private void UpdateFrontFace(YControl yControl, FrontFace frontFace)
|
|
|
|
{
|
|
|
|
bool isUpperLeftOrigin = !yControl.HasFlag(YControl.TriangleRastFlip);
|
|
|
|
|
|
|
|
if (isUpperLeftOrigin)
|
|
|
|
{
|
|
|
|
frontFace = frontFace == FrontFace.CounterClockwise ? FrontFace.Clockwise : FrontFace.CounterClockwise;
|
|
|
|
}
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetFrontFace(frontFace);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host render target color masks, based on guest GPU state.
|
2020-01-01 10:39:09 -05:00
|
|
|
/// This defines which color channels are written to each color buffer.
|
2019-12-31 14:19:44 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateRtColorMask(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-06 17:37:00 -05:00
|
|
|
bool rtColorMaskShared = state.Get<Boolean32>(MethodOffset.RtColorMaskShared);
|
|
|
|
|
2020-05-23 05:46:09 -04:00
|
|
|
Span<uint> componentMasks = stackalloc uint[Constants.TotalRenderTargets];
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
for (int index = 0; index < Constants.TotalRenderTargets; index++)
|
|
|
|
{
|
2019-12-06 17:37:00 -05:00
|
|
|
var colorMask = state.Get<RtColorMask>(MethodOffset.RtColorMask, rtColorMaskShared ? 0 : index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
uint componentMask;
|
2019-10-13 02:02:07 -04: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-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.SetRenderTargetColorMasks(componentMasks);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host render target color buffer blending state, based on guest state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateBlendState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-05 15:34:47 -05:00
|
|
|
bool blendIndependent = state.Get<Boolean32>(MethodOffset.BlendIndependent);
|
2020-04-25 09:00:43 -04:00
|
|
|
ColorF blendConstant = state.Get<ColorF>(MethodOffset.BlendConstant);
|
2019-12-05 15:34:47 -05:00
|
|
|
|
2020-04-25 09:00:43 -04:00
|
|
|
for (int index = 0; index < Constants.TotalRenderTargets; index++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-05 15:34:47 -05: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 09:00:43 -04:00
|
|
|
blendConstant,
|
2019-12-05 15:34:47 -05: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 09:00:43 -04:00
|
|
|
blendConstant,
|
2019-12-05 15:34:47 -05:00
|
|
|
blend.ColorOp,
|
|
|
|
blend.ColorSrcFactor,
|
|
|
|
blend.ColorDstFactor,
|
|
|
|
blend.AlphaOp,
|
|
|
|
blend.AlphaSrcFactor,
|
|
|
|
blend.AlphaDstFactor);
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
_context.Renderer.Pipeline.SetBlendState(index, descriptor);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 13:23:15 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host logical operation state, based on guest state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
public void UpdateLogicOpState(GpuState state)
|
|
|
|
{
|
|
|
|
LogicalOpState logicOpState = state.Get<LogicalOpState>(MethodOffset.LogicOpState);
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetLogicOpState(logicOpState.Enable, logicOpState.LogicalOp);
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Updates host shaders based on the guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void UpdateShaderState(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
ShaderAddresses addresses = new ShaderAddresses();
|
|
|
|
|
|
|
|
Span<ShaderAddresses> addressesSpan = MemoryMarshal.CreateSpan(ref addresses, 1);
|
|
|
|
|
|
|
|
Span<ulong> addressesArray = MemoryMarshal.Cast<ShaderAddresses, ulong>(addressesSpan);
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
ulong baseAddress = state.Get<GpuVa>(MethodOffset.ShaderBaseAddress).Pack();
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
for (int index = 0; index < 6; index++)
|
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var shader = state.Get<ShaderState>(MethodOffset.ShaderState, index);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (!shader.UnpackEnable() && index != 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addressesArray[index] = baseAddress + shader.Offset;
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
ShaderBundle gs = state.Channel.MemoryManager.Physical.ShaderCache.GetGraphicsShader(state, addresses);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-04-20 06:33:54 -04:00
|
|
|
byte oldVsClipDistancesWritten = _vsClipDistancesWritten;
|
|
|
|
|
|
|
|
_vsUsesInstanceId = gs.Shaders[0]?.Info.UsesInstanceId ?? false;
|
|
|
|
_vsClipDistancesWritten = gs.Shaders[0]?.Info.ClipDistancesWritten ?? 0;
|
|
|
|
|
|
|
|
if (oldVsClipDistancesWritten != _vsClipDistancesWritten)
|
|
|
|
{
|
|
|
|
UpdateUserClipState(state);
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-11-08 06:10:00 -05:00
|
|
|
int storageBufferBindingsCount = 0;
|
|
|
|
int uniformBufferBindingsCount = 0;
|
|
|
|
|
2020-01-01 10:39:09 -05:00
|
|
|
for (int stage = 0; stage < Constants.ShaderStages; stage++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-11-12 18:15:34 -05:00
|
|
|
ShaderProgramInfo info = gs.Shaders[stage]?.Info;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-26 13:50:52 -04:00
|
|
|
_currentProgramInfo[stage] = info;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
if (info == null)
|
|
|
|
{
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.SetGraphicsTextures(stage, Array.Empty<TextureBindingInfo>());
|
|
|
|
state.Channel.TextureManager.SetGraphicsImages(stage, Array.Empty<TextureBindingInfo>());
|
|
|
|
state.Channel.BufferManager.SetGraphicsStorageBufferBindings(stage, null);
|
|
|
|
state.Channel.BufferManager.SetGraphicsUniformBufferBindings(stage, null);
|
2019-10-13 02:02:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var textureBindings = new TextureBindingInfo[info.Textures.Count];
|
|
|
|
|
|
|
|
for (int index = 0; index < info.Textures.Count; index++)
|
|
|
|
{
|
|
|
|
var descriptor = info.Textures[index];
|
|
|
|
|
2020-10-20 18:03:20 -04:00
|
|
|
Target target = ShaderTexture.GetTarget(descriptor.Type);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-11-09 17:35:04 -05:00
|
|
|
textureBindings[index] = new TextureBindingInfo(
|
|
|
|
target,
|
|
|
|
descriptor.Binding,
|
|
|
|
descriptor.CbufSlot,
|
|
|
|
descriptor.HandleIndex,
|
|
|
|
descriptor.Flags);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.SetGraphicsTextures(stage, textureBindings);
|
2019-10-17 22:41:18 -04:00
|
|
|
|
|
|
|
var imageBindings = new TextureBindingInfo[info.Images.Count];
|
|
|
|
|
|
|
|
for (int index = 0; index < info.Images.Count; index++)
|
|
|
|
{
|
|
|
|
var descriptor = info.Images[index];
|
|
|
|
|
2020-10-20 18:03:20 -04:00
|
|
|
Target target = ShaderTexture.GetTarget(descriptor.Type);
|
|
|
|
Format format = ShaderTexture.GetFormat(descriptor.Format);
|
2019-10-17 22:41:18 -04:00
|
|
|
|
2020-11-09 17:35:04 -05:00
|
|
|
imageBindings[index] = new TextureBindingInfo(
|
|
|
|
target,
|
|
|
|
format,
|
|
|
|
descriptor.Binding,
|
|
|
|
descriptor.CbufSlot,
|
|
|
|
descriptor.HandleIndex,
|
|
|
|
descriptor.Flags);
|
2019-10-17 22:41:18 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.TextureManager.SetGraphicsImages(stage, imageBindings);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetGraphicsStorageBufferBindings(stage, info.SBuffers);
|
|
|
|
state.Channel.BufferManager.SetGraphicsUniformBufferBindings(stage, info.CBuffers);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-11-08 06:10:00 -05:00
|
|
|
if (info.SBuffers.Count != 0)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-11-08 06:10:00 -05:00
|
|
|
storageBufferBindingsCount = Math.Max(storageBufferBindingsCount, info.SBuffers.Max(x => x.Binding) + 1);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-11-08 06:10:00 -05:00
|
|
|
if (info.CBuffers.Count != 0)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-11-08 06:10:00 -05:00
|
|
|
uniformBufferBindingsCount = Math.Max(uniformBufferBindingsCount, info.CBuffers.Max(x => x.Binding) + 1);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetGraphicsStorageBufferBindingsCount(storageBufferBindingsCount);
|
|
|
|
state.Channel.BufferManager.SetGraphicsUniformBufferBindingsCount(uniformBufferBindingsCount);
|
2020-11-08 06:10:00 -05:00
|
|
|
|
2019-12-29 12:41:50 -05:00
|
|
|
_context.Renderer.Pipeline.SetProgram(gs.HostProgram);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2021-07-07 19:56:06 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Forces the shaders to be rebound on the next draw.
|
|
|
|
/// </summary>
|
|
|
|
public void ForceShaderUpdate()
|
|
|
|
{
|
|
|
|
_forceShaderUpdate = true;
|
|
|
|
}
|
|
|
|
|
2020-07-14 23:01:10 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Updates transform feedback buffer state based on the guest GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
private void UpdateTfBufferState(GpuState state)
|
|
|
|
{
|
|
|
|
for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
|
|
|
|
{
|
|
|
|
TfBufferState tfb = state.Get<TfBufferState>(MethodOffset.TfBufferState, index);
|
|
|
|
|
|
|
|
if (!tfb.Enable)
|
|
|
|
{
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetTransformFeedbackBuffer(index, 0, 0);
|
2020-07-14 23:01:10 -04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetTransformFeedbackBuffer(index, tfb.Address.Pack(), (uint)tfb.Size);
|
2020-07-14 23:01:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:04:49 -04: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)
|
|
|
|
{
|
2021-04-20 06:33:54 -04:00
|
|
|
int clipMask = state.Get<int>(MethodOffset.ClipDistanceEnable) & _vsClipDistancesWritten;
|
2020-05-03 22:04:49 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < Constants.TotalClipDistances; ++i)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.SetUserClipDistance(i, (clipMask & (1 << i)) != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05: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 10:39:09 -05:00
|
|
|
/// <param name="state">Current GPU state (unused)</param>
|
|
|
|
/// <param name="argument">Method call argument (unused)</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void TextureBarrier(GpuState state, int argument)
|
2019-10-17 22:41:18 -04:00
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.TextureBarrier();
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05: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 10:39:09 -05:00
|
|
|
/// <param name="state">Current GPU state (unused)</param>
|
|
|
|
/// <param name="argument">Method call argument (unused)</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void TextureBarrierTiled(GpuState state, int argument)
|
2019-10-17 22:41:18 -04:00
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.TextureBarrierTiled();
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|