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
This commit is contained in:
gdkchan 2021-11-10 15:37:49 -03:00 committed by GitHub
parent bc00a251dd
commit 611bec6e44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 421 additions and 14 deletions

View file

@ -0,0 +1,138 @@
using OpenTK.Graphics.OpenGL;
using Ryujinx.Graphics.OpenGL.Image;
using System;
namespace Ryujinx.Graphics.OpenGL
{
class DrawTextureEmulation
{
private const string VertexShader = @"#version 430 core
uniform float srcX0;
uniform float srcY0;
uniform float srcX1;
uniform float srcY1;
layout (location = 0) out vec2 texcoord;
void main()
{
bool x1 = (gl_VertexID & 1) != 0;
bool y1 = (gl_VertexID & 2) != 0;
gl_Position = vec4(x1 ? 1 : -1, y1 ? -1 : 1, 0, 1);
texcoord = vec2(x1 ? srcX1 : srcX0, y1 ? srcY1 : srcY0);
}";
private const string FragmentShader = @"#version 430 core
layout (location = 0) uniform sampler2D tex;
layout (location = 0) in vec2 texcoord;
layout (location = 0) out vec4 colour;
void main()
{
colour = texture(tex, texcoord);
}";
private int _vsHandle;
private int _fsHandle;
private int _programHandle;
private int _uniformSrcX0Location;
private int _uniformSrcY0Location;
private int _uniformSrcX1Location;
private int _uniformSrcY1Location;
private bool _initialized;
public void Draw(
TextureView texture,
Sampler sampler,
float x0,
float y0,
float x1,
float y1,
float s0,
float t0,
float s1,
float t1)
{
EnsureInitialized();
GL.UseProgram(_programHandle);
texture.Bind(0);
sampler.Bind(0);
if (x0 > x1)
{
float temp = s0;
s0 = s1;
s1 = temp;
}
if (y0 > y1)
{
float temp = t0;
t0 = t1;
t1 = temp;
}
GL.Uniform1(_uniformSrcX0Location, s0);
GL.Uniform1(_uniformSrcY0Location, t0);
GL.Uniform1(_uniformSrcX1Location, s1);
GL.Uniform1(_uniformSrcY1Location, t1);
GL.ViewportIndexed(0, MathF.Min(x0, x1), MathF.Min(y0, y1), MathF.Abs(x1 - x0), MathF.Abs(y1 - y0));
GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
}
private void EnsureInitialized()
{
if (_initialized)
{
return;
}
_initialized = true;
_vsHandle = GL.CreateShader(ShaderType.VertexShader);
_fsHandle = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(_vsHandle, VertexShader);
GL.ShaderSource(_fsHandle, FragmentShader);
GL.CompileShader(_vsHandle);
GL.CompileShader(_fsHandle);
_programHandle = GL.CreateProgram();
GL.AttachShader(_programHandle, _vsHandle);
GL.AttachShader(_programHandle, _fsHandle);
GL.LinkProgram(_programHandle);
GL.DetachShader(_programHandle, _vsHandle);
GL.DetachShader(_programHandle, _fsHandle);
_uniformSrcX0Location = GL.GetUniformLocation(_programHandle, "srcX0");
_uniformSrcY0Location = GL.GetUniformLocation(_programHandle, "srcY0");
_uniformSrcX1Location = GL.GetUniformLocation(_programHandle, "srcX1");
_uniformSrcY1Location = GL.GetUniformLocation(_programHandle, "srcY1");
}
public void Dispose()
{
if (!_initialized)
{
return;
}
GL.DeleteShader(_vsHandle);
GL.DeleteShader(_fsHandle);
GL.DeleteProgram(_programHandle);
_initialized = false;
}
}
}

View file

