From 611bec6e44effa90554c95ed1fe4dd4812893947 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 10 Nov 2021 15:37:49 -0300 Subject: Implement DrawTexture functionality (#2747) * Implement DrawTexture functionality * Non-NVIDIA support * Disable some features that should not affect draw texture (slow path) * Remove space from shader source * Match 2D engine names * Fix resolution scale and add missing XML docs * Disable transform feedback for draw texture fallback --- Ryujinx.Graphics.Gpu/Engine/Threed/DrawManager.cs | 59 ++++++++++++++++++++++ Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClass.cs | 10 ++++ .../Engine/Threed/ThreedClassState.cs | 13 ++++- 3 files changed, 81 insertions(+), 1 deletion(-) (limited to 'Ryujinx.Graphics.Gpu/Engine') diff --git a/Ryujinx.Graphics.Gpu/Engine/Threed/DrawManager.cs b/Ryujinx.Graphics.Gpu/Engine/Threed/DrawManager.cs index a060c6c9..518e71ad 100644 --- a/Ryujinx.Graphics.Gpu/Engine/Threed/DrawManager.cs +++ b/Ryujinx.Graphics.Gpu/Engine/Threed/DrawManager.cs @@ -319,6 +319,65 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed _drawState.DrawIndexed = oldDrawIndexed; } + /// + /// Performs a texture draw with a source texture and sampler ID, along with source + /// and destination coordinates and sizes. + /// + /// 3D engine where this method is being called + /// Method call argument + public void DrawTexture(ThreedClass engine, int argument) + { + static float FixedToFloat(int fixedValue) + { + return fixedValue * (1f / 4096); + } + + float dstX0 = FixedToFloat(_state.State.DrawTextureDstX); + float dstY0 = FixedToFloat(_state.State.DrawTextureDstY); + float dstWidth = FixedToFloat(_state.State.DrawTextureDstWidth); + float dstHeight = FixedToFloat(_state.State.DrawTextureDstHeight); + + // TODO: Confirm behaviour on hardware. + // When this is active, the origin appears to be on the bottom. + if (_state.State.YControl.HasFlag(YControl.NegateY)) + { + dstY0 -= dstHeight; + } + + float dstX1 = dstX0 + dstWidth; + float dstY1 = dstY0 + dstHeight; + + float srcX0 = FixedToFloat(_state.State.DrawTextureSrcX); + float srcY0 = FixedToFloat(_state.State.DrawTextureSrcY); + float srcX1 = ((float)_state.State.DrawTextureDuDx / (1UL << 32)) * dstWidth + srcX0; + float srcY1 = ((float)_state.State.DrawTextureDvDy / (1UL << 32)) * dstHeight + srcY0; + + engine.UpdateState(); + + int textureId = _state.State.DrawTextureTextureId; + int samplerId = _state.State.DrawTextureSamplerId; + + (var texture, var sampler) = _channel.TextureManager.GetGraphicsTextureAndSampler(textureId, samplerId); + + srcX0 *= texture.ScaleFactor; + srcY0 *= texture.ScaleFactor; + srcX1 *= texture.ScaleFactor; + srcY1 *= texture.ScaleFactor; + + float dstScale = _channel.TextureManager.RenderTargetScale; + + dstX0 *= dstScale; + dstY0 *= dstScale; + dstX1 *= dstScale; + dstY1 *= dstScale; + + _context.Renderer.Pipeline.DrawTexture( + texture?.HostTexture, + sampler?.HostSampler, + new Extents2DF(srcX0, srcY0, srcX1, srcY1), + new Extents2DF(dstX0, dstY0, dstX1, dstY1)); + } + /// /// Performs a indirect multi-draw, with parameters from a GPU buffer. /// diff --git a/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClass.cs b/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClass.cs index 5478704a..f3061c73 100644 --- a/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClass.cs +++ b/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClass.cs @@ -36,6 +36,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed { nameof(ThreedClassState.SyncpointAction), new RwCallback(IncrementSyncpoint, null) }, { nameof(ThreedClassState.TextureBarrier), new RwCallback(TextureBarrier, null) }, { nameof(ThreedClassState.TextureBarrierTiled), new RwCallback(TextureBarrierTiled, null) }, + { nameof(ThreedClassState.DrawTextureSrcY), new RwCallback(DrawTexture, null) }, { nameof(ThreedClassState.VbElementU8), new RwCallback(VbElementU8, null) }, { nameof(ThreedClassState.VbElementU16), new RwCallback(VbElementU16, null) }, { nameof(ThreedClassState.VbElementU32), new RwCallback(VbElementU32, null) }, @@ -251,6 +252,15 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed _context.Renderer.Pipeline.TextureBarrierTiled(); } + /// + /// Draws a texture, without needing to specify shader programs. + /// + /// Method call argument + private void DrawTexture(int argument) + { + _drawManager.DrawTexture(this, argument); + } + /// /// Pushes four 8-bit index buffer elements. /// diff --git a/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClassState.cs b/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClassState.cs index 58bc0957..165f5072 100644 --- a/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClassState.cs +++ b/Ryujinx.Graphics.Gpu/Engine/Threed/ThreedClassState.cs @@ -743,7 +743,18 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed public fixed uint ReservedF94[19]; public RtDepthStencilState RtDepthStencilState; public ScreenScissorState ScreenScissorState; - public fixed uint ReservedFFC[89]; + public fixed uint ReservedFFC[33]; + public int DrawTextureDstX; + public int DrawTextureDstY; + public int DrawTextureDstWidth; + public int DrawTextureDstHeight; + public long DrawTextureDuDx; + public long DrawTextureDvDy; + public int DrawTextureSamplerId; + public int DrawTextureTextureId; + public int DrawTextureSrcX; + public int DrawTextureSrcY; + public fixed uint Reserved10B0[44]; public Array16 VertexAttribState; public fixed uint Reserved11A0[31]; public RtControl RtControl; -- cgit v1.2.3