2019-10-12 23:02:07 -07:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
|
|
|
partial class Methods
|
|
|
|
{
|
|
|
|
private bool _drawIndexed;
|
|
|
|
|
|
|
|
private int _firstIndex;
|
|
|
|
private int _indexCount;
|
|
|
|
|
2019-12-06 14:37:00 -08:00
|
|
|
private bool _instancedDrawPending;
|
2019-10-12 23:02:07 -07: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;
|
|
|
|
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Primitive type of the current draw.
|
|
|
|
/// </summary>
|
2019-10-12 23:02:07 -07:00
|
|
|
public PrimitiveType PrimitiveType { get; private set; }
|
|
|
|
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Finishes 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="argument">Method call argument</param>
|
2019-11-21 18:46:14 -08:00
|
|
|
private void DrawEnd(GpuState state, int argument)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
2020-05-03 19:24:59 -07:00
|
|
|
ConditionalRenderEnabled renderEnable = GetRenderEnable(state);
|
2020-04-21 23:00:11 -07:00
|
|
|
|
2020-05-03 19:24:59 -07:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.False || _instancedDrawPending)
|
2019-12-08 13:08:00 -08:00
|
|
|
{
|
2020-05-03 19:24:59 -07:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.False)
|
2020-04-21 23:00:11 -07:00
|
|
|
{
|
|
|
|
PerformDeferredDraws();
|
|
|
|
}
|
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
_drawIndexed = false;
|
|
|
|
|
2020-05-03 19:24:59 -07:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.Host)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndHostConditionalRendering();
|
|
|
|
}
|
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
UpdateState(state);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
bool instanced = _vsUsesInstanceId || _isAnyVbInstanced;
|
|
|
|
|
|
|
|
if (instanced)
|
|
|
|
{
|
2019-12-08 13:08:00 -08:00
|
|
|
_instancedDrawPending = true;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
_instancedIndexed = _drawIndexed;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
_instancedFirstIndex = _firstIndex;
|
|
|
|
_instancedFirstVertex = state.Get<int>(MethodOffset.FirstVertex);
|
|
|
|
_instancedFirstInstance = state.Get<int>(MethodOffset.FirstInstance);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
_instancedIndexCount = _indexCount;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-12-08 13:08:00 -08:00
|
|
|
_instancedDrawStateFirst = drawState.First;
|
|
|
|
_instancedDrawStateCount = drawState.Count;
|
|
|
|
|
|
|
|
_drawIndexed = false;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2020-05-03 19:24:59 -07:00
|
|
|
if (renderEnable == ConditionalRenderEnabled.Host)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndHostConditionalRendering();
|
|
|
|
}
|
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
int firstInstance = state.Get<int>(MethodOffset.FirstInstance);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
if (_drawIndexed)
|
|
|
|
{
|
|
|
|
_drawIndexed = false;
|
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
int firstVertex = state.Get<int>(MethodOffset.FirstVertex);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-10-17 19:41:18 -07:00
|
|
|
_context.Renderer.Pipeline.DrawIndexed(
|
2019-10-12 23:02:07 -07:00
|
|
|
_indexCount,
|
|
|
|
1,
|
|
|
|
_firstIndex,
|
|
|
|
firstVertex,
|
|
|
|
firstInstance);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-21 18:46:14 -08:00
|
|
|
var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-10-17 19:41:18 -07:00
|
|
|
_context.Renderer.Pipeline.Draw(
|
2019-10-12 23:02:07 -07:00
|
|
|
drawState.Count,
|
|
|
|
1,
|
|
|
|
drawState.First,
|
|
|
|
firstInstance);
|
|
|
|
}
|
2020-05-03 19:24:59 -07:00
|
|
|
|
|
|
|
if (renderEnable == ConditionalRenderEnabled.Host)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.EndHostConditionalRendering();
|
|
|
|
}
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08: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 18:46:14 -08:00
|
|
|
private void DrawBegin(GpuState state, int argument)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
|
|
|
if ((argument & (1 << 26)) != 0)
|
|
|
|
{
|
|
|
|
_instanceIndex++;
|
|
|
|
}
|
|
|
|
else if ((argument & (1 << 27)) == 0)
|
|
|
|
{
|
2019-12-06 14:37:00 -08:00
|
|
|
PerformDeferredDraws();
|
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
_instanceIndex = 0;
|
|
|
|
}
|
2019-12-06 14:37:00 -08:00
|
|
|
|
|
|
|
PrimitiveType type = (PrimitiveType)(argument & 0xffff);
|
|
|
|
|
|
|
|
_context.Renderer.Pipeline.SetPrimitiveTopology(type.Convert());
|
|
|
|
|
|
|
|
PrimitiveType = type;
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the index buffer count.
|
2020-01-01 15:14:18 -08:00
|
|
|
/// This also sets internal state that indicates that the next draw is an indexed draw.
|
2019-12-31 11:19:44 -08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2019-11-21 18:46:14 -08:00
|
|
|
private void SetIndexBufferCount(GpuState state, int argument)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
|
|
|
_drawIndexed = true;
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08: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-12 23:02:07 -07:00
|
|
|
public void PerformDeferredDraws()
|
|
|
|
{
|
|
|
|
// Perform any pending instanced draw.
|
2019-12-06 14:37:00 -08:00
|
|
|
if (_instancedDrawPending)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
2019-12-06 14:37:00 -08:00
|
|
|
_instancedDrawPending = false;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
if (_instancedIndexed)
|
|
|
|
{
|
2019-10-17 19:41:18 -07:00
|
|
|
_context.Renderer.Pipeline.DrawIndexed(
|
2019-10-12 23:02:07 -07:00
|
|
|
_instancedIndexCount,
|
|
|
|
_instanceIndex + 1,
|
|
|
|
_instancedFirstIndex,
|
|
|
|
_instancedFirstVertex,
|
|
|
|
_instancedFirstInstance);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-17 19:41:18 -07:00
|
|
|
_context.Renderer.Pipeline.Draw(
|
2019-10-12 23:02:07 -07:00
|
|
|
_instancedDrawStateCount,
|
|
|
|
_instanceIndex + 1,
|
|
|
|
_instancedDrawStateFirst,
|
|
|
|
_instancedFirstInstance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|