2020-07-03 18:41:27 -04:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
2020-07-03 18:41:27 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
|
|
|
partial class Methods
|
|
|
|
{
|
|
|
|
private bool _drawIndexed;
|
|
|
|
|
2019-12-06 17:37:00 -05:00
|
|
|
private bool _instancedDrawPending;
|
2019-10-13 02:02:07 -04:00
|
|
|
private bool _instancedIndexed;
|
|
|
|
|
|
|
|
private int _instancedFirstIndex;
|
|
|
|
private int _instancedFirstVertex;
|
|
|
|
private int _instancedFirstInstance;
|
|
|
|
private int _instancedIndexCount;
|
|
|
|
private int _instancedDrawStateFirst;
|
|
|
|
private int _instancedDrawStateCount;
|
|
|
|
|
|
|
|
private int _instanceIndex;
|
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
private IbStreamer _ibStreamer;
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// Primitive topology of the current draw.
|
2019-12-31 14:19:44 -05:00
|
|
|
/// </summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
public PrimitiveTopology Topology { get; private set; }
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// Finishes the draw call.
|
2019-12-31 14:19:44 -05:00
|
|
|
/// This draws geometry on the bound buffers based on the current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void DrawEnd(GpuState state, int argument)
|
2020-09-23 19:48:34 -04:00
|
|
|
{
|
|
|
|
var indexBuffer = state.Get<IndexBufferState>(MethodOffset.IndexBufferState);
|
|
|
|
|
|
|
|
DrawEnd(state, indexBuffer.First, indexBuffer.Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finishes the draw call.
|
|
|
|
/// This draws geometry on the bound buffers based on the current GPU state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <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 DrawEnd(GpuState state, int firstIndex, int indexCount)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-05-03 22:24:59 -04:00
|
|
|
ConditionalRenderEnabled renderEnable = GetRenderEnable(state);
|
2020-04-22 02:00:11 -04:00
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.False || _instancedDrawPending)
|
2019-12-08 16:08:00 -05:00
|
|
|
{
|
2020-05-03 22:24:59 -04:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.False)
|
2020-04-22 02:00:11 -04:00
|
|
|
{
|
|
|
|
PerformDeferredDraws();
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:08:00 -05:00
|
|
|
_drawIndexed = false;
|
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.Host)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndHostConditionalRendering();
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:08:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
UpdateState(state, firstIndex, indexCount);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
bool instanced = _vsUsesInstanceId || _isAnyVbInstanced;
|
|
|
|
|
|
|
|
if (instanced)
|
|
|
|
{
|
2019-12-08 16:08:00 -05:00
|
|
|
_instancedDrawPending = true;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-08 16:08:00 -05:00
|
|
|
_instancedIndexed = _drawIndexed;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
_instancedFirstIndex = firstIndex;
|
|
|
|
_instancedFirstVertex = state.Get<int>(MethodOffset.FirstVertex);
|
2019-12-08 16:08:00 -05:00
|
|
|
_instancedFirstInstance = state.Get<int>(MethodOffset.FirstInstance);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
_instancedIndexCount = indexCount;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-08 16:08:00 -05:00
|
|
|
var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-12-08 16:08:00 -05:00
|
|
|
_instancedDrawStateFirst = drawState.First;
|
|
|
|
_instancedDrawStateCount = drawState.Count;
|
|
|
|
|
|
|
|
_drawIndexed = false;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.Host)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndHostConditionalRendering();
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
int firstInstance = state.Get<int>(MethodOffset.FirstInstance);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
int inlineIndexCount = _ibStreamer.GetAndResetInlineIndexCount();
|
|
|
|
|
|
|
|
if (inlineIndexCount != 0)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-07-03 18:41:27 -04:00
|
|
|
int firstVertex = state.Get<int>(MethodOffset.FirstVertex);
|
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
BufferRange br = new BufferRange(_ibStreamer.GetInlineIndexBuffer(), 0, inlineIndexCount * 4);
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2021-06-23 19:51:41 -04:00
|
|
|
state.Channel.BufferManager.SetIndexBuffer(br, IndexType.UInt);
|
2020-07-03 18:41:27 -04:00
|
|
|
|
|
|
|
_context.Renderer.Pipeline.DrawIndexed(
|
2020-09-23 19:48:34 -04:00
|
|
|
inlineIndexCount,
|
2020-07-03 18:41:27 -04:00
|
|
|
1,
|
2020-09-23 19:48:34 -04:00
|
|
|
firstIndex,
|
2020-07-03 18:41:27 -04:00
|
|
|
firstVertex,
|
|
|
|
firstInstance);
|
|
|
|
}
|
|
|
|
else if (_drawIndexed)
|
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
int firstVertex = state.Get<int>(MethodOffset.FirstVertex);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.DrawIndexed(
|
2020-09-23 19:48:34 -04:00
|
|
|
indexCount,
|
2019-10-13 02:02:07 -04:00
|
|
|
1,
|
2020-09-23 19:48:34 -04:00
|
|
|
firstIndex,
|
2019-10-13 02:02:07 -04:00
|
|
|
firstVertex,
|
|
|
|
firstInstance);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.Draw(
|
2019-10-13 02:02:07 -04:00
|
|
|
drawState.Count,
|
|
|
|
1,
|
|
|
|
drawState.First,
|
|
|
|
firstInstance);
|
|
|
|
}
|
2020-05-03 22:24:59 -04:00
|
|
|
|
2020-07-03 18:41:27 -04:00
|
|
|
_drawIndexed = false;
|
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.Host)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndHostConditionalRendering();
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Starts draw.
|
|
|
|
/// This sets primitive type and instanced draw parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void DrawBegin(GpuState state, int argument)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
bool incrementInstance = (argument & (1 << 26)) != 0;
|
|
|
|
bool resetInstance = (argument & (1 << 27)) == 0;
|
|
|
|
|
|
|
|
PrimitiveType type = (PrimitiveType)(argument & 0xffff);
|
|
|
|
|
|
|
|
PrimitiveTypeOverride typeOverride = state.Get<PrimitiveTypeOverride>(MethodOffset.PrimitiveTypeOverride);
|
|
|
|
|
|
|
|
if (typeOverride != PrimitiveTypeOverride.Invalid)
|
|
|
|
{
|
|
|
|
DrawBegin(incrementInstance, resetInstance, typeOverride.Convert());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DrawBegin(incrementInstance, resetInstance, type.Convert());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Starts draw.
|
|
|
|
/// This sets primitive type and instanced draw parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="incrementInstance">Indicates if the current instance should be incremented</param>
|
|
|
|
/// <param name="resetInstance">Indicates if the current instance should be set to zero</param>
|
|
|
|
/// <param name="topology">Primitive topology</param>
|
|
|
|
private void DrawBegin(bool incrementInstance, bool resetInstance, PrimitiveTopology topology)
|
|
|
|
{
|
|
|
|
if (incrementInstance)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
_instanceIndex++;
|
|
|
|
}
|
2020-09-23 19:48:34 -04:00
|
|
|
else if (resetInstance)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-06 17:37:00 -05:00
|
|
|
PerformDeferredDraws();
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
_instanceIndex = 0;
|
|
|
|
}
|
2019-12-06 17:37:00 -05:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
_context.Renderer.Pipeline.SetPrimitiveTopology(topology);
|
2019-12-06 17:37:00 -05:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
Topology = topology;
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the index buffer count.
|
2020-01-01 18:14:18 -05:00
|
|
|
/// This also sets internal state that indicates that the next draw is an indexed draw.
|
2019-12-31 14:19:44 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void SetIndexBufferCount(GpuState state, int argument)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
|
|
|
_drawIndexed = true;
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:41:27 -04:00
|
|
|
/// <summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// Performs a indexed draw with a low number of index buffer elements.
|
2020-07-03 18:41:27 -04:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2020-09-23 19:48:34 -04:00
|
|
|
private void DrawIndexedSmall(GpuState state, int argument)
|
2020-07-03 18:41:27 -04:00
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
DrawIndexedSmall(state, argument, false);
|
|
|
|
}
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a indexed draw with a low number of index buffer elements.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
private void DrawIndexedSmall2(GpuState state, int argument)
|
|
|
|
{
|
|
|
|
DrawIndexedSmall(state, argument);
|
|
|
|
}
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a indexed draw with a low number of index buffer elements,
|
|
|
|
/// while also pre-incrementing the current instance value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
private void DrawIndexedSmallIncInstance(GpuState state, int argument)
|
|
|
|
{
|
|
|
|
DrawIndexedSmall(state, argument, true);
|
|
|
|
}
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a indexed draw with a low number of index buffer elements,
|
|
|
|
/// while also pre-incrementing the current instance value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
private void DrawIndexedSmallIncInstance2(GpuState state, int argument)
|
|
|
|
{
|
|
|
|
DrawIndexedSmallIncInstance(state, argument);
|
2020-07-03 18:41:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// Performs a indexed draw with a low number of index buffer elements,
|
|
|
|
/// while optionally also pre-incrementing the current instance value.
|
2020-07-03 18:41:27 -04:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <param name="instanced">True to increment the current instance value, false otherwise</param>
|
|
|
|
private void DrawIndexedSmall(GpuState state, int argument, bool instanced)
|
2020-07-03 18:41:27 -04:00
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
PrimitiveTypeOverride typeOverride = state.Get<PrimitiveTypeOverride>(MethodOffset.PrimitiveTypeOverride);
|
|
|
|
|
|
|
|
DrawBegin(instanced, !instanced, typeOverride.Convert());
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
int firstIndex = argument & 0xffff;
|
|
|
|
int indexCount = (argument >> 16) & 0xfff;
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
bool oldDrawIndexed = _drawIndexed;
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
_drawIndexed = true;
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
DrawEnd(state, firstIndex, indexCount);
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
_drawIndexed = oldDrawIndexed;
|
2020-07-03 18:41:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// Pushes four 8-bit index buffer elements.
|
2020-07-03 18:41:27 -04:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2020-09-23 19:48:34 -04:00
|
|
|
private void VbElementU8(GpuState state, int argument)
|
2020-07-03 18:41:27 -04:00
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
_ibStreamer.VbElementU8(_context.Renderer, argument);
|
2020-07-03 18:41:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// Pushes two 16-bit index buffer elements.
|
2020-07-03 18:41:27 -04:00
|
|
|
/// </summary>
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
private void VbElementU16(GpuState state, int argument)
|
2020-07-03 18:41:27 -04:00
|
|
|
{
|
2020-09-23 19:48:34 -04:00
|
|
|
_ibStreamer.VbElementU16(_context.Renderer, argument);
|
|
|
|
}
|
2020-07-03 18:41:27 -04:00
|
|
|
|
2020-09-23 19:48:34 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Pushes one 32-bit index buffer element.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
private void VbElementU32(GpuState state, int argument)
|
|
|
|
{
|
|
|
|
_ibStreamer.VbElementU32(_context.Renderer, argument);
|
2020-07-03 18:41:27 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Perform any deferred draws.
|
|
|
|
/// This is used for instanced draws.
|
|
|
|
/// Since each instance is a separate draw, we defer the draw and accumulate the instance count.
|
|
|
|
/// Once we detect the last instanced draw, then we perform the host instanced draw,
|
|
|
|
/// with the accumulated instance count.
|
|
|
|
/// </summary>
|
2019-10-13 02:02:07 -04:00
|
|
|
public void PerformDeferredDraws()
|
|
|
|
{
|
|
|
|
// Perform any pending instanced draw.
|
2019-12-06 17:37:00 -05:00
|
|
|
if (_instancedDrawPending)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-12-06 17:37:00 -05:00
|
|
|
_instancedDrawPending = false;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (_instancedIndexed)
|
|
|
|
{
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.DrawIndexed(
|
2019-10-13 02:02:07 -04:00
|
|
|
_instancedIndexCount,
|
|
|
|
_instanceIndex + 1,
|
|
|
|
_instancedFirstIndex,
|
|
|
|
_instancedFirstVertex,
|
|
|
|
_instancedFirstInstance);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-17 22:41:18 -04:00
|
|
|
_context.Renderer.Pipeline.Draw(
|
2019-10-13 02:02:07 -04:00
|
|
|
_instancedDrawStateCount,
|
|
|
|
_instanceIndex + 1,
|
|
|
|
_instancedDrawStateFirst,
|
|
|
|
_instancedFirstInstance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|