@ -6,6 +6,7 @@ namespace Ryujinx.Graphics.OpenGL
static class HwCapabilities
{
private static readonly Lazy<bool> _supportsAstcCompression = new Lazy<bool>(() => HasExtension("GL_KHR_texture_compression_astc_ldr"));
private static readonly Lazy<bool> _supportsDrawTexture = new Lazy<bool>(() => HasExtension("GL_NV_draw_texture"));
private static readonly Lazy<bool> _supportsFragmentShaderInterlock = new Lazy<bool>(() => HasExtension("GL_ARB_fragment_shader_interlock"));
private static readonly Lazy<bool> _supportsFragmentShaderOrdering = new Lazy<bool>(() => HasExtension("GL_INTEL_fragment_shader_ordering"));
private static readonly Lazy<bool> _supportsImageLoadFormatted = new Lazy<bool>(() => HasExtension("GL_EXT_shader_image_load_formatted"));
@ -43,6 +44,7 @@ namespace Ryujinx.Graphics.OpenGL
public static bool UsePersistentBufferForFlush => _gpuVendor.Value == GpuVendor.AmdWindows || _gpuVendor.Value == GpuVendor.Nvidia;
public static bool SupportsAstcCompression => _supportsAstcCompression.Value;
public static bool SupportsDrawTexture => _supportsDrawTexture.Value;
public static bool SupportsFragmentShaderInterlock => _supportsFragmentShaderInterlock.Value;
public static bool SupportsFragmentShaderOrdering => _supportsFragmentShaderOrdering.Value;
public static bool SupportsImageLoadFormatted => _supportsImageLoadFormatted.Value;

View file

@ -12,6 +12,8 @@ namespace Ryujinx.Graphics.OpenGL
{
class Pipeline : IPipeline, IDisposable
{
private readonly DrawTextureEmulation _drawTexture;
internal ulong DrawCount { get; private set; }
private Program _program;
@ -29,6 +31,12 @@ namespace Ryujinx.Graphics.OpenGL
private int _stencilFrontMask;
private bool _depthMask;
private bool _depthTestEnable;
private bool _stencilTestEnable;
private bool _cullEnable;
private float[] _viewportArray = Array.Empty<float>();
private double[] _depthRangeArray = Array.Empty<double>();
private int _boundDrawFramebuffer;
private int _boundReadFramebuffer;
@ -47,6 +55,7 @@ namespace Ryujinx.Graphics.OpenGL
private Vector4<float>[] _renderScale = new Vector4<float>[65];
private TextureBase _unit0Texture;
private Sampler _unit0Sampler;
private FrontFaceDirection _frontFace;
private ClipOrigin _clipOrigin;
@ -67,6 +76,7 @@ namespace Ryujinx.Graphics.OpenGL
internal Pipeline()
{
_drawTexture = new DrawTextureEmulation();
_rasterizerDiscard = false;
_clipOrigin = ClipOrigin.LowerLeft;
_clipDepthMode = ClipDepthMode.NegativeOneToOne;
@ -544,6 +554,91 @@ namespace Ryujinx.Graphics.OpenGL
}
}
public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
{
if (texture is TextureView view && sampler is Sampler samp)
{
if (HwCapabilities.SupportsDrawTexture)
{
GL.NV.DrawTexture(
view.Handle,
samp.Handle,
dstRegion.X1,
dstRegion.Y1,
dstRegion.X2,
dstRegion.Y2,
0,
srcRegion.X1 / view.Width,
srcRegion.Y1 / view.Height,
srcRegion.X2 / view.Width,
srcRegion.Y2 / view.Height);
}
else
{
static void Disable(EnableCap cap, bool enabled)
{
if (enabled)
{
GL.Disable(cap);
}
}
static void Enable(EnableCap cap, bool enabled)
{
if (enabled)
{
GL.Enable(cap);
}
}
Disable(EnableCap.CullFace, _cullEnable);
Disable(EnableCap.StencilTest, _stencilTestEnable);
Disable(EnableCap.DepthTest, _depthTestEnable);
if (_depthMask)
{
GL.DepthMask(false);
}
if (_tfEnabled)
{
GL.EndTransformFeedback();
}
_drawTexture.Draw(
view,
samp,
dstRegion.X1,
dstRegion.Y1,
dstRegion.X2,
dstRegion.Y2,
srcRegion.X1 / view.Width,
srcRegion.Y1 / view.Height,
srcRegion.X2 / view.Width,
srcRegion.Y2 / view.Height);
_program?.Bind();
_unit0Sampler?.Bind(0);
GL.ViewportArray(0, 1, _viewportArray);
Enable(EnableCap.CullFace, _cullEnable);
Enable(EnableCap.StencilTest, _stencilTestEnable);
Enable(EnableCap.DepthTest, _depthTestEnable);
if (_depthMask)
{
GL.DepthMask(true);
}
if (_tfEnabled)
{
GL.BeginTransformFeedback(_tfTopology);
}
}
}
}
public void EndTransformFeedback()
{
GL.EndTransformFeedback();
@ -754,10 +849,13 @@ namespace Ryujinx.Graphics.OpenGL
GL.DepthMask(depthTest.WriteEnable);
_depthMask = depthTest.WriteEnable;
_depthTestEnable = depthTest.TestEnable;
}
public void SetFaceCulling(bool enable, Face face)
{
_cullEnable = enable;
if (!enable)
{
GL.Disable(EnableCap.CullFace);
@ -994,7 +1092,14 @@ namespace Ryujinx.Graphics.OpenGL
return;
}
((Sampler)sampler).Bind(binding);
Sampler samp = (Sampler)sampler;
if (binding == 0)
{
_unit0Sampler = samp;
}
samp.Bind(binding);
}
public void SetScissor(int index, bool enable, int x, int y, int width, int height)
@ -1023,6 +1128,8 @@ namespace Ryujinx.Graphics.OpenGL
public void SetStencilTest(StencilTestDescriptor stencilTest)
{
_stencilTestEnable = stencilTest.TestEnable;
if (!stencilTest.TestEnable)
{
GL.Disable(EnableCap.StencilTest);
@ -1152,9 +1259,11 @@ namespace Ryujinx.Graphics.OpenGL
public void SetViewports(int first, ReadOnlySpan<Viewport> viewports)
{
float[] viewportArray = new float[viewports.Length * 4];
Array.Resize(ref _viewportArray, viewports.Length * 4);
Array.Resize(ref _depthRangeArray, viewports.Length * 2);
double[] depthRangeArray = new double[viewports.Length * 2];
float[] viewportArray = _viewportArray;
double[] depthRangeArray = _depthRangeArray;
for (int index = 0; index < viewports.Length; index++)
{
@ -1186,7 +1295,6 @@ namespace Ryujinx.Graphics.OpenGL
SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
GL.ViewportArray(first, viewports.Length, viewportArray);
GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
}
@ -1307,10 +1415,7 @@ namespace Ryujinx.Graphics.OpenGL
private void PrepareForDispatch()
{
if (_unit0Texture != null)
{
_unit0Texture.Bind(0);
}
_unit0Texture?.Bind(0);
}
private void PreDraw()
@ -1318,11 +1423,7 @@ namespace Ryujinx.Graphics.OpenGL
DrawCount++;
_vertexArray.Validate();
if (_unit0Texture != null)
{
_unit0Texture.Bind(0);
}
_unit0Texture?.Bind(0);
}
private void PostDraw()
@ -1438,6 +1539,7 @@ namespace Ryujinx.Graphics.OpenGL
_activeConditionalRender?.ReleaseHostAccess();
_framebuffer?.Dispose();
_vertexArray?.Dispose();
_drawTexture.Dispose();
}
}
}