aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.OpenGL
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.Graphics.OpenGL
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.Graphics.OpenGL')
-rw-r--r--src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs91
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Buffer.cs103
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Constants.cs11
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Debugger.cs101
-rw-r--r--src/Ryujinx.Graphics.OpenGL/DrawTextureEmulation.cs138
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/FsrScalingFilter.cs177
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/FxaaPostProcessingEffect.cs81
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/IPostProcessingEffect.cs11
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/IScalingFilter.cs18
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/ShaderHelper.cs40
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_a.h2656
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_fsr1.h1199
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_scaling.glsl88
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_sharpening.glsl37
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fxaa.glsl1174
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa.hlsl1361
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_blend.glsl26
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_edge.glsl24
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_neighbour.glsl26
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/SmaaPostProcessingEffect.cs261
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaAreaTexture.binbin0 -> 179200 bytes
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaSearchTexture.binbin0 -> 1024 bytes
-rw-r--r--src/Ryujinx.Graphics.OpenGL/EnumConversion.cs675
-rw-r--r--src/Ryujinx.Graphics.OpenGL/FormatInfo.cs45
-rw-r--r--src/Ryujinx.Graphics.OpenGL/FormatTable.cs225
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Framebuffer.cs247
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Handle.cs23
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Helper/GLXHelper.cs36
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Helper/WGLHelper.cs15
-rw-r--r--src/Ryujinx.Graphics.OpenGL/HwCapabilities.cs142
-rw-r--r--src/Ryujinx.Graphics.OpenGL/IOpenGLContext.cs27
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/FormatConverter.cs149
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs14
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/IntermmediatePool.cs103
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/Sampler.cs64
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureBase.cs44
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureBuffer.cs108
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs524
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs252
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs276
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs212
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Image/TextureView.cs867
-rw-r--r--src/Ryujinx.Graphics.OpenGL/OpenGLRenderer.cs276
-rw-r--r--src/Ryujinx.Graphics.OpenGL/PersistentBuffers.cs136
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Pipeline.cs1770
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Program.cs177
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Queries/BufferedQuery.cs120
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs229
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Queries/CounterQueueEvent.cs163
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Queries/Counters.cs57
-rw-r--r--src/Ryujinx.Graphics.OpenGL/ResourcePool.cs122
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Ryujinx.Graphics.OpenGL.csproj32
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Sync.cs168
-rw-r--r--src/Ryujinx.Graphics.OpenGL/VertexArray.cs280
-rw-r--r--src/Ryujinx.Graphics.OpenGL/Window.cs420
55 files changed, 15621 insertions, 0 deletions
diff --git a/src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs b/src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs
new file mode 100644
index 00000000..764ea715
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs
@@ -0,0 +1,91 @@
+using Ryujinx.Common;
+using System;
+using System.Collections.Generic;
+using System.Threading;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ unsafe class BackgroundContextWorker : IDisposable
+ {
+ [ThreadStatic]
+ public static bool InBackground;
+ private Thread _thread;
+ private bool _running;
+
+ private AutoResetEvent _signal;
+ private Queue<Action> _work;
+ private ObjectPool<ManualResetEventSlim> _invokePool;
+ private readonly IOpenGLContext _backgroundContext;
+
+ public BackgroundContextWorker(IOpenGLContext backgroundContext)
+ {
+ _backgroundContext = backgroundContext;
+ _running = true;
+
+ _signal = new AutoResetEvent(false);
+ _work = new Queue<Action>();
+ _invokePool = new ObjectPool<ManualResetEventSlim>(() => new ManualResetEventSlim(), 10);
+
+ _thread = new Thread(Run);
+ _thread.Start();
+ }
+
+ private void Run()
+ {
+ InBackground = true;
+
+ _backgroundContext.MakeCurrent();
+
+ while (_running)
+ {
+ Action action;
+
+ lock (_work)
+ {
+ _work.TryDequeue(out action);
+ }
+
+ if (action != null)
+ {
+ action();
+ }
+ else
+ {
+ _signal.WaitOne();
+ }
+ }
+
+ _backgroundContext.Dispose();
+ }
+
+ public void Invoke(Action action)
+ {
+ ManualResetEventSlim actionComplete = _invokePool.Allocate();
+
+ lock (_work)
+ {
+ _work.Enqueue(() =>
+ {
+ action();
+ actionComplete.Set();
+ });
+ }
+
+ _signal.Set();
+
+ actionComplete.Wait();
+ actionComplete.Reset();
+
+ _invokePool.Release(actionComplete);
+ }
+
+ public void Dispose()
+ {
+ _running = false;
+ _signal.Set();
+
+ _thread.Join();
+ _signal.Dispose();
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Buffer.cs b/src/Ryujinx.Graphics.OpenGL/Buffer.cs
new file mode 100644
index 00000000..af7d191a
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Buffer.cs
@@ -0,0 +1,103 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ static class Buffer
+ {
+ public static void Clear(BufferHandle destination, int offset, int size, uint value)
+ {
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, destination.ToInt32());
+
+ unsafe
+ {
+ uint* valueArr = stackalloc uint[1];
+
+ valueArr[0] = value;
+
+ GL.ClearBufferSubData(
+ BufferTarget.CopyWriteBuffer,
+ PixelInternalFormat.Rgba8ui,
+ (IntPtr)offset,
+ (IntPtr)size,
+ PixelFormat.RgbaInteger,
+ PixelType.UnsignedByte,
+ (IntPtr)valueArr);
+ }
+ }
+
+ public static BufferHandle Create()
+ {
+ return Handle.FromInt32<BufferHandle>(GL.GenBuffer());
+ }
+
+ public static BufferHandle Create(int size)
+ {
+ int handle = GL.GenBuffer();
+
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle);
+ GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.DynamicDraw);
+
+ return Handle.FromInt32<BufferHandle>(handle);
+ }
+
+ public static void Copy(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
+ {
+ GL.BindBuffer(BufferTarget.CopyReadBuffer, source.ToInt32());
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, destination.ToInt32());
+
+ GL.CopyBufferSubData(
+ BufferTarget.CopyReadBuffer,
+ BufferTarget.CopyWriteBuffer,
+ (IntPtr)srcOffset,
+ (IntPtr)dstOffset,
+ (IntPtr)size);
+ }
+
+ public static unsafe PinnedSpan<byte> GetData(OpenGLRenderer renderer, BufferHandle buffer, int offset, int size)
+ {
+ // Data in the persistent buffer and host array is guaranteed to be available
+ // until the next time the host thread requests data.
+
+ if (HwCapabilities.UsePersistentBufferForFlush)
+ {
+ return PinnedSpan<byte>.UnsafeFromSpan(renderer.PersistentBuffers.Default.GetBufferData(buffer, offset, size));
+ }
+ else
+ {
+ IntPtr target = renderer.PersistentBuffers.Default.GetHostArray(size);
+
+ GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
+
+ GL.GetBufferSubData(BufferTarget.CopyReadBuffer, (IntPtr)offset, size, target);
+
+ return new PinnedSpan<byte>(target.ToPointer(), size);
+ }
+ }
+
+ public static void Resize(BufferHandle handle, int size)
+ {
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle.ToInt32());
+ GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.StreamCopy);
+ }
+
+ public static void SetData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
+ {
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, buffer.ToInt32());
+
+ unsafe
+ {
+ fixed (byte* ptr = data)
+ {
+ GL.BufferSubData(BufferTarget.CopyWriteBuffer, (IntPtr)offset, data.Length, (IntPtr)ptr);
+ }
+ }
+ }
+
+ public static void Delete(BufferHandle buffer)
+ {
+ GL.DeleteBuffer(buffer.ToInt32());
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Constants.cs b/src/Ryujinx.Graphics.OpenGL/Constants.cs
new file mode 100644
index 00000000..8817011a
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Constants.cs
@@ -0,0 +1,11 @@
+namespace Ryujinx.Graphics.OpenGL
+{
+ static class Constants
+ {
+ public const int MaxRenderTargets = 8;
+ public const int MaxViewports = 16;
+ public const int MaxVertexAttribs = 16;
+ public const int MaxVertexBuffers = 16;
+ public const int MaxTransformFeedbackBuffers = 4;
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Debugger.cs b/src/Ryujinx.Graphics.OpenGL/Debugger.cs
new file mode 100644
index 00000000..9f67cfc6
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Debugger.cs
@@ -0,0 +1,101 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Configuration;
+using Ryujinx.Common.Logging;
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ public static class Debugger
+ {
+ private static DebugProc _debugCallback;
+
+ private static int _counter;
+
+ public static void Initialize(GraphicsDebugLevel logLevel)
+ {
+ // Disable everything
+ GL.DebugMessageControl(DebugSourceControl.DontCare, DebugTypeControl.DontCare, DebugSeverityControl.DontCare, 0, (int[])null, false);
+
+ if (logLevel == GraphicsDebugLevel.None)
+ {
+ GL.Disable(EnableCap.DebugOutputSynchronous);
+ GL.DebugMessageCallback(null, IntPtr.Zero);
+
+ return;
+ }
+
+ GL.Enable(EnableCap.DebugOutputSynchronous);
+
+ if (logLevel == GraphicsDebugLevel.Error)
+ {
+ GL.DebugMessageControl(DebugSourceControl.DontCare, DebugTypeControl.DebugTypeError, DebugSeverityControl.DontCare, 0, (int[])null, true);
+ }
+ else if (logLevel == GraphicsDebugLevel.Slowdowns)
+ {
+ GL.DebugMessageControl(DebugSourceControl.DontCare, DebugTypeControl.DebugTypeError, DebugSeverityControl.DontCare, 0, (int[])null, true);
+ GL.DebugMessageControl(DebugSourceControl.DontCare, DebugTypeControl.DebugTypePerformance, DebugSeverityControl.DontCare, 0, (int[])null, true);
+ }
+ else
+ {
+ GL.DebugMessageControl(DebugSourceControl.DontCare, DebugTypeControl.DontCare, DebugSeverityControl.DontCare, 0, (int[])null, true);
+ }
+
+ _counter = 0;
+ _debugCallback = GLDebugHandler;
+
+ GL.DebugMessageCallback(_debugCallback, IntPtr.Zero);
+
+ Logger.Warning?.Print(LogClass.Gpu, "OpenGL Debugging is enabled. Performance will be negatively impacted.");
+ }
+
+ private static void GLDebugHandler(
+ DebugSource source,
+ DebugType type,
+ int id,
+ DebugSeverity severity,
+ int length,
+ IntPtr message,
+ IntPtr userParam)
+ {
+ string msg = Marshal.PtrToStringUTF8(message).Replace('\n', ' ');
+
+ switch (type)
+ {
+ case DebugType.DebugTypeError : Logger.Error?.Print(LogClass.Gpu, $"{severity}: {msg}\nCallStack={Environment.StackTrace}", "GLERROR"); break;
+ case DebugType.DebugTypePerformance: Logger.Warning?.Print(LogClass.Gpu, $"{severity}: {msg}", "GLPERF"); break;
+ case DebugType.DebugTypePushGroup : Logger.Info?.Print(LogClass.Gpu, $"{{ ({id}) {severity}: {msg}", "GLINFO"); break;
+ case DebugType.DebugTypePopGroup : Logger.Info?.Print(LogClass.Gpu, $"}} ({id}) {severity}: {msg}", "GLINFO"); break;
+ default:
+ if (source == DebugSource.DebugSourceApplication)
+ {
+ Logger.Info?.Print(LogClass.Gpu, $"{type} {severity}: {msg}", "GLINFO");
+ }
+ else
+ {
+ Logger.Debug?.Print(LogClass.Gpu, $"{type} {severity}: {msg}", "GLDEBUG");
+ }
+ break;
+ }
+ }
+
+ // Useful debug helpers
+ public static void PushGroup(string dbgMsg)
+ {
+ int counter = Interlocked.Increment(ref _counter);
+
+ GL.PushDebugGroup(DebugSourceExternal.DebugSourceApplication, counter, dbgMsg.Length, dbgMsg);
+ }
+
+ public static void PopGroup()
+ {
+ GL.PopDebugGroup();
+ }
+
+ public static void Print(string dbgMsg, DebugType type = DebugType.DebugTypeMarker, DebugSeverity severity = DebugSeverity.DebugSeverityNotification, int id = 999999)
+ {
+ GL.DebugMessageInsert(DebugSourceExternal.DebugSourceApplication, type, id, severity, dbgMsg.Length, dbgMsg);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/DrawTextureEmulation.cs b/src/Ryujinx.Graphics.OpenGL/DrawTextureEmulation.cs
new file mode 100644
index 00000000..509e20fe
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/DrawTextureEmulation.cs
@@ -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;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/FsrScalingFilter.cs b/src/Ryujinx.Graphics.OpenGL/Effects/FsrScalingFilter.cs
new file mode 100644
index 00000000..16678bb7
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/FsrScalingFilter.cs
@@ -0,0 +1,177 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+using static Ryujinx.Graphics.OpenGL.Effects.ShaderHelper;
+
+namespace Ryujinx.Graphics.OpenGL.Effects
+{
+ internal class FsrScalingFilter : IScalingFilter
+ {
+ private readonly OpenGLRenderer _renderer;
+ private int _inputUniform;
+ private int _outputUniform;
+ private int _sharpeningUniform;
+ private int _srcX0Uniform;
+ private int _srcX1Uniform;
+ private int _srcY0Uniform;
+ private int _scalingShaderProgram;
+ private int _sharpeningShaderProgram;
+ private float _scale = 1;
+ private int _srcY1Uniform;
+ private int _dstX0Uniform;
+ private int _dstX1Uniform;
+ private int _dstY0Uniform;
+ private int _dstY1Uniform;
+ private int _scaleXUniform;
+ private int _scaleYUniform;
+ private TextureStorage _intermediaryTexture;
+
+ public float Level
+ {
+ get => _scale;
+ set
+ {
+ _scale = MathF.Max(0.01f, value);
+ }
+ }
+
+ public FsrScalingFilter(OpenGLRenderer renderer, IPostProcessingEffect filter)
+ {
+ Initialize();
+
+ _renderer = renderer;
+ }
+
+ public void Dispose()
+ {
+ if (_scalingShaderProgram != 0)
+ {
+ GL.DeleteProgram(_scalingShaderProgram);
+ GL.DeleteProgram(_sharpeningShaderProgram);
+ }
+
+ _intermediaryTexture?.Dispose();
+ }
+
+ private void Initialize()
+ {
+ var scalingShader = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_scaling.glsl");
+ var sharpeningShader = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_sharpening.glsl");
+ var fsrA = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_a.h");
+ var fsr1 = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_fsr1.h");
+
+ scalingShader = scalingShader.Replace("#include \"ffx_a.h\"", fsrA);
+ scalingShader = scalingShader.Replace("#include \"ffx_fsr1.h\"", fsr1);
+ sharpeningShader = sharpeningShader.Replace("#include \"ffx_a.h\"", fsrA);
+ sharpeningShader = sharpeningShader.Replace("#include \"ffx_fsr1.h\"", fsr1);
+
+ _scalingShaderProgram = CompileProgram(scalingShader, ShaderType.ComputeShader);
+ _sharpeningShaderProgram = CompileProgram(sharpeningShader, ShaderType.ComputeShader);
+
+ _inputUniform = GL.GetUniformLocation(_scalingShaderProgram, "Source");
+ _outputUniform = GL.GetUniformLocation(_scalingShaderProgram, "imgOutput");
+ _sharpeningUniform = GL.GetUniformLocation(_sharpeningShaderProgram, "sharpening");
+
+ _srcX0Uniform = GL.GetUniformLocation(_scalingShaderProgram, "srcX0");
+ _srcX1Uniform = GL.GetUniformLocation(_scalingShaderProgram, "srcX1");
+ _srcY0Uniform = GL.GetUniformLocation(_scalingShaderProgram, "srcY0");
+ _srcY1Uniform = GL.GetUniformLocation(_scalingShaderProgram, "srcY1");
+ _dstX0Uniform = GL.GetUniformLocation(_scalingShaderProgram, "dstX0");
+ _dstX1Uniform = GL.GetUniformLocation(_scalingShaderProgram, "dstX1");
+ _dstY0Uniform = GL.GetUniformLocation(_scalingShaderProgram, "dstY0");
+ _dstY1Uniform = GL.GetUniformLocation(_scalingShaderProgram, "dstY1");
+ _scaleXUniform = GL.GetUniformLocation(_scalingShaderProgram, "scaleX");
+ _scaleYUniform = GL.GetUniformLocation(_scalingShaderProgram, "scaleY");
+ }
+
+ public void Run(
+ TextureView view,
+ TextureView destinationTexture,
+ int width,
+ int height,
+ Extents2D source,
+ Extents2D destination)
+ {
+ if (_intermediaryTexture == null || _intermediaryTexture.Info.Width != width || _intermediaryTexture.Info.Height != height)
+ {
+ _intermediaryTexture?.Dispose();
+ var originalInfo = view.Info;
+ var info = new TextureCreateInfo(width,
+ height,
+ originalInfo.Depth,
+ originalInfo.Levels,
+ originalInfo.Samples,
+ originalInfo.BlockWidth,
+ originalInfo.BlockHeight,
+ originalInfo.BytesPerPixel,
+ originalInfo.Format,
+ originalInfo.DepthStencilMode,
+ originalInfo.Target,
+ originalInfo.SwizzleR,
+ originalInfo.SwizzleG,
+ originalInfo.SwizzleB,
+ originalInfo.SwizzleA);
+
+ _intermediaryTexture = new TextureStorage(_renderer, info, view.ScaleFactor);
+ _intermediaryTexture.CreateDefaultView();
+ }
+
+ var textureView = _intermediaryTexture.CreateView(_intermediaryTexture.Info, 0, 0) as TextureView;
+
+ int previousProgram = GL.GetInteger(GetPName.CurrentProgram);
+ int previousUnit = GL.GetInteger(GetPName.ActiveTexture);
+ GL.ActiveTexture(TextureUnit.Texture0);
+ int previousTextureBinding = GL.GetInteger(GetPName.TextureBinding2D);
+
+ GL.BindImageTexture(0, textureView.Handle, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+
+ int threadGroupWorkRegionDim = 16;
+ int dispatchX = (width + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
+ int dispatchY = (height + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
+
+ // Scaling pass
+ float srcWidth = Math.Abs(source.X2 - source.X1);
+ float srcHeight = Math.Abs(source.Y2 - source.Y1);
+ float scaleX = srcWidth / view.Width;
+ float scaleY = srcHeight / view.Height;
+ GL.UseProgram(_scalingShaderProgram);
+ view.Bind(0);
+ GL.Uniform1(_inputUniform, 0);
+ GL.Uniform1(_outputUniform, 0);
+ GL.Uniform1(_srcX0Uniform, (float)source.X1);
+ GL.Uniform1(_srcX1Uniform, (float)source.X2);
+ GL.Uniform1(_srcY0Uniform, (float)source.Y1);
+ GL.Uniform1(_srcY1Uniform, (float)source.Y2);
+ GL.Uniform1(_dstX0Uniform, (float)destination.X1);
+ GL.Uniform1(_dstX1Uniform, (float)destination.X2);
+ GL.Uniform1(_dstY0Uniform, (float)destination.Y1);
+ GL.Uniform1(_dstY1Uniform, (float)destination.Y2);
+ GL.Uniform1(_scaleXUniform, scaleX);
+ GL.Uniform1(_scaleYUniform, scaleY);
+ GL.DispatchCompute(dispatchX, dispatchY, 1);
+
+ GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrierBit);
+
+ // Sharpening Pass
+ GL.UseProgram(_sharpeningShaderProgram);
+ GL.BindImageTexture(0, destinationTexture.Handle, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ textureView.Bind(0);
+ GL.Uniform1(_inputUniform, 0);
+ GL.Uniform1(_outputUniform, 0);
+ GL.Uniform1(_sharpeningUniform, 1.5f - (Level * 0.01f * 1.5f));
+ GL.DispatchCompute(dispatchX, dispatchY, 1);
+
+ GL.UseProgram(previousProgram);
+ GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrierBit);
+
+ (_renderer.Pipeline as Pipeline).RestoreImages1And2();
+
+ GL.ActiveTexture(TextureUnit.Texture0);
+ GL.BindTexture(TextureTarget.Texture2D, previousTextureBinding);
+
+ GL.ActiveTexture((TextureUnit)previousUnit);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/FxaaPostProcessingEffect.cs b/src/Ryujinx.Graphics.OpenGL/Effects/FxaaPostProcessingEffect.cs
new file mode 100644
index 00000000..3a2d685b
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/FxaaPostProcessingEffect.cs
@@ -0,0 +1,81 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common;
+using Ryujinx.Graphics.OpenGL.Image;
+
+namespace Ryujinx.Graphics.OpenGL.Effects
+{
+ internal class FxaaPostProcessingEffect : IPostProcessingEffect
+ {
+ private readonly OpenGLRenderer _renderer;
+ private int _resolutionUniform;
+ private int _inputUniform;
+ private int _outputUniform;
+ private int _shaderProgram;
+ private TextureStorage _textureStorage;
+
+ public FxaaPostProcessingEffect(OpenGLRenderer renderer)
+ {
+ Initialize();
+
+ _renderer = renderer;
+ }
+
+ public void Dispose()
+ {
+ if (_shaderProgram != 0)
+ {
+ GL.DeleteProgram(_shaderProgram);
+ _textureStorage?.Dispose();
+ }
+ }
+
+ private void Initialize()
+ {
+ _shaderProgram = ShaderHelper.CompileProgram(EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/fxaa.glsl"), ShaderType.ComputeShader);
+
+ _resolutionUniform = GL.GetUniformLocation(_shaderProgram, "invResolution");
+ _inputUniform = GL.GetUniformLocation(_shaderProgram, "inputTexture");
+ _outputUniform = GL.GetUniformLocation(_shaderProgram, "imgOutput");
+ }
+
+ public TextureView Run(TextureView view, int width, int height)
+ {
+ if (_textureStorage == null || _textureStorage.Info.Width != view.Width || _textureStorage.Info.Height != view.Height)
+ {
+ _textureStorage?.Dispose();
+ _textureStorage = new TextureStorage(_renderer, view.Info, view.ScaleFactor);
+ _textureStorage.CreateDefaultView();
+ }
+
+ var textureView = _textureStorage.CreateView(view.Info, 0, 0) as TextureView;
+
+ int previousProgram = GL.GetInteger(GetPName.CurrentProgram);
+ int previousUnit = GL.GetInteger(GetPName.ActiveTexture);
+ GL.ActiveTexture(TextureUnit.Texture0);
+ int previousTextureBinding = GL.GetInteger(GetPName.TextureBinding2D);
+
+ GL.BindImageTexture(0, textureView.Handle, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ GL.UseProgram(_shaderProgram);
+
+ var dispatchX = BitUtils.DivRoundUp(view.Width, IPostProcessingEffect.LocalGroupSize);
+ var dispatchY = BitUtils.DivRoundUp(view.Height, IPostProcessingEffect.LocalGroupSize);
+
+ view.Bind(0);
+ GL.Uniform1(_inputUniform, 0);
+ GL.Uniform1(_outputUniform, 0);
+ GL.Uniform2(_resolutionUniform, (float)view.Width, (float)view.Height);
+ GL.DispatchCompute(dispatchX, dispatchY, 1);
+ GL.UseProgram(previousProgram);
+ GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrierBit);
+
+ (_renderer.Pipeline as Pipeline).RestoreImages1And2();
+
+ GL.ActiveTexture(TextureUnit.Texture0);
+ GL.BindTexture(TextureTarget.Texture2D, previousTextureBinding);
+
+ GL.ActiveTexture((TextureUnit)previousUnit);
+
+ return textureView;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/IPostProcessingEffect.cs b/src/Ryujinx.Graphics.OpenGL/Effects/IPostProcessingEffect.cs
new file mode 100644
index 00000000..7a045a02
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/IPostProcessingEffect.cs
@@ -0,0 +1,11 @@
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Effects
+{
+ internal interface IPostProcessingEffect : IDisposable
+ {
+ const int LocalGroupSize = 64;
+ TextureView Run(TextureView view, int width, int height);
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/IScalingFilter.cs b/src/Ryujinx.Graphics.OpenGL/Effects/IScalingFilter.cs
new file mode 100644
index 00000000..e1e1b2c1
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/IScalingFilter.cs
@@ -0,0 +1,18 @@
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Effects
+{
+ internal interface IScalingFilter : IDisposable
+ {
+ float Level { get; set; }
+ void Run(
+ TextureView view,
+ TextureView destinationTexture,
+ int width,
+ int height,
+ Extents2D source,
+ Extents2D destination);
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/ShaderHelper.cs b/src/Ryujinx.Graphics.OpenGL/Effects/ShaderHelper.cs
new file mode 100644
index 00000000..72c5a98f
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/ShaderHelper.cs
@@ -0,0 +1,40 @@
+using OpenTK.Graphics.OpenGL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Effects
+{
+ internal static class ShaderHelper
+ {
+ public static int CompileProgram(string shaderCode, ShaderType shaderType)
+ {
+ var shader = GL.CreateShader(shaderType);
+ GL.ShaderSource(shader, shaderCode);
+ GL.CompileShader(shader);
+
+ var program = GL.CreateProgram();
+ GL.AttachShader(program, shader);
+ GL.LinkProgram(program);
+
+ GL.DetachShader(program, shader);
+ GL.DeleteShader(shader);
+
+ return program;
+ }
+
+ public static int CompileProgram(string[] shaders, ShaderType shaderType)
+ {
+ var shader = GL.CreateShader(shaderType);
+ GL.ShaderSource(shader, shaders.Length, shaders, (int[])null);
+ GL.CompileShader(shader);
+
+ var program = GL.CreateProgram();
+ GL.AttachShader(program, shader);
+ GL.LinkProgram(program);
+
+ GL.DetachShader(program, shader);
+ GL.DeleteShader(shader);
+
+ return program;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_a.h b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_a.h
new file mode 100644
index 00000000..d04bff55
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_a.h
@@ -0,0 +1,2656 @@
+//==============================================================================================================================
+//
+// [A] SHADER PORTABILITY 1.20210629
+//
+//==============================================================================================================================
+// FidelityFX Super Resolution Sample
+//
+// Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files(the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions :
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//------------------------------------------------------------------------------------------------------------------------------
+// MIT LICENSE
+// ===========
+// Copyright (c) 2014 Michal Drobot (for concepts used in "FLOAT APPROXIMATIONS").
+// -----------
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
+// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+// -----------
+// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+// Software.
+// -----------
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//------------------------------------------------------------------------------------------------------------------------------
+// ABOUT
+// =====
+// Common central point for high-level shading language and C portability for various shader headers.
+//------------------------------------------------------------------------------------------------------------------------------
+// DEFINES
+// =======
+// A_CPU ..... Include the CPU related code.
+// A_GPU ..... Include the GPU related code.
+// A_GLSL .... Using GLSL.
+// A_HLSL .... Using HLSL.
+// A_HLSL_6_2 Using HLSL 6.2 with new 'uint16_t' and related types (requires '-enable-16bit-types').
+// A_NO_16_BIT_CAST Don't use instructions that are not availabe in SPIR-V (needed for running A_HLSL_6_2 on Vulkan)
+// A_GCC ..... Using a GCC compatible compiler (else assume MSVC compatible compiler by default).
+// =======
+// A_BYTE .... Support 8-bit integer.
+// A_HALF .... Support 16-bit integer and floating point.
+// A_LONG .... Support 64-bit integer.
+// A_DUBL .... Support 64-bit floating point.
+// =======
+// A_WAVE .... Support wave-wide operations.
+//------------------------------------------------------------------------------------------------------------------------------
+// To get #include "ffx_a.h" working in GLSL use '#extension GL_GOOGLE_include_directive:require'.
+//------------------------------------------------------------------------------------------------------------------------------
+// SIMPLIFIED TYPE SYSTEM
+// ======================
+// - All ints will be unsigned with exception of when signed is required.
+// - Type naming simplified and shortened "A<type><#components>",
+// - H = 16-bit float (half)
+// - F = 32-bit float (float)
+// - D = 64-bit float (double)
+// - P = 1-bit integer (predicate, not using bool because 'B' is used for byte)
+// - B = 8-bit integer (byte)
+// - W = 16-bit integer (word)
+// - U = 32-bit integer (unsigned)
+// - L = 64-bit integer (long)
+// - Using "AS<type><#components>" for signed when required.
+//------------------------------------------------------------------------------------------------------------------------------
+// TODO
+// ====
+// - Make sure 'ALerp*(a,b,m)' does 'b*m+(-a*m+a)' (2 ops).
+//------------------------------------------------------------------------------------------------------------------------------
+// CHANGE LOG
+// ==========
+// 20200914 - Expanded wave ops and prx code.
+// 20200713 - Added [ZOL] section, fixed serious bugs in sRGB and Rec.709 color conversion code, etc.
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// COMMON
+//==============================================================================================================================
+#define A_2PI 6.28318530718
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// CPU
+//
+//
+//==============================================================================================================================
+#ifdef A_CPU
+ // Supporting user defined overrides.
+ #ifndef A_RESTRICT
+ #define A_RESTRICT __restrict
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifndef A_STATIC
+ #define A_STATIC static
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ // Same types across CPU and GPU.
+ // Predicate uses 32-bit integer (C friendly bool).
+ typedef uint32_t AP1;
+ typedef float AF1;
+ typedef double AD1;
+ typedef uint8_t AB1;
+ typedef uint16_t AW1;
+ typedef uint32_t AU1;
+ typedef uint64_t AL1;
+ typedef int8_t ASB1;
+ typedef int16_t ASW1;
+ typedef int32_t ASU1;
+ typedef int64_t ASL1;
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AD1_(a) ((AD1)(a))
+ #define AF1_(a) ((AF1)(a))
+ #define AL1_(a) ((AL1)(a))
+ #define AU1_(a) ((AU1)(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASL1_(a) ((ASL1)(a))
+ #define ASU1_(a) ((ASU1)(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AU1 AU1_AF1(AF1 a){union{AF1 f;AU1 u;}bits;bits.f=a;return bits.u;}
+//------------------------------------------------------------------------------------------------------------------------------
+ #define A_TRUE 1
+ #define A_FALSE 0
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// CPU/GPU PORTING
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// Get CPU and GPU to share all setup code, without duplicate code paths.
+// This uses a lower-case prefix for special vector constructs.
+// - In C restrict pointers are used.
+// - In the shading language, in/inout/out arguments are used.
+// This depends on the ability to access a vector value in both languages via array syntax (aka color[2]).
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// VECTOR ARGUMENT/RETURN/INITIALIZATION PORTABILITY
+//==============================================================================================================================
+ #define retAD2 AD1 *A_RESTRICT
+ #define retAD3 AD1 *A_RESTRICT
+ #define retAD4 AD1 *A_RESTRICT
+ #define retAF2 AF1 *A_RESTRICT
+ #define retAF3 AF1 *A_RESTRICT
+ #define retAF4 AF1 *A_RESTRICT
+ #define retAL2 AL1 *A_RESTRICT
+ #define retAL3 AL1 *A_RESTRICT
+ #define retAL4 AL1 *A_RESTRICT
+ #define retAU2 AU1 *A_RESTRICT
+ #define retAU3 AU1 *A_RESTRICT
+ #define retAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define inAD2 AD1 *A_RESTRICT
+ #define inAD3 AD1 *A_RESTRICT
+ #define inAD4 AD1 *A_RESTRICT
+ #define inAF2 AF1 *A_RESTRICT
+ #define inAF3 AF1 *A_RESTRICT
+ #define inAF4 AF1 *A_RESTRICT
+ #define inAL2 AL1 *A_RESTRICT
+ #define inAL3 AL1 *A_RESTRICT
+ #define inAL4 AL1 *A_RESTRICT
+ #define inAU2 AU1 *A_RESTRICT
+ #define inAU3 AU1 *A_RESTRICT
+ #define inAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define inoutAD2 AD1 *A_RESTRICT
+ #define inoutAD3 AD1 *A_RESTRICT
+ #define inoutAD4 AD1 *A_RESTRICT
+ #define inoutAF2 AF1 *A_RESTRICT
+ #define inoutAF3 AF1 *A_RESTRICT
+ #define inoutAF4 AF1 *A_RESTRICT
+ #define inoutAL2 AL1 *A_RESTRICT
+ #define inoutAL3 AL1 *A_RESTRICT
+ #define inoutAL4 AL1 *A_RESTRICT
+ #define inoutAU2 AU1 *A_RESTRICT
+ #define inoutAU3 AU1 *A_RESTRICT
+ #define inoutAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define outAD2 AD1 *A_RESTRICT
+ #define outAD3 AD1 *A_RESTRICT
+ #define outAD4 AD1 *A_RESTRICT
+ #define outAF2 AF1 *A_RESTRICT
+ #define outAF3 AF1 *A_RESTRICT
+ #define outAF4 AF1 *A_RESTRICT
+ #define outAL2 AL1 *A_RESTRICT
+ #define outAL3 AL1 *A_RESTRICT
+ #define outAL4 AL1 *A_RESTRICT
+ #define outAU2 AU1 *A_RESTRICT
+ #define outAU3 AU1 *A_RESTRICT
+ #define outAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define varAD2(x) AD1 x[2]
+ #define varAD3(x) AD1 x[3]
+ #define varAD4(x) AD1 x[4]
+ #define varAF2(x) AF1 x[2]
+ #define varAF3(x) AF1 x[3]
+ #define varAF4(x) AF1 x[4]
+ #define varAL2(x) AL1 x[2]
+ #define varAL3(x) AL1 x[3]
+ #define varAL4(x) AL1 x[4]
+ #define varAU2(x) AU1 x[2]
+ #define varAU3(x) AU1 x[3]
+ #define varAU4(x) AU1 x[4]
+//------------------------------------------------------------------------------------------------------------------------------
+ #define initAD2(x,y) {x,y}
+ #define initAD3(x,y,z) {x,y,z}
+ #define initAD4(x,y,z,w) {x,y,z,w}
+ #define initAF2(x,y) {x,y}
+ #define initAF3(x,y,z) {x,y,z}
+ #define initAF4(x,y,z,w) {x,y,z,w}
+ #define initAL2(x,y) {x,y}
+ #define initAL3(x,y,z) {x,y,z}
+ #define initAL4(x,y,z,w) {x,y,z,w}
+ #define initAU2(x,y) {x,y}
+ #define initAU3(x,y,z) {x,y,z}
+ #define initAU4(x,y,z,w) {x,y,z,w}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// SCALAR RETURN OPS
+//------------------------------------------------------------------------------------------------------------------------------
+// TODO
+// ====
+// - Replace transcendentals with manual versions.
+//==============================================================================================================================
+ #ifdef A_GCC
+ A_STATIC AD1 AAbsD1(AD1 a){return __builtin_fabs(a);}
+ A_STATIC AF1 AAbsF1(AF1 a){return __builtin_fabsf(a);}
+ A_STATIC AU1 AAbsSU1(AU1 a){return AU1_(__builtin_abs(ASU1_(a)));}
+ A_STATIC AL1 AAbsSL1(AL1 a){return AL1_(__builtin_llabs(ASL1_(a)));}
+ #else
+ A_STATIC AD1 AAbsD1(AD1 a){return fabs(a);}
+ A_STATIC AF1 AAbsF1(AF1 a){return fabsf(a);}
+ A_STATIC AU1 AAbsSU1(AU1 a){return AU1_(abs(ASU1_(a)));}
+ A_STATIC AL1 AAbsSL1(AL1 a){return AL1_(labs((long)ASL1_(a)));}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 ACosD1(AD1 a){return __builtin_cos(a);}
+ A_STATIC AF1 ACosF1(AF1 a){return __builtin_cosf(a);}
+ #else
+ A_STATIC AD1 ACosD1(AD1 a){return cos(a);}
+ A_STATIC AF1 ACosF1(AF1 a){return cosf(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ADotD2(inAD2 a,inAD2 b){return a[0]*b[0]+a[1]*b[1];}
+ A_STATIC AD1 ADotD3(inAD3 a,inAD3 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
+ A_STATIC AD1 ADotD4(inAD4 a,inAD4 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3];}
+ A_STATIC AF1 ADotF2(inAF2 a,inAF2 b){return a[0]*b[0]+a[1]*b[1];}
+ A_STATIC AF1 ADotF3(inAF3 a,inAF3 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
+ A_STATIC AF1 ADotF4(inAF4 a,inAF4 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3];}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 AExp2D1(AD1 a){return __builtin_exp2(a);}
+ A_STATIC AF1 AExp2F1(AF1 a){return __builtin_exp2f(a);}
+ #else
+ A_STATIC AD1 AExp2D1(AD1 a){return exp2(a);}
+ A_STATIC AF1 AExp2F1(AF1 a){return exp2f(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 AFloorD1(AD1 a){return __builtin_floor(a);}
+ A_STATIC AF1 AFloorF1(AF1 a){return __builtin_floorf(a);}
+ #else
+ A_STATIC AD1 AFloorD1(AD1 a){return floor(a);}
+ A_STATIC AF1 AFloorF1(AF1 a){return floorf(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ALerpD1(AD1 a,AD1 b,AD1 c){return b*c+(-a*c+a);}
+ A_STATIC AF1 ALerpF1(AF1 a,AF1 b,AF1 c){return b*c+(-a*c+a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 ALog2D1(AD1 a){return __builtin_log2(a);}
+ A_STATIC AF1 ALog2F1(AF1 a){return __builtin_log2f(a);}
+ #else
+ A_STATIC AD1 ALog2D1(AD1 a){return log2(a);}
+ A_STATIC AF1 ALog2F1(AF1 a){return log2f(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 AMaxD1(AD1 a,AD1 b){return a>b?a:b;}
+ A_STATIC AF1 AMaxF1(AF1 a,AF1 b){return a>b?a:b;}
+ A_STATIC AL1 AMaxL1(AL1 a,AL1 b){return a>b?a:b;}
+ A_STATIC AU1 AMaxU1(AU1 a,AU1 b){return a>b?a:b;}
+//------------------------------------------------------------------------------------------------------------------------------
+ // These follow the convention that A integer types don't have signage, until they are operated on.
+ A_STATIC AL1 AMaxSL1(AL1 a,AL1 b){return (ASL1_(a)>ASL1_(b))?a:b;}
+ A_STATIC AU1 AMaxSU1(AU1 a,AU1 b){return (ASU1_(a)>ASU1_(b))?a:b;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 AMinD1(AD1 a,AD1 b){return a<b?a:b;}
+ A_STATIC AF1 AMinF1(AF1 a,AF1 b){return a<b?a:b;}
+ A_STATIC AL1 AMinL1(AL1 a,AL1 b){return a<b?a:b;}
+ A_STATIC AU1 AMinU1(AU1 a,AU1 b){return a<b?a:b;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AL1 AMinSL1(AL1 a,AL1 b){return (ASL1_(a)<ASL1_(b))?a:b;}
+ A_STATIC AU1 AMinSU1(AU1 a,AU1 b){return (ASU1_(a)<ASU1_(b))?a:b;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ARcpD1(AD1 a){return 1.0/a;}
+ A_STATIC AF1 ARcpF1(AF1 a){return 1.0f/a;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AL1 AShrSL1(AL1 a,AL1 b){return AL1_(ASL1_(a)>>ASL1_(b));}
+ A_STATIC AU1 AShrSU1(AU1 a,AU1 b){return AU1_(ASU1_(a)>>ASU1_(b));}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 ASinD1(AD1 a){return __builtin_sin(a);}
+ A_STATIC AF1 ASinF1(AF1 a){return __builtin_sinf(a);}
+ #else
+ A_STATIC AD1 ASinD1(AD1 a){return sin(a);}
+ A_STATIC AF1 ASinF1(AF1 a){return sinf(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 ASqrtD1(AD1 a){return __builtin_sqrt(a);}
+ A_STATIC AF1 ASqrtF1(AF1 a){return __builtin_sqrtf(a);}
+ #else
+ A_STATIC AD1 ASqrtD1(AD1 a){return sqrt(a);}
+ A_STATIC AF1 ASqrtF1(AF1 a){return sqrtf(a);}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// SCALAR RETURN OPS - DEPENDENT
+//==============================================================================================================================
+ A_STATIC AD1 AClampD1(AD1 x,AD1 n,AD1 m){return AMaxD1(n,AMinD1(x,m));}
+ A_STATIC AF1 AClampF1(AF1 x,AF1 n,AF1 m){return AMaxF1(n,AMinF1(x,m));}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 AFractD1(AD1 a){return a-AFloorD1(a);}
+ A_STATIC AF1 AFractF1(AF1 a){return a-AFloorF1(a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 APowD1(AD1 a,AD1 b){return AExp2D1(b*ALog2D1(a));}
+ A_STATIC AF1 APowF1(AF1 a,AF1 b){return AExp2F1(b*ALog2F1(a));}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ARsqD1(AD1 a){return ARcpD1(ASqrtD1(a));}
+ A_STATIC AF1 ARsqF1(AF1 a){return ARcpF1(ASqrtF1(a));}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ASatD1(AD1 a){return AMinD1(1.0,AMaxD1(0.0,a));}
+ A_STATIC AF1 ASatF1(AF1 a){return AMinF1(1.0f,AMaxF1(0.0f,a));}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// VECTOR OPS
+//------------------------------------------------------------------------------------------------------------------------------
+// These are added as needed for production or prototyping, so not necessarily a complete set.
+// They follow a convention of taking in a destination and also returning the destination value to increase utility.
+//==============================================================================================================================
+ A_STATIC retAD2 opAAbsD2(outAD2 d,inAD2 a){d[0]=AAbsD1(a[0]);d[1]=AAbsD1(a[1]);return d;}
+ A_STATIC retAD3 opAAbsD3(outAD3 d,inAD3 a){d[0]=AAbsD1(a[0]);d[1]=AAbsD1(a[1]);d[2]=AAbsD1(a[2]);return d;}
+ A_STATIC retAD4 opAAbsD4(outAD4 d,inAD4 a){d[0]=AAbsD1(a[0]);d[1]=AAbsD1(a[1]);d[2]=AAbsD1(a[2]);d[3]=AAbsD1(a[3]);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAAbsF2(outAF2 d,inAF2 a){d[0]=AAbsF1(a[0]);d[1]=AAbsF1(a[1]);return d;}
+ A_STATIC retAF3 opAAbsF3(outAF3 d,inAF3 a){d[0]=AAbsF1(a[0]);d[1]=AAbsF1(a[1]);d[2]=AAbsF1(a[2]);return d;}
+ A_STATIC retAF4 opAAbsF4(outAF4 d,inAF4 a){d[0]=AAbsF1(a[0]);d[1]=AAbsF1(a[1]);d[2]=AAbsF1(a[2]);d[3]=AAbsF1(a[3]);return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opAAddD2(outAD2 d,inAD2 a,inAD2 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];return d;}
+ A_STATIC retAD3 opAAddD3(outAD3 d,inAD3 a,inAD3 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];return d;}
+ A_STATIC retAD4 opAAddD4(outAD4 d,inAD4 a,inAD4 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];d[3]=a[3]+b[3];return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAAddF2(outAF2 d,inAF2 a,inAF2 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];return d;}
+ A_STATIC retAF3 opAAddF3(outAF3 d,inAF3 a,inAF3 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];return d;}
+ A_STATIC retAF4 opAAddF4(outAF4 d,inAF4 a,inAF4 b){d[0]=a[0]+b[0];d[1]=a[1]+b[1];d[2]=a[2]+b[2];d[3]=a[3]+b[3];return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opAAddOneD2(outAD2 d,inAD2 a,AD1 b){d[0]=a[0]+b;d[1]=a[1]+b;return d;}
+ A_STATIC retAD3 opAAddOneD3(outAD3 d,inAD3 a,AD1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;return d;}
+ A_STATIC retAD4 opAAddOneD4(outAD4 d,inAD4 a,AD1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;d[3]=a[3]+b;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAAddOneF2(outAF2 d,inAF2 a,AF1 b){d[0]=a[0]+b;d[1]=a[1]+b;return d;}
+ A_STATIC retAF3 opAAddOneF3(outAF3 d,inAF3 a,AF1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;return d;}
+ A_STATIC retAF4 opAAddOneF4(outAF4 d,inAF4 a,AF1 b){d[0]=a[0]+b;d[1]=a[1]+b;d[2]=a[2]+b;d[3]=a[3]+b;return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opACpyD2(outAD2 d,inAD2 a){d[0]=a[0];d[1]=a[1];return d;}
+ A_STATIC retAD3 opACpyD3(outAD3 d,inAD3 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];return d;}
+ A_STATIC retAD4 opACpyD4(outAD4 d,inAD4 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];d[3]=a[3];return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opACpyF2(outAF2 d,inAF2 a){d[0]=a[0];d[1]=a[1];return d;}
+ A_STATIC retAF3 opACpyF3(outAF3 d,inAF3 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];return d;}
+ A_STATIC retAF4 opACpyF4(outAF4 d,inAF4 a){d[0]=a[0];d[1]=a[1];d[2]=a[2];d[3]=a[3];return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opALerpD2(outAD2 d,inAD2 a,inAD2 b,inAD2 c){d[0]=ALerpD1(a[0],b[0],c[0]);d[1]=ALerpD1(a[1],b[1],c[1]);return d;}
+ A_STATIC retAD3 opALerpD3(outAD3 d,inAD3 a,inAD3 b,inAD3 c){d[0]=ALerpD1(a[0],b[0],c[0]);d[1]=ALerpD1(a[1],b[1],c[1]);d[2]=ALerpD1(a[2],b[2],c[2]);return d;}
+ A_STATIC retAD4 opALerpD4(outAD4 d,inAD4 a,inAD4 b,inAD4 c){d[0]=ALerpD1(a[0],b[0],c[0]);d[1]=ALerpD1(a[1],b[1],c[1]);d[2]=ALerpD1(a[2],b[2],c[2]);d[3]=ALerpD1(a[3],b[3],c[3]);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opALerpF2(outAF2 d,inAF2 a,inAF2 b,inAF2 c){d[0]=ALerpF1(a[0],b[0],c[0]);d[1]=ALerpF1(a[1],b[1],c[1]);return d;}
+ A_STATIC retAF3 opALerpF3(outAF3 d,inAF3 a,inAF3 b,inAF3 c){d[0]=ALerpF1(a[0],b[0],c[0]);d[1]=ALerpF1(a[1],b[1],c[1]);d[2]=ALerpF1(a[2],b[2],c[2]);return d;}
+ A_STATIC retAF4 opALerpF4(outAF4 d,inAF4 a,inAF4 b,inAF4 c){d[0]=ALerpF1(a[0],b[0],c[0]);d[1]=ALerpF1(a[1],b[1],c[1]);d[2]=ALerpF1(a[2],b[2],c[2]);d[3]=ALerpF1(a[3],b[3],c[3]);return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opALerpOneD2(outAD2 d,inAD2 a,inAD2 b,AD1 c){d[0]=ALerpD1(a[0],b[0],c);d[1]=ALerpD1(a[1],b[1],c);return d;}
+ A_STATIC retAD3 opALerpOneD3(outAD3 d,inAD3 a,inAD3 b,AD1 c){d[0]=ALerpD1(a[0],b[0],c);d[1]=ALerpD1(a[1],b[1],c);d[2]=ALerpD1(a[2],b[2],c);return d;}
+ A_STATIC retAD4 opALerpOneD4(outAD4 d,inAD4 a,inAD4 b,AD1 c){d[0]=ALerpD1(a[0],b[0],c);d[1]=ALerpD1(a[1],b[1],c);d[2]=ALerpD1(a[2],b[2],c);d[3]=ALerpD1(a[3],b[3],c);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opALerpOneF2(outAF2 d,inAF2 a,inAF2 b,AF1 c){d[0]=ALerpF1(a[0],b[0],c);d[1]=ALerpF1(a[1],b[1],c);return d;}
+ A_STATIC retAF3 opALerpOneF3(outAF3 d,inAF3 a,inAF3 b,AF1 c){d[0]=ALerpF1(a[0],b[0],c);d[1]=ALerpF1(a[1],b[1],c);d[2]=ALerpF1(a[2],b[2],c);return d;}
+ A_STATIC retAF4 opALerpOneF4(outAF4 d,inAF4 a,inAF4 b,AF1 c){d[0]=ALerpF1(a[0],b[0],c);d[1]=ALerpF1(a[1],b[1],c);d[2]=ALerpF1(a[2],b[2],c);d[3]=ALerpF1(a[3],b[3],c);return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opAMaxD2(outAD2 d,inAD2 a,inAD2 b){d[0]=AMaxD1(a[0],b[0]);d[1]=AMaxD1(a[1],b[1]);return d;}
+ A_STATIC retAD3 opAMaxD3(outAD3 d,inAD3 a,inAD3 b){d[0]=AMaxD1(a[0],b[0]);d[1]=AMaxD1(a[1],b[1]);d[2]=AMaxD1(a[2],b[2]);return d;}
+ A_STATIC retAD4 opAMaxD4(outAD4 d,inAD4 a,inAD4 b){d[0]=AMaxD1(a[0],b[0]);d[1]=AMaxD1(a[1],b[1]);d[2]=AMaxD1(a[2],b[2]);d[3]=AMaxD1(a[3],b[3]);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAMaxF2(outAF2 d,inAF2 a,inAF2 b){d[0]=AMaxF1(a[0],b[0]);d[1]=AMaxF1(a[1],b[1]);return d;}
+ A_STATIC retAF3 opAMaxF3(outAF3 d,inAF3 a,inAF3 b){d[0]=AMaxF1(a[0],b[0]);d[1]=AMaxF1(a[1],b[1]);d[2]=AMaxF1(a[2],b[2]);return d;}
+ A_STATIC retAF4 opAMaxF4(outAF4 d,inAF4 a,inAF4 b){d[0]=AMaxF1(a[0],b[0]);d[1]=AMaxF1(a[1],b[1]);d[2]=AMaxF1(a[2],b[2]);d[3]=AMaxF1(a[3],b[3]);return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opAMinD2(outAD2 d,inAD2 a,inAD2 b){d[0]=AMinD1(a[0],b[0]);d[1]=AMinD1(a[1],b[1]);return d;}
+ A_STATIC retAD3 opAMinD3(outAD3 d,inAD3 a,inAD3 b){d[0]=AMinD1(a[0],b[0]);d[1]=AMinD1(a[1],b[1]);d[2]=AMinD1(a[2],b[2]);return d;}
+ A_STATIC retAD4 opAMinD4(outAD4 d,inAD4 a,inAD4 b){d[0]=AMinD1(a[0],b[0]);d[1]=AMinD1(a[1],b[1]);d[2]=AMinD1(a[2],b[2]);d[3]=AMinD1(a[3],b[3]);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAMinF2(outAF2 d,inAF2 a,inAF2 b){d[0]=AMinF1(a[0],b[0]);d[1]=AMinF1(a[1],b[1]);return d;}
+ A_STATIC retAF3 opAMinF3(outAF3 d,inAF3 a,inAF3 b){d[0]=AMinF1(a[0],b[0]);d[1]=AMinF1(a[1],b[1]);d[2]=AMinF1(a[2],b[2]);return d;}
+ A_STATIC retAF4 opAMinF4(outAF4 d,inAF4 a,inAF4 b){d[0]=AMinF1(a[0],b[0]);d[1]=AMinF1(a[1],b[1]);d[2]=AMinF1(a[2],b[2]);d[3]=AMinF1(a[3],b[3]);return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opAMulD2(outAD2 d,inAD2 a,inAD2 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];return d;}
+ A_STATIC retAD3 opAMulD3(outAD3 d,inAD3 a,inAD3 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];return d;}
+ A_STATIC retAD4 opAMulD4(outAD4 d,inAD4 a,inAD4 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];d[3]=a[3]*b[3];return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAMulF2(outAF2 d,inAF2 a,inAF2 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];return d;}
+ A_STATIC retAF3 opAMulF3(outAF3 d,inAF3 a,inAF3 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];return d;}
+ A_STATIC retAF4 opAMulF4(outAF4 d,inAF4 a,inAF4 b){d[0]=a[0]*b[0];d[1]=a[1]*b[1];d[2]=a[2]*b[2];d[3]=a[3]*b[3];return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opAMulOneD2(outAD2 d,inAD2 a,AD1 b){d[0]=a[0]*b;d[1]=a[1]*b;return d;}
+ A_STATIC retAD3 opAMulOneD3(outAD3 d,inAD3 a,AD1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;return d;}
+ A_STATIC retAD4 opAMulOneD4(outAD4 d,inAD4 a,AD1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;d[3]=a[3]*b;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opAMulOneF2(outAF2 d,inAF2 a,AF1 b){d[0]=a[0]*b;d[1]=a[1]*b;return d;}
+ A_STATIC retAF3 opAMulOneF3(outAF3 d,inAF3 a,AF1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;return d;}
+ A_STATIC retAF4 opAMulOneF4(outAF4 d,inAF4 a,AF1 b){d[0]=a[0]*b;d[1]=a[1]*b;d[2]=a[2]*b;d[3]=a[3]*b;return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opANegD2(outAD2 d,inAD2 a){d[0]=-a[0];d[1]=-a[1];return d;}
+ A_STATIC retAD3 opANegD3(outAD3 d,inAD3 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];return d;}
+ A_STATIC retAD4 opANegD4(outAD4 d,inAD4 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];d[3]=-a[3];return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opANegF2(outAF2 d,inAF2 a){d[0]=-a[0];d[1]=-a[1];return d;}
+ A_STATIC retAF3 opANegF3(outAF3 d,inAF3 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];return d;}
+ A_STATIC retAF4 opANegF4(outAF4 d,inAF4 a){d[0]=-a[0];d[1]=-a[1];d[2]=-a[2];d[3]=-a[3];return d;}
+//==============================================================================================================================
+ A_STATIC retAD2 opARcpD2(outAD2 d,inAD2 a){d[0]=ARcpD1(a[0]);d[1]=ARcpD1(a[1]);return d;}
+ A_STATIC retAD3 opARcpD3(outAD3 d,inAD3 a){d[0]=ARcpD1(a[0]);d[1]=ARcpD1(a[1]);d[2]=ARcpD1(a[2]);return d;}
+ A_STATIC retAD4 opARcpD4(outAD4 d,inAD4 a){d[0]=ARcpD1(a[0]);d[1]=ARcpD1(a[1]);d[2]=ARcpD1(a[2]);d[3]=ARcpD1(a[3]);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC retAF2 opARcpF2(outAF2 d,inAF2 a){d[0]=ARcpF1(a[0]);d[1]=ARcpF1(a[1]);return d;}
+ A_STATIC retAF3 opARcpF3(outAF3 d,inAF3 a){d[0]=ARcpF1(a[0]);d[1]=ARcpF1(a[1]);d[2]=ARcpF1(a[2]);return d;}
+ A_STATIC retAF4 opARcpF4(outAF4 d,inAF4 a){d[0]=ARcpF1(a[0]);d[1]=ARcpF1(a[1]);d[2]=ARcpF1(a[2]);d[3]=ARcpF1(a[3]);return d;}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// HALF FLOAT PACKING
+//==============================================================================================================================
+ // Convert float to half (in lower 16-bits of output).
+ // Same fast technique as documented here: ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf
+ // Supports denormals.
+ // Conversion rules are to make computations possibly "safer" on the GPU,
+ // -INF & -NaN -> -65504
+ // +INF & +NaN -> +65504
+ A_STATIC AU1 AU1_AH1_AF1(AF1 f){
+ static AW1 base[512]={
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+ 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,
+ 0x0200,0x0400,0x0800,0x0c00,0x1000,0x1400,0x1800,0x1c00,0x2000,0x2400,0x2800,0x2c00,0x3000,0x3400,0x3800,0x3c00,
+ 0x4000,0x4400,0x4800,0x4c00,0x5000,0x5400,0x5800,0x5c00,0x6000,0x6400,0x6800,0x6c00,0x7000,0x7400,0x7800,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,0x7bff,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
+ 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8001,0x8002,0x8004,0x8008,0x8010,0x8020,0x8040,0x8080,0x8100,
+ 0x8200,0x8400,0x8800,0x8c00,0x9000,0x9400,0x9800,0x9c00,0xa000,0xa400,0xa800,0xac00,0xb000,0xb400,0xb800,0xbc00,
+ 0xc000,0xc400,0xc800,0xcc00,0xd000,0xd400,0xd800,0xdc00,0xe000,0xe400,0xe800,0xec00,0xf000,0xf400,0xf800,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,
+ 0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff,0xfbff};
+ static AB1 shift[512]={
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,0x0f,
+ 0x0e,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,
+ 0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,0x0f,
+ 0x0e,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,
+ 0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
+ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18};
+ union{AF1 f;AU1 u;}bits;bits.f=f;AU1 u=bits.u;AU1 i=u>>23;return (AU1)(base[i])+((u&0x7fffff)>>shift[i]);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Used to output packed constant.
+ A_STATIC AU1 AU1_AH2_AF2(inAF2 a){return AU1_AH1_AF1(a[0])+(AU1_AH1_AF1(a[1])<<16);}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// GLSL
+//
+//
+//==============================================================================================================================
+#if defined(A_GLSL) && defined(A_GPU)
+ #ifndef A_SKIP_EXT
+ #ifdef A_HALF
+ #extension GL_EXT_shader_16bit_storage:require
+ #extension GL_EXT_shader_explicit_arithmetic_types:require
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_LONG
+ #extension GL_ARB_gpu_shader_int64:require
+ #extension GL_NV_shader_atomic_int64:require
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_WAVE
+ #extension GL_KHR_shader_subgroup_arithmetic:require
+ #extension GL_KHR_shader_subgroup_ballot:require
+ #extension GL_KHR_shader_subgroup_quad:require
+ #extension GL_KHR_shader_subgroup_shuffle:require
+ #endif
+ #endif
+//==============================================================================================================================
+ #define AP1 bool
+ #define AP2 bvec2
+ #define AP3 bvec3
+ #define AP4 bvec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AF1 float
+ #define AF2 vec2
+ #define AF3 vec3
+ #define AF4 vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1 uint
+ #define AU2 uvec2
+ #define AU3 uvec3
+ #define AU4 uvec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASU1 int
+ #define ASU2 ivec2
+ #define ASU3 ivec3
+ #define ASU4 ivec4
+//==============================================================================================================================
+ #define AF1_AU1(x) uintBitsToFloat(AU1(x))
+ #define AF2_AU2(x) uintBitsToFloat(AU2(x))
+ #define AF3_AU3(x) uintBitsToFloat(AU3(x))
+ #define AF4_AU4(x) uintBitsToFloat(AU4(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1_AF1(x) floatBitsToUint(AF1(x))
+ #define AU2_AF2(x) floatBitsToUint(AF2(x))
+ #define AU3_AF3(x) floatBitsToUint(AF3(x))
+ #define AU4_AF4(x) floatBitsToUint(AF4(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AU1_AH1_AF1_x(AF1 a){return packHalf2x16(AF2(a,0.0));}
+ #define AU1_AH1_AF1(a) AU1_AH1_AF1_x(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1_AH2_AF2 packHalf2x16
+ #define AU1_AW2Unorm_AF2 packUnorm2x16
+ #define AU1_AB4Unorm_AF4 packUnorm4x8
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AF2_AH2_AU1 unpackHalf2x16
+ #define AF2_AW2Unorm_AU1 unpackUnorm2x16
+ #define AF4_AB4Unorm_AU1 unpackUnorm4x8
+//==============================================================================================================================
+ AF1 AF1_x(AF1 a){return AF1(a);}
+ AF2 AF2_x(AF1 a){return AF2(a,a);}
+ AF3 AF3_x(AF1 a){return AF3(a,a,a);}
+ AF4 AF4_x(AF1 a){return AF4(a,a,a,a);}
+ #define AF1_(a) AF1_x(AF1(a))
+ #define AF2_(a) AF2_x(AF1(a))
+ #define AF3_(a) AF3_x(AF1(a))
+ #define AF4_(a) AF4_x(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AU1_x(AU1 a){return AU1(a);}
+ AU2 AU2_x(AU1 a){return AU2(a,a);}
+ AU3 AU3_x(AU1 a){return AU3(a,a,a);}
+ AU4 AU4_x(AU1 a){return AU4(a,a,a,a);}
+ #define AU1_(a) AU1_x(AU1(a))
+ #define AU2_(a) AU2_x(AU1(a))
+ #define AU3_(a) AU3_x(AU1(a))
+ #define AU4_(a) AU4_x(AU1(a))
+//==============================================================================================================================
+ AU1 AAbsSU1(AU1 a){return AU1(abs(ASU1(a)));}
+ AU2 AAbsSU2(AU2 a){return AU2(abs(ASU2(a)));}
+ AU3 AAbsSU3(AU3 a){return AU3(abs(ASU3(a)));}
+ AU4 AAbsSU4(AU4 a){return AU4(abs(ASU4(a)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 ABfe(AU1 src,AU1 off,AU1 bits){return bitfieldExtract(src,ASU1(off),ASU1(bits));}
+ AU1 ABfi(AU1 src,AU1 ins,AU1 mask){return (ins&mask)|(src&(~mask));}
+ // Proxy for V_BFI_B32 where the 'mask' is set as 'bits', 'mask=(1<<bits)-1', and 'bits' needs to be an immediate.
+ AU1 ABfiM(AU1 src,AU1 ins,AU1 bits){return bitfieldInsert(src,ins,0,ASU1(bits));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // V_MED3_F32.
+ AF1 AClampF1(AF1 x,AF1 n,AF1 m){return clamp(x,n,m);}
+ AF2 AClampF2(AF2 x,AF2 n,AF2 m){return clamp(x,n,m);}
+ AF3 AClampF3(AF3 x,AF3 n,AF3 m){return clamp(x,n,m);}
+ AF4 AClampF4(AF4 x,AF4 n,AF4 m){return clamp(x,n,m);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // V_FRACT_F32 (note DX frac() is different).
+ AF1 AFractF1(AF1 x){return fract(x);}
+ AF2 AFractF2(AF2 x){return fract(x);}
+ AF3 AFractF3(AF3 x){return fract(x);}
+ AF4 AFractF4(AF4 x){return fract(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ALerpF1(AF1 x,AF1 y,AF1 a){return mix(x,y,a);}
+ AF2 ALerpF2(AF2 x,AF2 y,AF2 a){return mix(x,y,a);}
+ AF3 ALerpF3(AF3 x,AF3 y,AF3 a){return mix(x,y,a);}
+ AF4 ALerpF4(AF4 x,AF4 y,AF4 a){return mix(x,y,a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // V_MAX3_F32.
+ AF1 AMax3F1(AF1 x,AF1 y,AF1 z){return max(x,max(y,z));}
+ AF2 AMax3F2(AF2 x,AF2 y,AF2 z){return max(x,max(y,z));}
+ AF3 AMax3F3(AF3 x,AF3 y,AF3 z){return max(x,max(y,z));}
+ AF4 AMax3F4(AF4 x,AF4 y,AF4 z){return max(x,max(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMax3SU1(AU1 x,AU1 y,AU1 z){return AU1(max(ASU1(x),max(ASU1(y),ASU1(z))));}
+ AU2 AMax3SU2(AU2 x,AU2 y,AU2 z){return AU2(max(ASU2(x),max(ASU2(y),ASU2(z))));}
+ AU3 AMax3SU3(AU3 x,AU3 y,AU3 z){return AU3(max(ASU3(x),max(ASU3(y),ASU3(z))));}
+ AU4 AMax3SU4(AU4 x,AU4 y,AU4 z){return AU4(max(ASU4(x),max(ASU4(y),ASU4(z))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMax3U1(AU1 x,AU1 y,AU1 z){return max(x,max(y,z));}
+ AU2 AMax3U2(AU2 x,AU2 y,AU2 z){return max(x,max(y,z));}
+ AU3 AMax3U3(AU3 x,AU3 y,AU3 z){return max(x,max(y,z));}
+ AU4 AMax3U4(AU4 x,AU4 y,AU4 z){return max(x,max(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMaxSU1(AU1 a,AU1 b){return AU1(max(ASU1(a),ASU1(b)));}
+ AU2 AMaxSU2(AU2 a,AU2 b){return AU2(max(ASU2(a),ASU2(b)));}
+ AU3 AMaxSU3(AU3 a,AU3 b){return AU3(max(ASU3(a),ASU3(b)));}
+ AU4 AMaxSU4(AU4 a,AU4 b){return AU4(max(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Clamp has an easier pattern match for med3 when some ordering is known.
+ // V_MED3_F32.
+ AF1 AMed3F1(AF1 x,AF1 y,AF1 z){return max(min(x,y),min(max(x,y),z));}
+ AF2 AMed3F2(AF2 x,AF2 y,AF2 z){return max(min(x,y),min(max(x,y),z));}
+ AF3 AMed3F3(AF3 x,AF3 y,AF3 z){return max(min(x,y),min(max(x,y),z));}
+ AF4 AMed3F4(AF4 x,AF4 y,AF4 z){return max(min(x,y),min(max(x,y),z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // V_MIN3_F32.
+ AF1 AMin3F1(AF1 x,AF1 y,AF1 z){return min(x,min(y,z));}
+ AF2 AMin3F2(AF2 x,AF2 y,AF2 z){return min(x,min(y,z));}
+ AF3 AMin3F3(AF3 x,AF3 y,AF3 z){return min(x,min(y,z));}
+ AF4 AMin3F4(AF4 x,AF4 y,AF4 z){return min(x,min(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMin3SU1(AU1 x,AU1 y,AU1 z){return AU1(min(ASU1(x),min(ASU1(y),ASU1(z))));}
+ AU2 AMin3SU2(AU2 x,AU2 y,AU2 z){return AU2(min(ASU2(x),min(ASU2(y),ASU2(z))));}
+ AU3 AMin3SU3(AU3 x,AU3 y,AU3 z){return AU3(min(ASU3(x),min(ASU3(y),ASU3(z))));}
+ AU4 AMin3SU4(AU4 x,AU4 y,AU4 z){return AU4(min(ASU4(x),min(ASU4(y),ASU4(z))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMin3U1(AU1 x,AU1 y,AU1 z){return min(x,min(y,z));}
+ AU2 AMin3U2(AU2 x,AU2 y,AU2 z){return min(x,min(y,z));}
+ AU3 AMin3U3(AU3 x,AU3 y,AU3 z){return min(x,min(y,z));}
+ AU4 AMin3U4(AU4 x,AU4 y,AU4 z){return min(x,min(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMinSU1(AU1 a,AU1 b){return AU1(min(ASU1(a),ASU1(b)));}
+ AU2 AMinSU2(AU2 a,AU2 b){return AU2(min(ASU2(a),ASU2(b)));}
+ AU3 AMinSU3(AU3 a,AU3 b){return AU3(min(ASU3(a),ASU3(b)));}
+ AU4 AMinSU4(AU4 a,AU4 b){return AU4(min(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Normalized trig. Valid input domain is {-256 to +256}. No GLSL compiler intrinsic exists to map to this currently.
+ // V_COS_F32.
+ AF1 ANCosF1(AF1 x){return cos(x*AF1_(A_2PI));}
+ AF2 ANCosF2(AF2 x){return cos(x*AF2_(A_2PI));}
+ AF3 ANCosF3(AF3 x){return cos(x*AF3_(A_2PI));}
+ AF4 ANCosF4(AF4 x){return cos(x*AF4_(A_2PI));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Normalized trig. Valid input domain is {-256 to +256}. No GLSL compiler intrinsic exists to map to this currently.
+ // V_SIN_F32.
+ AF1 ANSinF1(AF1 x){return sin(x*AF1_(A_2PI));}
+ AF2 ANSinF2(AF2 x){return sin(x*AF2_(A_2PI));}
+ AF3 ANSinF3(AF3 x){return sin(x*AF3_(A_2PI));}
+ AF4 ANSinF4(AF4 x){return sin(x*AF4_(A_2PI));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ARcpF1(AF1 x){return AF1_(1.0)/x;}
+ AF2 ARcpF2(AF2 x){return AF2_(1.0)/x;}
+ AF3 ARcpF3(AF3 x){return AF3_(1.0)/x;}
+ AF4 ARcpF4(AF4 x){return AF4_(1.0)/x;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ARsqF1(AF1 x){return AF1_(1.0)/sqrt(x);}
+ AF2 ARsqF2(AF2 x){return AF2_(1.0)/sqrt(x);}
+ AF3 ARsqF3(AF3 x){return AF3_(1.0)/sqrt(x);}
+ AF4 ARsqF4(AF4 x){return AF4_(1.0)/sqrt(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ASatF1(AF1 x){return clamp(x,AF1_(0.0),AF1_(1.0));}
+ AF2 ASatF2(AF2 x){return clamp(x,AF2_(0.0),AF2_(1.0));}
+ AF3 ASatF3(AF3 x){return clamp(x,AF3_(0.0),AF3_(1.0));}
+ AF4 ASatF4(AF4 x){return clamp(x,AF4_(0.0),AF4_(1.0));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AShrSU1(AU1 a,AU1 b){return AU1(ASU1(a)>>ASU1(b));}
+ AU2 AShrSU2(AU2 a,AU2 b){return AU2(ASU2(a)>>ASU2(b));}
+ AU3 AShrSU3(AU3 a,AU3 b){return AU3(ASU3(a)>>ASU3(b));}
+ AU4 AShrSU4(AU4 a,AU4 b){return AU4(ASU4(a)>>ASU4(b));}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// GLSL BYTE
+//==============================================================================================================================
+ #ifdef A_BYTE
+ #define AB1 uint8_t
+ #define AB2 u8vec2
+ #define AB3 u8vec3
+ #define AB4 u8vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASB1 int8_t
+ #define ASB2 i8vec2
+ #define ASB3 i8vec3
+ #define ASB4 i8vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ AB1 AB1_x(AB1 a){return AB1(a);}
+ AB2 AB2_x(AB1 a){return AB2(a,a);}
+ AB3 AB3_x(AB1 a){return AB3(a,a,a);}
+ AB4 AB4_x(AB1 a){return AB4(a,a,a,a);}
+ #define AB1_(a) AB1_x(AB1(a))
+ #define AB2_(a) AB2_x(AB1(a))
+ #define AB3_(a) AB3_x(AB1(a))
+ #define AB4_(a) AB4_x(AB1(a))
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// GLSL HALF
+//==============================================================================================================================
+ #ifdef A_HALF
+ #define AH1 float16_t
+ #define AH2 f16vec2
+ #define AH3 f16vec3
+ #define AH4 f16vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AW1 uint16_t
+ #define AW2 u16vec2
+ #define AW3 u16vec3
+ #define AW4 u16vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASW1 int16_t
+ #define ASW2 i16vec2
+ #define ASW3 i16vec3
+ #define ASW4 i16vec4
+//==============================================================================================================================
+ #define AH2_AU1(x) unpackFloat2x16(AU1(x))
+ AH4 AH4_AU2_x(AU2 x){return AH4(unpackFloat2x16(x.x),unpackFloat2x16(x.y));}
+ #define AH4_AU2(x) AH4_AU2_x(AU2(x))
+ #define AW2_AU1(x) unpackUint2x16(AU1(x))
+ #define AW4_AU2(x) unpackUint4x16(pack64(AU2(x)))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1_AH2(x) packFloat2x16(AH2(x))
+ AU2 AU2_AH4_x(AH4 x){return AU2(packFloat2x16(x.xy),packFloat2x16(x.zw));}
+ #define AU2_AH4(x) AU2_AH4_x(AH4(x))
+ #define AU1_AW2(x) packUint2x16(AW2(x))
+ #define AU2_AW4(x) unpack32(packUint4x16(AW4(x)))
+//==============================================================================================================================
+ #define AW1_AH1(x) halfBitsToUint16(AH1(x))
+ #define AW2_AH2(x) halfBitsToUint16(AH2(x))
+ #define AW3_AH3(x) halfBitsToUint16(AH3(x))
+ #define AW4_AH4(x) halfBitsToUint16(AH4(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AH1_AW1(x) uint16BitsToHalf(AW1(x))
+ #define AH2_AW2(x) uint16BitsToHalf(AW2(x))
+ #define AH3_AW3(x) uint16BitsToHalf(AW3(x))
+ #define AH4_AW4(x) uint16BitsToHalf(AW4(x))
+//==============================================================================================================================
+ AH1 AH1_x(AH1 a){return AH1(a);}
+ AH2 AH2_x(AH1 a){return AH2(a,a);}
+ AH3 AH3_x(AH1 a){return AH3(a,a,a);}
+ AH4 AH4_x(AH1 a){return AH4(a,a,a,a);}
+ #define AH1_(a) AH1_x(AH1(a))
+ #define AH2_(a) AH2_x(AH1(a))
+ #define AH3_(a) AH3_x(AH1(a))
+ #define AH4_(a) AH4_x(AH1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AW1_x(AW1 a){return AW1(a);}
+ AW2 AW2_x(AW1 a){return AW2(a,a);}
+ AW3 AW3_x(AW1 a){return AW3(a,a,a);}
+ AW4 AW4_x(AW1 a){return AW4(a,a,a,a);}
+ #define AW1_(a) AW1_x(AW1(a))
+ #define AW2_(a) AW2_x(AW1(a))
+ #define AW3_(a) AW3_x(AW1(a))
+ #define AW4_(a) AW4_x(AW1(a))
+//==============================================================================================================================
+ AW1 AAbsSW1(AW1 a){return AW1(abs(ASW1(a)));}
+ AW2 AAbsSW2(AW2 a){return AW2(abs(ASW2(a)));}
+ AW3 AAbsSW3(AW3 a){return AW3(abs(ASW3(a)));}
+ AW4 AAbsSW4(AW4 a){return AW4(abs(ASW4(a)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AClampH1(AH1 x,AH1 n,AH1 m){return clamp(x,n,m);}
+ AH2 AClampH2(AH2 x,AH2 n,AH2 m){return clamp(x,n,m);}
+ AH3 AClampH3(AH3 x,AH3 n,AH3 m){return clamp(x,n,m);}
+ AH4 AClampH4(AH4 x,AH4 n,AH4 m){return clamp(x,n,m);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AFractH1(AH1 x){return fract(x);}
+ AH2 AFractH2(AH2 x){return fract(x);}
+ AH3 AFractH3(AH3 x){return fract(x);}
+ AH4 AFractH4(AH4 x){return fract(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ALerpH1(AH1 x,AH1 y,AH1 a){return mix(x,y,a);}
+ AH2 ALerpH2(AH2 x,AH2 y,AH2 a){return mix(x,y,a);}
+ AH3 ALerpH3(AH3 x,AH3 y,AH3 a){return mix(x,y,a);}
+ AH4 ALerpH4(AH4 x,AH4 y,AH4 a){return mix(x,y,a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // No packed version of max3.
+ AH1 AMax3H1(AH1 x,AH1 y,AH1 z){return max(x,max(y,z));}
+ AH2 AMax3H2(AH2 x,AH2 y,AH2 z){return max(x,max(y,z));}
+ AH3 AMax3H3(AH3 x,AH3 y,AH3 z){return max(x,max(y,z));}
+ AH4 AMax3H4(AH4 x,AH4 y,AH4 z){return max(x,max(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AMaxSW1(AW1 a,AW1 b){return AW1(max(ASU1(a),ASU1(b)));}
+ AW2 AMaxSW2(AW2 a,AW2 b){return AW2(max(ASU2(a),ASU2(b)));}
+ AW3 AMaxSW3(AW3 a,AW3 b){return AW3(max(ASU3(a),ASU3(b)));}
+ AW4 AMaxSW4(AW4 a,AW4 b){return AW4(max(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // No packed version of min3.
+ AH1 AMin3H1(AH1 x,AH1 y,AH1 z){return min(x,min(y,z));}
+ AH2 AMin3H2(AH2 x,AH2 y,AH2 z){return min(x,min(y,z));}
+ AH3 AMin3H3(AH3 x,AH3 y,AH3 z){return min(x,min(y,z));}
+ AH4 AMin3H4(AH4 x,AH4 y,AH4 z){return min(x,min(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AMinSW1(AW1 a,AW1 b){return AW1(min(ASU1(a),ASU1(b)));}
+ AW2 AMinSW2(AW2 a,AW2 b){return AW2(min(ASU2(a),ASU2(b)));}
+ AW3 AMinSW3(AW3 a,AW3 b){return AW3(min(ASU3(a),ASU3(b)));}
+ AW4 AMinSW4(AW4 a,AW4 b){return AW4(min(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ARcpH1(AH1 x){return AH1_(1.0)/x;}
+ AH2 ARcpH2(AH2 x){return AH2_(1.0)/x;}
+ AH3 ARcpH3(AH3 x){return AH3_(1.0)/x;}
+ AH4 ARcpH4(AH4 x){return AH4_(1.0)/x;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ARsqH1(AH1 x){return AH1_(1.0)/sqrt(x);}
+ AH2 ARsqH2(AH2 x){return AH2_(1.0)/sqrt(x);}
+ AH3 ARsqH3(AH3 x){return AH3_(1.0)/sqrt(x);}
+ AH4 ARsqH4(AH4 x){return AH4_(1.0)/sqrt(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ASatH1(AH1 x){return clamp(x,AH1_(0.0),AH1_(1.0));}
+ AH2 ASatH2(AH2 x){return clamp(x,AH2_(0.0),AH2_(1.0));}
+ AH3 ASatH3(AH3 x){return clamp(x,AH3_(0.0),AH3_(1.0));}
+ AH4 ASatH4(AH4 x){return clamp(x,AH4_(0.0),AH4_(1.0));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AShrSW1(AW1 a,AW1 b){return AW1(ASW1(a)>>ASW1(b));}
+ AW2 AShrSW2(AW2 a,AW2 b){return AW2(ASW2(a)>>ASW2(b));}
+ AW3 AShrSW3(AW3 a,AW3 b){return AW3(ASW3(a)>>ASW3(b));}
+ AW4 AShrSW4(AW4 a,AW4 b){return AW4(ASW4(a)>>ASW4(b));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// GLSL DOUBLE
+//==============================================================================================================================
+ #ifdef A_DUBL
+ #define AD1 double
+ #define AD2 dvec2
+ #define AD3 dvec3
+ #define AD4 dvec4
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 AD1_x(AD1 a){return AD1(a);}
+ AD2 AD2_x(AD1 a){return AD2(a,a);}
+ AD3 AD3_x(AD1 a){return AD3(a,a,a);}
+ AD4 AD4_x(AD1 a){return AD4(a,a,a,a);}
+ #define AD1_(a) AD1_x(AD1(a))
+ #define AD2_(a) AD2_x(AD1(a))
+ #define AD3_(a) AD3_x(AD1(a))
+ #define AD4_(a) AD4_x(AD1(a))
+//==============================================================================================================================
+ AD1 AFractD1(AD1 x){return fract(x);}
+ AD2 AFractD2(AD2 x){return fract(x);}
+ AD3 AFractD3(AD3 x){return fract(x);}
+ AD4 AFractD4(AD4 x){return fract(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ALerpD1(AD1 x,AD1 y,AD1 a){return mix(x,y,a);}
+ AD2 ALerpD2(AD2 x,AD2 y,AD2 a){return mix(x,y,a);}
+ AD3 ALerpD3(AD3 x,AD3 y,AD3 a){return mix(x,y,a);}
+ AD4 ALerpD4(AD4 x,AD4 y,AD4 a){return mix(x,y,a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ARcpD1(AD1 x){return AD1_(1.0)/x;}
+ AD2 ARcpD2(AD2 x){return AD2_(1.0)/x;}
+ AD3 ARcpD3(AD3 x){return AD3_(1.0)/x;}
+ AD4 ARcpD4(AD4 x){return AD4_(1.0)/x;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ARsqD1(AD1 x){return AD1_(1.0)/sqrt(x);}
+ AD2 ARsqD2(AD2 x){return AD2_(1.0)/sqrt(x);}
+ AD3 ARsqD3(AD3 x){return AD3_(1.0)/sqrt(x);}
+ AD4 ARsqD4(AD4 x){return AD4_(1.0)/sqrt(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ASatD1(AD1 x){return clamp(x,AD1_(0.0),AD1_(1.0));}
+ AD2 ASatD2(AD2 x){return clamp(x,AD2_(0.0),AD2_(1.0));}
+ AD3 ASatD3(AD3 x){return clamp(x,AD3_(0.0),AD3_(1.0));}
+ AD4 ASatD4(AD4 x){return clamp(x,AD4_(0.0),AD4_(1.0));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// GLSL LONG
+//==============================================================================================================================
+ #ifdef A_LONG
+ #define AL1 uint64_t
+ #define AL2 u64vec2
+ #define AL3 u64vec3
+ #define AL4 u64vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASL1 int64_t
+ #define ASL2 i64vec2
+ #define ASL3 i64vec3
+ #define ASL4 i64vec4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AL1_AU2(x) packUint2x32(AU2(x))
+ #define AU2_AL1(x) unpackUint2x32(AL1(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ AL1 AL1_x(AL1 a){return AL1(a);}
+ AL2 AL2_x(AL1 a){return AL2(a,a);}
+ AL3 AL3_x(AL1 a){return AL3(a,a,a);}
+ AL4 AL4_x(AL1 a){return AL4(a,a,a,a);}
+ #define AL1_(a) AL1_x(AL1(a))
+ #define AL2_(a) AL2_x(AL1(a))
+ #define AL3_(a) AL3_x(AL1(a))
+ #define AL4_(a) AL4_x(AL1(a))
+//==============================================================================================================================
+ AL1 AAbsSL1(AL1 a){return AL1(abs(ASL1(a)));}
+ AL2 AAbsSL2(AL2 a){return AL2(abs(ASL2(a)));}
+ AL3 AAbsSL3(AL3 a){return AL3(abs(ASL3(a)));}
+ AL4 AAbsSL4(AL4 a){return AL4(abs(ASL4(a)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AL1 AMaxSL1(AL1 a,AL1 b){return AL1(max(ASU1(a),ASU1(b)));}
+ AL2 AMaxSL2(AL2 a,AL2 b){return AL2(max(ASU2(a),ASU2(b)));}
+ AL3 AMaxSL3(AL3 a,AL3 b){return AL3(max(ASU3(a),ASU3(b)));}
+ AL4 AMaxSL4(AL4 a,AL4 b){return AL4(max(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AL1 AMinSL1(AL1 a,AL1 b){return AL1(min(ASU1(a),ASU1(b)));}
+ AL2 AMinSL2(AL2 a,AL2 b){return AL2(min(ASU2(a),ASU2(b)));}
+ AL3 AMinSL3(AL3 a,AL3 b){return AL3(min(ASU3(a),ASU3(b)));}
+ AL4 AMinSL4(AL4 a,AL4 b){return AL4(min(ASU4(a),ASU4(b)));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// WAVE OPERATIONS
+//==============================================================================================================================
+ #ifdef A_WAVE
+ // Where 'x' must be a compile time literal.
+ AF1 AWaveXorF1(AF1 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AF2 AWaveXorF2(AF2 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AF3 AWaveXorF3(AF3 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AF4 AWaveXorF4(AF4 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AU1 AWaveXorU1(AU1 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AU2 AWaveXorU2(AU2 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AU3 AWaveXorU3(AU3 v,AU1 x){return subgroupShuffleXor(v,x);}
+ AU4 AWaveXorU4(AU4 v,AU1 x){return subgroupShuffleXor(v,x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_HALF
+ AH2 AWaveXorH2(AH2 v,AU1 x){return AH2_AU1(subgroupShuffleXor(AU1_AH2(v),x));}
+ AH4 AWaveXorH4(AH4 v,AU1 x){return AH4_AU2(subgroupShuffleXor(AU2_AH4(v),x));}
+ AW2 AWaveXorW2(AW2 v,AU1 x){return AW2_AU1(subgroupShuffleXor(AU1_AW2(v),x));}
+ AW4 AWaveXorW4(AW4 v,AU1 x){return AW4_AU2(subgroupShuffleXor(AU2_AW4(v),x));}
+ #endif
+ #endif
+//==============================================================================================================================
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// HLSL
+//
+//
+//==============================================================================================================================
+#if defined(A_HLSL) && defined(A_GPU)
+ #ifdef A_HLSL_6_2
+ #define AP1 bool
+ #define AP2 bool2
+ #define AP3 bool3
+ #define AP4 bool4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AF1 float32_t
+ #define AF2 float32_t2
+ #define AF3 float32_t3
+ #define AF4 float32_t4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1 uint32_t
+ #define AU2 uint32_t2
+ #define AU3 uint32_t3
+ #define AU4 uint32_t4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASU1 int32_t
+ #define ASU2 int32_t2
+ #define ASU3 int32_t3
+ #define ASU4 int32_t4
+ #else
+ #define AP1 bool
+ #define AP2 bool2
+ #define AP3 bool3
+ #define AP4 bool4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AF1 float
+ #define AF2 float2
+ #define AF3 float3
+ #define AF4 float4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1 uint
+ #define AU2 uint2
+ #define AU3 uint3
+ #define AU4 uint4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASU1 int
+ #define ASU2 int2
+ #define ASU3 int3
+ #define ASU4 int4
+ #endif
+//==============================================================================================================================
+ #define AF1_AU1(x) asfloat(AU1(x))
+ #define AF2_AU2(x) asfloat(AU2(x))
+ #define AF3_AU3(x) asfloat(AU3(x))
+ #define AF4_AU4(x) asfloat(AU4(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AU1_AF1(x) asuint(AF1(x))
+ #define AU2_AF2(x) asuint(AF2(x))
+ #define AU3_AF3(x) asuint(AF3(x))
+ #define AU4_AF4(x) asuint(AF4(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AU1_AH1_AF1_x(AF1 a){return f32tof16(a);}
+ #define AU1_AH1_AF1(a) AU1_AH1_AF1_x(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AU1_AH2_AF2_x(AF2 a){return f32tof16(a.x)|(f32tof16(a.y)<<16);}
+ #define AU1_AH2_AF2(a) AU1_AH2_AF2_x(AF2(a))
+ #define AU1_AB4Unorm_AF4(x) D3DCOLORtoUBYTE4(AF4(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 AF2_AH2_AU1_x(AU1 x){return AF2(f16tof32(x&0xFFFF),f16tof32(x>>16));}
+ #define AF2_AH2_AU1(x) AF2_AH2_AU1_x(AU1(x))
+//==============================================================================================================================
+ AF1 AF1_x(AF1 a){return AF1(a);}
+ AF2 AF2_x(AF1 a){return AF2(a,a);}
+ AF3 AF3_x(AF1 a){return AF3(a,a,a);}
+ AF4 AF4_x(AF1 a){return AF4(a,a,a,a);}
+ #define AF1_(a) AF1_x(AF1(a))
+ #define AF2_(a) AF2_x(AF1(a))
+ #define AF3_(a) AF3_x(AF1(a))
+ #define AF4_(a) AF4_x(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AU1_x(AU1 a){return AU1(a);}
+ AU2 AU2_x(AU1 a){return AU2(a,a);}
+ AU3 AU3_x(AU1 a){return AU3(a,a,a);}
+ AU4 AU4_x(AU1 a){return AU4(a,a,a,a);}
+ #define AU1_(a) AU1_x(AU1(a))
+ #define AU2_(a) AU2_x(AU1(a))
+ #define AU3_(a) AU3_x(AU1(a))
+ #define AU4_(a) AU4_x(AU1(a))
+//==============================================================================================================================
+ AU1 AAbsSU1(AU1 a){return AU1(abs(ASU1(a)));}
+ AU2 AAbsSU2(AU2 a){return AU2(abs(ASU2(a)));}
+ AU3 AAbsSU3(AU3 a){return AU3(abs(ASU3(a)));}
+ AU4 AAbsSU4(AU4 a){return AU4(abs(ASU4(a)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 ABfe(AU1 src,AU1 off,AU1 bits){AU1 mask=(1u<<bits)-1;return (src>>off)&mask;}
+ AU1 ABfi(AU1 src,AU1 ins,AU1 mask){return (ins&mask)|(src&(~mask));}
+ AU1 ABfiM(AU1 src,AU1 ins,AU1 bits){AU1 mask=(1u<<bits)-1;return (ins&mask)|(src&(~mask));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AClampF1(AF1 x,AF1 n,AF1 m){return max(n,min(x,m));}
+ AF2 AClampF2(AF2 x,AF2 n,AF2 m){return max(n,min(x,m));}
+ AF3 AClampF3(AF3 x,AF3 n,AF3 m){return max(n,min(x,m));}
+ AF4 AClampF4(AF4 x,AF4 n,AF4 m){return max(n,min(x,m));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AFractF1(AF1 x){return x-floor(x);}
+ AF2 AFractF2(AF2 x){return x-floor(x);}
+ AF3 AFractF3(AF3 x){return x-floor(x);}
+ AF4 AFractF4(AF4 x){return x-floor(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ALerpF1(AF1 x,AF1 y,AF1 a){return lerp(x,y,a);}
+ AF2 ALerpF2(AF2 x,AF2 y,AF2 a){return lerp(x,y,a);}
+ AF3 ALerpF3(AF3 x,AF3 y,AF3 a){return lerp(x,y,a);}
+ AF4 ALerpF4(AF4 x,AF4 y,AF4 a){return lerp(x,y,a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AMax3F1(AF1 x,AF1 y,AF1 z){return max(x,max(y,z));}
+ AF2 AMax3F2(AF2 x,AF2 y,AF2 z){return max(x,max(y,z));}
+ AF3 AMax3F3(AF3 x,AF3 y,AF3 z){return max(x,max(y,z));}
+ AF4 AMax3F4(AF4 x,AF4 y,AF4 z){return max(x,max(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMax3SU1(AU1 x,AU1 y,AU1 z){return AU1(max(ASU1(x),max(ASU1(y),ASU1(z))));}
+ AU2 AMax3SU2(AU2 x,AU2 y,AU2 z){return AU2(max(ASU2(x),max(ASU2(y),ASU2(z))));}
+ AU3 AMax3SU3(AU3 x,AU3 y,AU3 z){return AU3(max(ASU3(x),max(ASU3(y),ASU3(z))));}
+ AU4 AMax3SU4(AU4 x,AU4 y,AU4 z){return AU4(max(ASU4(x),max(ASU4(y),ASU4(z))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMax3U1(AU1 x,AU1 y,AU1 z){return max(x,max(y,z));}
+ AU2 AMax3U2(AU2 x,AU2 y,AU2 z){return max(x,max(y,z));}
+ AU3 AMax3U3(AU3 x,AU3 y,AU3 z){return max(x,max(y,z));}
+ AU4 AMax3U4(AU4 x,AU4 y,AU4 z){return max(x,max(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMaxSU1(AU1 a,AU1 b){return AU1(max(ASU1(a),ASU1(b)));}
+ AU2 AMaxSU2(AU2 a,AU2 b){return AU2(max(ASU2(a),ASU2(b)));}
+ AU3 AMaxSU3(AU3 a,AU3 b){return AU3(max(ASU3(a),ASU3(b)));}
+ AU4 AMaxSU4(AU4 a,AU4 b){return AU4(max(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AMed3F1(AF1 x,AF1 y,AF1 z){return max(min(x,y),min(max(x,y),z));}
+ AF2 AMed3F2(AF2 x,AF2 y,AF2 z){return max(min(x,y),min(max(x,y),z));}
+ AF3 AMed3F3(AF3 x,AF3 y,AF3 z){return max(min(x,y),min(max(x,y),z));}
+ AF4 AMed3F4(AF4 x,AF4 y,AF4 z){return max(min(x,y),min(max(x,y),z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AMin3F1(AF1 x,AF1 y,AF1 z){return min(x,min(y,z));}
+ AF2 AMin3F2(AF2 x,AF2 y,AF2 z){return min(x,min(y,z));}
+ AF3 AMin3F3(AF3 x,AF3 y,AF3 z){return min(x,min(y,z));}
+ AF4 AMin3F4(AF4 x,AF4 y,AF4 z){return min(x,min(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMin3SU1(AU1 x,AU1 y,AU1 z){return AU1(min(ASU1(x),min(ASU1(y),ASU1(z))));}
+ AU2 AMin3SU2(AU2 x,AU2 y,AU2 z){return AU2(min(ASU2(x),min(ASU2(y),ASU2(z))));}
+ AU3 AMin3SU3(AU3 x,AU3 y,AU3 z){return AU3(min(ASU3(x),min(ASU3(y),ASU3(z))));}
+ AU4 AMin3SU4(AU4 x,AU4 y,AU4 z){return AU4(min(ASU4(x),min(ASU4(y),ASU4(z))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMin3U1(AU1 x,AU1 y,AU1 z){return min(x,min(y,z));}
+ AU2 AMin3U2(AU2 x,AU2 y,AU2 z){return min(x,min(y,z));}
+ AU3 AMin3U3(AU3 x,AU3 y,AU3 z){return min(x,min(y,z));}
+ AU4 AMin3U4(AU4 x,AU4 y,AU4 z){return min(x,min(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AMinSU1(AU1 a,AU1 b){return AU1(min(ASU1(a),ASU1(b)));}
+ AU2 AMinSU2(AU2 a,AU2 b){return AU2(min(ASU2(a),ASU2(b)));}
+ AU3 AMinSU3(AU3 a,AU3 b){return AU3(min(ASU3(a),ASU3(b)));}
+ AU4 AMinSU4(AU4 a,AU4 b){return AU4(min(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ANCosF1(AF1 x){return cos(x*AF1_(A_2PI));}
+ AF2 ANCosF2(AF2 x){return cos(x*AF2_(A_2PI));}
+ AF3 ANCosF3(AF3 x){return cos(x*AF3_(A_2PI));}
+ AF4 ANCosF4(AF4 x){return cos(x*AF4_(A_2PI));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ANSinF1(AF1 x){return sin(x*AF1_(A_2PI));}
+ AF2 ANSinF2(AF2 x){return sin(x*AF2_(A_2PI));}
+ AF3 ANSinF3(AF3 x){return sin(x*AF3_(A_2PI));}
+ AF4 ANSinF4(AF4 x){return sin(x*AF4_(A_2PI));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ARcpF1(AF1 x){return rcp(x);}
+ AF2 ARcpF2(AF2 x){return rcp(x);}
+ AF3 ARcpF3(AF3 x){return rcp(x);}
+ AF4 ARcpF4(AF4 x){return rcp(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ARsqF1(AF1 x){return rsqrt(x);}
+ AF2 ARsqF2(AF2 x){return rsqrt(x);}
+ AF3 ARsqF3(AF3 x){return rsqrt(x);}
+ AF4 ARsqF4(AF4 x){return rsqrt(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ASatF1(AF1 x){return saturate(x);}
+ AF2 ASatF2(AF2 x){return saturate(x);}
+ AF3 ASatF3(AF3 x){return saturate(x);}
+ AF4 ASatF4(AF4 x){return saturate(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AShrSU1(AU1 a,AU1 b){return AU1(ASU1(a)>>ASU1(b));}
+ AU2 AShrSU2(AU2 a,AU2 b){return AU2(ASU2(a)>>ASU2(b));}
+ AU3 AShrSU3(AU3 a,AU3 b){return AU3(ASU3(a)>>ASU3(b));}
+ AU4 AShrSU4(AU4 a,AU4 b){return AU4(ASU4(a)>>ASU4(b));}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// HLSL BYTE
+//==============================================================================================================================
+ #ifdef A_BYTE
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// HLSL HALF
+//==============================================================================================================================
+ #ifdef A_HALF
+ #ifdef A_HLSL_6_2
+ #define AH1 float16_t
+ #define AH2 float16_t2
+ #define AH3 float16_t3
+ #define AH4 float16_t4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AW1 uint16_t
+ #define AW2 uint16_t2
+ #define AW3 uint16_t3
+ #define AW4 uint16_t4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASW1 int16_t
+ #define ASW2 int16_t2
+ #define ASW3 int16_t3
+ #define ASW4 int16_t4
+ #else
+ #define AH1 min16float
+ #define AH2 min16float2
+ #define AH3 min16float3
+ #define AH4 min16float4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AW1 min16uint
+ #define AW2 min16uint2
+ #define AW3 min16uint3
+ #define AW4 min16uint4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASW1 min16int
+ #define ASW2 min16int2
+ #define ASW3 min16int3
+ #define ASW4 min16int4
+ #endif
+//==============================================================================================================================
+ // Need to use manual unpack to get optimal execution (don't use packed types in buffers directly).
+ // Unpack requires this pattern: https://gpuopen.com/first-steps-implementing-fp16/
+ AH2 AH2_AU1_x(AU1 x){AF2 t=f16tof32(AU2(x&0xFFFF,x>>16));return AH2(t);}
+ AH4 AH4_AU2_x(AU2 x){return AH4(AH2_AU1_x(x.x),AH2_AU1_x(x.y));}
+ AW2 AW2_AU1_x(AU1 x){AU2 t=AU2(x&0xFFFF,x>>16);return AW2(t);}
+ AW4 AW4_AU2_x(AU2 x){return AW4(AW2_AU1_x(x.x),AW2_AU1_x(x.y));}
+ #define AH2_AU1(x) AH2_AU1_x(AU1(x))
+ #define AH4_AU2(x) AH4_AU2_x(AU2(x))
+ #define AW2_AU1(x) AW2_AU1_x(AU1(x))
+ #define AW4_AU2(x) AW4_AU2_x(AU2(x))
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AU1_AH2_x(AH2 x){return f32tof16(x.x)+(f32tof16(x.y)<<16);}
+ AU2 AU2_AH4_x(AH4 x){return AU2(AU1_AH2_x(x.xy),AU1_AH2_x(x.zw));}
+ AU1 AU1_AW2_x(AW2 x){return AU1(x.x)+(AU1(x.y)<<16);}
+ AU2 AU2_AW4_x(AW4 x){return AU2(AU1_AW2_x(x.xy),AU1_AW2_x(x.zw));}
+ #define AU1_AH2(x) AU1_AH2_x(AH2(x))
+ #define AU2_AH4(x) AU2_AH4_x(AH4(x))
+ #define AU1_AW2(x) AU1_AW2_x(AW2(x))
+ #define AU2_AW4(x) AU2_AW4_x(AW4(x))
+//==============================================================================================================================
+ #if defined(A_HLSL_6_2) && !defined(A_NO_16_BIT_CAST)
+ #define AW1_AH1(x) asuint16(x)
+ #define AW2_AH2(x) asuint16(x)
+ #define AW3_AH3(x) asuint16(x)
+ #define AW4_AH4(x) asuint16(x)
+ #else
+ #define AW1_AH1(a) AW1(f32tof16(AF1(a)))
+ #define AW2_AH2(a) AW2(AW1_AH1((a).x),AW1_AH1((a).y))
+ #define AW3_AH3(a) AW3(AW1_AH1((a).x),AW1_AH1((a).y),AW1_AH1((a).z))
+ #define AW4_AH4(a) AW4(AW1_AH1((a).x),AW1_AH1((a).y),AW1_AH1((a).z),AW1_AH1((a).w))
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #if defined(A_HLSL_6_2) && !defined(A_NO_16_BIT_CAST)
+ #define AH1_AW1(x) asfloat16(x)
+ #define AH2_AW2(x) asfloat16(x)
+ #define AH3_AW3(x) asfloat16(x)
+ #define AH4_AW4(x) asfloat16(x)
+ #else
+ #define AH1_AW1(a) AH1(f16tof32(AU1(a)))
+ #define AH2_AW2(a) AH2(AH1_AW1((a).x),AH1_AW1((a).y))
+ #define AH3_AW3(a) AH3(AH1_AW1((a).x),AH1_AW1((a).y),AH1_AW1((a).z))
+ #define AH4_AW4(a) AH4(AH1_AW1((a).x),AH1_AW1((a).y),AH1_AW1((a).z),AH1_AW1((a).w))
+ #endif
+//==============================================================================================================================
+ AH1 AH1_x(AH1 a){return AH1(a);}
+ AH2 AH2_x(AH1 a){return AH2(a,a);}
+ AH3 AH3_x(AH1 a){return AH3(a,a,a);}
+ AH4 AH4_x(AH1 a){return AH4(a,a,a,a);}
+ #define AH1_(a) AH1_x(AH1(a))
+ #define AH2_(a) AH2_x(AH1(a))
+ #define AH3_(a) AH3_x(AH1(a))
+ #define AH4_(a) AH4_x(AH1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AW1_x(AW1 a){return AW1(a);}
+ AW2 AW2_x(AW1 a){return AW2(a,a);}
+ AW3 AW3_x(AW1 a){return AW3(a,a,a);}
+ AW4 AW4_x(AW1 a){return AW4(a,a,a,a);}
+ #define AW1_(a) AW1_x(AW1(a))
+ #define AW2_(a) AW2_x(AW1(a))
+ #define AW3_(a) AW3_x(AW1(a))
+ #define AW4_(a) AW4_x(AW1(a))
+//==============================================================================================================================
+ AW1 AAbsSW1(AW1 a){return AW1(abs(ASW1(a)));}
+ AW2 AAbsSW2(AW2 a){return AW2(abs(ASW2(a)));}
+ AW3 AAbsSW3(AW3 a){return AW3(abs(ASW3(a)));}
+ AW4 AAbsSW4(AW4 a){return AW4(abs(ASW4(a)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AClampH1(AH1 x,AH1 n,AH1 m){return max(n,min(x,m));}
+ AH2 AClampH2(AH2 x,AH2 n,AH2 m){return max(n,min(x,m));}
+ AH3 AClampH3(AH3 x,AH3 n,AH3 m){return max(n,min(x,m));}
+ AH4 AClampH4(AH4 x,AH4 n,AH4 m){return max(n,min(x,m));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // V_FRACT_F16 (note DX frac() is different).
+ AH1 AFractH1(AH1 x){return x-floor(x);}
+ AH2 AFractH2(AH2 x){return x-floor(x);}
+ AH3 AFractH3(AH3 x){return x-floor(x);}
+ AH4 AFractH4(AH4 x){return x-floor(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ALerpH1(AH1 x,AH1 y,AH1 a){return lerp(x,y,a);}
+ AH2 ALerpH2(AH2 x,AH2 y,AH2 a){return lerp(x,y,a);}
+ AH3 ALerpH3(AH3 x,AH3 y,AH3 a){return lerp(x,y,a);}
+ AH4 ALerpH4(AH4 x,AH4 y,AH4 a){return lerp(x,y,a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AMax3H1(AH1 x,AH1 y,AH1 z){return max(x,max(y,z));}
+ AH2 AMax3H2(AH2 x,AH2 y,AH2 z){return max(x,max(y,z));}
+ AH3 AMax3H3(AH3 x,AH3 y,AH3 z){return max(x,max(y,z));}
+ AH4 AMax3H4(AH4 x,AH4 y,AH4 z){return max(x,max(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AMaxSW1(AW1 a,AW1 b){return AW1(max(ASU1(a),ASU1(b)));}
+ AW2 AMaxSW2(AW2 a,AW2 b){return AW2(max(ASU2(a),ASU2(b)));}
+ AW3 AMaxSW3(AW3 a,AW3 b){return AW3(max(ASU3(a),ASU3(b)));}
+ AW4 AMaxSW4(AW4 a,AW4 b){return AW4(max(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AMin3H1(AH1 x,AH1 y,AH1 z){return min(x,min(y,z));}
+ AH2 AMin3H2(AH2 x,AH2 y,AH2 z){return min(x,min(y,z));}
+ AH3 AMin3H3(AH3 x,AH3 y,AH3 z){return min(x,min(y,z));}
+ AH4 AMin3H4(AH4 x,AH4 y,AH4 z){return min(x,min(y,z));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AMinSW1(AW1 a,AW1 b){return AW1(min(ASU1(a),ASU1(b)));}
+ AW2 AMinSW2(AW2 a,AW2 b){return AW2(min(ASU2(a),ASU2(b)));}
+ AW3 AMinSW3(AW3 a,AW3 b){return AW3(min(ASU3(a),ASU3(b)));}
+ AW4 AMinSW4(AW4 a,AW4 b){return AW4(min(ASU4(a),ASU4(b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ARcpH1(AH1 x){return rcp(x);}
+ AH2 ARcpH2(AH2 x){return rcp(x);}
+ AH3 ARcpH3(AH3 x){return rcp(x);}
+ AH4 ARcpH4(AH4 x){return rcp(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ARsqH1(AH1 x){return rsqrt(x);}
+ AH2 ARsqH2(AH2 x){return rsqrt(x);}
+ AH3 ARsqH3(AH3 x){return rsqrt(x);}
+ AH4 ARsqH4(AH4 x){return rsqrt(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ASatH1(AH1 x){return saturate(x);}
+ AH2 ASatH2(AH2 x){return saturate(x);}
+ AH3 ASatH3(AH3 x){return saturate(x);}
+ AH4 ASatH4(AH4 x){return saturate(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AShrSW1(AW1 a,AW1 b){return AW1(ASW1(a)>>ASW1(b));}
+ AW2 AShrSW2(AW2 a,AW2 b){return AW2(ASW2(a)>>ASW2(b));}
+ AW3 AShrSW3(AW3 a,AW3 b){return AW3(ASW3(a)>>ASW3(b));}
+ AW4 AShrSW4(AW4 a,AW4 b){return AW4(ASW4(a)>>ASW4(b));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// HLSL DOUBLE
+//==============================================================================================================================
+ #ifdef A_DUBL
+ #ifdef A_HLSL_6_2
+ #define AD1 float64_t
+ #define AD2 float64_t2
+ #define AD3 float64_t3
+ #define AD4 float64_t4
+ #else
+ #define AD1 double
+ #define AD2 double2
+ #define AD3 double3
+ #define AD4 double4
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 AD1_x(AD1 a){return AD1(a);}
+ AD2 AD2_x(AD1 a){return AD2(a,a);}
+ AD3 AD3_x(AD1 a){return AD3(a,a,a);}
+ AD4 AD4_x(AD1 a){return AD4(a,a,a,a);}
+ #define AD1_(a) AD1_x(AD1(a))
+ #define AD2_(a) AD2_x(AD1(a))
+ #define AD3_(a) AD3_x(AD1(a))
+ #define AD4_(a) AD4_x(AD1(a))
+//==============================================================================================================================
+ AD1 AFractD1(AD1 a){return a-floor(a);}
+ AD2 AFractD2(AD2 a){return a-floor(a);}
+ AD3 AFractD3(AD3 a){return a-floor(a);}
+ AD4 AFractD4(AD4 a){return a-floor(a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ALerpD1(AD1 x,AD1 y,AD1 a){return lerp(x,y,a);}
+ AD2 ALerpD2(AD2 x,AD2 y,AD2 a){return lerp(x,y,a);}
+ AD3 ALerpD3(AD3 x,AD3 y,AD3 a){return lerp(x,y,a);}
+ AD4 ALerpD4(AD4 x,AD4 y,AD4 a){return lerp(x,y,a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ARcpD1(AD1 x){return rcp(x);}
+ AD2 ARcpD2(AD2 x){return rcp(x);}
+ AD3 ARcpD3(AD3 x){return rcp(x);}
+ AD4 ARcpD4(AD4 x){return rcp(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ARsqD1(AD1 x){return rsqrt(x);}
+ AD2 ARsqD2(AD2 x){return rsqrt(x);}
+ AD3 ARsqD3(AD3 x){return rsqrt(x);}
+ AD4 ARsqD4(AD4 x){return rsqrt(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD1 ASatD1(AD1 x){return saturate(x);}
+ AD2 ASatD2(AD2 x){return saturate(x);}
+ AD3 ASatD3(AD3 x){return saturate(x);}
+ AD4 ASatD4(AD4 x){return saturate(x);}
+ #endif
+//==============================================================================================================================
+// HLSL WAVE
+//==============================================================================================================================
+ #ifdef A_WAVE
+ // Where 'x' must be a compile time literal.
+ AF1 AWaveXorF1(AF1 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AF2 AWaveXorF2(AF2 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AF3 AWaveXorF3(AF3 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AF4 AWaveXorF4(AF4 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AU1 AWaveXorU1(AU1 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AU2 AWaveXorU1(AU2 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AU3 AWaveXorU1(AU3 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+ AU4 AWaveXorU1(AU4 v,AU1 x){return WaveReadLaneAt(v,WaveGetLaneIndex()^x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_HALF
+ AH2 AWaveXorH2(AH2 v,AU1 x){return AH2_AU1(WaveReadLaneAt(AU1_AH2(v),WaveGetLaneIndex()^x));}
+ AH4 AWaveXorH4(AH4 v,AU1 x){return AH4_AU2(WaveReadLaneAt(AU2_AH4(v),WaveGetLaneIndex()^x));}
+ AW2 AWaveXorW2(AW2 v,AU1 x){return AW2_AU1(WaveReadLaneAt(AU1_AW2(v),WaveGetLaneIndex()^x));}
+ AW4 AWaveXorW4(AW4 v,AU1 x){return AW4_AU1(WaveReadLaneAt(AU1_AW4(v),WaveGetLaneIndex()^x));}
+ #endif
+ #endif
+//==============================================================================================================================
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// GPU COMMON
+//
+//
+//==============================================================================================================================
+#ifdef A_GPU
+ // Negative and positive infinity.
+ #define A_INFP_F AF1_AU1(0x7f800000u)
+ #define A_INFN_F AF1_AU1(0xff800000u)
+//------------------------------------------------------------------------------------------------------------------------------
+ // Copy sign from 's' to positive 'd'.
+ AF1 ACpySgnF1(AF1 d,AF1 s){return AF1_AU1(AU1_AF1(d)|(AU1_AF1(s)&AU1_(0x80000000u)));}
+ AF2 ACpySgnF2(AF2 d,AF2 s){return AF2_AU2(AU2_AF2(d)|(AU2_AF2(s)&AU2_(0x80000000u)));}
+ AF3 ACpySgnF3(AF3 d,AF3 s){return AF3_AU3(AU3_AF3(d)|(AU3_AF3(s)&AU3_(0x80000000u)));}
+ AF4 ACpySgnF4(AF4 d,AF4 s){return AF4_AU4(AU4_AF4(d)|(AU4_AF4(s)&AU4_(0x80000000u)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Single operation to return (useful to create a mask to use in lerp for branch free logic),
+ // m=NaN := 0
+ // m>=0 := 0
+ // m<0 := 1
+ // Uses the following useful floating point logic,
+ // saturate(+a*(-INF)==-INF) := 0
+ // saturate( 0*(-INF)== NaN) := 0
+ // saturate(-a*(-INF)==+INF) := 1
+ AF1 ASignedF1(AF1 m){return ASatF1(m*AF1_(A_INFN_F));}
+ AF2 ASignedF2(AF2 m){return ASatF2(m*AF2_(A_INFN_F));}
+ AF3 ASignedF3(AF3 m){return ASatF3(m*AF3_(A_INFN_F));}
+ AF4 ASignedF4(AF4 m){return ASatF4(m*AF4_(A_INFN_F));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AGtZeroF1(AF1 m){return ASatF1(m*AF1_(A_INFP_F));}
+ AF2 AGtZeroF2(AF2 m){return ASatF2(m*AF2_(A_INFP_F));}
+ AF3 AGtZeroF3(AF3 m){return ASatF3(m*AF3_(A_INFP_F));}
+ AF4 AGtZeroF4(AF4 m){return ASatF4(m*AF4_(A_INFP_F));}
+//==============================================================================================================================
+ #ifdef A_HALF
+ #ifdef A_HLSL_6_2
+ #define A_INFP_H AH1_AW1((uint16_t)0x7c00u)
+ #define A_INFN_H AH1_AW1((uint16_t)0xfc00u)
+ #else
+ #define A_INFP_H AH1_AW1(0x7c00u)
+ #define A_INFN_H AH1_AW1(0xfc00u)
+ #endif
+
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ACpySgnH1(AH1 d,AH1 s){return AH1_AW1(AW1_AH1(d)|(AW1_AH1(s)&AW1_(0x8000u)));}
+ AH2 ACpySgnH2(AH2 d,AH2 s){return AH2_AW2(AW2_AH2(d)|(AW2_AH2(s)&AW2_(0x8000u)));}
+ AH3 ACpySgnH3(AH3 d,AH3 s){return AH3_AW3(AW3_AH3(d)|(AW3_AH3(s)&AW3_(0x8000u)));}
+ AH4 ACpySgnH4(AH4 d,AH4 s){return AH4_AW4(AW4_AH4(d)|(AW4_AH4(s)&AW4_(0x8000u)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ASignedH1(AH1 m){return ASatH1(m*AH1_(A_INFN_H));}
+ AH2 ASignedH2(AH2 m){return ASatH2(m*AH2_(A_INFN_H));}
+ AH3 ASignedH3(AH3 m){return ASatH3(m*AH3_(A_INFN_H));}
+ AH4 ASignedH4(AH4 m){return ASatH4(m*AH4_(A_INFN_H));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AGtZeroH1(AH1 m){return ASatH1(m*AH1_(A_INFP_H));}
+ AH2 AGtZeroH2(AH2 m){return ASatH2(m*AH2_(A_INFP_H));}
+ AH3 AGtZeroH3(AH3 m){return ASatH3(m*AH3_(A_INFP_H));}
+ AH4 AGtZeroH4(AH4 m){return ASatH4(m*AH4_(A_INFP_H));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// [FIS] FLOAT INTEGER SORTABLE
+//------------------------------------------------------------------------------------------------------------------------------
+// Float to integer sortable.
+// - If sign bit=0, flip the sign bit (positives).
+// - If sign bit=1, flip all bits (negatives).
+// Integer sortable to float.
+// - If sign bit=1, flip the sign bit (positives).
+// - If sign bit=0, flip all bits (negatives).
+// Has nice side effects.
+// - Larger integers are more positive values.
+// - Float zero is mapped to center of integers (so clear to integer zero is a nice default for atomic max usage).
+// Burns 3 ops for conversion {shift,or,xor}.
+//==============================================================================================================================
+ AU1 AFisToU1(AU1 x){return x^(( AShrSU1(x,AU1_(31)))|AU1_(0x80000000));}
+ AU1 AFisFromU1(AU1 x){return x^((~AShrSU1(x,AU1_(31)))|AU1_(0x80000000));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Just adjust high 16-bit value (useful when upper part of 32-bit word is a 16-bit float value).
+ AU1 AFisToHiU1(AU1 x){return x^(( AShrSU1(x,AU1_(15)))|AU1_(0x80000000));}
+ AU1 AFisFromHiU1(AU1 x){return x^((~AShrSU1(x,AU1_(15)))|AU1_(0x80000000));}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_HALF
+ AW1 AFisToW1(AW1 x){return x^(( AShrSW1(x,AW1_(15)))|AW1_(0x8000));}
+ AW1 AFisFromW1(AW1 x){return x^((~AShrSW1(x,AW1_(15)))|AW1_(0x8000));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW2 AFisToW2(AW2 x){return x^(( AShrSW2(x,AW2_(15)))|AW2_(0x8000));}
+ AW2 AFisFromW2(AW2 x){return x^((~AShrSW2(x,AW2_(15)))|AW2_(0x8000));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// [PERM] V_PERM_B32
+//------------------------------------------------------------------------------------------------------------------------------
+// Support for V_PERM_B32 started in the 3rd generation of GCN.
+//------------------------------------------------------------------------------------------------------------------------------
+// yyyyxxxx - The 'i' input.
+// 76543210
+// ========
+// HGFEDCBA - Naming on permutation.
+//------------------------------------------------------------------------------------------------------------------------------
+// TODO
+// ====
+// - Make sure compiler optimizes this.
+//==============================================================================================================================
+ #ifdef A_HALF
+ AU1 APerm0E0A(AU2 i){return((i.x )&0xffu)|((i.y<<16)&0xff0000u);}
+ AU1 APerm0F0B(AU2 i){return((i.x>> 8)&0xffu)|((i.y<< 8)&0xff0000u);}
+ AU1 APerm0G0C(AU2 i){return((i.x>>16)&0xffu)|((i.y )&0xff0000u);}
+ AU1 APerm0H0D(AU2 i){return((i.x>>24)&0xffu)|((i.y>> 8)&0xff0000u);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 APermHGFA(AU2 i){return((i.x )&0x000000ffu)|(i.y&0xffffff00u);}
+ AU1 APermHGFC(AU2 i){return((i.x>>16)&0x000000ffu)|(i.y&0xffffff00u);}
+ AU1 APermHGAE(AU2 i){return((i.x<< 8)&0x0000ff00u)|(i.y&0xffff00ffu);}
+ AU1 APermHGCE(AU2 i){return((i.x>> 8)&0x0000ff00u)|(i.y&0xffff00ffu);}
+ AU1 APermHAFE(AU2 i){return((i.x<<16)&0x00ff0000u)|(i.y&0xff00ffffu);}
+ AU1 APermHCFE(AU2 i){return((i.x )&0x00ff0000u)|(i.y&0xff00ffffu);}
+ AU1 APermAGFE(AU2 i){return((i.x<<24)&0xff000000u)|(i.y&0x00ffffffu);}
+ AU1 APermCGFE(AU2 i){return((i.x<< 8)&0xff000000u)|(i.y&0x00ffffffu);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 APermGCEA(AU2 i){return((i.x)&0x00ff00ffu)|((i.y<<8)&0xff00ff00u);}
+ AU1 APermGECA(AU2 i){return(((i.x)&0xffu)|((i.x>>8)&0xff00u)|((i.y<<16)&0xff0000u)|((i.y<<8)&0xff000000u));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// [BUC] BYTE UNSIGNED CONVERSION
+//------------------------------------------------------------------------------------------------------------------------------
+// Designed to use the optimal conversion, enables the scaling to possibly be factored into other computation.
+// Works on a range of {0 to A_BUC_<32,16>}, for <32-bit, and 16-bit> respectively.
+//------------------------------------------------------------------------------------------------------------------------------
+// OPCODE NOTES
+// ============
+// GCN does not do UNORM or SNORM for bytes in opcodes.
+// - V_CVT_F32_UBYTE{0,1,2,3} - Unsigned byte to float.
+// - V_CVT_PKACC_U8_F32 - Float to unsigned byte (does bit-field insert into 32-bit integer).
+// V_PERM_B32 does byte packing with ability to zero fill bytes as well.
+// - Can pull out byte values from two sources, and zero fill upper 8-bits of packed hi and lo.
+//------------------------------------------------------------------------------------------------------------------------------
+// BYTE : FLOAT - ABuc{0,1,2,3}{To,From}U1() - Designed for V_CVT_F32_UBYTE* and V_CVT_PKACCUM_U8_F32 ops.
+// ==== =====
+// 0 : 0
+// 1 : 1
+// ...
+// 255 : 255
+// : 256 (just outside the encoding range)
+//------------------------------------------------------------------------------------------------------------------------------
+// BYTE : FLOAT - ABuc{0,1,2,3}{To,From}U2() - Designed for 16-bit denormal tricks and V_PERM_B32.
+// ==== =====
+// 0 : 0
+// 1 : 1/512
+// 2 : 1/256
+// ...
+// 64 : 1/8
+// 128 : 1/4
+// 255 : 255/512
+// : 1/2 (just outside the encoding range)
+//------------------------------------------------------------------------------------------------------------------------------
+// OPTIMAL IMPLEMENTATIONS ON AMD ARCHITECTURES
+// ============================================
+// r=ABuc0FromU1(i)
+// V_CVT_F32_UBYTE0 r,i
+// --------------------------------------------
+// r=ABuc0ToU1(d,i)
+// V_CVT_PKACCUM_U8_F32 r,i,0,d
+// --------------------------------------------
+// d=ABuc0FromU2(i)
+// Where 'k0' is an SGPR with 0x0E0A
+// Where 'k1' is an SGPR with {32768.0} packed into the lower 16-bits
+// V_PERM_B32 d,i.x,i.y,k0
+// V_PK_FMA_F16 d,d,k1.x,0
+// --------------------------------------------
+// r=ABuc0ToU2(d,i)
+// Where 'k0' is an SGPR with {1.0/32768.0} packed into the lower 16-bits
+// Where 'k1' is an SGPR with 0x????
+// Where 'k2' is an SGPR with 0x????
+// V_PK_FMA_F16 i,i,k0.x,0
+// V_PERM_B32 r.x,i,i,k1
+// V_PERM_B32 r.y,i,i,k2
+//==============================================================================================================================
+ // Peak range for 32-bit and 16-bit operations.
+ #define A_BUC_32 (255.0)
+ #define A_BUC_16 (255.0/512.0)
+//==============================================================================================================================
+ #if 1
+ // Designed to be one V_CVT_PKACCUM_U8_F32.
+ // The extra min is required to pattern match to V_CVT_PKACCUM_U8_F32.
+ AU1 ABuc0ToU1(AU1 d,AF1 i){return (d&0xffffff00u)|((min(AU1(i),255u) )&(0x000000ffu));}
+ AU1 ABuc1ToU1(AU1 d,AF1 i){return (d&0xffff00ffu)|((min(AU1(i),255u)<< 8)&(0x0000ff00u));}
+ AU1 ABuc2ToU1(AU1 d,AF1 i){return (d&0xff00ffffu)|((min(AU1(i),255u)<<16)&(0x00ff0000u));}
+ AU1 ABuc3ToU1(AU1 d,AF1 i){return (d&0x00ffffffu)|((min(AU1(i),255u)<<24)&(0xff000000u));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Designed to be one V_CVT_F32_UBYTE*.
+ AF1 ABuc0FromU1(AU1 i){return AF1((i )&255u);}
+ AF1 ABuc1FromU1(AU1 i){return AF1((i>> 8)&255u);}
+ AF1 ABuc2FromU1(AU1 i){return AF1((i>>16)&255u);}
+ AF1 ABuc3FromU1(AU1 i){return AF1((i>>24)&255u);}
+ #endif
+//==============================================================================================================================
+ #ifdef A_HALF
+ // Takes {x0,x1} and {y0,y1} and builds {{x0,y0},{x1,y1}}.
+ AW2 ABuc01ToW2(AH2 x,AH2 y){x*=AH2_(1.0/32768.0);y*=AH2_(1.0/32768.0);
+ return AW2_AU1(APermGCEA(AU2(AU1_AW2(AW2_AH2(x)),AU1_AW2(AW2_AH2(y)))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Designed for 3 ops to do SOA to AOS and conversion.
+ AU2 ABuc0ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
+ return AU2(APermHGFA(AU2(d.x,b)),APermHGFC(AU2(d.y,b)));}
+ AU2 ABuc1ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
+ return AU2(APermHGAE(AU2(d.x,b)),APermHGCE(AU2(d.y,b)));}
+ AU2 ABuc2ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
+ return AU2(APermHAFE(AU2(d.x,b)),APermHCFE(AU2(d.y,b)));}
+ AU2 ABuc3ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)));
+ return AU2(APermAGFE(AU2(d.x,b)),APermCGFE(AU2(d.y,b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Designed for 2 ops to do both AOS to SOA, and conversion.
+ AH2 ABuc0FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0E0A(i)))*AH2_(32768.0);}
+ AH2 ABuc1FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0F0B(i)))*AH2_(32768.0);}
+ AH2 ABuc2FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0G0C(i)))*AH2_(32768.0);}
+ AH2 ABuc3FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0H0D(i)))*AH2_(32768.0);}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// [BSC] BYTE SIGNED CONVERSION
+//------------------------------------------------------------------------------------------------------------------------------
+// Similar to [BUC].
+// Works on a range of {-/+ A_BSC_<32,16>}, for <32-bit, and 16-bit> respectively.
+//------------------------------------------------------------------------------------------------------------------------------
+// ENCODING (without zero-based encoding)
+// ========
+// 0 = unused (can be used to mean something else)
+// 1 = lowest value
+// 128 = exact zero center (zero based encoding
+// 255 = highest value
+//------------------------------------------------------------------------------------------------------------------------------
+// Zero-based [Zb] flips the MSB bit of the byte (making 128 "exact zero" actually zero).
+// This is useful if there is a desire for cleared values to decode as zero.
+//------------------------------------------------------------------------------------------------------------------------------
+// BYTE : FLOAT - ABsc{0,1,2,3}{To,From}U2() - Designed for 16-bit denormal tricks and V_PERM_B32.
+// ==== =====
+// 0 : -127/512 (unused)
+// 1 : -126/512
+// 2 : -125/512
+// ...
+// 128 : 0
+// ...
+// 255 : 127/512
+// : 1/4 (just outside the encoding range)
+//==============================================================================================================================
+ // Peak range for 32-bit and 16-bit operations.
+ #define A_BSC_32 (127.0)
+ #define A_BSC_16 (127.0/512.0)
+//==============================================================================================================================
+ #if 1
+ AU1 ABsc0ToU1(AU1 d,AF1 i){return (d&0xffffff00u)|((min(AU1(i+128.0),255u) )&(0x000000ffu));}
+ AU1 ABsc1ToU1(AU1 d,AF1 i){return (d&0xffff00ffu)|((min(AU1(i+128.0),255u)<< 8)&(0x0000ff00u));}
+ AU1 ABsc2ToU1(AU1 d,AF1 i){return (d&0xff00ffffu)|((min(AU1(i+128.0),255u)<<16)&(0x00ff0000u));}
+ AU1 ABsc3ToU1(AU1 d,AF1 i){return (d&0x00ffffffu)|((min(AU1(i+128.0),255u)<<24)&(0xff000000u));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 ABsc0ToZbU1(AU1 d,AF1 i){return ((d&0xffffff00u)|((min(AU1(trunc(i)+128.0),255u) )&(0x000000ffu)))^0x00000080u;}
+ AU1 ABsc1ToZbU1(AU1 d,AF1 i){return ((d&0xffff00ffu)|((min(AU1(trunc(i)+128.0),255u)<< 8)&(0x0000ff00u)))^0x00008000u;}
+ AU1 ABsc2ToZbU1(AU1 d,AF1 i){return ((d&0xff00ffffu)|((min(AU1(trunc(i)+128.0),255u)<<16)&(0x00ff0000u)))^0x00800000u;}
+ AU1 ABsc3ToZbU1(AU1 d,AF1 i){return ((d&0x00ffffffu)|((min(AU1(trunc(i)+128.0),255u)<<24)&(0xff000000u)))^0x80000000u;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ABsc0FromU1(AU1 i){return AF1((i )&255u)-128.0;}
+ AF1 ABsc1FromU1(AU1 i){return AF1((i>> 8)&255u)-128.0;}
+ AF1 ABsc2FromU1(AU1 i){return AF1((i>>16)&255u)-128.0;}
+ AF1 ABsc3FromU1(AU1 i){return AF1((i>>24)&255u)-128.0;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ABsc0FromZbU1(AU1 i){return AF1(((i )&255u)^0x80u)-128.0;}
+ AF1 ABsc1FromZbU1(AU1 i){return AF1(((i>> 8)&255u)^0x80u)-128.0;}
+ AF1 ABsc2FromZbU1(AU1 i){return AF1(((i>>16)&255u)^0x80u)-128.0;}
+ AF1 ABsc3FromZbU1(AU1 i){return AF1(((i>>24)&255u)^0x80u)-128.0;}
+ #endif
+//==============================================================================================================================
+ #ifdef A_HALF
+ // Takes {x0,x1} and {y0,y1} and builds {{x0,y0},{x1,y1}}.
+ AW2 ABsc01ToW2(AH2 x,AH2 y){x=x*AH2_(1.0/32768.0)+AH2_(0.25/32768.0);y=y*AH2_(1.0/32768.0)+AH2_(0.25/32768.0);
+ return AW2_AU1(APermGCEA(AU2(AU1_AW2(AW2_AH2(x)),AU1_AW2(AW2_AH2(y)))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU2 ABsc0ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
+ return AU2(APermHGFA(AU2(d.x,b)),APermHGFC(AU2(d.y,b)));}
+ AU2 ABsc1ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
+ return AU2(APermHGAE(AU2(d.x,b)),APermHGCE(AU2(d.y,b)));}
+ AU2 ABsc2ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
+ return AU2(APermHAFE(AU2(d.x,b)),APermHCFE(AU2(d.y,b)));}
+ AU2 ABsc3ToU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)));
+ return AU2(APermAGFE(AU2(d.x,b)),APermCGFE(AU2(d.y,b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU2 ABsc0ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
+ return AU2(APermHGFA(AU2(d.x,b)),APermHGFC(AU2(d.y,b)));}
+ AU2 ABsc1ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
+ return AU2(APermHGAE(AU2(d.x,b)),APermHGCE(AU2(d.y,b)));}
+ AU2 ABsc2ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
+ return AU2(APermHAFE(AU2(d.x,b)),APermHCFE(AU2(d.y,b)));}
+ AU2 ABsc3ToZbU2(AU2 d,AH2 i){AU1 b=AU1_AW2(AW2_AH2(i*AH2_(1.0/32768.0)+AH2_(0.25/32768.0)))^0x00800080u;
+ return AU2(APermAGFE(AU2(d.x,b)),APermCGFE(AU2(d.y,b)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH2 ABsc0FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0E0A(i)))*AH2_(32768.0)-AH2_(0.25);}
+ AH2 ABsc1FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0F0B(i)))*AH2_(32768.0)-AH2_(0.25);}
+ AH2 ABsc2FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0G0C(i)))*AH2_(32768.0)-AH2_(0.25);}
+ AH2 ABsc3FromU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0H0D(i)))*AH2_(32768.0)-AH2_(0.25);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH2 ABsc0FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0E0A(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
+ AH2 ABsc1FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0F0B(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
+ AH2 ABsc2FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0G0C(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
+ AH2 ABsc3FromZbU2(AU2 i){return AH2_AW2(AW2_AU1(APerm0H0D(i)^0x00800080u))*AH2_(32768.0)-AH2_(0.25);}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// HALF APPROXIMATIONS
+//------------------------------------------------------------------------------------------------------------------------------
+// These support only positive inputs.
+// Did not see value yet in specialization for range.
+// Using quick testing, ended up mostly getting the same "best" approximation for various ranges.
+// With hardware that can co-execute transcendentals, the value in approximations could be less than expected.
+// However from a latency perspective, if execution of a transcendental is 4 clk, with no packed support, -> 8 clk total.
+// And co-execution would require a compiler interleaving a lot of independent work for packed usage.
+//------------------------------------------------------------------------------------------------------------------------------
+// The one Newton Raphson iteration form of rsq() was skipped (requires 6 ops total).
+// Same with sqrt(), as this could be x*rsq() (7 ops).
+//==============================================================================================================================
+ #ifdef A_HALF
+ // Minimize squared error across full positive range, 2 ops.
+ // The 0x1de2 based approximation maps {0 to 1} input maps to < 1 output.
+ AH1 APrxLoSqrtH1(AH1 a){return AH1_AW1((AW1_AH1(a)>>AW1_(1))+AW1_(0x1de2));}
+ AH2 APrxLoSqrtH2(AH2 a){return AH2_AW2((AW2_AH2(a)>>AW2_(1))+AW2_(0x1de2));}
+ AH3 APrxLoSqrtH3(AH3 a){return AH3_AW3((AW3_AH3(a)>>AW3_(1))+AW3_(0x1de2));}
+ AH4 APrxLoSqrtH4(AH4 a){return AH4_AW4((AW4_AH4(a)>>AW4_(1))+AW4_(0x1de2));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Lower precision estimation, 1 op.
+ // Minimize squared error across {smallest normal to 16384.0}.
+ AH1 APrxLoRcpH1(AH1 a){return AH1_AW1(AW1_(0x7784)-AW1_AH1(a));}
+ AH2 APrxLoRcpH2(AH2 a){return AH2_AW2(AW2_(0x7784)-AW2_AH2(a));}
+ AH3 APrxLoRcpH3(AH3 a){return AH3_AW3(AW3_(0x7784)-AW3_AH3(a));}
+ AH4 APrxLoRcpH4(AH4 a){return AH4_AW4(AW4_(0x7784)-AW4_AH4(a));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Medium precision estimation, one Newton Raphson iteration, 3 ops.
+ AH1 APrxMedRcpH1(AH1 a){AH1 b=AH1_AW1(AW1_(0x778d)-AW1_AH1(a));return b*(-b*a+AH1_(2.0));}
+ AH2 APrxMedRcpH2(AH2 a){AH2 b=AH2_AW2(AW2_(0x778d)-AW2_AH2(a));return b*(-b*a+AH2_(2.0));}
+ AH3 APrxMedRcpH3(AH3 a){AH3 b=AH3_AW3(AW3_(0x778d)-AW3_AH3(a));return b*(-b*a+AH3_(2.0));}
+ AH4 APrxMedRcpH4(AH4 a){AH4 b=AH4_AW4(AW4_(0x778d)-AW4_AH4(a));return b*(-b*a+AH4_(2.0));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Minimize squared error across {smallest normal to 16384.0}, 2 ops.
+ AH1 APrxLoRsqH1(AH1 a){return AH1_AW1(AW1_(0x59a3)-(AW1_AH1(a)>>AW1_(1)));}
+ AH2 APrxLoRsqH2(AH2 a){return AH2_AW2(AW2_(0x59a3)-(AW2_AH2(a)>>AW2_(1)));}
+ AH3 APrxLoRsqH3(AH3 a){return AH3_AW3(AW3_(0x59a3)-(AW3_AH3(a)>>AW3_(1)));}
+ AH4 APrxLoRsqH4(AH4 a){return AH4_AW4(AW4_(0x59a3)-(AW4_AH4(a)>>AW4_(1)));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// FLOAT APPROXIMATIONS
+//------------------------------------------------------------------------------------------------------------------------------
+// Michal Drobot has an excellent presentation on these: "Low Level Optimizations For GCN",
+// - Idea dates back to SGI, then to Quake 3, etc.
+// - https://michaldrobot.files.wordpress.com/2014/05/gcn_alu_opt_digitaldragons2014.pdf
+// - sqrt(x)=rsqrt(x)*x
+// - rcp(x)=rsqrt(x)*rsqrt(x) for positive x
+// - https://github.com/michaldrobot/ShaderFastLibs/blob/master/ShaderFastMathLib.h
+//------------------------------------------------------------------------------------------------------------------------------
+// These below are from perhaps less complete searching for optimal.
+// Used FP16 normal range for testing with +4096 32-bit step size for sampling error.
+// So these match up well with the half approximations.
+//==============================================================================================================================
+ AF1 APrxLoSqrtF1(AF1 a){return AF1_AU1((AU1_AF1(a)>>AU1_(1))+AU1_(0x1fbc4639));}
+ AF1 APrxLoRcpF1(AF1 a){return AF1_AU1(AU1_(0x7ef07ebb)-AU1_AF1(a));}
+ AF1 APrxMedRcpF1(AF1 a){AF1 b=AF1_AU1(AU1_(0x7ef19fff)-AU1_AF1(a));return b*(-b*a+AF1_(2.0));}
+ AF1 APrxLoRsqF1(AF1 a){return AF1_AU1(AU1_(0x5f347d74)-(AU1_AF1(a)>>AU1_(1)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 APrxLoSqrtF2(AF2 a){return AF2_AU2((AU2_AF2(a)>>AU2_(1))+AU2_(0x1fbc4639));}
+ AF2 APrxLoRcpF2(AF2 a){return AF2_AU2(AU2_(0x7ef07ebb)-AU2_AF2(a));}
+ AF2 APrxMedRcpF2(AF2 a){AF2 b=AF2_AU2(AU2_(0x7ef19fff)-AU2_AF2(a));return b*(-b*a+AF2_(2.0));}
+ AF2 APrxLoRsqF2(AF2 a){return AF2_AU2(AU2_(0x5f347d74)-(AU2_AF2(a)>>AU2_(1)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF3 APrxLoSqrtF3(AF3 a){return AF3_AU3((AU3_AF3(a)>>AU3_(1))+AU3_(0x1fbc4639));}
+ AF3 APrxLoRcpF3(AF3 a){return AF3_AU3(AU3_(0x7ef07ebb)-AU3_AF3(a));}
+ AF3 APrxMedRcpF3(AF3 a){AF3 b=AF3_AU3(AU3_(0x7ef19fff)-AU3_AF3(a));return b*(-b*a+AF3_(2.0));}
+ AF3 APrxLoRsqF3(AF3 a){return AF3_AU3(AU3_(0x5f347d74)-(AU3_AF3(a)>>AU3_(1)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF4 APrxLoSqrtF4(AF4 a){return AF4_AU4((AU4_AF4(a)>>AU4_(1))+AU4_(0x1fbc4639));}
+ AF4 APrxLoRcpF4(AF4 a){return AF4_AU4(AU4_(0x7ef07ebb)-AU4_AF4(a));}
+ AF4 APrxMedRcpF4(AF4 a){AF4 b=AF4_AU4(AU4_(0x7ef19fff)-AU4_AF4(a));return b*(-b*a+AF4_(2.0));}
+ AF4 APrxLoRsqF4(AF4 a){return AF4_AU4(AU4_(0x5f347d74)-(AU4_AF4(a)>>AU4_(1)));}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// PQ APPROXIMATIONS
+//------------------------------------------------------------------------------------------------------------------------------
+// PQ is very close to x^(1/8). The functions below Use the fast float approximation method to do
+// PQ<~>Gamma2 (4th power and fast 4th root) and PQ<~>Linear (8th power and fast 8th root). Maximum error is ~0.2%.
+//==============================================================================================================================
+// Helpers
+ AF1 Quart(AF1 a) { a = a * a; return a * a;}
+ AF1 Oct(AF1 a) { a = a * a; a = a * a; return a * a; }
+ AF2 Quart(AF2 a) { a = a * a; return a * a; }
+ AF2 Oct(AF2 a) { a = a * a; a = a * a; return a * a; }
+ AF3 Quart(AF3 a) { a = a * a; return a * a; }
+ AF3 Oct(AF3 a) { a = a * a; a = a * a; return a * a; }
+ AF4 Quart(AF4 a) { a = a * a; return a * a; }
+ AF4 Oct(AF4 a) { a = a * a; a = a * a; return a * a; }
+ //------------------------------------------------------------------------------------------------------------------------------
+ AF1 APrxPQToGamma2(AF1 a) { return Quart(a); }
+ AF1 APrxPQToLinear(AF1 a) { return Oct(a); }
+ AF1 APrxLoGamma2ToPQ(AF1 a) { return AF1_AU1((AU1_AF1(a) >> AU1_(2)) + AU1_(0x2F9A4E46)); }
+ AF1 APrxMedGamma2ToPQ(AF1 a) { AF1 b = AF1_AU1((AU1_AF1(a) >> AU1_(2)) + AU1_(0x2F9A4E46)); AF1 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
+ AF1 APrxHighGamma2ToPQ(AF1 a) { return sqrt(sqrt(a)); }
+ AF1 APrxLoLinearToPQ(AF1 a) { return AF1_AU1((AU1_AF1(a) >> AU1_(3)) + AU1_(0x378D8723)); }
+ AF1 APrxMedLinearToPQ(AF1 a) { AF1 b = AF1_AU1((AU1_AF1(a) >> AU1_(3)) + AU1_(0x378D8723)); AF1 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
+ AF1 APrxHighLinearToPQ(AF1 a) { return sqrt(sqrt(sqrt(a))); }
+ //------------------------------------------------------------------------------------------------------------------------------
+ AF2 APrxPQToGamma2(AF2 a) { return Quart(a); }
+ AF2 APrxPQToLinear(AF2 a) { return Oct(a); }
+ AF2 APrxLoGamma2ToPQ(AF2 a) { return AF2_AU2((AU2_AF2(a) >> AU2_(2)) + AU2_(0x2F9A4E46)); }
+ AF2 APrxMedGamma2ToPQ(AF2 a) { AF2 b = AF2_AU2((AU2_AF2(a) >> AU2_(2)) + AU2_(0x2F9A4E46)); AF2 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
+ AF2 APrxHighGamma2ToPQ(AF2 a) { return sqrt(sqrt(a)); }
+ AF2 APrxLoLinearToPQ(AF2 a) { return AF2_AU2((AU2_AF2(a) >> AU2_(3)) + AU2_(0x378D8723)); }
+ AF2 APrxMedLinearToPQ(AF2 a) { AF2 b = AF2_AU2((AU2_AF2(a) >> AU2_(3)) + AU2_(0x378D8723)); AF2 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
+ AF2 APrxHighLinearToPQ(AF2 a) { return sqrt(sqrt(sqrt(a))); }
+ //------------------------------------------------------------------------------------------------------------------------------
+ AF3 APrxPQToGamma2(AF3 a) { return Quart(a); }
+ AF3 APrxPQToLinear(AF3 a) { return Oct(a); }
+ AF3 APrxLoGamma2ToPQ(AF3 a) { return AF3_AU3((AU3_AF3(a) >> AU3_(2)) + AU3_(0x2F9A4E46)); }
+ AF3 APrxMedGamma2ToPQ(AF3 a) { AF3 b = AF3_AU3((AU3_AF3(a) >> AU3_(2)) + AU3_(0x2F9A4E46)); AF3 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
+ AF3 APrxHighGamma2ToPQ(AF3 a) { return sqrt(sqrt(a)); }
+ AF3 APrxLoLinearToPQ(AF3 a) { return AF3_AU3((AU3_AF3(a) >> AU3_(3)) + AU3_(0x378D8723)); }
+ AF3 APrxMedLinearToPQ(AF3 a) { AF3 b = AF3_AU3((AU3_AF3(a) >> AU3_(3)) + AU3_(0x378D8723)); AF3 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
+ AF3 APrxHighLinearToPQ(AF3 a) { return sqrt(sqrt(sqrt(a))); }
+ //------------------------------------------------------------------------------------------------------------------------------
+ AF4 APrxPQToGamma2(AF4 a) { return Quart(a); }
+ AF4 APrxPQToLinear(AF4 a) { return Oct(a); }
+ AF4 APrxLoGamma2ToPQ(AF4 a) { return AF4_AU4((AU4_AF4(a) >> AU4_(2)) + AU4_(0x2F9A4E46)); }
+ AF4 APrxMedGamma2ToPQ(AF4 a) { AF4 b = AF4_AU4((AU4_AF4(a) >> AU4_(2)) + AU4_(0x2F9A4E46)); AF4 b4 = Quart(b); return b - b * (b4 - a) / (AF1_(4.0) * b4); }
+ AF4 APrxHighGamma2ToPQ(AF4 a) { return sqrt(sqrt(a)); }
+ AF4 APrxLoLinearToPQ(AF4 a) { return AF4_AU4((AU4_AF4(a) >> AU4_(3)) + AU4_(0x378D8723)); }
+ AF4 APrxMedLinearToPQ(AF4 a) { AF4 b = AF4_AU4((AU4_AF4(a) >> AU4_(3)) + AU4_(0x378D8723)); AF4 b8 = Oct(b); return b - b * (b8 - a) / (AF1_(8.0) * b8); }
+ AF4 APrxHighLinearToPQ(AF4 a) { return sqrt(sqrt(sqrt(a))); }
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// PARABOLIC SIN & COS
+//------------------------------------------------------------------------------------------------------------------------------
+// Approximate answers to transcendental questions.
+//------------------------------------------------------------------------------------------------------------------------------
+//==============================================================================================================================
+ #if 1
+ // Valid input range is {-1 to 1} representing {0 to 2 pi}.
+ // Output range is {-1/4 to 1/4} representing {-1 to 1}.
+ AF1 APSinF1(AF1 x){return x*abs(x)-x;} // MAD.
+ AF2 APSinF2(AF2 x){return x*abs(x)-x;}
+ AF1 APCosF1(AF1 x){x=AFractF1(x*AF1_(0.5)+AF1_(0.75));x=x*AF1_(2.0)-AF1_(1.0);return APSinF1(x);} // 3x MAD, FRACT
+ AF2 APCosF2(AF2 x){x=AFractF2(x*AF2_(0.5)+AF2_(0.75));x=x*AF2_(2.0)-AF2_(1.0);return APSinF2(x);}
+ AF2 APSinCosF1(AF1 x){AF1 y=AFractF1(x*AF1_(0.5)+AF1_(0.75));y=y*AF1_(2.0)-AF1_(1.0);return APSinF2(AF2(x,y));}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_HALF
+ // For a packed {sin,cos} pair,
+ // - Native takes 16 clocks and 4 issue slots (no packed transcendentals).
+ // - Parabolic takes 8 clocks and 8 issue slots (only fract is non-packed).
+ AH1 APSinH1(AH1 x){return x*abs(x)-x;}
+ AH2 APSinH2(AH2 x){return x*abs(x)-x;} // AND,FMA
+ AH1 APCosH1(AH1 x){x=AFractH1(x*AH1_(0.5)+AH1_(0.75));x=x*AH1_(2.0)-AH1_(1.0);return APSinH1(x);}
+ AH2 APCosH2(AH2 x){x=AFractH2(x*AH2_(0.5)+AH2_(0.75));x=x*AH2_(2.0)-AH2_(1.0);return APSinH2(x);} // 3x FMA, 2xFRACT, AND
+ AH2 APSinCosH1(AH1 x){AH1 y=AFractH1(x*AH1_(0.5)+AH1_(0.75));y=y*AH1_(2.0)-AH1_(1.0);return APSinH2(AH2(x,y));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// [ZOL] ZERO ONE LOGIC
+//------------------------------------------------------------------------------------------------------------------------------
+// Conditional free logic designed for easy 16-bit packing, and backwards porting to 32-bit.
+//------------------------------------------------------------------------------------------------------------------------------
+// 0 := false
+// 1 := true
+//------------------------------------------------------------------------------------------------------------------------------
+// AndNot(x,y) -> !(x&y) .... One op.
+// AndOr(x,y,z) -> (x&y)|z ... One op.
+// GtZero(x) -> x>0.0 ..... One op.
+// Sel(x,y,z) -> x?y:z ..... Two ops, has no precision loss.
+// Signed(x) -> x<0.0 ..... One op.
+// ZeroPass(x,y) -> x?0:y ..... Two ops, 'y' is a pass through safe for aliasing as integer.
+//------------------------------------------------------------------------------------------------------------------------------
+// OPTIMIZATION NOTES
+// ==================
+// - On Vega to use 2 constants in a packed op, pass in as one AW2 or one AH2 'k.xy' and use as 'k.xx' and 'k.yy'.
+// For example 'a.xy*k.xx+k.yy'.
+//==============================================================================================================================
+ #if 1
+ AU1 AZolAndU1(AU1 x,AU1 y){return min(x,y);}
+ AU2 AZolAndU2(AU2 x,AU2 y){return min(x,y);}
+ AU3 AZolAndU3(AU3 x,AU3 y){return min(x,y);}
+ AU4 AZolAndU4(AU4 x,AU4 y){return min(x,y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AZolNotU1(AU1 x){return x^AU1_(1);}
+ AU2 AZolNotU2(AU2 x){return x^AU2_(1);}
+ AU3 AZolNotU3(AU3 x){return x^AU3_(1);}
+ AU4 AZolNotU4(AU4 x){return x^AU4_(1);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AU1 AZolOrU1(AU1 x,AU1 y){return max(x,y);}
+ AU2 AZolOrU2(AU2 x,AU2 y){return max(x,y);}
+ AU3 AZolOrU3(AU3 x,AU3 y){return max(x,y);}
+ AU4 AZolOrU4(AU4 x,AU4 y){return max(x,y);}
+//==============================================================================================================================
+ AU1 AZolF1ToU1(AF1 x){return AU1(x);}
+ AU2 AZolF2ToU2(AF2 x){return AU2(x);}
+ AU3 AZolF3ToU3(AF3 x){return AU3(x);}
+ AU4 AZolF4ToU4(AF4 x){return AU4(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // 2 ops, denormals don't work in 32-bit on PC (and if they are enabled, OMOD is disabled).
+ AU1 AZolNotF1ToU1(AF1 x){return AU1(AF1_(1.0)-x);}
+ AU2 AZolNotF2ToU2(AF2 x){return AU2(AF2_(1.0)-x);}
+ AU3 AZolNotF3ToU3(AF3 x){return AU3(AF3_(1.0)-x);}
+ AU4 AZolNotF4ToU4(AF4 x){return AU4(AF4_(1.0)-x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolU1ToF1(AU1 x){return AF1(x);}
+ AF2 AZolU2ToF2(AU2 x){return AF2(x);}
+ AF3 AZolU3ToF3(AU3 x){return AF3(x);}
+ AF4 AZolU4ToF4(AU4 x){return AF4(x);}
+//==============================================================================================================================
+ AF1 AZolAndF1(AF1 x,AF1 y){return min(x,y);}
+ AF2 AZolAndF2(AF2 x,AF2 y){return min(x,y);}
+ AF3 AZolAndF3(AF3 x,AF3 y){return min(x,y);}
+ AF4 AZolAndF4(AF4 x,AF4 y){return min(x,y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 ASolAndNotF1(AF1 x,AF1 y){return (-x)*y+AF1_(1.0);}
+ AF2 ASolAndNotF2(AF2 x,AF2 y){return (-x)*y+AF2_(1.0);}
+ AF3 ASolAndNotF3(AF3 x,AF3 y){return (-x)*y+AF3_(1.0);}
+ AF4 ASolAndNotF4(AF4 x,AF4 y){return (-x)*y+AF4_(1.0);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolAndOrF1(AF1 x,AF1 y,AF1 z){return ASatF1(x*y+z);}
+ AF2 AZolAndOrF2(AF2 x,AF2 y,AF2 z){return ASatF2(x*y+z);}
+ AF3 AZolAndOrF3(AF3 x,AF3 y,AF3 z){return ASatF3(x*y+z);}
+ AF4 AZolAndOrF4(AF4 x,AF4 y,AF4 z){return ASatF4(x*y+z);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolGtZeroF1(AF1 x){return ASatF1(x*AF1_(A_INFP_F));}
+ AF2 AZolGtZeroF2(AF2 x){return ASatF2(x*AF2_(A_INFP_F));}
+ AF3 AZolGtZeroF3(AF3 x){return ASatF3(x*AF3_(A_INFP_F));}
+ AF4 AZolGtZeroF4(AF4 x){return ASatF4(x*AF4_(A_INFP_F));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolNotF1(AF1 x){return AF1_(1.0)-x;}
+ AF2 AZolNotF2(AF2 x){return AF2_(1.0)-x;}
+ AF3 AZolNotF3(AF3 x){return AF3_(1.0)-x;}
+ AF4 AZolNotF4(AF4 x){return AF4_(1.0)-x;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolOrF1(AF1 x,AF1 y){return max(x,y);}
+ AF2 AZolOrF2(AF2 x,AF2 y){return max(x,y);}
+ AF3 AZolOrF3(AF3 x,AF3 y){return max(x,y);}
+ AF4 AZolOrF4(AF4 x,AF4 y){return max(x,y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolSelF1(AF1 x,AF1 y,AF1 z){AF1 r=(-x)*z+z;return x*y+r;}
+ AF2 AZolSelF2(AF2 x,AF2 y,AF2 z){AF2 r=(-x)*z+z;return x*y+r;}
+ AF3 AZolSelF3(AF3 x,AF3 y,AF3 z){AF3 r=(-x)*z+z;return x*y+r;}
+ AF4 AZolSelF4(AF4 x,AF4 y,AF4 z){AF4 r=(-x)*z+z;return x*y+r;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolSignedF1(AF1 x){return ASatF1(x*AF1_(A_INFN_F));}
+ AF2 AZolSignedF2(AF2 x){return ASatF2(x*AF2_(A_INFN_F));}
+ AF3 AZolSignedF3(AF3 x){return ASatF3(x*AF3_(A_INFN_F));}
+ AF4 AZolSignedF4(AF4 x){return ASatF4(x*AF4_(A_INFN_F));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AZolZeroPassF1(AF1 x,AF1 y){return AF1_AU1((AU1_AF1(x)!=AU1_(0))?AU1_(0):AU1_AF1(y));}
+ AF2 AZolZeroPassF2(AF2 x,AF2 y){return AF2_AU2((AU2_AF2(x)!=AU2_(0))?AU2_(0):AU2_AF2(y));}
+ AF3 AZolZeroPassF3(AF3 x,AF3 y){return AF3_AU3((AU3_AF3(x)!=AU3_(0))?AU3_(0):AU3_AF3(y));}
+ AF4 AZolZeroPassF4(AF4 x,AF4 y){return AF4_AU4((AU4_AF4(x)!=AU4_(0))?AU4_(0):AU4_AF4(y));}
+ #endif
+//==============================================================================================================================
+ #ifdef A_HALF
+ AW1 AZolAndW1(AW1 x,AW1 y){return min(x,y);}
+ AW2 AZolAndW2(AW2 x,AW2 y){return min(x,y);}
+ AW3 AZolAndW3(AW3 x,AW3 y){return min(x,y);}
+ AW4 AZolAndW4(AW4 x,AW4 y){return min(x,y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AZolNotW1(AW1 x){return x^AW1_(1);}
+ AW2 AZolNotW2(AW2 x){return x^AW2_(1);}
+ AW3 AZolNotW3(AW3 x){return x^AW3_(1);}
+ AW4 AZolNotW4(AW4 x){return x^AW4_(1);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AW1 AZolOrW1(AW1 x,AW1 y){return max(x,y);}
+ AW2 AZolOrW2(AW2 x,AW2 y){return max(x,y);}
+ AW3 AZolOrW3(AW3 x,AW3 y){return max(x,y);}
+ AW4 AZolOrW4(AW4 x,AW4 y){return max(x,y);}
+//==============================================================================================================================
+ // Uses denormal trick.
+ AW1 AZolH1ToW1(AH1 x){return AW1_AH1(x*AH1_AW1(AW1_(1)));}
+ AW2 AZolH2ToW2(AH2 x){return AW2_AH2(x*AH2_AW2(AW2_(1)));}
+ AW3 AZolH3ToW3(AH3 x){return AW3_AH3(x*AH3_AW3(AW3_(1)));}
+ AW4 AZolH4ToW4(AH4 x){return AW4_AH4(x*AH4_AW4(AW4_(1)));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // AMD arch lacks a packed conversion opcode.
+ AH1 AZolW1ToH1(AW1 x){return AH1_AW1(x*AW1_AH1(AH1_(1.0)));}
+ AH2 AZolW2ToH2(AW2 x){return AH2_AW2(x*AW2_AH2(AH2_(1.0)));}
+ AH3 AZolW1ToH3(AW3 x){return AH3_AW3(x*AW3_AH3(AH3_(1.0)));}
+ AH4 AZolW2ToH4(AW4 x){return AH4_AW4(x*AW4_AH4(AH4_(1.0)));}
+//==============================================================================================================================
+ AH1 AZolAndH1(AH1 x,AH1 y){return min(x,y);}
+ AH2 AZolAndH2(AH2 x,AH2 y){return min(x,y);}
+ AH3 AZolAndH3(AH3 x,AH3 y){return min(x,y);}
+ AH4 AZolAndH4(AH4 x,AH4 y){return min(x,y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 ASolAndNotH1(AH1 x,AH1 y){return (-x)*y+AH1_(1.0);}
+ AH2 ASolAndNotH2(AH2 x,AH2 y){return (-x)*y+AH2_(1.0);}
+ AH3 ASolAndNotH3(AH3 x,AH3 y){return (-x)*y+AH3_(1.0);}
+ AH4 ASolAndNotH4(AH4 x,AH4 y){return (-x)*y+AH4_(1.0);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AZolAndOrH1(AH1 x,AH1 y,AH1 z){return ASatH1(x*y+z);}
+ AH2 AZolAndOrH2(AH2 x,AH2 y,AH2 z){return ASatH2(x*y+z);}
+ AH3 AZolAndOrH3(AH3 x,AH3 y,AH3 z){return ASatH3(x*y+z);}
+ AH4 AZolAndOrH4(AH4 x,AH4 y,AH4 z){return ASatH4(x*y+z);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AZolGtZeroH1(AH1 x){return ASatH1(x*AH1_(A_INFP_H));}
+ AH2 AZolGtZeroH2(AH2 x){return ASatH2(x*AH2_(A_INFP_H));}
+ AH3 AZolGtZeroH3(AH3 x){return ASatH3(x*AH3_(A_INFP_H));}
+ AH4 AZolGtZeroH4(AH4 x){return ASatH4(x*AH4_(A_INFP_H));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AZolNotH1(AH1 x){return AH1_(1.0)-x;}
+ AH2 AZolNotH2(AH2 x){return AH2_(1.0)-x;}
+ AH3 AZolNotH3(AH3 x){return AH3_(1.0)-x;}
+ AH4 AZolNotH4(AH4 x){return AH4_(1.0)-x;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AZolOrH1(AH1 x,AH1 y){return max(x,y);}
+ AH2 AZolOrH2(AH2 x,AH2 y){return max(x,y);}
+ AH3 AZolOrH3(AH3 x,AH3 y){return max(x,y);}
+ AH4 AZolOrH4(AH4 x,AH4 y){return max(x,y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AZolSelH1(AH1 x,AH1 y,AH1 z){AH1 r=(-x)*z+z;return x*y+r;}
+ AH2 AZolSelH2(AH2 x,AH2 y,AH2 z){AH2 r=(-x)*z+z;return x*y+r;}
+ AH3 AZolSelH3(AH3 x,AH3 y,AH3 z){AH3 r=(-x)*z+z;return x*y+r;}
+ AH4 AZolSelH4(AH4 x,AH4 y,AH4 z){AH4 r=(-x)*z+z;return x*y+r;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AZolSignedH1(AH1 x){return ASatH1(x*AH1_(A_INFN_H));}
+ AH2 AZolSignedH2(AH2 x){return ASatH2(x*AH2_(A_INFN_H));}
+ AH3 AZolSignedH3(AH3 x){return ASatH3(x*AH3_(A_INFN_H));}
+ AH4 AZolSignedH4(AH4 x){return ASatH4(x*AH4_(A_INFN_H));}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// COLOR CONVERSIONS
+//------------------------------------------------------------------------------------------------------------------------------
+// These are all linear to/from some other space (where 'linear' has been shortened out of the function name).
+// So 'ToGamma' is 'LinearToGamma', and 'FromGamma' is 'LinearFromGamma'.
+// These are branch free implementations.
+// The AToSrgbF1() function is useful for stores for compute shaders for GPUs without hardware linear->sRGB store conversion.
+//------------------------------------------------------------------------------------------------------------------------------
+// TRANSFER FUNCTIONS
+// ==================
+// 709 ..... Rec709 used for some HDTVs
+// Gamma ... Typically 2.2 for some PC displays, or 2.4-2.5 for CRTs, or 2.2 FreeSync2 native
+// Pq ...... PQ native for HDR10
+// Srgb .... The sRGB output, typical of PC displays, useful for 10-bit output, or storing to 8-bit UNORM without SRGB type
+// Two ..... Gamma 2.0, fastest conversion (useful for intermediate pass approximations)
+// Three ... Gamma 3.0, less fast, but good for HDR.
+//------------------------------------------------------------------------------------------------------------------------------
+// KEEPING TO SPEC
+// ===============
+// Both Rec.709 and sRGB have a linear segment which as spec'ed would intersect the curved segment 2 times.
+// (a.) For 8-bit sRGB, steps {0 to 10.3} are in the linear region (4% of the encoding range).
+// (b.) For 8-bit 709, steps {0 to 20.7} are in the linear region (8% of the encoding range).
+// Also there is a slight step in the transition regions.
+// Precision of the coefficients in the spec being the likely cause.
+// Main usage case of the sRGB code is to do the linear->sRGB converstion in a compute shader before store.
+// This is to work around lack of hardware (typically only ROP does the conversion for free).
+// To "correct" the linear segment, would be to introduce error, because hardware decode of sRGB->linear is fixed (and free).
+// So this header keeps with the spec.
+// For linear->sRGB transforms, the linear segment in some respects reduces error, because rounding in that region is linear.
+// Rounding in the curved region in hardware (and fast software code) introduces error due to rounding in non-linear.
+//------------------------------------------------------------------------------------------------------------------------------
+// FOR PQ
+// ======
+// Both input and output is {0.0-1.0}, and where output 1.0 represents 10000.0 cd/m^2.
+// All constants are only specified to FP32 precision.
+// External PQ source reference,
+// - https://github.com/ampas/aces-dev/blob/master/transforms/ctl/utilities/ACESlib.Utilities_Color.a1.0.1.ctl
+//------------------------------------------------------------------------------------------------------------------------------
+// PACKED VERSIONS
+// ===============
+// These are the A*H2() functions.
+// There is no PQ functions as FP16 seemed to not have enough precision for the conversion.
+// The remaining functions are "good enough" for 8-bit, and maybe 10-bit if not concerned about a few 1-bit errors.
+// Precision is lowest in the 709 conversion, higher in sRGB, higher still in Two and Gamma (when using 2.2 at least).
+//------------------------------------------------------------------------------------------------------------------------------
+// NOTES
+// =====
+// Could be faster for PQ conversions to be in ALU or a texture lookup depending on usage case.
+//==============================================================================================================================
+ #if 1
+ AF1 ATo709F1(AF1 c){AF3 j=AF3(0.018*4.5,4.5,0.45);AF2 k=AF2(1.099,-0.099);
+ return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
+ AF2 ATo709F2(AF2 c){AF3 j=AF3(0.018*4.5,4.5,0.45);AF2 k=AF2(1.099,-0.099);
+ return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
+ AF3 ATo709F3(AF3 c){AF3 j=AF3(0.018*4.5,4.5,0.45);AF2 k=AF2(1.099,-0.099);
+ return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Note 'rcpX' is '1/x', where the 'x' is what would be used in AFromGamma().
+ AF1 AToGammaF1(AF1 c,AF1 rcpX){return pow(c,AF1_(rcpX));}
+ AF2 AToGammaF2(AF2 c,AF1 rcpX){return pow(c,AF2_(rcpX));}
+ AF3 AToGammaF3(AF3 c,AF1 rcpX){return pow(c,AF3_(rcpX));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AToPqF1(AF1 x){AF1 p=pow(x,AF1_(0.159302));
+ return pow((AF1_(0.835938)+AF1_(18.8516)*p)/(AF1_(1.0)+AF1_(18.6875)*p),AF1_(78.8438));}
+ AF2 AToPqF1(AF2 x){AF2 p=pow(x,AF2_(0.159302));
+ return pow((AF2_(0.835938)+AF2_(18.8516)*p)/(AF2_(1.0)+AF2_(18.6875)*p),AF2_(78.8438));}
+ AF3 AToPqF1(AF3 x){AF3 p=pow(x,AF3_(0.159302));
+ return pow((AF3_(0.835938)+AF3_(18.8516)*p)/(AF3_(1.0)+AF3_(18.6875)*p),AF3_(78.8438));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AToSrgbF1(AF1 c){AF3 j=AF3(0.0031308*12.92,12.92,1.0/2.4);AF2 k=AF2(1.055,-0.055);
+ return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
+ AF2 AToSrgbF2(AF2 c){AF3 j=AF3(0.0031308*12.92,12.92,1.0/2.4);AF2 k=AF2(1.055,-0.055);
+ return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
+ AF3 AToSrgbF3(AF3 c){AF3 j=AF3(0.0031308*12.92,12.92,1.0/2.4);AF2 k=AF2(1.055,-0.055);
+ return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AToTwoF1(AF1 c){return sqrt(c);}
+ AF2 AToTwoF2(AF2 c){return sqrt(c);}
+ AF3 AToTwoF3(AF3 c){return sqrt(c);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AToThreeF1(AF1 c){return pow(c,AF1_(1.0/3.0));}
+ AF2 AToThreeF2(AF2 c){return pow(c,AF2_(1.0/3.0));}
+ AF3 AToThreeF3(AF3 c){return pow(c,AF3_(1.0/3.0));}
+ #endif
+//==============================================================================================================================
+ #if 1
+ // Unfortunately median won't work here.
+ AF1 AFrom709F1(AF1 c){AF3 j=AF3(0.081/4.5,1.0/4.5,1.0/0.45);AF2 k=AF2(1.0/1.099,0.099/1.099);
+ return AZolSelF1(AZolSignedF1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
+ AF2 AFrom709F2(AF2 c){AF3 j=AF3(0.081/4.5,1.0/4.5,1.0/0.45);AF2 k=AF2(1.0/1.099,0.099/1.099);
+ return AZolSelF2(AZolSignedF2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
+ AF3 AFrom709F3(AF3 c){AF3 j=AF3(0.081/4.5,1.0/4.5,1.0/0.45);AF2 k=AF2(1.0/1.099,0.099/1.099);
+ return AZolSelF3(AZolSignedF3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AFromGammaF1(AF1 c,AF1 x){return pow(c,AF1_(x));}
+ AF2 AFromGammaF2(AF2 c,AF1 x){return pow(c,AF2_(x));}
+ AF3 AFromGammaF3(AF3 c,AF1 x){return pow(c,AF3_(x));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AFromPqF1(AF1 x){AF1 p=pow(x,AF1_(0.0126833));
+ return pow(ASatF1(p-AF1_(0.835938))/(AF1_(18.8516)-AF1_(18.6875)*p),AF1_(6.27739));}
+ AF2 AFromPqF1(AF2 x){AF2 p=pow(x,AF2_(0.0126833));
+ return pow(ASatF2(p-AF2_(0.835938))/(AF2_(18.8516)-AF2_(18.6875)*p),AF2_(6.27739));}
+ AF3 AFromPqF1(AF3 x){AF3 p=pow(x,AF3_(0.0126833));
+ return pow(ASatF3(p-AF3_(0.835938))/(AF3_(18.8516)-AF3_(18.6875)*p),AF3_(6.27739));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Unfortunately median won't work here.
+ AF1 AFromSrgbF1(AF1 c){AF3 j=AF3(0.04045/12.92,1.0/12.92,2.4);AF2 k=AF2(1.0/1.055,0.055/1.055);
+ return AZolSelF1(AZolSignedF1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
+ AF2 AFromSrgbF2(AF2 c){AF3 j=AF3(0.04045/12.92,1.0/12.92,2.4);AF2 k=AF2(1.0/1.055,0.055/1.055);
+ return AZolSelF2(AZolSignedF2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
+ AF3 AFromSrgbF3(AF3 c){AF3 j=AF3(0.04045/12.92,1.0/12.92,2.4);AF2 k=AF2(1.0/1.055,0.055/1.055);
+ return AZolSelF3(AZolSignedF3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AFromTwoF1(AF1 c){return c*c;}
+ AF2 AFromTwoF2(AF2 c){return c*c;}
+ AF3 AFromTwoF3(AF3 c){return c*c;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF1 AFromThreeF1(AF1 c){return c*c*c;}
+ AF2 AFromThreeF2(AF2 c){return c*c*c;}
+ AF3 AFromThreeF3(AF3 c){return c*c*c;}
+ #endif
+//==============================================================================================================================
+ #ifdef A_HALF
+ AH1 ATo709H1(AH1 c){AH3 j=AH3(0.018*4.5,4.5,0.45);AH2 k=AH2(1.099,-0.099);
+ return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
+ AH2 ATo709H2(AH2 c){AH3 j=AH3(0.018*4.5,4.5,0.45);AH2 k=AH2(1.099,-0.099);
+ return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
+ AH3 ATo709H3(AH3 c){AH3 j=AH3(0.018*4.5,4.5,0.45);AH2 k=AH2(1.099,-0.099);
+ return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AToGammaH1(AH1 c,AH1 rcpX){return pow(c,AH1_(rcpX));}
+ AH2 AToGammaH2(AH2 c,AH1 rcpX){return pow(c,AH2_(rcpX));}
+ AH3 AToGammaH3(AH3 c,AH1 rcpX){return pow(c,AH3_(rcpX));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AToSrgbH1(AH1 c){AH3 j=AH3(0.0031308*12.92,12.92,1.0/2.4);AH2 k=AH2(1.055,-0.055);
+ return clamp(j.x ,c*j.y ,pow(c,j.z )*k.x +k.y );}
+ AH2 AToSrgbH2(AH2 c){AH3 j=AH3(0.0031308*12.92,12.92,1.0/2.4);AH2 k=AH2(1.055,-0.055);
+ return clamp(j.xx ,c*j.yy ,pow(c,j.zz )*k.xx +k.yy );}
+ AH3 AToSrgbH3(AH3 c){AH3 j=AH3(0.0031308*12.92,12.92,1.0/2.4);AH2 k=AH2(1.055,-0.055);
+ return clamp(j.xxx,c*j.yyy,pow(c,j.zzz)*k.xxx+k.yyy);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AToTwoH1(AH1 c){return sqrt(c);}
+ AH2 AToTwoH2(AH2 c){return sqrt(c);}
+ AH3 AToTwoH3(AH3 c){return sqrt(c);}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AToThreeF1(AH1 c){return pow(c,AH1_(1.0/3.0));}
+ AH2 AToThreeF2(AH2 c){return pow(c,AH2_(1.0/3.0));}
+ AH3 AToThreeF3(AH3 c){return pow(c,AH3_(1.0/3.0));}
+ #endif
+//==============================================================================================================================
+ #ifdef A_HALF
+ AH1 AFrom709H1(AH1 c){AH3 j=AH3(0.081/4.5,1.0/4.5,1.0/0.45);AH2 k=AH2(1.0/1.099,0.099/1.099);
+ return AZolSelH1(AZolSignedH1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
+ AH2 AFrom709H2(AH2 c){AH3 j=AH3(0.081/4.5,1.0/4.5,1.0/0.45);AH2 k=AH2(1.0/1.099,0.099/1.099);
+ return AZolSelH2(AZolSignedH2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
+ AH3 AFrom709H3(AH3 c){AH3 j=AH3(0.081/4.5,1.0/4.5,1.0/0.45);AH2 k=AH2(1.0/1.099,0.099/1.099);
+ return AZolSelH3(AZolSignedH3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AFromGammaH1(AH1 c,AH1 x){return pow(c,AH1_(x));}
+ AH2 AFromGammaH2(AH2 c,AH1 x){return pow(c,AH2_(x));}
+ AH3 AFromGammaH3(AH3 c,AH1 x){return pow(c,AH3_(x));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AHromSrgbF1(AH1 c){AH3 j=AH3(0.04045/12.92,1.0/12.92,2.4);AH2 k=AH2(1.0/1.055,0.055/1.055);
+ return AZolSelH1(AZolSignedH1(c-j.x ),c*j.y ,pow(c*k.x +k.y ,j.z ));}
+ AH2 AHromSrgbF2(AH2 c){AH3 j=AH3(0.04045/12.92,1.0/12.92,2.4);AH2 k=AH2(1.0/1.055,0.055/1.055);
+ return AZolSelH2(AZolSignedH2(c-j.xx ),c*j.yy ,pow(c*k.xx +k.yy ,j.zz ));}
+ AH3 AHromSrgbF3(AH3 c){AH3 j=AH3(0.04045/12.92,1.0/12.92,2.4);AH2 k=AH2(1.0/1.055,0.055/1.055);
+ return AZolSelH3(AZolSignedH3(c-j.xxx),c*j.yyy,pow(c*k.xxx+k.yyy,j.zzz));}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AFromTwoH1(AH1 c){return c*c;}
+ AH2 AFromTwoH2(AH2 c){return c*c;}
+ AH3 AFromTwoH3(AH3 c){return c*c;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AH1 AFromThreeH1(AH1 c){return c*c*c;}
+ AH2 AFromThreeH2(AH2 c){return c*c*c;}
+ AH3 AFromThreeH3(AH3 c){return c*c*c;}
+ #endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// CS REMAP
+//==============================================================================================================================
+ // Simple remap 64x1 to 8x8 with rotated 2x2 pixel quads in quad linear.
+ // 543210
+ // ======
+ // ..xxx.
+ // yy...y
+ AU2 ARmp8x8(AU1 a){return AU2(ABfe(a,1u,3u),ABfiM(ABfe(a,3u,3u),a,1u));}
+//==============================================================================================================================
+ // More complex remap 64x1 to 8x8 which is necessary for 2D wave reductions.
+ // 543210
+ // ======
+ // .xx..x
+ // y..yy.
+ // Details,
+ // LANE TO 8x8 MAPPING
+ // ===================
+ // 00 01 08 09 10 11 18 19
+ // 02 03 0a 0b 12 13 1a 1b
+ // 04 05 0c 0d 14 15 1c 1d
+ // 06 07 0e 0f 16 17 1e 1f
+ // 20 21 28 29 30 31 38 39
+ // 22 23 2a 2b 32 33 3a 3b
+ // 24 25 2c 2d 34 35 3c 3d
+ // 26 27 2e 2f 36 37 3e 3f
+ AU2 ARmpRed8x8(AU1 a){return AU2(ABfiM(ABfe(a,2u,3u),a,1u),ABfiM(ABfe(a,3u,3u),ABfe(a,1u,2u),2u));}
+//==============================================================================================================================
+ #ifdef A_HALF
+ AW2 ARmp8x8H(AU1 a){return AW2(ABfe(a,1u,3u),ABfiM(ABfe(a,3u,3u),a,1u));}
+ AW2 ARmpRed8x8H(AU1 a){return AW2(ABfiM(ABfe(a,2u,3u),a,1u),ABfiM(ABfe(a,3u,3u),ABfe(a,1u,2u),2u));}
+ #endif
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// REFERENCE
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// IEEE FLOAT RULES
+// ================
+// - saturate(NaN)=0, saturate(-INF)=0, saturate(+INF)=1
+// - {+/-}0 * {+/-}INF = NaN
+// - -INF + (+INF) = NaN
+// - {+/-}0 / {+/-}0 = NaN
+// - {+/-}INF / {+/-}INF = NaN
+// - a<(-0) := sqrt(a) = NaN (a=-0.0 won't NaN)
+// - 0 == -0
+// - 4/0 = +INF
+// - 4/-0 = -INF
+// - 4+INF = +INF
+// - 4-INF = -INF
+// - 4*(+INF) = +INF
+// - 4*(-INF) = -INF
+// - -4*(+INF) = -INF
+// - sqrt(+INF) = +INF
+//------------------------------------------------------------------------------------------------------------------------------
+// FP16 ENCODING
+// =============
+// fedcba9876543210
+// ----------------
+// ......mmmmmmmmmm 10-bit mantissa (encodes 11-bit 0.5 to 1.0 except for denormals)
+// .eeeee.......... 5-bit exponent
+// .00000.......... denormals
+// .00001.......... -14 exponent
+// .11110.......... 15 exponent
+// .111110000000000 infinity
+// .11111nnnnnnnnnn NaN with n!=0
+// s............... sign
+//------------------------------------------------------------------------------------------------------------------------------
+// FP16/INT16 ALIASING DENORMAL
+// ============================
+// 11-bit unsigned integers alias with half float denormal/normal values,
+// 1 = 2^(-24) = 1/16777216 ....................... first denormal value
+// 2 = 2^(-23)
+// ...
+// 1023 = 2^(-14)*(1-2^(-10)) = 2^(-14)*(1-1/1024) ... last denormal value
+// 1024 = 2^(-14) = 1/16384 .......................... first normal value that still maps to integers
+// 2047 .............................................. last normal value that still maps to integers
+// Scaling limits,
+// 2^15 = 32768 ...................................... largest power of 2 scaling
+// Largest pow2 conversion mapping is at *32768,
+// 1 : 2^(-9) = 1/512
+// 2 : 1/256
+// 4 : 1/128
+// 8 : 1/64
+// 16 : 1/32
+// 32 : 1/16
+// 64 : 1/8
+// 128 : 1/4
+// 256 : 1/2
+// 512 : 1
+// 1024 : 2
+// 2047 : a little less than 4
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// GPU/CPU PORTABILITY
+//
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// This is the GPU implementation.
+// See the CPU implementation for docs.
+//==============================================================================================================================
+#ifdef A_GPU
+ #define A_TRUE true
+ #define A_FALSE false
+ #define A_STATIC
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// VECTOR ARGUMENT/RETURN/INITIALIZATION PORTABILITY
+//==============================================================================================================================
+ #define retAD2 AD2
+ #define retAD3 AD3
+ #define retAD4 AD4
+ #define retAF2 AF2
+ #define retAF3 AF3
+ #define retAF4 AF4
+ #define retAL2 AL2
+ #define retAL3 AL3
+ #define retAL4 AL4
+ #define retAU2 AU2
+ #define retAU3 AU3
+ #define retAU4 AU4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define inAD2 in AD2
+ #define inAD3 in AD3
+ #define inAD4 in AD4
+ #define inAF2 in AF2
+ #define inAF3 in AF3
+ #define inAF4 in AF4
+ #define inAL2 in AL2
+ #define inAL3 in AL3
+ #define inAL4 in AL4
+ #define inAU2 in AU2
+ #define inAU3 in AU3
+ #define inAU4 in AU4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define inoutAD2 inout AD2
+ #define inoutAD3 inout AD3
+ #define inoutAD4 inout AD4
+ #define inoutAF2 inout AF2
+ #define inoutAF3 inout AF3
+ #define inoutAF4 inout AF4
+ #define inoutAL2 inout AL2
+ #define inoutAL3 inout AL3
+ #define inoutAL4 inout AL4
+ #define inoutAU2 inout AU2
+ #define inoutAU3 inout AU3
+ #define inoutAU4 inout AU4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define outAD2 out AD2
+ #define outAD3 out AD3
+ #define outAD4 out AD4
+ #define outAF2 out AF2
+ #define outAF3 out AF3
+ #define outAF4 out AF4
+ #define outAL2 out AL2
+ #define outAL3 out AL3
+ #define outAL4 out AL4
+ #define outAU2 out AU2
+ #define outAU3 out AU3
+ #define outAU4 out AU4
+//------------------------------------------------------------------------------------------------------------------------------
+ #define varAD2(x) AD2 x
+ #define varAD3(x) AD3 x
+ #define varAD4(x) AD4 x
+ #define varAF2(x) AF2 x
+ #define varAF3(x) AF3 x
+ #define varAF4(x) AF4 x
+ #define varAL2(x) AL2 x
+ #define varAL3(x) AL3 x
+ #define varAL4(x) AL4 x
+ #define varAU2(x) AU2 x
+ #define varAU3(x) AU3 x
+ #define varAU4(x) AU4 x
+//------------------------------------------------------------------------------------------------------------------------------
+ #define initAD2(x,y) AD2(x,y)
+ #define initAD3(x,y,z) AD3(x,y,z)
+ #define initAD4(x,y,z,w) AD4(x,y,z,w)
+ #define initAF2(x,y) AF2(x,y)
+ #define initAF3(x,y,z) AF3(x,y,z)
+ #define initAF4(x,y,z,w) AF4(x,y,z,w)
+ #define initAL2(x,y) AL2(x,y)
+ #define initAL3(x,y,z) AL3(x,y,z)
+ #define initAL4(x,y,z,w) AL4(x,y,z,w)
+ #define initAU2(x,y) AU2(x,y)
+ #define initAU3(x,y,z) AU3(x,y,z)
+ #define initAU4(x,y,z,w) AU4(x,y,z,w)
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// SCALAR RETURN OPS
+//==============================================================================================================================
+ #define AAbsD1(a) abs(AD1(a))
+ #define AAbsF1(a) abs(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ACosD1(a) cos(AD1(a))
+ #define ACosF1(a) cos(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ADotD2(a,b) dot(AD2(a),AD2(b))
+ #define ADotD3(a,b) dot(AD3(a),AD3(b))
+ #define ADotD4(a,b) dot(AD4(a),AD4(b))
+ #define ADotF2(a,b) dot(AF2(a),AF2(b))
+ #define ADotF3(a,b) dot(AF3(a),AF3(b))
+ #define ADotF4(a,b) dot(AF4(a),AF4(b))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AExp2D1(a) exp2(AD1(a))
+ #define AExp2F1(a) exp2(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AFloorD1(a) floor(AD1(a))
+ #define AFloorF1(a) floor(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ALog2D1(a) log2(AD1(a))
+ #define ALog2F1(a) log2(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AMaxD1(a,b) max(a,b)
+ #define AMaxF1(a,b) max(a,b)
+ #define AMaxL1(a,b) max(a,b)
+ #define AMaxU1(a,b) max(a,b)
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AMinD1(a,b) min(a,b)
+ #define AMinF1(a,b) min(a,b)
+ #define AMinL1(a,b) min(a,b)
+ #define AMinU1(a,b) min(a,b)
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASinD1(a) sin(AD1(a))
+ #define ASinF1(a) sin(AF1(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASqrtD1(a) sqrt(AD1(a))
+ #define ASqrtF1(a) sqrt(AF1(a))
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// SCALAR RETURN OPS - DEPENDENT
+//==============================================================================================================================
+ #define APowD1(a,b) pow(AD1(a),AF1(b))
+ #define APowF1(a,b) pow(AF1(a),AF1(b))
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// VECTOR OPS
+//------------------------------------------------------------------------------------------------------------------------------
+// These are added as needed for production or prototyping, so not necessarily a complete set.
+// They follow a convention of taking in a destination and also returning the destination value to increase utility.
+//==============================================================================================================================
+ #ifdef A_DUBL
+ AD2 opAAbsD2(outAD2 d,inAD2 a){d=abs(a);return d;}
+ AD3 opAAbsD3(outAD3 d,inAD3 a){d=abs(a);return d;}
+ AD4 opAAbsD4(outAD4 d,inAD4 a){d=abs(a);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opAAddD2(outAD2 d,inAD2 a,inAD2 b){d=a+b;return d;}
+ AD3 opAAddD3(outAD3 d,inAD3 a,inAD3 b){d=a+b;return d;}
+ AD4 opAAddD4(outAD4 d,inAD4 a,inAD4 b){d=a+b;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opAAddOneD2(outAD2 d,inAD2 a,AD1 b){d=a+AD2_(b);return d;}
+ AD3 opAAddOneD3(outAD3 d,inAD3 a,AD1 b){d=a+AD3_(b);return d;}
+ AD4 opAAddOneD4(outAD4 d,inAD4 a,AD1 b){d=a+AD4_(b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opACpyD2(outAD2 d,inAD2 a){d=a;return d;}
+ AD3 opACpyD3(outAD3 d,inAD3 a){d=a;return d;}
+ AD4 opACpyD4(outAD4 d,inAD4 a){d=a;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opALerpD2(outAD2 d,inAD2 a,inAD2 b,inAD2 c){d=ALerpD2(a,b,c);return d;}
+ AD3 opALerpD3(outAD3 d,inAD3 a,inAD3 b,inAD3 c){d=ALerpD3(a,b,c);return d;}
+ AD4 opALerpD4(outAD4 d,inAD4 a,inAD4 b,inAD4 c){d=ALerpD4(a,b,c);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opALerpOneD2(outAD2 d,inAD2 a,inAD2 b,AD1 c){d=ALerpD2(a,b,AD2_(c));return d;}
+ AD3 opALerpOneD3(outAD3 d,inAD3 a,inAD3 b,AD1 c){d=ALerpD3(a,b,AD3_(c));return d;}
+ AD4 opALerpOneD4(outAD4 d,inAD4 a,inAD4 b,AD1 c){d=ALerpD4(a,b,AD4_(c));return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opAMaxD2(outAD2 d,inAD2 a,inAD2 b){d=max(a,b);return d;}
+ AD3 opAMaxD3(outAD3 d,inAD3 a,inAD3 b){d=max(a,b);return d;}
+ AD4 opAMaxD4(outAD4 d,inAD4 a,inAD4 b){d=max(a,b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opAMinD2(outAD2 d,inAD2 a,inAD2 b){d=min(a,b);return d;}
+ AD3 opAMinD3(outAD3 d,inAD3 a,inAD3 b){d=min(a,b);return d;}
+ AD4 opAMinD4(outAD4 d,inAD4 a,inAD4 b){d=min(a,b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opAMulD2(outAD2 d,inAD2 a,inAD2 b){d=a*b;return d;}
+ AD3 opAMulD3(outAD3 d,inAD3 a,inAD3 b){d=a*b;return d;}
+ AD4 opAMulD4(outAD4 d,inAD4 a,inAD4 b){d=a*b;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opAMulOneD2(outAD2 d,inAD2 a,AD1 b){d=a*AD2_(b);return d;}
+ AD3 opAMulOneD3(outAD3 d,inAD3 a,AD1 b){d=a*AD3_(b);return d;}
+ AD4 opAMulOneD4(outAD4 d,inAD4 a,AD1 b){d=a*AD4_(b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opANegD2(outAD2 d,inAD2 a){d=-a;return d;}
+ AD3 opANegD3(outAD3 d,inAD3 a){d=-a;return d;}
+ AD4 opANegD4(outAD4 d,inAD4 a){d=-a;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AD2 opARcpD2(outAD2 d,inAD2 a){d=ARcpD2(a);return d;}
+ AD3 opARcpD3(outAD3 d,inAD3 a){d=ARcpD3(a);return d;}
+ AD4 opARcpD4(outAD4 d,inAD4 a){d=ARcpD4(a);return d;}
+ #endif
+//==============================================================================================================================
+ AF2 opAAbsF2(outAF2 d,inAF2 a){d=abs(a);return d;}
+ AF3 opAAbsF3(outAF3 d,inAF3 a){d=abs(a);return d;}
+ AF4 opAAbsF4(outAF4 d,inAF4 a){d=abs(a);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opAAddF2(outAF2 d,inAF2 a,inAF2 b){d=a+b;return d;}
+ AF3 opAAddF3(outAF3 d,inAF3 a,inAF3 b){d=a+b;return d;}
+ AF4 opAAddF4(outAF4 d,inAF4 a,inAF4 b){d=a+b;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opAAddOneF2(outAF2 d,inAF2 a,AF1 b){d=a+AF2_(b);return d;}
+ AF3 opAAddOneF3(outAF3 d,inAF3 a,AF1 b){d=a+AF3_(b);return d;}
+ AF4 opAAddOneF4(outAF4 d,inAF4 a,AF1 b){d=a+AF4_(b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opACpyF2(outAF2 d,inAF2 a){d=a;return d;}
+ AF3 opACpyF3(outAF3 d,inAF3 a){d=a;return d;}
+ AF4 opACpyF4(outAF4 d,inAF4 a){d=a;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opALerpF2(outAF2 d,inAF2 a,inAF2 b,inAF2 c){d=ALerpF2(a,b,c);return d;}
+ AF3 opALerpF3(outAF3 d,inAF3 a,inAF3 b,inAF3 c){d=ALerpF3(a,b,c);return d;}
+ AF4 opALerpF4(outAF4 d,inAF4 a,inAF4 b,inAF4 c){d=ALerpF4(a,b,c);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opALerpOneF2(outAF2 d,inAF2 a,inAF2 b,AF1 c){d=ALerpF2(a,b,AF2_(c));return d;}
+ AF3 opALerpOneF3(outAF3 d,inAF3 a,inAF3 b,AF1 c){d=ALerpF3(a,b,AF3_(c));return d;}
+ AF4 opALerpOneF4(outAF4 d,inAF4 a,inAF4 b,AF1 c){d=ALerpF4(a,b,AF4_(c));return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opAMaxF2(outAF2 d,inAF2 a,inAF2 b){d=max(a,b);return d;}
+ AF3 opAMaxF3(outAF3 d,inAF3 a,inAF3 b){d=max(a,b);return d;}
+ AF4 opAMaxF4(outAF4 d,inAF4 a,inAF4 b){d=max(a,b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opAMinF2(outAF2 d,inAF2 a,inAF2 b){d=min(a,b);return d;}
+ AF3 opAMinF3(outAF3 d,inAF3 a,inAF3 b){d=min(a,b);return d;}
+ AF4 opAMinF4(outAF4 d,inAF4 a,inAF4 b){d=min(a,b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opAMulF2(outAF2 d,inAF2 a,inAF2 b){d=a*b;return d;}
+ AF3 opAMulF3(outAF3 d,inAF3 a,inAF3 b){d=a*b;return d;}
+ AF4 opAMulF4(outAF4 d,inAF4 a,inAF4 b){d=a*b;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opAMulOneF2(outAF2 d,inAF2 a,AF1 b){d=a*AF2_(b);return d;}
+ AF3 opAMulOneF3(outAF3 d,inAF3 a,AF1 b){d=a*AF3_(b);return d;}
+ AF4 opAMulOneF4(outAF4 d,inAF4 a,AF1 b){d=a*AF4_(b);return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opANegF2(outAF2 d,inAF2 a){d=-a;return d;}
+ AF3 opANegF3(outAF3 d,inAF3 a){d=-a;return d;}
+ AF4 opANegF4(outAF4 d,inAF4 a){d=-a;return d;}
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 opARcpF2(outAF2 d,inAF2 a){d=ARcpF2(a);return d;}
+ AF3 opARcpF3(outAF3 d,inAF3 a){d=ARcpF3(a);return d;}
+ AF4 opARcpF4(outAF4 d,inAF4 a){d=ARcpF4(a);return d;}
+#endif
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_fsr1.h b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_fsr1.h
new file mode 100644
index 00000000..4e0b3d54
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/ffx_fsr1.h
@@ -0,0 +1,1199 @@
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// AMD FidelityFX SUPER RESOLUTION [FSR 1] ::: SPATIAL SCALING & EXTRAS - v1.20210629
+//
+//
+//------------------------------------------------------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//------------------------------------------------------------------------------------------------------------------------------
+// FidelityFX Super Resolution Sample
+//
+// Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files(the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions :
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//------------------------------------------------------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//------------------------------------------------------------------------------------------------------------------------------
+// ABOUT
+// =====
+// FSR is a collection of algorithms relating to generating a higher resolution image.
+// This specific header focuses on single-image non-temporal image scaling, and related tools.
+//
+// The core functions are EASU and RCAS:
+// [EASU] Edge Adaptive Spatial Upsampling ....... 1x to 4x area range spatial scaling, clamped adaptive elliptical filter.
+// [RCAS] Robust Contrast Adaptive Sharpening .... A non-scaling variation on CAS.
+// RCAS needs to be applied after EASU as a separate pass.
+//
+// Optional utility functions are:
+// [LFGA] Linear Film Grain Applicator ........... Tool to apply film grain after scaling.
+// [SRTM] Simple Reversible Tone-Mapper .......... Linear HDR {0 to FP16_MAX} to {0 to 1} and back.
+// [TEPD] Temporal Energy Preserving Dither ...... Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.
+// See each individual sub-section for inline documentation.
+//------------------------------------------------------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//------------------------------------------------------------------------------------------------------------------------------
+// FUNCTION PERMUTATIONS
+// =====================
+// *F() ..... Single item computation with 32-bit.
+// *H() ..... Single item computation with 16-bit, with packing (aka two 16-bit ops in parallel) when possible.
+// *Hx2() ... Processing two items in parallel with 16-bit, easier packing.
+// Not all interfaces in this file have a *Hx2() form.
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// FSR - [EASU] EDGE ADAPTIVE SPATIAL UPSAMPLING
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// EASU provides a high quality spatial-only scaling at relatively low cost.
+// Meaning EASU is appropiate for laptops and other low-end GPUs.
+// Quality from 1x to 4x area scaling is good.
+//------------------------------------------------------------------------------------------------------------------------------
+// The scalar uses a modified fast approximation to the standard lanczos(size=2) kernel.
+// EASU runs in a single pass, so it applies a directionally and anisotropically adaptive radial lanczos.
+// This is also kept as simple as possible to have minimum runtime.
+//------------------------------------------------------------------------------------------------------------------------------
+// The lanzcos filter has negative lobes, so by itself it will introduce ringing.
+// To remove all ringing, the algorithm uses the nearest 2x2 input texels as a neighborhood,
+// and limits output to the minimum and maximum of that neighborhood.
+//------------------------------------------------------------------------------------------------------------------------------
+// Input image requirements:
+//
+// Color needs to be encoded as 3 channel[red, green, blue](e.g.XYZ not supported)
+// Each channel needs to be in the range[0, 1]
+// Any color primaries are supported
+// Display / tonemapping curve needs to be as if presenting to sRGB display or similar(e.g.Gamma 2.0)
+// There should be no banding in the input
+// There should be no high amplitude noise in the input
+// There should be no noise in the input that is not at input pixel granularity
+// For performance purposes, use 32bpp formats
+//------------------------------------------------------------------------------------------------------------------------------
+// Best to apply EASU at the end of the frame after tonemapping
+// but before film grain or composite of the UI.
+//------------------------------------------------------------------------------------------------------------------------------
+// Example of including this header for D3D HLSL :
+//
+// #define A_GPU 1
+// #define A_HLSL 1
+// #define A_HALF 1
+// #include "ffx_a.h"
+// #define FSR_EASU_H 1
+// #define FSR_RCAS_H 1
+// //declare input callbacks
+// #include "ffx_fsr1.h"
+//
+// Example of including this header for Vulkan GLSL :
+//
+// #define A_GPU 1
+// #define A_GLSL 1
+// #define A_HALF 1
+// #include "ffx_a.h"
+// #define FSR_EASU_H 1
+// #define FSR_RCAS_H 1
+// //declare input callbacks
+// #include "ffx_fsr1.h"
+//
+// Example of including this header for Vulkan HLSL :
+//
+// #define A_GPU 1
+// #define A_HLSL 1
+// #define A_HLSL_6_2 1
+// #define A_NO_16_BIT_CAST 1
+// #define A_HALF 1
+// #include "ffx_a.h"
+// #define FSR_EASU_H 1
+// #define FSR_RCAS_H 1
+// //declare input callbacks
+// #include "ffx_fsr1.h"
+//
+// Example of declaring the required input callbacks for GLSL :
+// The callbacks need to gather4 for each color channel using the specified texture coordinate 'p'.
+// EASU uses gather4 to reduce position computation logic and for free Arrays of Structures to Structures of Arrays conversion.
+//
+// AH4 FsrEasuRH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,0));}
+// AH4 FsrEasuGH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,1));}
+// AH4 FsrEasuBH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,2));}
+// ...
+// The FsrEasuCon function needs to be called from the CPU or GPU to set up constants.
+// The difference in viewport and input image size is there to support Dynamic Resolution Scaling.
+// To use FsrEasuCon() on the CPU, define A_CPU before including ffx_a and ffx_fsr1.
+// Including a GPU example here, the 'con0' through 'con3' values would be stored out to a constant buffer.
+// AU4 con0,con1,con2,con3;
+// FsrEasuCon(con0,con1,con2,con3,
+// 1920.0,1080.0, // Viewport size (top left aligned) in the input image which is to be scaled.
+// 3840.0,2160.0, // The size of the input image.
+// 2560.0,1440.0); // The output resolution.
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// CONSTANT SETUP
+//==============================================================================================================================
+// Call to setup required constant values (works on CPU or GPU).
+A_STATIC void FsrEasuCon(
+outAU4 con0,
+outAU4 con1,
+outAU4 con2,
+outAU4 con3,
+// This the rendered image resolution being upscaled
+AF1 inputViewportInPixelsX,
+AF1 inputViewportInPixelsY,
+// This is the resolution of the resource containing the input image (useful for dynamic resolution)
+AF1 inputSizeInPixelsX,
+AF1 inputSizeInPixelsY,
+// This is the display resolution which the input image gets upscaled to
+AF1 outputSizeInPixelsX,
+AF1 outputSizeInPixelsY){
+ // Output integer position to a pixel position in viewport.
+ con0[0]=AU1_AF1(inputViewportInPixelsX*ARcpF1(outputSizeInPixelsX));
+ con0[1]=AU1_AF1(inputViewportInPixelsY*ARcpF1(outputSizeInPixelsY));
+ con0[2]=AU1_AF1(AF1_(0.5)*inputViewportInPixelsX*ARcpF1(outputSizeInPixelsX)-AF1_(0.5));
+ con0[3]=AU1_AF1(AF1_(0.5)*inputViewportInPixelsY*ARcpF1(outputSizeInPixelsY)-AF1_(0.5));
+ // Viewport pixel position to normalized image space.
+ // This is used to get upper-left of 'F' tap.
+ con1[0]=AU1_AF1(ARcpF1(inputSizeInPixelsX));
+ con1[1]=AU1_AF1(ARcpF1(inputSizeInPixelsY));
+ // Centers of gather4, first offset from upper-left of 'F'.
+ // +---+---+
+ // | | |
+ // +--(0)--+
+ // | b | c |
+ // +---F---+---+---+
+ // | e | f | g | h |
+ // +--(1)--+--(2)--+
+ // | i | j | k | l |
+ // +---+---+---+---+
+ // | n | o |
+ // +--(3)--+
+ // | | |
+ // +---+---+
+ con1[2]=AU1_AF1(AF1_( 1.0)*ARcpF1(inputSizeInPixelsX));
+ con1[3]=AU1_AF1(AF1_(-1.0)*ARcpF1(inputSizeInPixelsY));
+ // These are from (0) instead of 'F'.
+ con2[0]=AU1_AF1(AF1_(-1.0)*ARcpF1(inputSizeInPixelsX));
+ con2[1]=AU1_AF1(AF1_( 2.0)*ARcpF1(inputSizeInPixelsY));
+ con2[2]=AU1_AF1(AF1_( 1.0)*ARcpF1(inputSizeInPixelsX));
+ con2[3]=AU1_AF1(AF1_( 2.0)*ARcpF1(inputSizeInPixelsY));
+ con3[0]=AU1_AF1(AF1_( 0.0)*ARcpF1(inputSizeInPixelsX));
+ con3[1]=AU1_AF1(AF1_( 4.0)*ARcpF1(inputSizeInPixelsY));
+ con3[2]=con3[3]=0;}
+
+//If the an offset into the input image resource
+A_STATIC void FsrEasuConOffset(
+ outAU4 con0,
+ outAU4 con1,
+ outAU4 con2,
+ outAU4 con3,
+ // This the rendered image resolution being upscaled
+ AF1 inputViewportInPixelsX,
+ AF1 inputViewportInPixelsY,
+ // This is the resolution of the resource containing the input image (useful for dynamic resolution)
+ AF1 inputSizeInPixelsX,
+ AF1 inputSizeInPixelsY,
+ // This is the display resolution which the input image gets upscaled to
+ AF1 outputSizeInPixelsX,
+ AF1 outputSizeInPixelsY,
+ // This is the input image offset into the resource containing it (useful for dynamic resolution)
+ AF1 inputOffsetInPixelsX,
+ AF1 inputOffsetInPixelsY) {
+ FsrEasuCon(con0, con1, con2, con3, inputViewportInPixelsX, inputViewportInPixelsY, inputSizeInPixelsX, inputSizeInPixelsY, outputSizeInPixelsX, outputSizeInPixelsY);
+ con0[2] = AU1_AF1(AF1_(0.5) * inputViewportInPixelsX * ARcpF1(outputSizeInPixelsX) - AF1_(0.5) + inputOffsetInPixelsX);
+ con0[3] = AU1_AF1(AF1_(0.5) * inputViewportInPixelsY * ARcpF1(outputSizeInPixelsY) - AF1_(0.5) + inputOffsetInPixelsY);
+}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// NON-PACKED 32-BIT VERSION
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(FSR_EASU_F)
+ // Input callback prototypes, need to be implemented by calling shader
+ AF4 FsrEasuRF(AF2 p);
+ AF4 FsrEasuGF(AF2 p);
+ AF4 FsrEasuBF(AF2 p);
+//------------------------------------------------------------------------------------------------------------------------------
+ // Filtering for a given tap for the scalar.
+ void FsrEasuTapF(
+ inout AF3 aC, // Accumulated color, with negative lobe.
+ inout AF1 aW, // Accumulated weight.
+ AF2 off, // Pixel offset from resolve position to tap.
+ AF2 dir, // Gradient direction.
+ AF2 len, // Length.
+ AF1 lob, // Negative lobe strength.
+ AF1 clp, // Clipping point.
+ AF3 c){ // Tap color.
+ // Rotate offset by direction.
+ AF2 v;
+ v.x=(off.x*( dir.x))+(off.y*dir.y);
+ v.y=(off.x*(-dir.y))+(off.y*dir.x);
+ // Anisotropy.
+ v*=len;
+ // Compute distance^2.
+ AF1 d2=v.x*v.x+v.y*v.y;
+ // Limit to the window as at corner, 2 taps can easily be outside.
+ d2=min(d2,clp);
+ // Approximation of lancos2 without sin() or rcp(), or sqrt() to get x.
+ // (25/16 * (2/5 * x^2 - 1)^2 - (25/16 - 1)) * (1/4 * x^2 - 1)^2
+ // |_______________________________________| |_______________|
+ // base window
+ // The general form of the 'base' is,
+ // (a*(b*x^2-1)^2-(a-1))
+ // Where 'a=1/(2*b-b^2)' and 'b' moves around the negative lobe.
+ AF1 wB=AF1_(2.0/5.0)*d2+AF1_(-1.0);
+ AF1 wA=lob*d2+AF1_(-1.0);
+ wB*=wB;
+ wA*=wA;
+ wB=AF1_(25.0/16.0)*wB+AF1_(-(25.0/16.0-1.0));
+ AF1 w=wB*wA;
+ // Do weighted average.
+ aC+=c*w;aW+=w;}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Accumulate direction and length.
+ void FsrEasuSetF(
+ inout AF2 dir,
+ inout AF1 len,
+ AF2 pp,
+ AP1 biS,AP1 biT,AP1 biU,AP1 biV,
+ AF1 lA,AF1 lB,AF1 lC,AF1 lD,AF1 lE){
+ // Compute bilinear weight, branches factor out as predicates are compiler time immediates.
+ // s t
+ // u v
+ AF1 w = AF1_(0.0);
+ if(biS)w=(AF1_(1.0)-pp.x)*(AF1_(1.0)-pp.y);
+ if(biT)w= pp.x *(AF1_(1.0)-pp.y);
+ if(biU)w=(AF1_(1.0)-pp.x)* pp.y ;
+ if(biV)w= pp.x * pp.y ;
+ // Direction is the '+' diff.
+ // a
+ // b c d
+ // e
+ // Then takes magnitude from abs average of both sides of 'c'.
+ // Length converts gradient reversal to 0, smoothly to non-reversal at 1, shaped, then adding horz and vert terms.
+ AF1 dc=lD-lC;
+ AF1 cb=lC-lB;
+ AF1 lenX=max(abs(dc),abs(cb));
+ lenX=APrxLoRcpF1(lenX);
+ AF1 dirX=lD-lB;
+ dir.x+=dirX*w;
+ lenX=ASatF1(abs(dirX)*lenX);
+ lenX*=lenX;
+ len+=lenX*w;
+ // Repeat for the y axis.
+ AF1 ec=lE-lC;
+ AF1 ca=lC-lA;
+ AF1 lenY=max(abs(ec),abs(ca));
+ lenY=APrxLoRcpF1(lenY);
+ AF1 dirY=lE-lA;
+ dir.y+=dirY*w;
+ lenY=ASatF1(abs(dirY)*lenY);
+ lenY*=lenY;
+ len+=lenY*w;}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrEasuF(
+ out AF3 pix,
+ AU2 ip, // Integer pixel position in output.
+ AU4 con0, // Constants generated by FsrEasuCon().
+ AU4 con1,
+ AU4 con2,
+ AU4 con3){
+//------------------------------------------------------------------------------------------------------------------------------
+ // Get position of 'f'.
+ AF2 pp=AF2(ip)*AF2_AU2(con0.xy)+AF2_AU2(con0.zw);
+ AF2 fp=floor(pp);
+ pp-=fp;
+//------------------------------------------------------------------------------------------------------------------------------
+ // 12-tap kernel.
+ // b c
+ // e f g h
+ // i j k l
+ // n o
+ // Gather 4 ordering.
+ // a b
+ // r g
+ // For packed FP16, need either {rg} or {ab} so using the following setup for gather in all versions,
+ // a b <- unused (z)
+ // r g
+ // a b a b
+ // r g r g
+ // a b
+ // r g <- unused (z)
+ // Allowing dead-code removal to remove the 'z's.
+ AF2 p0=fp*AF2_AU2(con1.xy)+AF2_AU2(con1.zw);
+ // These are from p0 to avoid pulling two constants on pre-Navi hardware.
+ AF2 p1=p0+AF2_AU2(con2.xy);
+ AF2 p2=p0+AF2_AU2(con2.zw);
+ AF2 p3=p0+AF2_AU2(con3.xy);
+ AF4 bczzR=FsrEasuRF(p0);
+ AF4 bczzG=FsrEasuGF(p0);
+ AF4 bczzB=FsrEasuBF(p0);
+ AF4 ijfeR=FsrEasuRF(p1);
+ AF4 ijfeG=FsrEasuGF(p1);
+ AF4 ijfeB=FsrEasuBF(p1);
+ AF4 klhgR=FsrEasuRF(p2);
+ AF4 klhgG=FsrEasuGF(p2);
+ AF4 klhgB=FsrEasuBF(p2);
+ AF4 zzonR=FsrEasuRF(p3);
+ AF4 zzonG=FsrEasuGF(p3);
+ AF4 zzonB=FsrEasuBF(p3);
+//------------------------------------------------------------------------------------------------------------------------------
+ // Simplest multi-channel approximate luma possible (luma times 2, in 2 FMA/MAD).
+ AF4 bczzL=bczzB*AF4_(0.5)+(bczzR*AF4_(0.5)+bczzG);
+ AF4 ijfeL=ijfeB*AF4_(0.5)+(ijfeR*AF4_(0.5)+ijfeG);
+ AF4 klhgL=klhgB*AF4_(0.5)+(klhgR*AF4_(0.5)+klhgG);
+ AF4 zzonL=zzonB*AF4_(0.5)+(zzonR*AF4_(0.5)+zzonG);
+ // Rename.
+ AF1 bL=bczzL.x;
+ AF1 cL=bczzL.y;
+ AF1 iL=ijfeL.x;
+ AF1 jL=ijfeL.y;
+ AF1 fL=ijfeL.z;
+ AF1 eL=ijfeL.w;
+ AF1 kL=klhgL.x;
+ AF1 lL=klhgL.y;
+ AF1 hL=klhgL.z;
+ AF1 gL=klhgL.w;
+ AF1 oL=zzonL.z;
+ AF1 nL=zzonL.w;
+ // Accumulate for bilinear interpolation.
+ AF2 dir=AF2_(0.0);
+ AF1 len=AF1_(0.0);
+ FsrEasuSetF(dir,len,pp,true, false,false,false,bL,eL,fL,gL,jL);
+ FsrEasuSetF(dir,len,pp,false,true ,false,false,cL,fL,gL,hL,kL);
+ FsrEasuSetF(dir,len,pp,false,false,true ,false,fL,iL,jL,kL,nL);
+ FsrEasuSetF(dir,len,pp,false,false,false,true ,gL,jL,kL,lL,oL);
+//------------------------------------------------------------------------------------------------------------------------------
+ // Normalize with approximation, and cleanup close to zero.
+ AF2 dir2=dir*dir;
+ AF1 dirR=dir2.x+dir2.y;
+ AP1 zro=dirR<AF1_(1.0/32768.0);
+ dirR=APrxLoRsqF1(dirR);
+ dirR=zro?AF1_(1.0):dirR;
+ dir.x=zro?AF1_(1.0):dir.x;
+ dir*=AF2_(dirR);
+ // Transform from {0 to 2} to {0 to 1} range, and shape with square.
+ len=len*AF1_(0.5);
+ len*=len;
+ // Stretch kernel {1.0 vert|horz, to sqrt(2.0) on diagonal}.
+ AF1 stretch=(dir.x*dir.x+dir.y*dir.y)*APrxLoRcpF1(max(abs(dir.x),abs(dir.y)));
+ // Anisotropic length after rotation,
+ // x := 1.0 lerp to 'stretch' on edges
+ // y := 1.0 lerp to 2x on edges
+ AF2 len2=AF2(AF1_(1.0)+(stretch-AF1_(1.0))*len,AF1_(1.0)+AF1_(-0.5)*len);
+ // Based on the amount of 'edge',
+ // the window shifts from +/-{sqrt(2.0) to slightly beyond 2.0}.
+ AF1 lob=AF1_(0.5)+AF1_((1.0/4.0-0.04)-0.5)*len;
+ // Set distance^2 clipping point to the end of the adjustable window.
+ AF1 clp=APrxLoRcpF1(lob);
+//------------------------------------------------------------------------------------------------------------------------------
+ // Accumulation mixed with min/max of 4 nearest.
+ // b c
+ // e f g h
+ // i j k l
+ // n o
+ AF3 min4=min(AMin3F3(AF3(ijfeR.z,ijfeG.z,ijfeB.z),AF3(klhgR.w,klhgG.w,klhgB.w),AF3(ijfeR.y,ijfeG.y,ijfeB.y)),
+ AF3(klhgR.x,klhgG.x,klhgB.x));
+ AF3 max4=max(AMax3F3(AF3(ijfeR.z,ijfeG.z,ijfeB.z),AF3(klhgR.w,klhgG.w,klhgB.w),AF3(ijfeR.y,ijfeG.y,ijfeB.y)),
+ AF3(klhgR.x,klhgG.x,klhgB.x));
+ // Accumulation.
+ AF3 aC=AF3_(0.0);
+ AF1 aW=AF1_(0.0);
+ FsrEasuTapF(aC,aW,AF2( 0.0,-1.0)-pp,dir,len2,lob,clp,AF3(bczzR.x,bczzG.x,bczzB.x)); // b
+ FsrEasuTapF(aC,aW,AF2( 1.0,-1.0)-pp,dir,len2,lob,clp,AF3(bczzR.y,bczzG.y,bczzB.y)); // c
+ FsrEasuTapF(aC,aW,AF2(-1.0, 1.0)-pp,dir,len2,lob,clp,AF3(ijfeR.x,ijfeG.x,ijfeB.x)); // i
+ FsrEasuTapF(aC,aW,AF2( 0.0, 1.0)-pp,dir,len2,lob,clp,AF3(ijfeR.y,ijfeG.y,ijfeB.y)); // j
+ FsrEasuTapF(aC,aW,AF2( 0.0, 0.0)-pp,dir,len2,lob,clp,AF3(ijfeR.z,ijfeG.z,ijfeB.z)); // f
+ FsrEasuTapF(aC,aW,AF2(-1.0, 0.0)-pp,dir,len2,lob,clp,AF3(ijfeR.w,ijfeG.w,ijfeB.w)); // e
+ FsrEasuTapF(aC,aW,AF2( 1.0, 1.0)-pp,dir,len2,lob,clp,AF3(klhgR.x,klhgG.x,klhgB.x)); // k
+ FsrEasuTapF(aC,aW,AF2( 2.0, 1.0)-pp,dir,len2,lob,clp,AF3(klhgR.y,klhgG.y,klhgB.y)); // l
+ FsrEasuTapF(aC,aW,AF2( 2.0, 0.0)-pp,dir,len2,lob,clp,AF3(klhgR.z,klhgG.z,klhgB.z)); // h
+ FsrEasuTapF(aC,aW,AF2( 1.0, 0.0)-pp,dir,len2,lob,clp,AF3(klhgR.w,klhgG.w,klhgB.w)); // g
+ FsrEasuTapF(aC,aW,AF2( 1.0, 2.0)-pp,dir,len2,lob,clp,AF3(zzonR.z,zzonG.z,zzonB.z)); // o
+ FsrEasuTapF(aC,aW,AF2( 0.0, 2.0)-pp,dir,len2,lob,clp,AF3(zzonR.w,zzonG.w,zzonB.w)); // n
+//------------------------------------------------------------------------------------------------------------------------------
+ // Normalize and dering.
+ pix=min(max4,max(min4,aC*AF3_(ARcpF1(aW))));}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// PACKED 16-BIT VERSION
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_EASU_H)
+// Input callback prototypes, need to be implemented by calling shader
+ AH4 FsrEasuRH(AF2 p);
+ AH4 FsrEasuGH(AF2 p);
+ AH4 FsrEasuBH(AF2 p);
+//------------------------------------------------------------------------------------------------------------------------------
+ // This runs 2 taps in parallel.
+ void FsrEasuTapH(
+ inout AH2 aCR,inout AH2 aCG,inout AH2 aCB,
+ inout AH2 aW,
+ AH2 offX,AH2 offY,
+ AH2 dir,
+ AH2 len,
+ AH1 lob,
+ AH1 clp,
+ AH2 cR,AH2 cG,AH2 cB){
+ AH2 vX,vY;
+ vX=offX* dir.xx +offY*dir.yy;
+ vY=offX*(-dir.yy)+offY*dir.xx;
+ vX*=len.x;vY*=len.y;
+ AH2 d2=vX*vX+vY*vY;
+ d2=min(d2,AH2_(clp));
+ AH2 wB=AH2_(2.0/5.0)*d2+AH2_(-1.0);
+ AH2 wA=AH2_(lob)*d2+AH2_(-1.0);
+ wB*=wB;
+ wA*=wA;
+ wB=AH2_(25.0/16.0)*wB+AH2_(-(25.0/16.0-1.0));
+ AH2 w=wB*wA;
+ aCR+=cR*w;aCG+=cG*w;aCB+=cB*w;aW+=w;}
+//------------------------------------------------------------------------------------------------------------------------------
+ // This runs 2 taps in parallel.
+ void FsrEasuSetH(
+ inout AH2 dirPX,inout AH2 dirPY,
+ inout AH2 lenP,
+ AH2 pp,
+ AP1 biST,AP1 biUV,
+ AH2 lA,AH2 lB,AH2 lC,AH2 lD,AH2 lE){
+ AH2 w = AH2_(0.0);
+ if(biST)w=(AH2(1.0,0.0)+AH2(-pp.x,pp.x))*AH2_(AH1_(1.0)-pp.y);
+ if(biUV)w=(AH2(1.0,0.0)+AH2(-pp.x,pp.x))*AH2_( pp.y);
+ // ABS is not free in the packed FP16 path.
+ AH2 dc=lD-lC;
+ AH2 cb=lC-lB;
+ AH2 lenX=max(abs(dc),abs(cb));
+ lenX=ARcpH2(lenX);
+ AH2 dirX=lD-lB;
+ dirPX+=dirX*w;
+ lenX=ASatH2(abs(dirX)*lenX);
+ lenX*=lenX;
+ lenP+=lenX*w;
+ AH2 ec=lE-lC;
+ AH2 ca=lC-lA;
+ AH2 lenY=max(abs(ec),abs(ca));
+ lenY=ARcpH2(lenY);
+ AH2 dirY=lE-lA;
+ dirPY+=dirY*w;
+ lenY=ASatH2(abs(dirY)*lenY);
+ lenY*=lenY;
+ lenP+=lenY*w;}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrEasuH(
+ out AH3 pix,
+ AU2 ip,
+ AU4 con0,
+ AU4 con1,
+ AU4 con2,
+ AU4 con3){
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 pp=AF2(ip)*AF2_AU2(con0.xy)+AF2_AU2(con0.zw);
+ AF2 fp=floor(pp);
+ pp-=fp;
+ AH2 ppp=AH2(pp);
+//------------------------------------------------------------------------------------------------------------------------------
+ AF2 p0=fp*AF2_AU2(con1.xy)+AF2_AU2(con1.zw);
+ AF2 p1=p0+AF2_AU2(con2.xy);
+ AF2 p2=p0+AF2_AU2(con2.zw);
+ AF2 p3=p0+AF2_AU2(con3.xy);
+ AH4 bczzR=FsrEasuRH(p0);
+ AH4 bczzG=FsrEasuGH(p0);
+ AH4 bczzB=FsrEasuBH(p0);
+ AH4 ijfeR=FsrEasuRH(p1);
+ AH4 ijfeG=FsrEasuGH(p1);
+ AH4 ijfeB=FsrEasuBH(p1);
+ AH4 klhgR=FsrEasuRH(p2);
+ AH4 klhgG=FsrEasuGH(p2);
+ AH4 klhgB=FsrEasuBH(p2);
+ AH4 zzonR=FsrEasuRH(p3);
+ AH4 zzonG=FsrEasuGH(p3);
+ AH4 zzonB=FsrEasuBH(p3);
+//------------------------------------------------------------------------------------------------------------------------------
+ AH4 bczzL=bczzB*AH4_(0.5)+(bczzR*AH4_(0.5)+bczzG);
+ AH4 ijfeL=ijfeB*AH4_(0.5)+(ijfeR*AH4_(0.5)+ijfeG);
+ AH4 klhgL=klhgB*AH4_(0.5)+(klhgR*AH4_(0.5)+klhgG);
+ AH4 zzonL=zzonB*AH4_(0.5)+(zzonR*AH4_(0.5)+zzonG);
+ AH1 bL=bczzL.x;
+ AH1 cL=bczzL.y;
+ AH1 iL=ijfeL.x;
+ AH1 jL=ijfeL.y;
+ AH1 fL=ijfeL.z;
+ AH1 eL=ijfeL.w;
+ AH1 kL=klhgL.x;
+ AH1 lL=klhgL.y;
+ AH1 hL=klhgL.z;
+ AH1 gL=klhgL.w;
+ AH1 oL=zzonL.z;
+ AH1 nL=zzonL.w;
+ // This part is different, accumulating 2 taps in parallel.
+ AH2 dirPX=AH2_(0.0);
+ AH2 dirPY=AH2_(0.0);
+ AH2 lenP=AH2_(0.0);
+ FsrEasuSetH(dirPX,dirPY,lenP,ppp,true, false,AH2(bL,cL),AH2(eL,fL),AH2(fL,gL),AH2(gL,hL),AH2(jL,kL));
+ FsrEasuSetH(dirPX,dirPY,lenP,ppp,false,true ,AH2(fL,gL),AH2(iL,jL),AH2(jL,kL),AH2(kL,lL),AH2(nL,oL));
+ AH2 dir=AH2(dirPX.r+dirPX.g,dirPY.r+dirPY.g);
+ AH1 len=lenP.r+lenP.g;
+//------------------------------------------------------------------------------------------------------------------------------
+ AH2 dir2=dir*dir;
+ AH1 dirR=dir2.x+dir2.y;
+ AP1 zro=dirR<AH1_(1.0/32768.0);
+ dirR=APrxLoRsqH1(dirR);
+ dirR=zro?AH1_(1.0):dirR;
+ dir.x=zro?AH1_(1.0):dir.x;
+ dir*=AH2_(dirR);
+ len=len*AH1_(0.5);
+ len*=len;
+ AH1 stretch=(dir.x*dir.x+dir.y*dir.y)*APrxLoRcpH1(max(abs(dir.x),abs(dir.y)));
+ AH2 len2=AH2(AH1_(1.0)+(stretch-AH1_(1.0))*len,AH1_(1.0)+AH1_(-0.5)*len);
+ AH1 lob=AH1_(0.5)+AH1_((1.0/4.0-0.04)-0.5)*len;
+ AH1 clp=APrxLoRcpH1(lob);
+//------------------------------------------------------------------------------------------------------------------------------
+ // FP16 is different, using packed trick to do min and max in same operation.
+ AH2 bothR=max(max(AH2(-ijfeR.z,ijfeR.z),AH2(-klhgR.w,klhgR.w)),max(AH2(-ijfeR.y,ijfeR.y),AH2(-klhgR.x,klhgR.x)));
+ AH2 bothG=max(max(AH2(-ijfeG.z,ijfeG.z),AH2(-klhgG.w,klhgG.w)),max(AH2(-ijfeG.y,ijfeG.y),AH2(-klhgG.x,klhgG.x)));
+ AH2 bothB=max(max(AH2(-ijfeB.z,ijfeB.z),AH2(-klhgB.w,klhgB.w)),max(AH2(-ijfeB.y,ijfeB.y),AH2(-klhgB.x,klhgB.x)));
+ // This part is different for FP16, working pairs of taps at a time.
+ AH2 pR=AH2_(0.0);
+ AH2 pG=AH2_(0.0);
+ AH2 pB=AH2_(0.0);
+ AH2 pW=AH2_(0.0);
+ FsrEasuTapH(pR,pG,pB,pW,AH2( 0.0, 1.0)-ppp.xx,AH2(-1.0,-1.0)-ppp.yy,dir,len2,lob,clp,bczzR.xy,bczzG.xy,bczzB.xy);
+ FsrEasuTapH(pR,pG,pB,pW,AH2(-1.0, 0.0)-ppp.xx,AH2( 1.0, 1.0)-ppp.yy,dir,len2,lob,clp,ijfeR.xy,ijfeG.xy,ijfeB.xy);
+ FsrEasuTapH(pR,pG,pB,pW,AH2( 0.0,-1.0)-ppp.xx,AH2( 0.0, 0.0)-ppp.yy,dir,len2,lob,clp,ijfeR.zw,ijfeG.zw,ijfeB.zw);
+ FsrEasuTapH(pR,pG,pB,pW,AH2( 1.0, 2.0)-ppp.xx,AH2( 1.0, 1.0)-ppp.yy,dir,len2,lob,clp,klhgR.xy,klhgG.xy,klhgB.xy);
+ FsrEasuTapH(pR,pG,pB,pW,AH2( 2.0, 1.0)-ppp.xx,AH2( 0.0, 0.0)-ppp.yy,dir,len2,lob,clp,klhgR.zw,klhgG.zw,klhgB.zw);
+ FsrEasuTapH(pR,pG,pB,pW,AH2( 1.0, 0.0)-ppp.xx,AH2( 2.0, 2.0)-ppp.yy,dir,len2,lob,clp,zzonR.zw,zzonG.zw,zzonB.zw);
+ AH3 aC=AH3(pR.x+pR.y,pG.x+pG.y,pB.x+pB.y);
+ AH1 aW=pW.x+pW.y;
+//------------------------------------------------------------------------------------------------------------------------------
+ // Slightly different for FP16 version due to combined min and max.
+ pix=min(AH3(bothR.y,bothG.y,bothB.y),max(-AH3(bothR.x,bothG.x,bothB.x),aC*AH3_(ARcpH1(aW))));}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// FSR - [RCAS] ROBUST CONTRAST ADAPTIVE SHARPENING
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// CAS uses a simplified mechanism to convert local contrast into a variable amount of sharpness.
+// RCAS uses a more exact mechanism, solving for the maximum local sharpness possible before clipping.
+// RCAS also has a built in process to limit sharpening of what it detects as possible noise.
+// RCAS sharper does not support scaling, as it should be applied after EASU scaling.
+// Pass EASU output straight into RCAS, no color conversions necessary.
+//------------------------------------------------------------------------------------------------------------------------------
+// RCAS is based on the following logic.
+// RCAS uses a 5 tap filter in a cross pattern (same as CAS),
+// w n
+// w 1 w for taps w m e
+// w s
+// Where 'w' is the negative lobe weight.
+// output = (w*(n+e+w+s)+m)/(4*w+1)
+// RCAS solves for 'w' by seeing where the signal might clip out of the {0 to 1} input range,
+// 0 == (w*(n+e+w+s)+m)/(4*w+1) -> w = -m/(n+e+w+s)
+// 1 == (w*(n+e+w+s)+m)/(4*w+1) -> w = (1-m)/(n+e+w+s-4*1)
+// Then chooses the 'w' which results in no clipping, limits 'w', and multiplies by the 'sharp' amount.
+// This solution above has issues with MSAA input as the steps along the gradient cause edge detection issues.
+// So RCAS uses 4x the maximum and 4x the minimum (depending on equation)in place of the individual taps.
+// As well as switching from 'm' to either the minimum or maximum (depending on side), to help in energy conservation.
+// This stabilizes RCAS.
+// RCAS does a simple highpass which is normalized against the local contrast then shaped,
+// 0.25
+// 0.25 -1 0.25
+// 0.25
+// This is used as a noise detection filter, to reduce the effect of RCAS on grain, and focus on real edges.
+//
+// GLSL example for the required callbacks :
+//
+// AH4 FsrRcasLoadH(ASW2 p){return AH4(imageLoad(imgSrc,ASU2(p)));}
+// void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b)
+// {
+// //do any simple input color conversions here or leave empty if none needed
+// }
+//
+// FsrRcasCon need to be called from the CPU or GPU to set up constants.
+// Including a GPU example here, the 'con' value would be stored out to a constant buffer.
+//
+// AU4 con;
+// FsrRcasCon(con,
+// 0.0); // The scale is {0.0 := maximum sharpness, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
+// ---------------
+// RCAS sharpening supports a CAS-like pass-through alpha via,
+// #define FSR_RCAS_PASSTHROUGH_ALPHA 1
+// RCAS also supports a define to enable a more expensive path to avoid some sharpening of noise.
+// Would suggest it is better to apply film grain after RCAS sharpening (and after scaling) instead of using this define,
+// #define FSR_RCAS_DENOISE 1
+//==============================================================================================================================
+// This is set at the limit of providing unnatural results for sharpening.
+#define FSR_RCAS_LIMIT (0.25-(1.0/16.0))
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// CONSTANT SETUP
+//==============================================================================================================================
+// Call to setup required constant values (works on CPU or GPU).
+A_STATIC void FsrRcasCon(
+outAU4 con,
+// The scale is {0.0 := maximum, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
+AF1 sharpness){
+ // Transform from stops to linear value.
+ sharpness=AExp2F1(-sharpness);
+ varAF2(hSharp)=initAF2(sharpness,sharpness);
+ con[0]=AU1_AF1(sharpness);
+ con[1]=AU1_AH2_AF2(hSharp);
+ con[2]=0;
+ con[3]=0;}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// NON-PACKED 32-BIT VERSION
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(FSR_RCAS_F)
+ // Input callback prototypes that need to be implemented by calling shader
+ AF4 FsrRcasLoadF(ASU2 p);
+ void FsrRcasInputF(inout AF1 r,inout AF1 g,inout AF1 b);
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrRcasF(
+ out AF1 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.
+ out AF1 pixG,
+ out AF1 pixB,
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ out AF1 pixA,
+ #endif
+ AU2 ip, // Integer pixel position in output.
+ AU4 con){ // Constant generated by RcasSetup().
+ // Algorithm uses minimal 3x3 pixel neighborhood.
+ // b
+ // d e f
+ // h
+ ASU2 sp=ASU2(ip);
+ AF3 b=FsrRcasLoadF(sp+ASU2( 0,-1)).rgb;
+ AF3 d=FsrRcasLoadF(sp+ASU2(-1, 0)).rgb;
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ AF4 ee=FsrRcasLoadF(sp);
+ AF3 e=ee.rgb;pixA=ee.a;
+ #else
+ AF3 e=FsrRcasLoadF(sp).rgb;
+ #endif
+ AF3 f=FsrRcasLoadF(sp+ASU2( 1, 0)).rgb;
+ AF3 h=FsrRcasLoadF(sp+ASU2( 0, 1)).rgb;
+ // Rename (32-bit) or regroup (16-bit).
+ AF1 bR=b.r;
+ AF1 bG=b.g;
+ AF1 bB=b.b;
+ AF1 dR=d.r;
+ AF1 dG=d.g;
+ AF1 dB=d.b;
+ AF1 eR=e.r;
+ AF1 eG=e.g;
+ AF1 eB=e.b;
+ AF1 fR=f.r;
+ AF1 fG=f.g;
+ AF1 fB=f.b;
+ AF1 hR=h.r;
+ AF1 hG=h.g;
+ AF1 hB=h.b;
+ // Run optional input transform.
+ FsrRcasInputF(bR,bG,bB);
+ FsrRcasInputF(dR,dG,dB);
+ FsrRcasInputF(eR,eG,eB);
+ FsrRcasInputF(fR,fG,fB);
+ FsrRcasInputF(hR,hG,hB);
+ // Luma times 2.
+ AF1 bL=bB*AF1_(0.5)+(bR*AF1_(0.5)+bG);
+ AF1 dL=dB*AF1_(0.5)+(dR*AF1_(0.5)+dG);
+ AF1 eL=eB*AF1_(0.5)+(eR*AF1_(0.5)+eG);
+ AF1 fL=fB*AF1_(0.5)+(fR*AF1_(0.5)+fG);
+ AF1 hL=hB*AF1_(0.5)+(hR*AF1_(0.5)+hG);
+ // Noise detection.
+ AF1 nz=AF1_(0.25)*bL+AF1_(0.25)*dL+AF1_(0.25)*fL+AF1_(0.25)*hL-eL;
+ nz=ASatF1(abs(nz)*APrxMedRcpF1(AMax3F1(AMax3F1(bL,dL,eL),fL,hL)-AMin3F1(AMin3F1(bL,dL,eL),fL,hL)));
+ nz=AF1_(-0.5)*nz+AF1_(1.0);
+ // Min and max of ring.
+ AF1 mn4R=min(AMin3F1(bR,dR,fR),hR);
+ AF1 mn4G=min(AMin3F1(bG,dG,fG),hG);
+ AF1 mn4B=min(AMin3F1(bB,dB,fB),hB);
+ AF1 mx4R=max(AMax3F1(bR,dR,fR),hR);
+ AF1 mx4G=max(AMax3F1(bG,dG,fG),hG);
+ AF1 mx4B=max(AMax3F1(bB,dB,fB),hB);
+ // Immediate constants for peak range.
+ AF2 peakC=AF2(1.0,-1.0*4.0);
+ // Limiters, these need to be high precision RCPs.
+ AF1 hitMinR=min(mn4R,eR)*ARcpF1(AF1_(4.0)*mx4R);
+ AF1 hitMinG=min(mn4G,eG)*ARcpF1(AF1_(4.0)*mx4G);
+ AF1 hitMinB=min(mn4B,eB)*ARcpF1(AF1_(4.0)*mx4B);
+ AF1 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpF1(AF1_(4.0)*mn4R+peakC.y);
+ AF1 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpF1(AF1_(4.0)*mn4G+peakC.y);
+ AF1 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpF1(AF1_(4.0)*mn4B+peakC.y);
+ AF1 lobeR=max(-hitMinR,hitMaxR);
+ AF1 lobeG=max(-hitMinG,hitMaxG);
+ AF1 lobeB=max(-hitMinB,hitMaxB);
+ AF1 lobe=max(AF1_(-FSR_RCAS_LIMIT),min(AMax3F1(lobeR,lobeG,lobeB),AF1_(0.0)))*AF1_AU1(con.x);
+ // Apply noise removal.
+ #ifdef FSR_RCAS_DENOISE
+ lobe*=nz;
+ #endif
+ // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
+ AF1 rcpL=APrxMedRcpF1(AF1_(4.0)*lobe+AF1_(1.0));
+ pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
+ pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
+ pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;
+ return;}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// NON-PACKED 16-BIT VERSION
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_RCAS_H)
+ // Input callback prototypes that need to be implemented by calling shader
+ AH4 FsrRcasLoadH(ASW2 p);
+ void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b);
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrRcasH(
+ out AH1 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.
+ out AH1 pixG,
+ out AH1 pixB,
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ out AH1 pixA,
+ #endif
+ AU2 ip, // Integer pixel position in output.
+ AU4 con){ // Constant generated by RcasSetup().
+ // Sharpening algorithm uses minimal 3x3 pixel neighborhood.
+ // b
+ // d e f
+ // h
+ ASW2 sp=ASW2(ip);
+ AH3 b=FsrRcasLoadH(sp+ASW2( 0,-1)).rgb;
+ AH3 d=FsrRcasLoadH(sp+ASW2(-1, 0)).rgb;
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ AH4 ee=FsrRcasLoadH(sp);
+ AH3 e=ee.rgb;pixA=ee.a;
+ #else
+ AH3 e=FsrRcasLoadH(sp).rgb;
+ #endif
+ AH3 f=FsrRcasLoadH(sp+ASW2( 1, 0)).rgb;
+ AH3 h=FsrRcasLoadH(sp+ASW2( 0, 1)).rgb;
+ // Rename (32-bit) or regroup (16-bit).
+ AH1 bR=b.r;
+ AH1 bG=b.g;
+ AH1 bB=b.b;
+ AH1 dR=d.r;
+ AH1 dG=d.g;
+ AH1 dB=d.b;
+ AH1 eR=e.r;
+ AH1 eG=e.g;
+ AH1 eB=e.b;
+ AH1 fR=f.r;
+ AH1 fG=f.g;
+ AH1 fB=f.b;
+ AH1 hR=h.r;
+ AH1 hG=h.g;
+ AH1 hB=h.b;
+ // Run optional input transform.
+ FsrRcasInputH(bR,bG,bB);
+ FsrRcasInputH(dR,dG,dB);
+ FsrRcasInputH(eR,eG,eB);
+ FsrRcasInputH(fR,fG,fB);
+ FsrRcasInputH(hR,hG,hB);
+ // Luma times 2.
+ AH1 bL=bB*AH1_(0.5)+(bR*AH1_(0.5)+bG);
+ AH1 dL=dB*AH1_(0.5)+(dR*AH1_(0.5)+dG);
+ AH1 eL=eB*AH1_(0.5)+(eR*AH1_(0.5)+eG);
+ AH1 fL=fB*AH1_(0.5)+(fR*AH1_(0.5)+fG);
+ AH1 hL=hB*AH1_(0.5)+(hR*AH1_(0.5)+hG);
+ // Noise detection.
+ AH1 nz=AH1_(0.25)*bL+AH1_(0.25)*dL+AH1_(0.25)*fL+AH1_(0.25)*hL-eL;
+ nz=ASatH1(abs(nz)*APrxMedRcpH1(AMax3H1(AMax3H1(bL,dL,eL),fL,hL)-AMin3H1(AMin3H1(bL,dL,eL),fL,hL)));
+ nz=AH1_(-0.5)*nz+AH1_(1.0);
+ // Min and max of ring.
+ AH1 mn4R=min(AMin3H1(bR,dR,fR),hR);
+ AH1 mn4G=min(AMin3H1(bG,dG,fG),hG);
+ AH1 mn4B=min(AMin3H1(bB,dB,fB),hB);
+ AH1 mx4R=max(AMax3H1(bR,dR,fR),hR);
+ AH1 mx4G=max(AMax3H1(bG,dG,fG),hG);
+ AH1 mx4B=max(AMax3H1(bB,dB,fB),hB);
+ // Immediate constants for peak range.
+ AH2 peakC=AH2(1.0,-1.0*4.0);
+ // Limiters, these need to be high precision RCPs.
+ AH1 hitMinR=min(mn4R,eR)*ARcpH1(AH1_(4.0)*mx4R);
+ AH1 hitMinG=min(mn4G,eG)*ARcpH1(AH1_(4.0)*mx4G);
+ AH1 hitMinB=min(mn4B,eB)*ARcpH1(AH1_(4.0)*mx4B);
+ AH1 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpH1(AH1_(4.0)*mn4R+peakC.y);
+ AH1 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpH1(AH1_(4.0)*mn4G+peakC.y);
+ AH1 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpH1(AH1_(4.0)*mn4B+peakC.y);
+ AH1 lobeR=max(-hitMinR,hitMaxR);
+ AH1 lobeG=max(-hitMinG,hitMaxG);
+ AH1 lobeB=max(-hitMinB,hitMaxB);
+ AH1 lobe=max(AH1_(-FSR_RCAS_LIMIT),min(AMax3H1(lobeR,lobeG,lobeB),AH1_(0.0)))*AH2_AU1(con.y).x;
+ // Apply noise removal.
+ #ifdef FSR_RCAS_DENOISE
+ lobe*=nz;
+ #endif
+ // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
+ AH1 rcpL=APrxMedRcpH1(AH1_(4.0)*lobe+AH1_(1.0));
+ pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
+ pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
+ pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// PACKED 16-BIT VERSION
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_RCAS_HX2)
+ // Input callback prototypes that need to be implemented by the calling shader
+ AH4 FsrRcasLoadHx2(ASW2 p);
+ void FsrRcasInputHx2(inout AH2 r,inout AH2 g,inout AH2 b);
+//------------------------------------------------------------------------------------------------------------------------------
+ // Can be used to convert from packed Structures of Arrays to Arrays of Structures for store.
+ void FsrRcasDepackHx2(out AH4 pix0,out AH4 pix1,AH2 pixR,AH2 pixG,AH2 pixB){
+ #ifdef A_HLSL
+ // Invoke a slower path for DX only, since it won't allow uninitialized values.
+ pix0.a=pix1.a=0.0;
+ #endif
+ pix0.rgb=AH3(pixR.x,pixG.x,pixB.x);
+ pix1.rgb=AH3(pixR.y,pixG.y,pixB.y);}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrRcasHx2(
+ // Output values are for 2 8x8 tiles in a 16x8 region.
+ // pix<R,G,B>.x = left 8x8 tile
+ // pix<R,G,B>.y = right 8x8 tile
+ // This enables later processing to easily be packed as well.
+ out AH2 pixR,
+ out AH2 pixG,
+ out AH2 pixB,
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ out AH2 pixA,
+ #endif
+ AU2 ip, // Integer pixel position in output.
+ AU4 con){ // Constant generated by RcasSetup().
+ // No scaling algorithm uses minimal 3x3 pixel neighborhood.
+ ASW2 sp0=ASW2(ip);
+ AH3 b0=FsrRcasLoadHx2(sp0+ASW2( 0,-1)).rgb;
+ AH3 d0=FsrRcasLoadHx2(sp0+ASW2(-1, 0)).rgb;
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ AH4 ee0=FsrRcasLoadHx2(sp0);
+ AH3 e0=ee0.rgb;pixA.r=ee0.a;
+ #else
+ AH3 e0=FsrRcasLoadHx2(sp0).rgb;
+ #endif
+ AH3 f0=FsrRcasLoadHx2(sp0+ASW2( 1, 0)).rgb;
+ AH3 h0=FsrRcasLoadHx2(sp0+ASW2( 0, 1)).rgb;
+ ASW2 sp1=sp0+ASW2(8,0);
+ AH3 b1=FsrRcasLoadHx2(sp1+ASW2( 0,-1)).rgb;
+ AH3 d1=FsrRcasLoadHx2(sp1+ASW2(-1, 0)).rgb;
+ #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
+ AH4 ee1=FsrRcasLoadHx2(sp1);
+ AH3 e1=ee1.rgb;pixA.g=ee1.a;
+ #else
+ AH3 e1=FsrRcasLoadHx2(sp1).rgb;
+ #endif
+ AH3 f1=FsrRcasLoadHx2(sp1+ASW2( 1, 0)).rgb;
+ AH3 h1=FsrRcasLoadHx2(sp1+ASW2( 0, 1)).rgb;
+ // Arrays of Structures to Structures of Arrays conversion.
+ AH2 bR=AH2(b0.r,b1.r);
+ AH2 bG=AH2(b0.g,b1.g);
+ AH2 bB=AH2(b0.b,b1.b);
+ AH2 dR=AH2(d0.r,d1.r);
+ AH2 dG=AH2(d0.g,d1.g);
+ AH2 dB=AH2(d0.b,d1.b);
+ AH2 eR=AH2(e0.r,e1.r);
+ AH2 eG=AH2(e0.g,e1.g);
+ AH2 eB=AH2(e0.b,e1.b);
+ AH2 fR=AH2(f0.r,f1.r);
+ AH2 fG=AH2(f0.g,f1.g);
+ AH2 fB=AH2(f0.b,f1.b);
+ AH2 hR=AH2(h0.r,h1.r);
+ AH2 hG=AH2(h0.g,h1.g);
+ AH2 hB=AH2(h0.b,h1.b);
+ // Run optional input transform.
+ FsrRcasInputHx2(bR,bG,bB);
+ FsrRcasInputHx2(dR,dG,dB);
+ FsrRcasInputHx2(eR,eG,eB);
+ FsrRcasInputHx2(fR,fG,fB);
+ FsrRcasInputHx2(hR,hG,hB);
+ // Luma times 2.
+ AH2 bL=bB*AH2_(0.5)+(bR*AH2_(0.5)+bG);
+ AH2 dL=dB*AH2_(0.5)+(dR*AH2_(0.5)+dG);
+ AH2 eL=eB*AH2_(0.5)+(eR*AH2_(0.5)+eG);
+ AH2 fL=fB*AH2_(0.5)+(fR*AH2_(0.5)+fG);
+ AH2 hL=hB*AH2_(0.5)+(hR*AH2_(0.5)+hG);
+ // Noise detection.
+ AH2 nz=AH2_(0.25)*bL+AH2_(0.25)*dL+AH2_(0.25)*fL+AH2_(0.25)*hL-eL;
+ nz=ASatH2(abs(nz)*APrxMedRcpH2(AMax3H2(AMax3H2(bL,dL,eL),fL,hL)-AMin3H2(AMin3H2(bL,dL,eL),fL,hL)));
+ nz=AH2_(-0.5)*nz+AH2_(1.0);
+ // Min and max of ring.
+ AH2 mn4R=min(AMin3H2(bR,dR,fR),hR);
+ AH2 mn4G=min(AMin3H2(bG,dG,fG),hG);
+ AH2 mn4B=min(AMin3H2(bB,dB,fB),hB);
+ AH2 mx4R=max(AMax3H2(bR,dR,fR),hR);
+ AH2 mx4G=max(AMax3H2(bG,dG,fG),hG);
+ AH2 mx4B=max(AMax3H2(bB,dB,fB),hB);
+ // Immediate constants for peak range.
+ AH2 peakC=AH2(1.0,-1.0*4.0);
+ // Limiters, these need to be high precision RCPs.
+ AH2 hitMinR=min(mn4R,eR)*ARcpH2(AH2_(4.0)*mx4R);
+ AH2 hitMinG=min(mn4G,eG)*ARcpH2(AH2_(4.0)*mx4G);
+ AH2 hitMinB=min(mn4B,eB)*ARcpH2(AH2_(4.0)*mx4B);
+ AH2 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpH2(AH2_(4.0)*mn4R+peakC.y);
+ AH2 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpH2(AH2_(4.0)*mn4G+peakC.y);
+ AH2 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpH2(AH2_(4.0)*mn4B+peakC.y);
+ AH2 lobeR=max(-hitMinR,hitMaxR);
+ AH2 lobeG=max(-hitMinG,hitMaxG);
+ AH2 lobeB=max(-hitMinB,hitMaxB);
+ AH2 lobe=max(AH2_(-FSR_RCAS_LIMIT),min(AMax3H2(lobeR,lobeG,lobeB),AH2_(0.0)))*AH2_(AH2_AU1(con.y).x);
+ // Apply noise removal.
+ #ifdef FSR_RCAS_DENOISE
+ lobe*=nz;
+ #endif
+ // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
+ AH2 rcpL=APrxMedRcpH2(AH2_(4.0)*lobe+AH2_(1.0));
+ pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
+ pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
+ pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// FSR - [LFGA] LINEAR FILM GRAIN APPLICATOR
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// Adding output-resolution film grain after scaling is a good way to mask both rendering and scaling artifacts.
+// Suggest using tiled blue noise as film grain input, with peak noise frequency set for a specific look and feel.
+// The 'Lfga*()' functions provide a convenient way to introduce grain.
+// These functions limit grain based on distance to signal limits.
+// This is done so that the grain is temporally energy preserving, and thus won't modify image tonality.
+// Grain application should be done in a linear colorspace.
+// The grain should be temporally changing, but have a temporal sum per pixel that adds to zero (non-biased).
+//------------------------------------------------------------------------------------------------------------------------------
+// Usage,
+// FsrLfga*(
+// color, // In/out linear colorspace color {0 to 1} ranged.
+// grain, // Per pixel grain texture value {-0.5 to 0.5} ranged, input is 3-channel to support colored grain.
+// amount); // Amount of grain (0 to 1} ranged.
+//------------------------------------------------------------------------------------------------------------------------------
+// Example if grain texture is monochrome: 'FsrLfgaF(color,AF3_(grain),amount)'
+//==============================================================================================================================
+#if defined(A_GPU)
+ // Maximum grain is the minimum distance to the signal limit.
+ void FsrLfgaF(inout AF3 c,AF3 t,AF1 a){c+=(t*AF3_(a))*min(AF3_(1.0)-c,c);}
+#endif
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(A_HALF)
+ // Half precision version (slower).
+ void FsrLfgaH(inout AH3 c,AH3 t,AH1 a){c+=(t*AH3_(a))*min(AH3_(1.0)-c,c);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // Packed half precision version (faster).
+ void FsrLfgaHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 tR,AH2 tG,AH2 tB,AH1 a){
+ cR+=(tR*AH2_(a))*min(AH2_(1.0)-cR,cR);cG+=(tG*AH2_(a))*min(AH2_(1.0)-cG,cG);cB+=(tB*AH2_(a))*min(AH2_(1.0)-cB,cB);}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// FSR - [SRTM] SIMPLE REVERSIBLE TONE-MAPPER
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// This provides a way to take linear HDR color {0 to FP16_MAX} and convert it into a temporary {0 to 1} ranged post-tonemapped linear.
+// The tonemapper preserves RGB ratio, which helps maintain HDR color bleed during filtering.
+//------------------------------------------------------------------------------------------------------------------------------
+// Reversible tonemapper usage,
+// FsrSrtm*(color); // {0 to FP16_MAX} converted to {0 to 1}.
+// FsrSrtmInv*(color); // {0 to 1} converted into {0 to 32768, output peak safe for FP16}.
+//==============================================================================================================================
+#if defined(A_GPU)
+ void FsrSrtmF(inout AF3 c){c*=AF3_(ARcpF1(AMax3F1(c.r,c.g,c.b)+AF1_(1.0)));}
+ // The extra max solves the c=1.0 case (which is a /0).
+ void FsrSrtmInvF(inout AF3 c){c*=AF3_(ARcpF1(max(AF1_(1.0/32768.0),AF1_(1.0)-AMax3F1(c.r,c.g,c.b))));}
+#endif
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(A_HALF)
+ void FsrSrtmH(inout AH3 c){c*=AH3_(ARcpH1(AMax3H1(c.r,c.g,c.b)+AH1_(1.0)));}
+ void FsrSrtmInvH(inout AH3 c){c*=AH3_(ARcpH1(max(AH1_(1.0/32768.0),AH1_(1.0)-AMax3H1(c.r,c.g,c.b))));}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrSrtmHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB){
+ AH2 rcp=ARcpH2(AMax3H2(cR,cG,cB)+AH2_(1.0));cR*=rcp;cG*=rcp;cB*=rcp;}
+ void FsrSrtmInvHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB){
+ AH2 rcp=ARcpH2(max(AH2_(1.0/32768.0),AH2_(1.0)-AMax3H2(cR,cG,cB)));cR*=rcp;cG*=rcp;cB*=rcp;}
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// FSR - [TEPD] TEMPORAL ENERGY PRESERVING DITHER
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.
+// Gamma 2.0 is used so that the conversion back to linear is just to square the color.
+// The conversion comes in 8-bit and 10-bit modes, designed for output to 8-bit UNORM or 10:10:10:2 respectively.
+// Given good non-biased temporal blue noise as dither input,
+// the output dither will temporally conserve energy.
+// This is done by choosing the linear nearest step point instead of perceptual nearest.
+// See code below for details.
+//------------------------------------------------------------------------------------------------------------------------------
+// DX SPEC RULES FOR FLOAT->UNORM 8-BIT CONVERSION
+// ===============================================
+// - Output is 'uint(floor(saturate(n)*255.0+0.5))'.
+// - Thus rounding is to nearest.
+// - NaN gets converted to zero.
+// - INF is clamped to {0.0 to 1.0}.
+//==============================================================================================================================
+#if defined(A_GPU)
+ // Hand tuned integer position to dither value, with more values than simple checkerboard.
+ // Only 32-bit has enough precision for this compddation.
+ // Output is {0 to <1}.
+ AF1 FsrTepdDitF(AU2 p,AU1 f){
+ AF1 x=AF1_(p.x+f);
+ AF1 y=AF1_(p.y);
+ // The 1.61803 golden ratio.
+ AF1 a=AF1_((1.0+sqrt(5.0))/2.0);
+ // Number designed to provide a good visual pattern.
+ AF1 b=AF1_(1.0/3.69);
+ x=x*a+(y*b);
+ return AFractF1(x);}
+//------------------------------------------------------------------------------------------------------------------------------
+ // This version is 8-bit gamma 2.0.
+ // The 'c' input is {0 to 1}.
+ // Output is {0 to 1} ready for image store.
+ void FsrTepdC8F(inout AF3 c,AF1 dit){
+ AF3 n=sqrt(c);
+ n=floor(n*AF3_(255.0))*AF3_(1.0/255.0);
+ AF3 a=n*n;
+ AF3 b=n+AF3_(1.0/255.0);b=b*b;
+ // Ratio of 'a' to 'b' required to produce 'c'.
+ // APrxLoRcpF1() won't work here (at least for very high dynamic ranges).
+ // APrxMedRcpF1() is an IADD,FMA,MUL.
+ AF3 r=(c-b)*APrxMedRcpF3(a-b);
+ // Use the ratio as a cutoff to choose 'a' or 'b'.
+ // AGtZeroF1() is a MUL.
+ c=ASatF3(n+AGtZeroF3(AF3_(dit)-r)*AF3_(1.0/255.0));}
+//------------------------------------------------------------------------------------------------------------------------------
+ // This version is 10-bit gamma 2.0.
+ // The 'c' input is {0 to 1}.
+ // Output is {0 to 1} ready for image store.
+ void FsrTepdC10F(inout AF3 c,AF1 dit){
+ AF3 n=sqrt(c);
+ n=floor(n*AF3_(1023.0))*AF3_(1.0/1023.0);
+ AF3 a=n*n;
+ AF3 b=n+AF3_(1.0/1023.0);b=b*b;
+ AF3 r=(c-b)*APrxMedRcpF3(a-b);
+ c=ASatF3(n+AGtZeroF3(AF3_(dit)-r)*AF3_(1.0/1023.0));}
+#endif
+//==============================================================================================================================
+#if defined(A_GPU)&&defined(A_HALF)
+ AH1 FsrTepdDitH(AU2 p,AU1 f){
+ AF1 x=AF1_(p.x+f);
+ AF1 y=AF1_(p.y);
+ AF1 a=AF1_((1.0+sqrt(5.0))/2.0);
+ AF1 b=AF1_(1.0/3.69);
+ x=x*a+(y*b);
+ return AH1(AFractF1(x));}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrTepdC8H(inout AH3 c,AH1 dit){
+ AH3 n=sqrt(c);
+ n=floor(n*AH3_(255.0))*AH3_(1.0/255.0);
+ AH3 a=n*n;
+ AH3 b=n+AH3_(1.0/255.0);b=b*b;
+ AH3 r=(c-b)*APrxMedRcpH3(a-b);
+ c=ASatH3(n+AGtZeroH3(AH3_(dit)-r)*AH3_(1.0/255.0));}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrTepdC10H(inout AH3 c,AH1 dit){
+ AH3 n=sqrt(c);
+ n=floor(n*AH3_(1023.0))*AH3_(1.0/1023.0);
+ AH3 a=n*n;
+ AH3 b=n+AH3_(1.0/1023.0);b=b*b;
+ AH3 r=(c-b)*APrxMedRcpH3(a-b);
+ c=ASatH3(n+AGtZeroH3(AH3_(dit)-r)*AH3_(1.0/1023.0));}
+//==============================================================================================================================
+ // This computes dither for positions 'p' and 'p+{8,0}'.
+ AH2 FsrTepdDitHx2(AU2 p,AU1 f){
+ AF2 x;
+ x.x=AF1_(p.x+f);
+ x.y=x.x+AF1_(8.0);
+ AF1 y=AF1_(p.y);
+ AF1 a=AF1_((1.0+sqrt(5.0))/2.0);
+ AF1 b=AF1_(1.0/3.69);
+ x=x*AF2_(a)+AF2_(y*b);
+ return AH2(AFractF2(x));}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrTepdC8Hx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 dit){
+ AH2 nR=sqrt(cR);
+ AH2 nG=sqrt(cG);
+ AH2 nB=sqrt(cB);
+ nR=floor(nR*AH2_(255.0))*AH2_(1.0/255.0);
+ nG=floor(nG*AH2_(255.0))*AH2_(1.0/255.0);
+ nB=floor(nB*AH2_(255.0))*AH2_(1.0/255.0);
+ AH2 aR=nR*nR;
+ AH2 aG=nG*nG;
+ AH2 aB=nB*nB;
+ AH2 bR=nR+AH2_(1.0/255.0);bR=bR*bR;
+ AH2 bG=nG+AH2_(1.0/255.0);bG=bG*bG;
+ AH2 bB=nB+AH2_(1.0/255.0);bB=bB*bB;
+ AH2 rR=(cR-bR)*APrxMedRcpH2(aR-bR);
+ AH2 rG=(cG-bG)*APrxMedRcpH2(aG-bG);
+ AH2 rB=(cB-bB)*APrxMedRcpH2(aB-bB);
+ cR=ASatH2(nR+AGtZeroH2(dit-rR)*AH2_(1.0/255.0));
+ cG=ASatH2(nG+AGtZeroH2(dit-rG)*AH2_(1.0/255.0));
+ cB=ASatH2(nB+AGtZeroH2(dit-rB)*AH2_(1.0/255.0));}
+//------------------------------------------------------------------------------------------------------------------------------
+ void FsrTepdC10Hx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 dit){
+ AH2 nR=sqrt(cR);
+ AH2 nG=sqrt(cG);
+ AH2 nB=sqrt(cB);
+ nR=floor(nR*AH2_(1023.0))*AH2_(1.0/1023.0);
+ nG=floor(nG*AH2_(1023.0))*AH2_(1.0/1023.0);
+ nB=floor(nB*AH2_(1023.0))*AH2_(1.0/1023.0);
+ AH2 aR=nR*nR;
+ AH2 aG=nG*nG;
+ AH2 aB=nB*nB;
+ AH2 bR=nR+AH2_(1.0/1023.0);bR=bR*bR;
+ AH2 bG=nG+AH2_(1.0/1023.0);bG=bG*bG;
+ AH2 bB=nB+AH2_(1.0/1023.0);bB=bB*bB;
+ AH2 rR=(cR-bR)*APrxMedRcpH2(aR-bR);
+ AH2 rG=(cG-bG)*APrxMedRcpH2(aG-bG);
+ AH2 rB=(cB-bB)*APrxMedRcpH2(aB-bB);
+ cR=ASatH2(nR+AGtZeroH2(dit-rR)*AH2_(1.0/1023.0));
+ cG=ASatH2(nG+AGtZeroH2(dit-rG)*AH2_(1.0/1023.0));
+ cB=ASatH2(nB+AGtZeroH2(dit-rB)*AH2_(1.0/1023.0));}
+#endif
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_scaling.glsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_scaling.glsl
new file mode 100644
index 00000000..8e8755db
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_scaling.glsl
@@ -0,0 +1,88 @@
+#version 430 core
+precision mediump float;
+layout (local_size_x = 64) in;
+layout(rgba8, binding = 0, location=0) uniform image2D imgOutput;
+layout( location=1 ) uniform sampler2D Source;
+layout( location=2 ) uniform float srcX0;
+layout( location=3 ) uniform float srcX1;
+layout( location=4 ) uniform float srcY0;
+layout( location=5 ) uniform float srcY1;
+layout( location=6 ) uniform float dstX0;
+layout( location=7 ) uniform float dstX1;
+layout( location=8 ) uniform float dstY0;
+layout( location=9 ) uniform float dstY1;
+layout( location=10 ) uniform float scaleX;
+layout( location=11 ) uniform float scaleY;
+
+#define A_GPU 1
+#define A_GLSL 1
+#include "ffx_a.h"
+
+#define FSR_EASU_F 1
+AU4 con0, con1, con2, con3;
+float srcW, srcH, dstW, dstH;
+vec2 bLeft, tRight;
+
+AF2 translate(AF2 pos) {
+ return AF2(pos.x * scaleX, pos.y * scaleY);
+}
+
+void setBounds(vec2 bottomLeft, vec2 topRight) {
+ bLeft = bottomLeft;
+ tRight = topRight;
+}
+
+AF2 translateDest(AF2 pos) {
+ AF2 translatedPos = AF2(pos.x, pos.y);
+ translatedPos.x = dstX1 < dstX0 ? dstX1 - translatedPos.x : translatedPos.x;
+ translatedPos.y = dstY0 > dstY1 ? dstY0 + dstY1 - translatedPos.y - 1: translatedPos.y;
+ return translatedPos;
+}
+
+AF4 FsrEasuRF(AF2 p) { AF4 res = textureGather(Source, translate(p), 0); return res; }
+AF4 FsrEasuGF(AF2 p) { AF4 res = textureGather(Source, translate(p), 1); return res; }
+AF4 FsrEasuBF(AF2 p) { AF4 res = textureGather(Source, translate(p), 2); return res; }
+
+#include "ffx_fsr1.h"
+
+float insideBox(vec2 v) {
+ vec2 s = step(bLeft, v) - step(tRight, v);
+ return s.x * s.y;
+}
+
+void CurrFilter(AU2 pos)
+{
+ if((insideBox(vec2(pos.x, pos.y))) == 0) {
+ imageStore(imgOutput, ASU2(pos.x, pos.y), AF4(0,0,0,1));
+ return;
+ }
+ AF3 c;
+ FsrEasuF(c, AU2(pos.x - bLeft.x, pos.y - bLeft.y), con0, con1, con2, con3);
+ imageStore(imgOutput, ASU2(translateDest(pos)), AF4(c, 1));
+}
+
+void main() {
+ srcW = abs(srcX1 - srcX0);
+ srcH = abs(srcY1 - srcY0);
+ dstW = abs(dstX1 - dstX0);
+ dstH = abs(dstY1 - dstY0);
+
+ AU2 gxy = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
+
+ setBounds(vec2(dstX0 < dstX1 ? dstX0 : dstX1, dstY0 < dstY1 ? dstY0 : dstY1),
+ vec2(dstX1 > dstX0 ? dstX1 : dstX0, dstY1 > dstY0 ? dstY1 : dstY0));
+
+ // Upscaling
+ FsrEasuCon(con0, con1, con2, con3,
+ srcW, srcH, // Viewport size (top left aligned) in the input image which is to be scaled.
+ srcW, srcH, // The size of the input image.
+ dstW, dstH); // The output resolution.
+
+ CurrFilter(gxy);
+ gxy.x += 8u;
+ CurrFilter(gxy);
+ gxy.y += 8u;
+ CurrFilter(gxy);
+ gxy.x -= 8u;
+ CurrFilter(gxy);
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_sharpening.glsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_sharpening.glsl
new file mode 100644
index 00000000..d3b98729
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fsr_sharpening.glsl
@@ -0,0 +1,37 @@
+#version 430 core
+precision mediump float;
+layout (local_size_x = 64) in;
+layout(rgba8, binding = 0, location=0) uniform image2D imgOutput;
+layout( location=1 ) uniform sampler2D source;
+layout( location=2 ) uniform float sharpening;
+
+#define A_GPU 1
+#define A_GLSL 1
+#include "ffx_a.h"
+
+#define FSR_RCAS_F 1
+AU4 con0;
+
+AF4 FsrRcasLoadF(ASU2 p) { return AF4(texelFetch(source, p, 0)); }
+void FsrRcasInputF(inout AF1 r, inout AF1 g, inout AF1 b) {}
+
+#include "ffx_fsr1.h"
+
+void CurrFilter(AU2 pos)
+{
+ AF3 c;
+ FsrRcasF(c.r, c.g, c.b, pos, con0);
+ imageStore(imgOutput, ASU2(pos), AF4(c, 1));
+}
+
+void main() {
+ FsrRcasCon(con0, sharpening);
+ AU2 gxy = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
+ CurrFilter(gxy);
+ gxy.x += 8u;
+ CurrFilter(gxy);
+ gxy.y += 8u;
+ CurrFilter(gxy);
+ gxy.x -= 8u;
+ CurrFilter(gxy);
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fxaa.glsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fxaa.glsl
new file mode 100644
index 00000000..8bdcbca6
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/fxaa.glsl
@@ -0,0 +1,1174 @@
+/*============================================================================
+
+
+ NVIDIA FXAA 3.11 by TIMOTHY LOTTES
+
+
+------------------------------------------------------------------------------
+COPYRIGHT (C) 2010, 2011 NVIDIA CORPORATION. ALL RIGHTS RESERVED.
+------------------------------------------------------------------------------
+TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
+*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA
+OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR
+CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR
+LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,
+OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE
+THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+------------------------------------------------------------------------------
+ INTEGRATION CHECKLIST
+------------------------------------------------------------------------------
+(1.)
+In the shader source, setup defines for the desired configuration.
+When providing multiple shaders (for different presets),
+simply setup the defines differently in multiple files.
+Example,
+
+ #define FXAA_PC 1
+ #define FXAA_HLSL_5 1
+ #define FXAA_QUALITY__PRESET 12
+
+Or,
+
+ #define FXAA_360 1
+
+Or,
+
+ #define FXAA_PS3 1
+
+Etc.
+
+(2.)
+Then include this file,
+
+ #include "Fxaa3_11.h"
+
+(3.)
+Then call the FXAA pixel shader from within your desired shader.
+Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
+As for FXAA 3.11 all inputs for all shaders are the same
+to enable easy porting between platforms.
+
+ return FxaaPixelShader(...);
+
+(4.)
+Insure pass prior to FXAA outputs RGBL (see next section).
+Or use,
+
+ #define FXAA_GREEN_AS_LUMA 1
+
+(5.)
+Setup engine to provide the following constants
+which are used in the FxaaPixelShader() inputs,
+
+ FxaaFloat2 fxaaQualityRcpFrame,
+ FxaaFloat4 fxaaConsoleRcpFrameOpt,
+ FxaaFloat4 fxaaConsoleRcpFrameOpt2,
+ FxaaFloat4 fxaaConsole360RcpFrameOpt2,
+ FxaaFloat fxaaQualitySubpix,
+ FxaaFloat fxaaQualityEdgeThreshold,
+ FxaaFloat fxaaQualityEdgeThresholdMin,
+ FxaaFloat fxaaConsoleEdgeSharpness,
+ FxaaFloat fxaaConsoleEdgeThreshold,
+ FxaaFloat fxaaConsoleEdgeThresholdMin,
+ FxaaFloat4 fxaaConsole360ConstDir
+
+Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
+
+(6.)
+Have FXAA vertex shader run as a full screen triangle,
+and output "pos" and "fxaaConsolePosPos"
+such that inputs in the pixel shader provide,
+
+ // {xy} = center of pixel
+ FxaaFloat2 pos,
+
+ // {xy__} = upper left of pixel
+ // {__zw} = lower right of pixel
+ FxaaFloat4 fxaaConsolePosPos,
+
+(7.)
+Insure the texture sampler(s) used by FXAA are set to bilinear filtering.
+
+
+------------------------------------------------------------------------------
+ INTEGRATION - RGBL AND COLORSPACE
+------------------------------------------------------------------------------
+FXAA3 requires RGBL as input unless the following is set,
+
+ #define FXAA_GREEN_AS_LUMA 1
+
+In which case the engine uses green in place of luma,
+and requires RGB input is in a non-linear colorspace.
+
+RGB should be LDR (low dynamic range).
+Specifically do FXAA after tonemapping.
+
+RGB data as returned by a texture fetch can be non-linear,
+or linear when FXAA_GREEN_AS_LUMA is not set.
+Note an "sRGB format" texture counts as linear,
+because the result of a texture fetch is linear data.
+Regular "RGBA8" textures in the sRGB colorspace are non-linear.
+
+If FXAA_GREEN_AS_LUMA is not set,
+luma must be stored in the alpha channel prior to running FXAA.
+This luma should be in a perceptual space (could be gamma 2.0).
+Example pass before FXAA where output is gamma 2.0 encoded,
+
+ color.rgb = ToneMap(color.rgb); // linear color output
+ color.rgb = sqrt(color.rgb); // gamma 2.0 color output
+ return color;
+
+To use FXAA,
+
+ color.rgb = ToneMap(color.rgb); // linear color output
+ color.rgb = sqrt(color.rgb); // gamma 2.0 color output
+ color.a = dot(color.rgb, FxaaFloat3(0.299, 0.587, 0.114)); // compute luma
+ return color;
+
+Another example where output is linear encoded,
+say for instance writing to an sRGB formated render target,
+where the render target does the conversion back to sRGB after blending,
+
+ color.rgb = ToneMap(color.rgb); // linear color output
+ return color;
+
+To use FXAA,
+
+ color.rgb = ToneMap(color.rgb); // linear color output
+ color.a = sqrt(dot(color.rgb, FxaaFloat3(0.299, 0.587, 0.114))); // compute luma
+ return color;
+
+Getting luma correct is required for the algorithm to work correctly.
+
+
+------------------------------------------------------------------------------
+ BEING LINEARLY CORRECT?
+------------------------------------------------------------------------------
+Applying FXAA to a framebuffer with linear RGB color will look worse.
+This is very counter intuitive, but happends to be true in this case.
+The reason is because dithering artifacts will be more visiable
+in a linear colorspace.
+
+
+------------------------------------------------------------------------------
+ COMPLEX INTEGRATION
+------------------------------------------------------------------------------
+Q. What if the engine is blending into RGB before wanting to run FXAA?
+
+A. In the last opaque pass prior to FXAA,
+ have the pass write out luma into alpha.
+ Then blend into RGB only.
+ FXAA should be able to run ok
+ assuming the blending pass did not any add aliasing.
+ This should be the common case for particles and common blending passes.
+
+A. Or use FXAA_GREEN_AS_LUMA.
+
+============================================================================*/
+
+#version 430 core
+
+layout(local_size_x = 16, local_size_y = 16) in;
+layout(rgba8, binding = 0) uniform image2D imgOutput;
+
+uniform sampler2D inputTexture;
+layout(location=0) uniform vec2 invResolution;
+
+#define FXAA_QUALITY__PRESET 12
+#define FXAA_GREEN_AS_LUMA 1
+#define FXAA_PC 1
+#define FXAA_GLSL_130 1
+
+
+/*============================================================================
+
+ INTEGRATION KNOBS
+
+/*==========================================================================*/
+#ifndef FXAA_PC
+ //
+ // FXAA Quality
+ // The high quality PC algorithm.
+ //
+ #define FXAA_PC 0
+#endif
+/*--------------------------------------------------------------------------*/
+#ifndef FXAA_GLSL_120
+ #define FXAA_GLSL_120 0
+#endif
+/*--------------------------------------------------------------------------*/
+#ifndef FXAA_GLSL_130
+ #define FXAA_GLSL_130 0
+#endif
+/*==========================================================================*/
+#ifndef FXAA_GREEN_AS_LUMA
+ //
+ // For those using non-linear color,
+ // and either not able to get luma in alpha, or not wanting to,
+ // this enables FXAA to run using green as a proxy for luma.
+ // So with this enabled, no need to pack luma in alpha.
+ //
+ // This will turn off AA on anything which lacks some amount of green.
+ // Pure red and blue or combination of only R and B, will get no AA.
+ //
+ // Might want to lower the settings for both,
+ // fxaaConsoleEdgeThresholdMin
+ // fxaaQualityEdgeThresholdMin
+ // In order to insure AA does not get turned off on colors
+ // which contain a minor amount of green.
+ //
+ // 1 = On.
+ // 0 = Off.
+ //
+ #define FXAA_GREEN_AS_LUMA 0
+#endif
+/*--------------------------------------------------------------------------*/
+#ifndef FXAA_EARLY_EXIT
+ //
+ // Controls algorithm's early exit path.
+ // On PS3 turning this ON adds 2 cycles to the shader.
+ // On 360 turning this OFF adds 10ths of a millisecond to the shader.
+ // Turning this off on console will result in a more blurry image.
+ // So this defaults to on.
+ //
+ // 1 = On.
+ // 0 = Off.
+ //
+ #define FXAA_EARLY_EXIT 1
+#endif
+/*--------------------------------------------------------------------------*/
+#ifndef FXAA_DISCARD
+ //
+ // Only valid for PC OpenGL currently.
+ // Probably will not work when FXAA_GREEN_AS_LUMA = 1.
+ //
+ // 1 = Use discard on pixels which don't need AA.
+ // For APIs which enable concurrent TEX+ROP from same surface.
+ // 0 = Return unchanged color on pixels which don't need AA.
+ //
+ #define FXAA_DISCARD 0
+#endif
+/*--------------------------------------------------------------------------*/
+#ifndef FXAA_FAST_PIXEL_OFFSET
+ //
+ // Used for GLSL 120 only.
+ //
+ // 1 = GL API supports fast pixel offsets
+ // 0 = do not use fast pixel offsets
+ //
+ #ifdef GL_EXT_gpu_shader4
+ #define FXAA_FAST_PIXEL_OFFSET 1
+ #endif
+ #ifdef GL_NV_gpu_shader5
+ #define FXAA_FAST_PIXEL_OFFSET 1
+ #endif
+ #ifdef GL_ARB_gpu_shader5
+ #define FXAA_FAST_PIXEL_OFFSET 1
+ #endif
+ #ifndef FXAA_FAST_PIXEL_OFFSET
+ #define FXAA_FAST_PIXEL_OFFSET 0
+ #endif
+#endif
+/*--------------------------------------------------------------------------*/
+#ifndef FXAA_GATHER4_ALPHA
+ //
+ // 1 = API supports gather4 on alpha channel.
+ // 0 = API does not support gather4 on alpha channel.
+ //
+ #if (FXAA_HLSL_5 == 1)
+ #define FXAA_GATHER4_ALPHA 1
+ #endif
+ #ifdef GL_ARB_gpu_shader5
+ #define FXAA_GATHER4_ALPHA 1
+ #endif
+ #ifdef GL_NV_gpu_shader5
+ #define FXAA_GATHER4_ALPHA 1
+ #endif
+ #ifndef FXAA_GATHER4_ALPHA
+ #define FXAA_GATHER4_ALPHA 0
+ #endif
+#endif
+
+/*============================================================================
+ FXAA QUALITY - TUNING KNOBS
+------------------------------------------------------------------------------
+NOTE the other tuning knobs are now in the shader function inputs!
+============================================================================*/
+#ifndef FXAA_QUALITY__PRESET
+ //
+ // Choose the quality preset.
+ // This needs to be compiled into the shader as it effects code.
+ // Best option to include multiple presets is to
+ // in each shader define the preset, then include this file.
+ //
+ // OPTIONS
+ // -----------------------------------------------------------------------
+ // 10 to 15 - default medium dither (10=fastest, 15=highest quality)
+ // 20 to 29 - less dither, more expensive (20=fastest, 29=highest quality)
+ // 39 - no dither, very expensive
+ //
+ // NOTES
+ // -----------------------------------------------------------------------
+ // 12 = slightly faster then FXAA 3.9 and higher edge quality (default)
+ // 13 = about same speed as FXAA 3.9 and better than 12
+ // 23 = closest to FXAA 3.9 visually and performance wise
+ // _ = the lowest digit is directly related to performance
+ // _ = the highest digit is directly related to style
+ //
+ #define FXAA_QUALITY__PRESET 12
+#endif
+
+
+/*============================================================================
+
+ FXAA QUALITY - PRESETS
+
+============================================================================*/
+
+/*============================================================================
+ FXAA QUALITY - MEDIUM DITHER PRESETS
+============================================================================*/
+#if (FXAA_QUALITY__PRESET == 10)
+ #define FXAA_QUALITY__PS 3
+ #define FXAA_QUALITY__P0 1.5
+ #define FXAA_QUALITY__P1 3.0
+ #define FXAA_QUALITY__P2 12.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 11)
+ #define FXAA_QUALITY__PS 4
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 3.0
+ #define FXAA_QUALITY__P3 12.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 12)
+ #define FXAA_QUALITY__PS 5
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 4.0
+ #define FXAA_QUALITY__P4 12.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 13)
+ #define FXAA_QUALITY__PS 6
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 4.0
+ #define FXAA_QUALITY__P5 12.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 14)
+ #define FXAA_QUALITY__PS 7
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 4.0
+ #define FXAA_QUALITY__P6 12.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 15)
+ #define FXAA_QUALITY__PS 8
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 2.0
+ #define FXAA_QUALITY__P6 4.0
+ #define FXAA_QUALITY__P7 12.0
+#endif
+
+/*============================================================================
+ FXAA QUALITY - LOW DITHER PRESETS
+============================================================================*/
+#if (FXAA_QUALITY__PRESET == 20)
+ #define FXAA_QUALITY__PS 3
+ #define FXAA_QUALITY__P0 1.5
+ #define FXAA_QUALITY__P1 2.0
+ #define FXAA_QUALITY__P2 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 21)
+ #define FXAA_QUALITY__PS 4
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 22)
+ #define FXAA_QUALITY__PS 5
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 23)
+ #define FXAA_QUALITY__PS 6
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 24)
+ #define FXAA_QUALITY__PS 7
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 3.0
+ #define FXAA_QUALITY__P6 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 25)
+ #define FXAA_QUALITY__PS 8
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 2.0
+ #define FXAA_QUALITY__P6 4.0
+ #define FXAA_QUALITY__P7 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 26)
+ #define FXAA_QUALITY__PS 9
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 2.0
+ #define FXAA_QUALITY__P6 2.0
+ #define FXAA_QUALITY__P7 4.0
+ #define FXAA_QUALITY__P8 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 27)
+ #define FXAA_QUALITY__PS 10
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 2.0
+ #define FXAA_QUALITY__P6 2.0
+ #define FXAA_QUALITY__P7 2.0
+ #define FXAA_QUALITY__P8 4.0
+ #define FXAA_QUALITY__P9 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 28)
+ #define FXAA_QUALITY__PS 11
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 2.0
+ #define FXAA_QUALITY__P6 2.0
+ #define FXAA_QUALITY__P7 2.0
+ #define FXAA_QUALITY__P8 2.0
+ #define FXAA_QUALITY__P9 4.0
+ #define FXAA_QUALITY__P10 8.0
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_QUALITY__PRESET == 29)
+ #define FXAA_QUALITY__PS 12
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.5
+ #define FXAA_QUALITY__P2 2.0
+ #define FXAA_QUALITY__P3 2.0
+ #define FXAA_QUALITY__P4 2.0
+ #define FXAA_QUALITY__P5 2.0
+ #define FXAA_QUALITY__P6 2.0
+ #define FXAA_QUALITY__P7 2.0
+ #define FXAA_QUALITY__P8 2.0
+ #define FXAA_QUALITY__P9 2.0
+ #define FXAA_QUALITY__P10 4.0
+ #define FXAA_QUALITY__P11 8.0
+#endif
+
+/*============================================================================
+ FXAA QUALITY - EXTREME QUALITY
+============================================================================*/
+#if (FXAA_QUALITY__PRESET == 39)
+ #define FXAA_QUALITY__PS 12
+ #define FXAA_QUALITY__P0 1.0
+ #define FXAA_QUALITY__P1 1.0
+ #define FXAA_QUALITY__P2 1.0
+ #define FXAA_QUALITY__P3 1.0
+ #define FXAA_QUALITY__P4 1.0
+ #define FXAA_QUALITY__P5 1.5
+ #define FXAA_QUALITY__P6 2.0
+ #define FXAA_QUALITY__P7 2.0
+ #define FXAA_QUALITY__P8 2.0
+ #define FXAA_QUALITY__P9 2.0
+ #define FXAA_QUALITY__P10 4.0
+ #define FXAA_QUALITY__P11 8.0
+#endif
+
+
+
+/*============================================================================
+
+ API PORTING
+
+============================================================================*/
+#if (FXAA_GLSL_120 == 1) || (FXAA_GLSL_130 == 1)
+ #define FxaaBool bool
+ #define FxaaDiscard discard
+ #define FxaaFloat float
+ #define FxaaFloat2 vec2
+ #define FxaaFloat3 vec3
+ #define FxaaFloat4 vec4
+ #define FxaaHalf float
+ #define FxaaHalf2 vec2
+ #define FxaaHalf3 vec3
+ #define FxaaHalf4 vec4
+ #define FxaaInt2 ivec2
+ #define FxaaSat(x) clamp(x, 0.0, 1.0)
+ #define FxaaTex sampler2D
+#else
+ #define FxaaBool bool
+ #define FxaaDiscard clip(-1)
+ #define FxaaFloat float
+ #define FxaaFloat2 float2
+ #define FxaaFloat3 float3
+ #define FxaaFloat4 float4
+ #define FxaaHalf half
+ #define FxaaHalf2 half2
+ #define FxaaHalf3 half3
+ #define FxaaHalf4 half4
+ #define FxaaSat(x) saturate(x)
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_GLSL_120 == 1)
+ // Requires,
+ // #version 120
+ // And at least,
+ // #extension GL_EXT_gpu_shader4 : enable
+ // (or set FXAA_FAST_PIXEL_OFFSET 1 to work like DX9)
+ #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
+ #if (FXAA_FAST_PIXEL_OFFSET == 1)
+ #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
+ #else
+ #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
+ #endif
+ #if (FXAA_GATHER4_ALPHA == 1)
+ // use #extension GL_ARB_gpu_shader5 : enable
+ #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
+ #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
+ #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
+ #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
+ #endif
+#endif
+/*--------------------------------------------------------------------------*/
+#if (FXAA_GLSL_130 == 1)
+ // Requires "#version 130" or better
+ #define FxaaTexTop(t, p) textureLod(t, p, 0.0)
+ #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
+ #if (FXAA_GATHER4_ALPHA == 1)
+ // use #extension GL_ARB_gpu_shader5 : enable
+ #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
+ #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
+ #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
+ #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
+ #endif
+#endif
+
+
+/*============================================================================
+ GREEN AS LUMA OPTION SUPPORT FUNCTION
+============================================================================*/
+#if (FXAA_GREEN_AS_LUMA == 0)
+ FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return rgba.w; }
+#else
+ FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return rgba.y; }
+#endif
+
+
+
+
+/*============================================================================
+
+ FXAA3 QUALITY - PC
+
+============================================================================*/
+#if (FXAA_PC == 1)
+/*--------------------------------------------------------------------------*/
+FxaaFloat4 FxaaPixelShader(
+ //
+ // Use noperspective interpolation here (turn off perspective interpolation).
+ // {xy} = center of pixel
+ FxaaFloat2 pos,
+ //
+ // Used only for FXAA Console, and not used on the 360 version.
+ // Use noperspective interpolation here (turn off perspective interpolation).
+ // {xy__} = upper left of pixel
+ // {__zw} = lower right of pixel
+ FxaaFloat4 fxaaConsolePosPos,
+ //
+ // Input color texture.
+ // {rgb_} = color in linear or perceptual color space
+ // if (FXAA_GREEN_AS_LUMA == 0)
+ // {___a} = luma in perceptual color space (not linear)
+ FxaaTex tex,
+ //
+ // Only used on the optimized 360 version of FXAA Console.
+ // For everything but 360, just use the same input here as for "tex".
+ // For 360, same texture, just alias with a 2nd sampler.
+ // This sampler needs to have an exponent bias of -1.
+ FxaaTex fxaaConsole360TexExpBiasNegOne,
+ //
+ // Only used on the optimized 360 version of FXAA Console.
+ // For everything but 360, just use the same input here as for "tex".
+ // For 360, same texture, just alias with a 3nd sampler.
+ // This sampler needs to have an exponent bias of -2.
+ FxaaTex fxaaConsole360TexExpBiasNegTwo,
+ //
+ // Only used on FXAA Quality.
+ // This must be from a constant/uniform.
+ // {x_} = 1.0/screenWidthInPixels
+ // {_y} = 1.0/screenHeightInPixels
+ FxaaFloat2 fxaaQualityRcpFrame,
+ //
+ // Only used on FXAA Console.
+ // This must be from a constant/uniform.
+ // This effects sub-pixel AA quality and inversely sharpness.
+ // Where N ranges between,
+ // N = 0.50 (default)
+ // N = 0.33 (sharper)
+ // {x___} = -N/screenWidthInPixels
+ // {_y__} = -N/screenHeightInPixels
+ // {__z_} = N/screenWidthInPixels
+ // {___w} = N/screenHeightInPixels
+ FxaaFloat4 fxaaConsoleRcpFrameOpt,
+ //
+ // Only used on FXAA Console.
+ // Not used on 360, but used on PS3 and PC.
+ // This must be from a constant/uniform.
+ // {x___} = -2.0/screenWidthInPixels
+ // {_y__} = -2.0/screenHeightInPixels
+ // {__z_} = 2.0/screenWidthInPixels
+ // {___w} = 2.0/screenHeightInPixels
+ FxaaFloat4 fxaaConsoleRcpFrameOpt2,
+ //
+ // Only used on FXAA Console.
+ // Only used on 360 in place of fxaaConsoleRcpFrameOpt2.
+ // This must be from a constant/uniform.
+ // {x___} = 8.0/screenWidthInPixels
+ // {_y__} = 8.0/screenHeightInPixels
+ // {__z_} = -4.0/screenWidthInPixels
+ // {___w} = -4.0/screenHeightInPixels
+ FxaaFloat4 fxaaConsole360RcpFrameOpt2,
+ //
+ // Only used on FXAA Quality.
+ // This used to be the FXAA_QUALITY__SUBPIX define.
+ // It is here now to allow easier tuning.
+ // Choose the amount of sub-pixel aliasing removal.
+ // This can effect sharpness.
+ // 1.00 - upper limit (softer)
+ // 0.75 - default amount of filtering
+ // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
+ // 0.25 - almost off
+ // 0.00 - completely off
+ FxaaFloat fxaaQualitySubpix,
+ //
+ // Only used on FXAA Quality.
+ // This used to be the FXAA_QUALITY__EDGE_THRESHOLD define.
+ // It is here now to allow easier tuning.
+ // The minimum amount of local contrast required to apply algorithm.
+ // 0.333 - too little (faster)
+ // 0.250 - low quality
+ // 0.166 - default
+ // 0.125 - high quality
+ // 0.063 - overkill (slower)
+ FxaaFloat fxaaQualityEdgeThreshold,
+ //
+ // Only used on FXAA Quality.
+ // This used to be the FXAA_QUALITY__EDGE_THRESHOLD_MIN define.
+ // It is here now to allow easier tuning.
+ // Trims the algorithm from processing darks.
+ // 0.0833 - upper limit (default, the start of visible unfiltered edges)
+ // 0.0625 - high quality (faster)
+ // 0.0312 - visible limit (slower)
+ // Special notes when using FXAA_GREEN_AS_LUMA,
+ // Likely want to set this to zero.
+ // As colors that are mostly not-green
+ // will appear very dark in the green channel!
+ // Tune by looking at mostly non-green content,
+ // then start at zero and increase until aliasing is a problem.
+ FxaaFloat fxaaQualityEdgeThresholdMin,
+ //
+ // Only used on FXAA Console.
+ // This used to be the FXAA_CONSOLE__EDGE_SHARPNESS define.
+ // It is here now to allow easier tuning.
+ // This does not effect PS3, as this needs to be compiled in.
+ // Use FXAA_CONSOLE__PS3_EDGE_SHARPNESS for PS3.
+ // Due to the PS3 being ALU bound,
+ // there are only three safe values here: 2 and 4 and 8.
+ // These options use the shaders ability to a free *|/ by 2|4|8.
+ // For all other platforms can be a non-power of two.
+ // 8.0 is sharper (default!!!)
+ // 4.0 is softer
+ // 2.0 is really soft (good only for vector graphics inputs)
+ FxaaFloat fxaaConsoleEdgeSharpness,
+ //
+ // Only used on FXAA Console.
+ // This used to be the FXAA_CONSOLE__EDGE_THRESHOLD define.
+ // It is here now to allow easier tuning.
+ // This does not effect PS3, as this needs to be compiled in.
+ // Use FXAA_CONSOLE__PS3_EDGE_THRESHOLD for PS3.
+ // Due to the PS3 being ALU bound,
+ // there are only two safe values here: 1/4 and 1/8.
+ // These options use the shaders ability to a free *|/ by 2|4|8.
+ // The console setting has a different mapping than the quality setting.
+ // Other platforms can use other values.
+ // 0.125 leaves less aliasing, but is softer (default!!!)
+ // 0.25 leaves more aliasing, and is sharper
+ FxaaFloat fxaaConsoleEdgeThreshold,
+ //
+ // Only used on FXAA Console.
+ // This used to be the FXAA_CONSOLE__EDGE_THRESHOLD_MIN define.
+ // It is here now to allow easier tuning.
+ // Trims the algorithm from processing darks.
+ // The console setting has a different mapping than the quality setting.
+ // This only applies when FXAA_EARLY_EXIT is 1.
+ // This does not apply to PS3,
+ // PS3 was simplified to avoid more shader instructions.
+ // 0.06 - faster but more aliasing in darks
+ // 0.05 - default
+ // 0.04 - slower and less aliasing in darks
+ // Special notes when using FXAA_GREEN_AS_LUMA,
+ // Likely want to set this to zero.
+ // As colors that are mostly not-green
+ // will appear very dark in the green channel!
+ // Tune by looking at mostly non-green content,
+ // then start at zero and increase until aliasing is a problem.
+ FxaaFloat fxaaConsoleEdgeThresholdMin,
+ //
+ // Extra constants for 360 FXAA Console only.
+ // Use zeros or anything else for other platforms.
+ // These must be in physical constant registers and NOT immedates.
+ // Immedates will result in compiler un-optimizing.
+ // {xyzw} = float4(1.0, -1.0, 0.25, -0.25)
+ FxaaFloat4 fxaaConsole360ConstDir
+) {
+/*--------------------------------------------------------------------------*/
+ FxaaFloat2 posM;
+ posM.x = pos.x;
+ posM.y = pos.y;
+ #if (FXAA_GATHER4_ALPHA == 1)
+ #if (FXAA_DISCARD == 0)
+ FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
+ #if (FXAA_GREEN_AS_LUMA == 0)
+ #define lumaM rgbyM.w
+ #else
+ #define lumaM rgbyM.y
+ #endif
+ #endif
+ #if (FXAA_GREEN_AS_LUMA == 0)
+ FxaaFloat4 luma4A = FxaaTexAlpha4(tex, posM);
+ FxaaFloat4 luma4B = FxaaTexOffAlpha4(tex, posM, FxaaInt2(-1, -1));
+ #else
+ FxaaFloat4 luma4A = FxaaTexGreen4(tex, posM);
+ FxaaFloat4 luma4B = FxaaTexOffGreen4(tex, posM, FxaaInt2(-1, -1));
+ #endif
+ #if (FXAA_DISCARD == 1)
+ #define lumaM luma4A.w
+ #endif
+ #define lumaE luma4A.z
+ #define lumaS luma4A.x
+ #define lumaSE luma4A.y
+ #define lumaNW luma4B.w
+ #define lumaN luma4B.z
+ #define lumaW luma4B.x
+ #else
+ FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
+ #if (FXAA_GREEN_AS_LUMA == 0)
+ #define lumaM rgbyM.w
+ #else
+ #define lumaM rgbyM.y
+ #endif
+ FxaaFloat lumaS = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0, 1), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 0), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaN = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0,-1), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 0), fxaaQualityRcpFrame.xy));
+ #endif
+/*--------------------------------------------------------------------------*/
+ FxaaFloat maxSM = max(lumaS, lumaM);
+ FxaaFloat minSM = min(lumaS, lumaM);
+ FxaaFloat maxESM = max(lumaE, maxSM);
+ FxaaFloat minESM = min(lumaE, minSM);
+ FxaaFloat maxWN = max(lumaN, lumaW);
+ FxaaFloat minWN = min(lumaN, lumaW);
+ FxaaFloat rangeMax = max(maxWN, maxESM);
+ FxaaFloat rangeMin = min(minWN, minESM);
+ FxaaFloat rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
+ FxaaFloat range = rangeMax - rangeMin;
+ FxaaFloat rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
+ FxaaBool earlyExit = range < rangeMaxClamped;
+/*--------------------------------------------------------------------------*/
+ if(earlyExit)
+ #if (FXAA_DISCARD == 1)
+ FxaaDiscard;
+ #else
+ return rgbyM;
+ #endif
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_GATHER4_ALPHA == 0)
+ FxaaFloat lumaNW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1,-1), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaSE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 1), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1,-1), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
+ #else
+ FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(1, -1), fxaaQualityRcpFrame.xy));
+ FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
+ #endif
+/*--------------------------------------------------------------------------*/
+ FxaaFloat lumaNS = lumaN + lumaS;
+ FxaaFloat lumaWE = lumaW + lumaE;
+ FxaaFloat subpixRcpRange = 1.0/range;
+ FxaaFloat subpixNSWE = lumaNS + lumaWE;
+ FxaaFloat edgeHorz1 = (-2.0 * lumaM) + lumaNS;
+ FxaaFloat edgeVert1 = (-2.0 * lumaM) + lumaWE;
+/*--------------------------------------------------------------------------*/
+ FxaaFloat lumaNESE = lumaNE + lumaSE;
+ FxaaFloat lumaNWNE = lumaNW + lumaNE;
+ FxaaFloat edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
+ FxaaFloat edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
+/*--------------------------------------------------------------------------*/
+ FxaaFloat lumaNWSW = lumaNW + lumaSW;
+ FxaaFloat lumaSWSE = lumaSW + lumaSE;
+ FxaaFloat edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
+ FxaaFloat edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
+ FxaaFloat edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
+ FxaaFloat edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
+ FxaaFloat edgeHorz = abs(edgeHorz3) + edgeHorz4;
+ FxaaFloat edgeVert = abs(edgeVert3) + edgeVert4;
+/*--------------------------------------------------------------------------*/
+ FxaaFloat subpixNWSWNESE = lumaNWSW + lumaNESE;
+ FxaaFloat lengthSign = fxaaQualityRcpFrame.x;
+ FxaaBool horzSpan = edgeHorz >= edgeVert;
+ FxaaFloat subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
+/*--------------------------------------------------------------------------*/
+ if(!horzSpan) lumaN = lumaW;
+ if(!horzSpan) lumaS = lumaE;
+ if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
+ FxaaFloat subpixB = (subpixA * (1.0/12.0)) - lumaM;
+/*--------------------------------------------------------------------------*/
+ FxaaFloat gradientN = lumaN - lumaM;
+ FxaaFloat gradientS = lumaS - lumaM;
+ FxaaFloat lumaNN = lumaN + lumaM;
+ FxaaFloat lumaSS = lumaS + lumaM;
+ FxaaBool pairN = abs(gradientN) >= abs(gradientS);
+ FxaaFloat gradient = max(abs(gradientN), abs(gradientS));
+ if(pairN) lengthSign = -lengthSign;
+ FxaaFloat subpixC = FxaaSat(abs(subpixB) * subpixRcpRange);
+/*--------------------------------------------------------------------------*/
+ FxaaFloat2 posB;
+ posB.x = posM.x;
+ posB.y = posM.y;
+ FxaaFloat2 offNP;
+ offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
+ offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
+ if(!horzSpan) posB.x += lengthSign * 0.5;
+ if( horzSpan) posB.y += lengthSign * 0.5;
+/*--------------------------------------------------------------------------*/
+ FxaaFloat2 posN;
+ posN.x = posB.x - offNP.x * FXAA_QUALITY__P0;
+ posN.y = posB.y - offNP.y * FXAA_QUALITY__P0;
+ FxaaFloat2 posP;
+ posP.x = posB.x + offNP.x * FXAA_QUALITY__P0;
+ posP.y = posB.y + offNP.y * FXAA_QUALITY__P0;
+ FxaaFloat subpixD = ((-2.0)*subpixC) + 3.0;
+ FxaaFloat lumaEndN = FxaaLuma(FxaaTexTop(tex, posN));
+ FxaaFloat subpixE = subpixC * subpixC;
+ FxaaFloat lumaEndP = FxaaLuma(FxaaTexTop(tex, posP));
+/*--------------------------------------------------------------------------*/
+ if(!pairN) lumaNN = lumaSS;
+ FxaaFloat gradientScaled = gradient * 1.0/4.0;
+ FxaaFloat lumaMM = lumaM - lumaNN * 0.5;
+ FxaaFloat subpixF = subpixD * subpixE;
+ FxaaBool lumaMLTZero = lumaMM < 0.0;
+/*--------------------------------------------------------------------------*/
+ lumaEndN -= lumaNN * 0.5;
+ lumaEndP -= lumaNN * 0.5;
+ FxaaBool doneN = abs(lumaEndN) >= gradientScaled;
+ FxaaBool doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P1;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P1;
+ FxaaBool doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P1;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P1;
+/*--------------------------------------------------------------------------*/
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P2;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P2;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P2;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P2;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 3)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P3;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P3;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P3;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P3;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 4)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P4;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P4;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P4;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P4;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 5)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P5;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P5;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P5;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P5;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 6)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P6;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P6;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P6;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P6;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 7)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P7;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P7;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P7;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P7;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 8)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P8;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P8;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P8;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P8;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 9)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P9;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P9;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P9;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P9;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 10)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P10;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P10;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P10;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P10;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 11)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P11;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P11;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P11;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P11;
+/*--------------------------------------------------------------------------*/
+ #if (FXAA_QUALITY__PS > 12)
+ if(doneNP) {
+ if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
+ if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
+ if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
+ if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
+ doneN = abs(lumaEndN) >= gradientScaled;
+ doneP = abs(lumaEndP) >= gradientScaled;
+ if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P12;
+ if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P12;
+ doneNP = (!doneN) || (!doneP);
+ if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P12;
+ if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P12;
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+ #endif
+/*--------------------------------------------------------------------------*/
+ }
+/*--------------------------------------------------------------------------*/
+ FxaaFloat dstN = posM.x - posN.x;
+ FxaaFloat dstP = posP.x - posM.x;
+ if(!horzSpan) dstN = posM.y - posN.y;
+ if(!horzSpan) dstP = posP.y - posM.y;
+/*--------------------------------------------------------------------------*/
+ FxaaBool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
+ FxaaFloat spanLength = (dstP + dstN);
+ FxaaBool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
+ FxaaFloat spanLengthRcp = 1.0/spanLength;
+/*--------------------------------------------------------------------------*/
+ FxaaBool directionN = dstN < dstP;
+ FxaaFloat dst = min(dstN, dstP);
+ FxaaBool goodSpan = directionN ? goodSpanN : goodSpanP;
+ FxaaFloat subpixG = subpixF * subpixF;
+ FxaaFloat pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
+ FxaaFloat subpixH = subpixG * fxaaQualitySubpix;
+/*--------------------------------------------------------------------------*/
+ FxaaFloat pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
+ FxaaFloat pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
+ if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
+ if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
+ #if (FXAA_DISCARD == 1)
+ return FxaaTexTop(tex, posM);
+ #else
+ return FxaaFloat4(FxaaTexTop(tex, posM).xyz, lumaM);
+ #endif
+}
+/*==========================================================================*/
+#endif
+
+vec4 mainImage(vec2 fragCoord)
+{
+ vec2 rcpFrame = 1./invResolution.xy;
+ vec2 uv2 = fragCoord.xy / invResolution.xy;
+
+ float fxaaQualitySubpix = 0.75; // [0..1], default 0.75
+ float fxaaQualityEdgeThreshold = 0.166; // [0.125..0.33], default 0.166
+ float fxaaQualityEdgeThresholdMin = 0.02;//0.0625; // ?
+ vec4 dummy4 = vec4(0.0,0.0,0.0,0.0);
+ float dummy1 = 0.0;
+
+ vec4 col = FxaaPixelShader(uv2, dummy4,
+ inputTexture, inputTexture, inputTexture,
+ rcpFrame, dummy4, dummy4, dummy4,
+ fxaaQualitySubpix, fxaaQualityEdgeThreshold,
+ fxaaQualityEdgeThresholdMin,
+ dummy1, dummy1, dummy1, dummy4);
+
+ vec4 fragColor = vec4( col.xyz, 1. );
+
+ return fragColor;
+}
+
+void main()
+{
+ ivec2 loc = ivec2(gl_GlobalInvocationID.x * 4, gl_GlobalInvocationID.y * 4);
+ for(int i = 0; i < 4; i++)
+ {
+ for(int j = 0; j < 4; j++)
+ {
+ ivec2 texelCoord = ivec2(loc.x + i, loc.y + j);
+ vec4 outColor = mainImage(texelCoord + vec2(0.5));
+ imageStore(imgOutput, texelCoord, outColor);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa.hlsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa.hlsl
new file mode 100644
index 00000000..2201f78c
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa.hlsl
@@ -0,0 +1,1361 @@
+/**
+ * Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com)
+ * Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com)
+ * Copyright (C) 2013 Belen Masia (bmasia@unizar.es)
+ * Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com)
+ * Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to
+ * do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software. As clarification, there
+ * is no requirement that the copyright notice and permission be included in
+ * binary distributions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+
+/**
+ * _______ ___ ___ ___ ___
+ * / || \/ | / \ / \
+ * | (---- | \ / | / ^ \ / ^ \
+ * \ \ | |\/| | / /_\ \ / /_\ \
+ * ----) | | | | | / _____ \ / _____ \
+ * |_______/ |__| |__| /__/ \__\ /__/ \__\
+ *
+ * E N H A N C E D
+ * S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G
+ *
+ * http://www.iryoku.com/smaa/
+ *
+ * Hi, welcome aboard!
+ *
+ * Here you'll find instructions to get the shader up and running as fast as
+ * possible.
+ *
+ * IMPORTANTE NOTICE: when updating, remember to update both this file and the
+ * precomputed textures! They may change from version to version.
+ *
+ * The shader has three passes, chained together as follows:
+ *
+ * |input|------------------�
+ * v |
+ * [ SMAA*EdgeDetection ] |
+ * v |
+ * |edgesTex| |
+ * v |
+ * [ SMAABlendingWeightCalculation ] |
+ * v |
+ * |blendTex| |
+ * v |
+ * [ SMAANeighborhoodBlending ] <------�
+ * v
+ * |output|
+ *
+ * Note that each [pass] has its own vertex and pixel shader. Remember to use
+ * oversized triangles instead of quads to avoid overshading along the
+ * diagonal.
+ *
+ * You've three edge detection methods to choose from: luma, color or depth.
+ * They represent different quality/performance and anti-aliasing/sharpness
+ * tradeoffs, so our recommendation is for you to choose the one that best
+ * suits your particular scenario:
+ *
+ * - Depth edge detection is usually the fastest but it may miss some edges.
+ *
+ * - Luma edge detection is usually more expensive than depth edge detection,
+ * but catches visible edges that depth edge detection can miss.
+ *
+ * - Color edge detection is usually the most expensive one but catches
+ * chroma-only edges.
+ *
+ * For quickstarters: just use luma edge detection.
+ *
+ * The general advice is to not rush the integration process and ensure each
+ * step is done correctly (don't try to integrate SMAA T2x with predicated edge
+ * detection from the start!). Ok then, let's go!
+ *
+ * 1. The first step is to create two RGBA temporal render targets for holding
+ * |edgesTex| and |blendTex|.
+ *
+ * In DX10 or DX11, you can use a RG render target for the edges texture.
+ * In the case of NVIDIA GPUs, using RG render targets seems to actually be
+ * slower.
+ *
+ * On the Xbox 360, you can use the same render target for resolving both
+ * |edgesTex| and |blendTex|, as they aren't needed simultaneously.
+ *
+ * 2. Both temporal render targets |edgesTex| and |blendTex| must be cleared
+ * each frame. Do not forget to clear the alpha channel!
+ *
+ * 3. The next step is loading the two supporting precalculated textures,
+ * 'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as
+ * C++ headers, and also as regular DDS files. They'll be needed for the
+ * 'SMAABlendingWeightCalculation' pass.
+ *
+ * If you use the C++ headers, be sure to load them in the format specified
+ * inside of them.
+ *
+ * You can also compress 'areaTex' and 'searchTex' using BC5 and BC4
+ * respectively, if you have that option in your content processor pipeline.
+ * When compressing then, you get a non-perceptible quality decrease, and a
+ * marginal performance increase.
+ *
+ * 4. All samplers must be set to linear filtering and clamp.
+ *
+ * After you get the technique working, remember that 64-bit inputs have
+ * half-rate linear filtering on GCN.
+ *
+ * If SMAA is applied to 64-bit color buffers, switching to point filtering
+ * when accesing them will increase the performance. Search for
+ * 'SMAASamplePoint' to see which textures may benefit from point
+ * filtering, and where (which is basically the color input in the edge
+ * detection and resolve passes).
+ *
+ * 5. All texture reads and buffer writes must be non-sRGB, with the exception
+ * of the input read and the output write in
+ * 'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in
+ * this last pass are not possible, the technique will work anyway, but
+ * will perform antialiasing in gamma space.
+ *
+ * IMPORTANT: for best results the input read for the color/luma edge
+ * detection should *NOT* be sRGB.
+ *
+ * 6. Before including SMAA.h you'll have to setup the render target metrics,
+ * the target and any optional configuration defines. Optionally you can
+ * use a preset.
+ *
+ * You have the following targets available:
+ * SMAA_HLSL_3
+ * SMAA_HLSL_4
+ * SMAA_HLSL_4_1
+ * SMAA_GLSL_3 *
+ * SMAA_GLSL_4 *
+ *
+ * * (See SMAA_INCLUDE_VS and SMAA_INCLUDE_PS below).
+ *
+ * And four presets:
+ * SMAA_PRESET_LOW (%60 of the quality)
+ * SMAA_PRESET_MEDIUM (%80 of the quality)
+ * SMAA_PRESET_HIGH (%95 of the quality)
+ * SMAA_PRESET_ULTRA (%99 of the quality)
+ *
+ * For example:
+ * #define SMAA_RT_METRICS float4(1.0 / 1280.0, 1.0 / 720.0, 1280.0, 720.0)
+ * #define SMAA_HLSL_4
+ * #define SMAA_PRESET_HIGH
+ * #include "SMAA.h"
+ *
+ * Note that SMAA_RT_METRICS doesn't need to be a macro, it can be a
+ * uniform variable. The code is designed to minimize the impact of not
+ * using a constant value, but it is still better to hardcode it.
+ *
+ * Depending on how you encoded 'areaTex' and 'searchTex', you may have to
+ * add (and customize) the following defines before including SMAA.h:
+ * #define SMAA_AREATEX_SELECT(sample) sample.rg
+ * #define SMAA_SEARCHTEX_SELECT(sample) sample.r
+ *
+ * If your engine is already using porting macros, you can define
+ * SMAA_CUSTOM_SL, and define the porting functions by yourself.
+ *
+ * 7. Then, you'll have to setup the passes as indicated in the scheme above.
+ * You can take a look into SMAA.fx, to see how we did it for our demo.
+ * Checkout the function wrappers, you may want to copy-paste them!
+ *
+ * 8. It's recommended to validate the produced |edgesTex| and |blendTex|.
+ * You can use a screenshot from your engine to compare the |edgesTex|
+ * and |blendTex| produced inside of the engine with the results obtained
+ * with the reference demo.
+ *
+ * 9. After you get the last pass to work, it's time to optimize. You'll have
+ * to initialize a stencil buffer in the first pass (discard is already in
+ * the code), then mask execution by using it the second pass. The last
+ * pass should be executed in all pixels.
+ *
+ *
+ * After this point you can choose to enable predicated thresholding,
+ * temporal supersampling and motion blur integration:
+ *
+ * a) If you want to use predicated thresholding, take a look into
+ * SMAA_PREDICATION; you'll need to pass an extra texture in the edge
+ * detection pass.
+ *
+ * b) If you want to enable temporal supersampling (SMAA T2x):
+ *
+ * 1. The first step is to render using subpixel jitters. I won't go into
+ * detail, but it's as simple as moving each vertex position in the
+ * vertex shader, you can check how we do it in our DX10 demo.
+ *
+ * 2. Then, you must setup the temporal resolve. You may want to take a look
+ * into SMAAResolve for resolving 2x modes. After you get it working, you'll
+ * probably see ghosting everywhere. But fear not, you can enable the
+ * CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro.
+ * Check out SMAA_DECODE_VELOCITY if your velocity buffer is encoded.
+ *
+ * 3. The next step is to apply SMAA to each subpixel jittered frame, just as
+ * done for 1x.
+ *
+ * 4. At this point you should already have something usable, but for best
+ * results the proper area textures must be set depending on current jitter.
+ * For this, the parameter 'subsampleIndices' of
+ * 'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x
+ * mode:
+ *
+ * @SUBSAMPLE_INDICES
+ *
+ * | S# | Camera Jitter | subsampleIndices |
+ * +----+------------------+---------------------+
+ * | 0 | ( 0.25, -0.25) | float4(1, 1, 1, 0) |
+ * | 1 | (-0.25, 0.25) | float4(2, 2, 2, 0) |
+ *
+ * These jitter positions assume a bottom-to-top y axis. S# stands for the
+ * sample number.
+ *
+ * More information about temporal supersampling here:
+ * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
+ *
+ * c) If you want to enable spatial multisampling (SMAA S2x):
+ *
+ * 1. The scene must be rendered using MSAA 2x. The MSAA 2x buffer must be
+ * created with:
+ * - DX10: see below (*)
+ * - DX10.1: D3D10_STANDARD_MULTISAMPLE_PATTERN or
+ * - DX11: D3D11_STANDARD_MULTISAMPLE_PATTERN
+ *
+ * This allows to ensure that the subsample order matches the table in
+ * @SUBSAMPLE_INDICES.
+ *
+ * (*) In the case of DX10, we refer the reader to:
+ * - SMAA::detectMSAAOrder and
+ * - SMAA::msaaReorder
+ *
+ * These functions allow to match the standard multisample patterns by
+ * detecting the subsample order for a specific GPU, and reordering
+ * them appropriately.
+ *
+ * 2. A shader must be run to output each subsample into a separate buffer
+ * (DX10 is required). You can use SMAASeparate for this purpose, or just do
+ * it in an existing pass (for example, in the tone mapping pass, which has
+ * the advantage of feeding tone mapped subsamples to SMAA, which will yield
+ * better results).
+ *
+ * 3. The full SMAA 1x pipeline must be run for each separated buffer, storing
+ * the results in the final buffer. The second run should alpha blend with
+ * the existing final buffer using a blending factor of 0.5.
+ * 'subsampleIndices' must be adjusted as in the SMAA T2x case (see point
+ * b).
+ *
+ * d) If you want to enable temporal supersampling on top of SMAA S2x
+ * (which actually is SMAA 4x):
+ *
+ * 1. SMAA 4x consists on temporally jittering SMAA S2x, so the first step is
+ * to calculate SMAA S2x for current frame. In this case, 'subsampleIndices'
+ * must be set as follows:
+ *
+ * | F# | S# | Camera Jitter | Net Jitter | subsampleIndices |
+ * +----+----+--------------------+-------------------+----------------------+
+ * | 0 | 0 | ( 0.125, 0.125) | ( 0.375, -0.125) | float4(5, 3, 1, 3) |
+ * | 0 | 1 | ( 0.125, 0.125) | (-0.125, 0.375) | float4(4, 6, 2, 3) |
+ * +----+----+--------------------+-------------------+----------------------+
+ * | 1 | 2 | (-0.125, -0.125) | ( 0.125, -0.375) | float4(3, 5, 1, 4) |
+ * | 1 | 3 | (-0.125, -0.125) | (-0.375, 0.125) | float4(6, 4, 2, 4) |
+ *
+ * These jitter positions assume a bottom-to-top y axis. F# stands for the
+ * frame number. S# stands for the sample number.
+ *
+ * 2. After calculating SMAA S2x for current frame (with the new subsample
+ * indices), previous frame must be reprojected as in SMAA T2x mode (see
+ * point b).
+ *
+ * e) If motion blur is used, you may want to do the edge detection pass
+ * together with motion blur. This has two advantages:
+ *
+ * 1. Pixels under heavy motion can be omitted from the edge detection process.
+ * For these pixels we can just store "no edge", as motion blur will take
+ * care of them.
+ * 2. The center pixel tap is reused.
+ *
+ * Note that in this case depth testing should be used instead of stenciling,
+ * as we have to write all the pixels in the motion blur pass.
+ *
+ * That's it!
+ */
+
+//-----------------------------------------------------------------------------
+// SMAA Presets
+
+/**
+ * Note that if you use one of these presets, the following configuration
+ * macros will be ignored if set in the "Configurable Defines" section.
+ */
+
+#if defined(SMAA_PRESET_LOW)
+#define SMAA_THRESHOLD 0.15
+#define SMAA_MAX_SEARCH_STEPS 4
+#define SMAA_DISABLE_DIAG_DETECTION
+#define SMAA_DISABLE_CORNER_DETECTION
+#elif defined(SMAA_PRESET_MEDIUM)
+#define SMAA_THRESHOLD 0.1
+#define SMAA_MAX_SEARCH_STEPS 8
+#define SMAA_DISABLE_DIAG_DETECTION
+#define SMAA_DISABLE_CORNER_DETECTION
+#elif defined(SMAA_PRESET_HIGH)
+#define SMAA_THRESHOLD 0.1
+#define SMAA_MAX_SEARCH_STEPS 16
+#define SMAA_MAX_SEARCH_STEPS_DIAG 8
+#define SMAA_CORNER_ROUNDING 25
+#elif defined(SMAA_PRESET_ULTRA)
+#define SMAA_THRESHOLD 0.05
+#define SMAA_MAX_SEARCH_STEPS 32
+#define SMAA_MAX_SEARCH_STEPS_DIAG 16
+#define SMAA_CORNER_ROUNDING 25
+#endif
+
+//-----------------------------------------------------------------------------
+// Configurable Defines
+
+/**
+ * SMAA_THRESHOLD specifies the threshold or sensitivity to edges.
+ * Lowering this value you will be able to detect more edges at the expense of
+ * performance.
+ *
+ * Range: [0, 0.5]
+ * 0.1 is a reasonable value, and allows to catch most visible edges.
+ * 0.05 is a rather overkill value, that allows to catch 'em all.
+ *
+ * If temporal supersampling is used, 0.2 could be a reasonable value, as low
+ * contrast edges are properly filtered by just 2x.
+ */
+#ifndef SMAA_THRESHOLD
+#define SMAA_THRESHOLD 0.1
+#endif
+
+/**
+ * SMAA_DEPTH_THRESHOLD specifies the threshold for depth edge detection.
+ *
+ * Range: depends on the depth range of the scene.
+ */
+#ifndef SMAA_DEPTH_THRESHOLD
+#define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD)
+#endif
+
+/**
+ * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the
+ * horizontal/vertical pattern searches, at each side of the pixel.
+ *
+ * In number of pixels, it's actually the double. So the maximum line length
+ * perfectly handled by, for example 16, is 64 (by perfectly, we meant that
+ * longer lines won't look as good, but still antialiased).
+ *
+ * Range: [0, 112]
+ */
+#ifndef SMAA_MAX_SEARCH_STEPS
+#define SMAA_MAX_SEARCH_STEPS 16
+#endif
+
+/**
+ * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the
+ * diagonal pattern searches, at each side of the pixel. In this case we jump
+ * one pixel at time, instead of two.
+ *
+ * Range: [0, 20]
+ *
+ * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16
+ * steps), but it can have a significant impact on older machines.
+ *
+ * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing.
+ */
+#ifndef SMAA_MAX_SEARCH_STEPS_DIAG
+#define SMAA_MAX_SEARCH_STEPS_DIAG 8
+#endif
+
+/**
+ * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded.
+ *
+ * Range: [0, 100]
+ *
+ * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing.
+ */
+#ifndef SMAA_CORNER_ROUNDING
+#define SMAA_CORNER_ROUNDING 25
+#endif
+
+/**
+ * If there is an neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times
+ * bigger contrast than current edge, current edge will be discarded.
+ *
+ * This allows to eliminate spurious crossing edges, and is based on the fact
+ * that, if there is too much contrast in a direction, that will hide
+ * perceptually contrast in the other neighbors.
+ */
+#ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR
+#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0
+#endif
+
+/**
+ * Predicated thresholding allows to better preserve texture details and to
+ * improve performance, by decreasing the number of detected edges using an
+ * additional buffer like the light accumulation buffer, object ids or even the
+ * depth buffer (the depth buffer usage may be limited to indoor or short range
+ * scenes).
+ *
+ * It locally decreases the luma or color threshold if an edge is found in an
+ * additional buffer (so the global threshold can be higher).
+ *
+ * This method was developed by Playstation EDGE MLAA team, and used in
+ * Killzone 3, by using the light accumulation buffer. More information here:
+ * http://iryoku.com/aacourse/downloads/06-MLAA-on-PS3.pptx
+ */
+#ifndef SMAA_PREDICATION
+#define SMAA_PREDICATION 0
+#endif
+
+/**
+ * Threshold to be used in the additional predication buffer.
+ *
+ * Range: depends on the input, so you'll have to find the magic number that
+ * works for you.
+ */
+#ifndef SMAA_PREDICATION_THRESHOLD
+#define SMAA_PREDICATION_THRESHOLD 0.01
+#endif
+
+/**
+ * How much to scale the global threshold used for luma or color edge
+ * detection when using predication.
+ *
+ * Range: [1, 5]
+ */
+#ifndef SMAA_PREDICATION_SCALE
+#define SMAA_PREDICATION_SCALE 2.0
+#endif
+
+/**
+ * How much to locally decrease the threshold.
+ *
+ * Range: [0, 1]
+ */
+#ifndef SMAA_PREDICATION_STRENGTH
+#define SMAA_PREDICATION_STRENGTH 0.4
+#endif
+
+/**
+ * Temporal reprojection allows to remove ghosting artifacts when using
+ * temporal supersampling. We use the CryEngine 3 method which also introduces
+ * velocity weighting. This feature is of extreme importance for totally
+ * removing ghosting. More information here:
+ * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
+ *
+ * Note that you'll need to setup a velocity buffer for enabling reprojection.
+ * For static geometry, saving the previous depth buffer is a viable
+ * alternative.
+ */
+#ifndef SMAA_REPROJECTION
+#define SMAA_REPROJECTION 0
+#endif
+
+/**
+ * SMAA_REPROJECTION_WEIGHT_SCALE controls the velocity weighting. It allows to
+ * remove ghosting trails behind the moving object, which are not removed by
+ * just using reprojection. Using low values will exhibit ghosting, while using
+ * high values will disable temporal supersampling under motion.
+ *
+ * Behind the scenes, velocity weighting removes temporal supersampling when
+ * the velocity of the subsamples differs (meaning they are different objects).
+ *
+ * Range: [0, 80]
+ */
+#ifndef SMAA_REPROJECTION_WEIGHT_SCALE
+#define SMAA_REPROJECTION_WEIGHT_SCALE 30.0
+#endif
+
+/**
+ * On some compilers, discard cannot be used in vertex shaders. Thus, they need
+ * to be compiled separately.
+ */
+#ifndef SMAA_INCLUDE_VS
+#define SMAA_INCLUDE_VS 1
+#endif
+#ifndef SMAA_INCLUDE_PS
+#define SMAA_INCLUDE_PS 1
+#endif
+
+//-----------------------------------------------------------------------------
+// Texture Access Defines
+
+#ifndef SMAA_AREATEX_SELECT
+#if defined(SMAA_HLSL_3)
+#define SMAA_AREATEX_SELECT(sample) sample.ra
+#else
+#define SMAA_AREATEX_SELECT(sample) sample.rg
+#endif
+#endif
+
+#ifndef SMAA_SEARCHTEX_SELECT
+#define SMAA_SEARCHTEX_SELECT(sample) sample.r
+#endif
+
+#ifndef SMAA_DECODE_VELOCITY
+#define SMAA_DECODE_VELOCITY(sample) sample.rg
+#endif
+
+//-----------------------------------------------------------------------------
+// Non-Configurable Defines
+
+#define SMAA_AREATEX_MAX_DISTANCE 16
+#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
+#define SMAA_AREATEX_PIXEL_SIZE (1.0 / float2(160.0, 560.0))
+#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)
+#define SMAA_SEARCHTEX_SIZE float2(66.0, 33.0)
+#define SMAA_SEARCHTEX_PACKED_SIZE float2(64.0, 16.0)
+#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0)
+
+//-----------------------------------------------------------------------------
+// Porting Functions
+
+#if defined(SMAA_HLSL_3)
+#define SMAATexture2D(tex) sampler2D tex
+#define SMAATexturePass2D(tex) tex
+#define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
+#define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
+#define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0))
+#define SMAASample(tex, coord) tex2D(tex, coord)
+#define SMAASamplePoint(tex, coord) tex2D(tex, coord)
+#define SMAASampleOffset(tex, coord, offset) tex2D(tex, coord + offset * SMAA_RT_METRICS.xy)
+#define SMAA_FLATTEN [flatten]
+#define SMAA_BRANCH [branch]
+#endif
+#if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1)
+SamplerState LinearSampler { Filter = MIN_MAG_LINEAR_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };
+SamplerState PointSampler { Filter = MIN_MAG_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };
+#define SMAATexture2D(tex) Texture2D tex
+#define SMAATexturePass2D(tex) tex
+#define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, 0)
+#define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, 0)
+#define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset)
+#define SMAASample(tex, coord) tex.Sample(LinearSampler, coord)
+#define SMAASamplePoint(tex, coord) tex.Sample(PointSampler, coord)
+#define SMAASampleOffset(tex, coord, offset) tex.Sample(LinearSampler, coord, offset)
+#define SMAA_FLATTEN [flatten]
+#define SMAA_BRANCH [branch]
+#define SMAATexture2DMS2(tex) Texture2DMS<float4, 2> tex
+#define SMAALoad(tex, pos, sample) tex.Load(pos, sample)
+#if defined(SMAA_HLSL_4_1)
+#define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0)
+#endif
+#endif
+#if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4)
+#define SMAATexture2D(tex) sampler2D tex
+#define SMAATexturePass2D(tex) tex
+#define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0)
+#define SMAASampleLevelZeroPoint(tex, coord) textureLod(tex, coord, 0.0)
+#define SMAASampleLevelZeroOffset(tex, coord, offset) textureLodOffset(tex, coord, 0.0, offset)
+#define SMAASample(tex, coord) texture(tex, coord)
+#define SMAASamplePoint(tex, coord) texture(tex, coord)
+#define SMAASampleOffset(tex, coord, offset) texture(tex, coord, offset)
+#define SMAA_FLATTEN
+#define SMAA_BRANCH
+#define lerp(a, b, t) mix(a, b, t)
+#define saturate(a) clamp(a, 0.0, 1.0)
+#if defined(SMAA_GLSL_4)
+#define mad(a, b, c) fma(a, b, c)
+#define SMAAGather(tex, coord) textureGather(tex, coord)
+#else
+#define mad(a, b, c) (a * b + c)
+#endif
+#define float2 vec2
+#define float3 vec3
+#define float4 vec4
+#define int2 ivec2
+#define int3 ivec3
+#define int4 ivec4
+#define bool2 bvec2
+#define bool3 bvec3
+#define bool4 bvec4
+#endif
+
+#if !defined(SMAA_HLSL_3) && !defined(SMAA_HLSL_4) && !defined(SMAA_HLSL_4_1) && !defined(SMAA_GLSL_3) && !defined(SMAA_GLSL_4) && !defined(SMAA_CUSTOM_SL)
+#error you must define the shading language: SMAA_HLSL_*, SMAA_GLSL_* or SMAA_CUSTOM_SL
+#endif
+
+//-----------------------------------------------------------------------------
+// Misc functions
+
+/**
+ * Gathers current pixel, and the top-left neighbors.
+ */
+float3 SMAAGatherNeighbours(float2 texcoord,
+ float4 offset[3],
+ SMAATexture2D(tex)) {
+ #ifdef SMAAGather
+ return SMAAGather(tex, texcoord + SMAA_RT_METRICS.xy * float2(-0.5, -0.5)).grb;
+ #else
+ float P = SMAASamplePoint(tex, texcoord).r;
+ float Pleft = SMAASamplePoint(tex, offset[0].xy).r;
+ float Ptop = SMAASamplePoint(tex, offset[0].zw).r;
+ return float3(P, Pleft, Ptop);
+ #endif
+}
+
+/**
+ * Adjusts the threshold by means of predication.
+ */
+float2 SMAACalculatePredicatedThreshold(float2 texcoord,
+ float4 offset[3],
+ SMAATexture2D(predicationTex)) {
+ float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(predicationTex));
+ float2 delta = abs(neighbours.xx - neighbours.yz);
+ float2 edges = step(SMAA_PREDICATION_THRESHOLD, delta);
+ return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges);
+}
+
+/**
+ * Conditional move:
+ */
+void SMAAMovc(bool2 cond, inout float2 variable, float2 value) {
+ SMAA_FLATTEN if (cond.x) variable.x = value.x;
+ SMAA_FLATTEN if (cond.y) variable.y = value.y;
+}
+
+void SMAAMovc(bool4 cond, inout float4 variable, float4 value) {
+ SMAAMovc(cond.xy, variable.xy, value.xy);
+ SMAAMovc(cond.zw, variable.zw, value.zw);
+}
+
+
+#if SMAA_INCLUDE_VS
+//-----------------------------------------------------------------------------
+// Vertex Shaders
+
+/**
+ * Edge Detection Vertex Shader
+ */
+void SMAAEdgeDetectionVS(float2 texcoord,
+ out float4 offset[3]) {
+ offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), texcoord.xyxy);
+ offset[1] = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), texcoord.xyxy);
+ offset[2] = mad(SMAA_RT_METRICS.xyxy, float4(-2.0, 0.0, 0.0, -2.0), texcoord.xyxy);
+}
+
+/**
+ * Blend Weight Calculation Vertex Shader
+ */
+void SMAABlendingWeightCalculationVS(float2 texcoord,
+ out float2 pixcoord,
+ out float4 offset[3]) {
+ pixcoord = texcoord * SMAA_RT_METRICS.zw;
+
+ // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
+ offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-0.25, -0.125, 1.25, -0.125), texcoord.xyxy);
+ offset[1] = mad(SMAA_RT_METRICS.xyxy, float4(-0.125, -0.25, -0.125, 1.25), texcoord.xyxy);
+
+ // And these for the searches, they indicate the ends of the loops:
+ offset[2] = mad(SMAA_RT_METRICS.xxyy,
+ float4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS),
+ float4(offset[0].xz, offset[1].yw));
+}
+
+/**
+ * Neighborhood Blending Vertex Shader
+ */
+void SMAANeighborhoodBlendingVS(float2 texcoord,
+ out float4 offset) {
+ offset = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), texcoord.xyxy);
+}
+#endif // SMAA_INCLUDE_VS
+
+#if SMAA_INCLUDE_PS
+//-----------------------------------------------------------------------------
+// Edge Detection Pixel Shaders (First Pass)
+
+/**
+ * Luma Edge Detection
+ *
+ * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
+ * thus 'colorTex' should be a non-sRGB texture.
+ */
+float2 SMAALumaEdgeDetectionPS(float2 texcoord,
+ float4 offset[3],
+ SMAATexture2D(colorTex)
+ #if SMAA_PREDICATION
+ , SMAATexture2D(predicationTex)
+ #endif
+ ) {
+ // Calculate the threshold:
+ #if SMAA_PREDICATION
+ float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, SMAATexturePass2D(predicationTex));
+ #else
+ float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD);
+ #endif
+
+ // Calculate lumas:
+ float3 weights = float3(0.2126, 0.7152, 0.0722);
+ float L = dot(SMAASamplePoint(colorTex, texcoord).rgb, weights);
+
+ float Lleft = dot(SMAASamplePoint(colorTex, offset[0].xy).rgb, weights);
+ float Ltop = dot(SMAASamplePoint(colorTex, offset[0].zw).rgb, weights);
+
+ // We do the usual threshold:
+ float4 delta;
+ delta.xy = abs(L - float2(Lleft, Ltop));
+ float2 edges = step(threshold, delta.xy);
+
+ // Then discard if there is no edge:
+ if (dot(edges, float2(1.0, 1.0)) == 0.0)
+ return float2(-2.0, -2.0);
+
+ // Calculate right and bottom deltas:
+ float Lright = dot(SMAASamplePoint(colorTex, offset[1].xy).rgb, weights);
+ float Lbottom = dot(SMAASamplePoint(colorTex, offset[1].zw).rgb, weights);
+ delta.zw = abs(L - float2(Lright, Lbottom));
+
+ // Calculate the maximum delta in the direct neighborhood:
+ float2 maxDelta = max(delta.xy, delta.zw);
+
+ // Calculate left-left and top-top deltas:
+ float Lleftleft = dot(SMAASamplePoint(colorTex, offset[2].xy).rgb, weights);
+ float Ltoptop = dot(SMAASamplePoint(colorTex, offset[2].zw).rgb, weights);
+ delta.zw = abs(float2(Lleft, Ltop) - float2(Lleftleft, Ltoptop));
+
+ // Calculate the final maximum delta:
+ maxDelta = max(maxDelta.xy, delta.zw);
+ float finalDelta = max(maxDelta.x, maxDelta.y);
+
+ // Local contrast adaptation:
+ edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
+
+ return edges;
+}
+
+/**
+ * Color Edge Detection
+ *
+ * IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and
+ * thus 'colorTex' should be a non-sRGB texture.
+ */
+float2 SMAAColorEdgeDetectionPS(float2 texcoord,
+ float4 offset[3],
+ SMAATexture2D(colorTex)
+ #if SMAA_PREDICATION
+ , SMAATexture2D(predicationTex)
+ #endif
+ ) {
+ // Calculate the threshold:
+ #if SMAA_PREDICATION
+ float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, predicationTex);
+ #else
+ float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD);
+ #endif
+
+ // Calculate color deltas:
+ float4 delta;
+ float3 C = SMAASamplePoint(colorTex, texcoord).rgb;
+
+ float3 Cleft = SMAASamplePoint(colorTex, offset[0].xy).rgb;
+ float3 t = abs(C - Cleft);
+ delta.x = max(max(t.r, t.g), t.b);
+
+ float3 Ctop = SMAASamplePoint(colorTex, offset[0].zw).rgb;
+ t = abs(C - Ctop);
+ delta.y = max(max(t.r, t.g), t.b);
+
+ // We do the usual threshold:
+ float2 edges = step(threshold, delta.xy);
+
+ // Then discard if there is no edge:
+ if (dot(edges, float2(1.0, 1.0)) == 0.0)
+ return float2(-2.0, -2.0);
+
+ // Calculate right and bottom deltas:
+ float3 Cright = SMAASamplePoint(colorTex, offset[1].xy).rgb;
+ t = abs(C - Cright);
+ delta.z = max(max(t.r, t.g), t.b);
+
+ float3 Cbottom = SMAASamplePoint(colorTex, offset[1].zw).rgb;
+ t = abs(C - Cbottom);
+ delta.w = max(max(t.r, t.g), t.b);
+
+ // Calculate the maximum delta in the direct neighborhood:
+ float2 maxDelta = max(delta.xy, delta.zw);
+
+ // Calculate left-left and top-top deltas:
+ float3 Cleftleft = SMAASamplePoint(colorTex, offset[2].xy).rgb;
+ t = abs(C - Cleftleft);
+ delta.z = max(max(t.r, t.g), t.b);
+
+ float3 Ctoptop = SMAASamplePoint(colorTex, offset[2].zw).rgb;
+ t = abs(C - Ctoptop);
+ delta.w = max(max(t.r, t.g), t.b);
+
+ // Calculate the final maximum delta:
+ maxDelta = max(maxDelta.xy, delta.zw);
+ float finalDelta = max(maxDelta.x, maxDelta.y);
+
+ // Local contrast adaptation:
+ edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
+
+ return edges;
+}
+
+/**
+ * Depth Edge Detection
+ */
+float2 SMAADepthEdgeDetectionPS(float2 texcoord,
+ float4 offset[3],
+ SMAATexture2D(depthTex)) {
+ float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(depthTex));
+ float2 delta = abs(neighbours.xx - float2(neighbours.y, neighbours.z));
+ float2 edges = step(SMAA_DEPTH_THRESHOLD, delta);
+
+ if (dot(edges, float2(1.0, 1.0)) == 0.0)
+ return float2(-2.0, -2.0);
+
+ return edges;
+}
+
+//-----------------------------------------------------------------------------
+// Diagonal Search Functions
+
+#if !defined(SMAA_DISABLE_DIAG_DETECTION)
+
+/**
+ * Allows to decode two binary values from a bilinear-filtered access.
+ */
+float2 SMAADecodeDiagBilinearAccess(float2 e) {
+ // Bilinear access for fetching 'e' have a 0.25 offset, and we are
+ // interested in the R and G edges:
+ //
+ // +---G---+-------+
+ // | x o R x |
+ // +-------+-------+
+ //
+ // Then, if one of these edge is enabled:
+ // Red: (0.75 * X + 0.25 * 1) => 0.25 or 1.0
+ // Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0
+ //
+ // This function will unpack the values (mad + mul + round):
+ // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1
+ e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75);
+ return round(e);
+}
+
+float4 SMAADecodeDiagBilinearAccess(float4 e) {
+ e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75);
+ return round(e);
+}
+
+/**
+ * These functions allows to perform diagonal pattern searches.
+ */
+float2 SMAASearchDiag1(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) {
+ float4 coord = float4(texcoord, -1.0, 1.0);
+ float3 t = float3(SMAA_RT_METRICS.xy, 1.0);
+ while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
+ coord.w > 0.9) {
+ coord.xyz = mad(t, float3(dir, 1.0), coord.xyz);
+ e = SMAASampleLevelZero(edgesTex, coord.xy).rg;
+ coord.w = dot(e, float2(0.5, 0.5));
+ }
+ return coord.zw;
+}
+
+float2 SMAASearchDiag2(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) {
+ float4 coord = float4(texcoord, -1.0, 1.0);
+ coord.x += 0.25 * SMAA_RT_METRICS.x; // See @SearchDiag2Optimization
+ float3 t = float3(SMAA_RT_METRICS.xy, 1.0);
+ while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
+ coord.w > 0.9) {
+ coord.xyz = mad(t, float3(dir, 1.0), coord.xyz);
+
+ // @SearchDiag2Optimization
+ // Fetch both edges at once using bilinear filtering:
+ e = SMAASampleLevelZero(edgesTex, coord.xy).rg;
+ e = SMAADecodeDiagBilinearAccess(e);
+
+ // Non-optimized version:
+ // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g;
+ // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r;
+
+ coord.w = dot(e, float2(0.5, 0.5));
+ }
+ return coord.zw;
+}
+
+/**
+ * Similar to SMAAArea, this calculates the area corresponding to a certain
+ * diagonal distance and crossing edges 'e'.
+ */
+float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset) {
+ float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist);
+
+ // We do a scale and bias for mapping to texel space:
+ texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
+
+ // Diagonal areas are on the second half of the texture:
+ texcoord.x += 0.5;
+
+ // Move to proper place, according to the subpixel offset:
+ texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
+
+ // Do it!
+ return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord));
+}
+
+/**
+ * This searches for diagonal patterns and returns the corresponding weights.
+ */
+float2 SMAACalculateDiagWeights(SMAATexture2D(edgesTex), SMAATexture2D(areaTex), float2 texcoord, float2 e, float4 subsampleIndices) {
+ float2 weights = float2(0.0, 0.0);
+
+ // Search for the line ends:
+ float4 d;
+ float2 end;
+ if (e.r > 0.0) {
+ d.xz = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, 1.0), end);
+ d.x += float(end.y > 0.9);
+ } else
+ d.xz = float2(0.0, 0.0);
+ d.yw = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, -1.0), end);
+
+ SMAA_BRANCH
+ if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
+ // Fetch the crossing edges:
+ float4 coords = mad(float4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
+ float4 c;
+ c.xy = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).rg;
+ c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).rg;
+ c.yxwz = SMAADecodeDiagBilinearAccess(c.xyzw);
+
+ // Non-optimized version:
+ // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
+ // float4 c;
+ // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g;
+ // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r;
+ // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g;
+ // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r;
+
+ // Merge crossing edges at each side into a single value:
+ float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw);
+
+ // Remove the crossing edge if we didn't found the end of the line:
+ SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0));
+
+ // Fetch the areas for this line:
+ weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.z);
+ }
+
+ // Search for the line ends:
+ d.xz = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, -1.0), end);
+ if (SMAASampleLevelZeroOffset(edgesTex, texcoord, int2(1, 0)).r > 0.0) {
+ d.yw = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, 1.0), end);
+ d.y += float(end.y > 0.9);
+ } else
+ d.yw = float2(0.0, 0.0);
+
+ SMAA_BRANCH
+ if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3
+ // Fetch the crossing edges:
+ float4 coords = mad(float4(-d.x, -d.x, d.y, d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy);
+ float4 c;
+ c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g;
+ c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, -1)).r;
+ c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).gr;
+ float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw);
+
+ // Remove the crossing edge if we didn't found the end of the line:
+ SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0));
+
+ // Fetch the areas for this line:
+ weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.w).gr;
+ }
+
+ return weights;
+}
+#endif
+
+//-----------------------------------------------------------------------------
+// Horizontal/Vertical Search Functions
+
+/**
+ * This allows to determine how much length should we add in the last step
+ * of the searches. It takes the bilinearly interpolated edge (see
+ * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
+ * crossing edges are active.
+ */
+float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset) {
+ // The texture is flipped vertically, with left and right cases taking half
+ // of the space horizontally:
+ float2 scale = SMAA_SEARCHTEX_SIZE * float2(0.5, -1.0);
+ float2 bias = SMAA_SEARCHTEX_SIZE * float2(offset, 1.0);
+
+ // Scale and bias to access texel centers:
+ scale += float2(-1.0, 1.0);
+ bias += float2( 0.5, -0.5);
+
+ // Convert from pixel coordinates to texcoords:
+ // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped)
+ scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
+ bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
+
+ // Lookup the search texture:
+ return SMAA_SEARCHTEX_SELECT(SMAASampleLevelZero(searchTex, mad(scale, e, bias)));
+}
+
+/**
+ * Horizontal/vertical search functions for the 2nd pass.
+ */
+float SMAASearchXLeft(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
+ /**
+ * @PSEUDO_GATHER4
+ * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
+ * sample between edge, thus fetching four edges in a row.
+ * Sampling with different offsets in each direction allows to disambiguate
+ * which edges are active from the four fetched ones.
+ */
+ float2 e = float2(0.0, 1.0);
+ while (texcoord.x > end &&
+ e.g > 0.8281 && // Is there some edge not activated?
+ e.r == 0.0) { // Or is there a crossing edge that breaks the line?
+ e = SMAASampleLevelZero(edgesTex, texcoord).rg;
+ texcoord = mad(-float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
+ }
+
+ float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0), 3.25);
+ return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
+
+ // Non-optimized version:
+ // We correct the previous (-0.25, -0.125) offset we applied:
+ // texcoord.x += 0.25 * SMAA_RT_METRICS.x;
+
+ // The searches are bias by 1, so adjust the coords accordingly:
+ // texcoord.x += SMAA_RT_METRICS.x;
+
+ // Disambiguate the length added by the last step:
+ // texcoord.x += 2.0 * SMAA_RT_METRICS.x; // Undo last step
+ // texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) * SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0);
+ // return mad(SMAA_RT_METRICS.x, offset, texcoord.x);
+}
+
+float SMAASearchXRight(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
+ float2 e = float2(0.0, 1.0);
+ while (texcoord.x < end &&
+ e.g > 0.8281 && // Is there some edge not activated?
+ e.r == 0.0) { // Or is there a crossing edge that breaks the line?
+ e = SMAASampleLevelZero(edgesTex, texcoord).rg;
+ texcoord = mad(float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord);
+ }
+ float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.5), 3.25);
+ return mad(-SMAA_RT_METRICS.x, offset, texcoord.x);
+}
+
+float SMAASearchYUp(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
+ float2 e = float2(1.0, 0.0);
+ while (texcoord.y > end &&
+ e.r > 0.8281 && // Is there some edge not activated?
+ e.g == 0.0) { // Or is there a crossing edge that breaks the line?
+ e = SMAASampleLevelZero(edgesTex, texcoord).rg;
+ texcoord = mad(-float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
+ }
+ float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.0), 3.25);
+ return mad(SMAA_RT_METRICS.y, offset, texcoord.y);
+}
+
+float SMAASearchYDown(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) {
+ float2 e = float2(1.0, 0.0);
+ while (texcoord.y < end &&
+ e.r > 0.8281 && // Is there some edge not activated?
+ e.g == 0.0) { // Or is there a crossing edge that breaks the line?
+ e = SMAASampleLevelZero(edgesTex, texcoord).rg;
+ texcoord = mad(float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord);
+ }
+ float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.5), 3.25);
+ return mad(-SMAA_RT_METRICS.y, offset, texcoord.y);
+}
+
+/**
+ * Ok, we have the distance and both crossing edges. So, what are the areas
+ * at each side of current edge?
+ */
+float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset) {
+ // Rounding prevents precision errors of bilinear filtering:
+ float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * float2(e1, e2)), dist);
+
+ // We do a scale and bias for mapping to texel space:
+ texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
+
+ // Move to proper place, according to the subpixel offset:
+ texcoord.y = mad(SMAA_AREATEX_SUBTEX_SIZE, offset, texcoord.y);
+
+ // Do it!
+ return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord));
+}
+
+//-----------------------------------------------------------------------------
+// Corner Detection Functions
+
+void SMAADetectHorizontalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) {
+ #if !defined(SMAA_DISABLE_CORNER_DETECTION)
+ float2 leftRight = step(d.xy, d.yx);
+ float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
+
+ rounding /= leftRight.x + leftRight.y; // Reduce blending for pixels in the center of a line.
+
+ float2 factor = float2(1.0, 1.0);
+ factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, 1)).r;
+ factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, 1)).r;
+ factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, -2)).r;
+ factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, -2)).r;
+
+ weights *= saturate(factor);
+ #endif
+}
+
+void SMAADetectVerticalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) {
+ #if !defined(SMAA_DISABLE_CORNER_DETECTION)
+ float2 leftRight = step(d.xy, d.yx);
+ float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
+
+ rounding /= leftRight.x + leftRight.y;
+
+ float2 factor = float2(1.0, 1.0);
+ factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2( 1, 0)).g;
+ factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2( 1, 1)).g;
+ factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(-2, 0)).g;
+ factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(-2, 1)).g;
+
+ weights *= saturate(factor);
+ #endif
+}
+
+//-----------------------------------------------------------------------------
+// Blending Weight Calculation Pixel Shader (Second Pass)
+
+float4 SMAABlendingWeightCalculationPS(float2 texcoord,
+ float2 pixcoord,
+ float4 offset[3],
+ SMAATexture2D(edgesTex),
+ SMAATexture2D(areaTex),
+ SMAATexture2D(searchTex),
+ float4 subsampleIndices) { // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES.
+ float4 weights = float4(0.0, 0.0, 0.0, 0.0);
+
+ float2 e = SMAASample(edgesTex, texcoord).rg;
+
+ SMAA_BRANCH
+ if (e.g > 0.0) { // Edge at north
+ #if !defined(SMAA_DISABLE_DIAG_DETECTION)
+ // Diagonals have both north and west edges, so searching for them in
+ // one of the boundaries is enough.
+ weights.rg = SMAACalculateDiagWeights(SMAATexturePass2D(edgesTex), SMAATexturePass2D(areaTex), texcoord, e, subsampleIndices);
+
+ // We give priority to diagonals, so if we find a diagonal we skip
+ // horizontal/vertical processing.
+ SMAA_BRANCH
+ if (weights.r == -weights.g) { // weights.r + weights.g == 0.0
+ #endif
+
+ float2 d;
+
+ // Find the distance to the left:
+ float3 coords;
+ coords.x = SMAASearchXLeft(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].xy, offset[2].x);
+ coords.y = offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET)
+ d.x = coords.x;
+
+ // Now fetch the left crossing edges, two at a time using bilinear
+ // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
+ // discern what value each edge has:
+ float e1 = SMAASampleLevelZero(edgesTex, coords.xy).r;
+
+ // Find the distance to the right:
+ coords.z = SMAASearchXRight(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].zw, offset[2].y);
+ d.y = coords.z;
+
+ // We want the distances to be in pixel units (doing this here allow to
+ // better interleave arithmetic and memory accesses):
+ d = abs(round(mad(SMAA_RT_METRICS.zz, d, -pixcoord.xx)));
+
+ // SMAAArea below needs a sqrt, as the areas texture is compressed
+ // quadratically:
+ float2 sqrt_d = sqrt(d);
+
+ // Fetch the right crossing edges:
+ float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, int2(1, 0)).r;
+
+ // Ok, we know how this pattern looks like, now it is time for getting
+ // the actual area:
+ weights.rg = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.y);
+
+ // Fix corners:
+ coords.y = texcoord.y;
+ SMAADetectHorizontalCornerPattern(SMAATexturePass2D(edgesTex), weights.rg, coords.xyzy, d);
+
+ #if !defined(SMAA_DISABLE_DIAG_DETECTION)
+ } else
+ e.r = 0.0; // Skip vertical processing.
+ #endif
+ }
+
+ SMAA_BRANCH
+ if (e.r > 0.0) { // Edge at west
+ float2 d;
+
+ // Find the distance to the top:
+ float3 coords;
+ coords.y = SMAASearchYUp(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].xy, offset[2].z);
+ coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x;
+ d.x = coords.y;
+
+ // Fetch the top crossing edges:
+ float e1 = SMAASampleLevelZero(edgesTex, coords.xy).g;
+
+ // Find the distance to the bottom:
+ coords.z = SMAASearchYDown(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].zw, offset[2].w);
+ d.y = coords.z;
+
+ // We want the distances to be in pixel units:
+ d = abs(round(mad(SMAA_RT_METRICS.ww, d, -pixcoord.yy)));
+
+ // SMAAArea below needs a sqrt, as the areas texture is compressed
+ // quadratically:
+ float2 sqrt_d = sqrt(d);
+
+ // Fetch the bottom crossing edges:
+ float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.xz, int2(0, 1)).g;
+
+ // Get the area for this direction:
+ weights.ba = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.x);
+
+ // Fix corners:
+ coords.x = texcoord.x;
+ SMAADetectVerticalCornerPattern(SMAATexturePass2D(edgesTex), weights.ba, coords.xyxz, d);
+ }
+
+ return weights;
+}
+
+//-----------------------------------------------------------------------------
+// Neighborhood Blending Pixel Shader (Third Pass)
+
+float4 SMAANeighborhoodBlendingPS(float2 texcoord,
+ float4 offset,
+ SMAATexture2D(colorTex),
+ SMAATexture2D(blendTex)
+ #if SMAA_REPROJECTION
+ , SMAATexture2D(velocityTex)
+ #endif
+ ) {
+ // Fetch the blending weights for current pixel:
+ float4 a;
+ a.x = SMAASample(blendTex, offset.xy).a; // Right
+ a.y = SMAASample(blendTex, offset.zw).g; // Top
+ a.wz = SMAASample(blendTex, texcoord).xz; // Bottom / Left
+
+ // Is there any blending weight with a value greater than 0.0?
+ SMAA_BRANCH
+ if (dot(a, float4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
+ float4 color = SMAASampleLevelZero(colorTex, texcoord);
+
+ #if SMAA_REPROJECTION
+ float2 velocity = SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, texcoord));
+
+ // Pack velocity into the alpha channel:
+ color.a = sqrt(5.0 * length(velocity));
+ #endif
+
+ return color;
+ } else {
+ bool h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical)
+
+ // Calculate the blending offsets:
+ float4 blendingOffset = float4(0.0, a.y, 0.0, a.w);
+ float2 blendingWeight = a.yw;
+ SMAAMovc(bool4(h, h, h, h), blendingOffset, float4(a.x, 0.0, a.z, 0.0));
+ SMAAMovc(bool2(h, h), blendingWeight, a.xz);
+ blendingWeight /= dot(blendingWeight, float2(1.0, 1.0));
+
+ // Calculate the texture coordinates:
+ float4 blendingCoord = mad(blendingOffset, float4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy), texcoord.xyxy);
+
+ // We exploit bilinear filtering to mix current pixel with the chosen
+ // neighbor:
+ float4 color = blendingWeight.x * SMAASampleLevelZero(colorTex, blendingCoord.xy);
+ color += blendingWeight.y * SMAASampleLevelZero(colorTex, blendingCoord.zw);
+
+ #if SMAA_REPROJECTION
+ // Antialias velocity for proper reprojection in a later stage:
+ float2 velocity = blendingWeight.x * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.xy));
+ velocity += blendingWeight.y * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.zw));
+
+ // Pack velocity into the alpha channel:
+ color.a = sqrt(5.0 * length(velocity));
+ #endif
+
+ return color;
+ }
+}
+
+//-----------------------------------------------------------------------------
+// Temporal Resolve Pixel Shader (Optional Pass)
+
+float4 SMAAResolvePS(float2 texcoord,
+ SMAATexture2D(currentColorTex),
+ SMAATexture2D(previousColorTex)
+ #if SMAA_REPROJECTION
+ , SMAATexture2D(velocityTex)
+ #endif
+ ) {
+ #if SMAA_REPROJECTION
+ // Velocity is assumed to be calculated for motion blur, so we need to
+ // inverse it for reprojection:
+ float2 velocity = -SMAA_DECODE_VELOCITY(SMAASamplePoint(velocityTex, texcoord).rg);
+
+ // Fetch current pixel:
+ float4 current = SMAASamplePoint(currentColorTex, texcoord);
+
+ // Reproject current coordinates and fetch previous pixel:
+ float4 previous = SMAASamplePoint(previousColorTex, texcoord + velocity);
+
+ // Attenuate the previous pixel if the velocity is different:
+ float delta = abs(current.a * current.a - previous.a * previous.a) / 5.0;
+ float weight = 0.5 * saturate(1.0 - sqrt(delta) * SMAA_REPROJECTION_WEIGHT_SCALE);
+
+ // Blend the pixels according to the calculated weight:
+ return lerp(current, previous, weight);
+ #else
+ // Just blend the pixels:
+ float4 current = SMAASamplePoint(currentColorTex, texcoord);
+ float4 previous = SMAASamplePoint(previousColorTex, texcoord);
+ return lerp(current, previous, 0.5);
+ #endif
+}
+
+//-----------------------------------------------------------------------------
+// Separate Multisamples Pixel Shader (Optional Pass)
+
+#ifdef SMAALoad
+void SMAASeparatePS(float4 position,
+ float2 texcoord,
+ out float4 target0,
+ out float4 target1,
+ SMAATexture2DMS2(colorTexMS)) {
+ int2 pos = int2(position.xy);
+ target0 = SMAALoad(colorTexMS, pos, 0);
+ target1 = SMAALoad(colorTexMS, pos, 1);
+}
+#endif
+
+//-----------------------------------------------------------------------------
+#endif // SMAA_INCLUDE_PS
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_blend.glsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_blend.glsl
new file mode 100644
index 00000000..c875ce12
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_blend.glsl
@@ -0,0 +1,26 @@
+layout(rgba8, binding = 0) uniform image2D imgOutput;
+
+uniform sampler2D inputTexture;
+layout( location=0 ) uniform vec2 invResolution;
+uniform sampler2D samplerArea;
+uniform sampler2D samplerSearch;
+
+void main() {
+ ivec2 loc = ivec2(gl_GlobalInvocationID.x * 4, gl_GlobalInvocationID.y * 4);
+ for(int i = 0; i < 4; i++)
+ {
+ for(int j = 0; j < 4; j++)
+ {
+ ivec2 texelCoord = ivec2(loc.x + i, loc.y + j);
+ vec2 coord = (texelCoord + vec2(0.5)) / invResolution;
+ vec2 pixCoord;
+ vec4 offset[3];
+
+ SMAABlendingWeightCalculationVS(coord, pixCoord, offset);
+
+ vec4 oColor = SMAABlendingWeightCalculationPS(coord, pixCoord, offset, inputTexture, samplerArea, samplerSearch, ivec4(0));
+
+ imageStore(imgOutput, texelCoord, oColor);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_edge.glsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_edge.glsl
new file mode 100644
index 00000000..fd5d9715
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_edge.glsl
@@ -0,0 +1,24 @@
+layout(rgba8, binding = 0) uniform image2D imgOutput;
+
+uniform sampler2D inputTexture;
+layout( location=0 ) uniform vec2 invResolution;
+
+void main()
+{
+ vec2 loc = ivec2(gl_GlobalInvocationID.x * 4, gl_GlobalInvocationID.y * 4);
+ for(int i = 0; i < 4; i++)
+ {
+ for(int j = 0; j < 4; j++)
+ {
+ ivec2 texelCoord = ivec2(loc.x + i, loc.y + j);
+ vec2 coord = (texelCoord + vec2(0.5)) / invResolution;
+ vec4 offset[3];
+ SMAAEdgeDetectionVS(coord, offset);
+ vec2 oColor = SMAAColorEdgeDetectionPS(coord, offset, inputTexture);
+ if (oColor != float2(-2.0, -2.0))
+ {
+ imageStore(imgOutput, texelCoord, vec4(oColor, 0.0, 1.0));
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_neighbour.glsl b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_neighbour.glsl
new file mode 100644
index 00000000..2e9432ae
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_neighbour.glsl
@@ -0,0 +1,26 @@
+layout(rgba8, binding = 0) uniform image2D imgOutput;
+
+uniform sampler2D inputTexture;
+layout( location=0 ) uniform vec2 invResolution;
+uniform sampler2D samplerBlend;
+
+void main() {
+ vec2 loc = ivec2(gl_GlobalInvocationID.x * 4, gl_GlobalInvocationID.y * 4);
+ for(int i = 0; i < 4; i++)
+ {
+ for(int j = 0; j < 4; j++)
+ {
+ ivec2 texelCoord = ivec2(loc.x + i, loc.y + j);
+ vec2 coord = (texelCoord + vec2(0.5)) / invResolution;
+ vec2 pixCoord;
+ vec4 offset;
+
+ SMAANeighborhoodBlendingVS(coord, offset);
+
+ vec4 oColor = SMAANeighborhoodBlendingPS(coord, offset, inputTexture, samplerBlend);
+
+ imageStore(imgOutput, texelCoord, oColor);
+ }
+ }
+
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/SmaaPostProcessingEffect.cs b/src/Ryujinx.Graphics.OpenGL/Effects/SmaaPostProcessingEffect.cs
new file mode 100644
index 00000000..1ad300c8
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/SmaaPostProcessingEffect.cs
@@ -0,0 +1,261 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Effects.Smaa
+{
+ internal partial class SmaaPostProcessingEffect : IPostProcessingEffect
+ {
+ public const int AreaWidth = 160;
+ public const int AreaHeight = 560;
+ public const int SearchWidth = 64;
+ public const int SearchHeight = 16;
+
+ private readonly OpenGLRenderer _renderer;
+ private TextureStorage _outputTexture;
+ private TextureStorage _searchTexture;
+ private TextureStorage _areaTexture;
+ private int[] _edgeShaderPrograms;
+ private int[] _blendShaderPrograms;
+ private int[] _neighbourShaderPrograms;
+ private TextureStorage _edgeOutputTexture;
+ private TextureStorage _blendOutputTexture;
+ private string[] _qualities;
+ private int _inputUniform;
+ private int _outputUniform;
+ private int _samplerAreaUniform;
+ private int _samplerSearchUniform;
+ private int _samplerBlendUniform;
+ private int _resolutionUniform;
+ private int _quality = 1;
+
+ public int Quality
+ {
+ get => _quality; set
+ {
+ _quality = Math.Clamp(value, 0, _qualities.Length - 1);
+ }
+ }
+ public SmaaPostProcessingEffect(OpenGLRenderer renderer, int quality)
+ {
+ _renderer = renderer;
+
+ _edgeShaderPrograms = Array.Empty<int>();
+ _blendShaderPrograms = Array.Empty<int>();
+ _neighbourShaderPrograms = Array.Empty<int>();
+
+ _qualities = new string[] { "SMAA_PRESET_LOW", "SMAA_PRESET_MEDIUM", "SMAA_PRESET_HIGH", "SMAA_PRESET_ULTRA" };
+
+ Quality = quality;
+
+ Initialize();
+ }
+
+ public void Dispose()
+ {
+ _searchTexture?.Dispose();
+ _areaTexture?.Dispose();
+ _outputTexture?.Dispose();
+ _edgeOutputTexture?.Dispose();
+ _blendOutputTexture?.Dispose();
+
+ DeleteShaders();
+ }
+
+ private void DeleteShaders()
+ {
+ for (int i = 0; i < _edgeShaderPrograms.Length; i++)
+ {
+ GL.DeleteProgram(_edgeShaderPrograms[i]);
+ GL.DeleteProgram(_blendShaderPrograms[i]);
+ GL.DeleteProgram(_neighbourShaderPrograms[i]);
+ }
+ }
+
+ private unsafe void RecreateShaders(int width, int height)
+ {
+ string baseShader = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa.hlsl");
+ var pixelSizeDefine = $"#define SMAA_RT_METRICS float4(1.0 / {width}.0, 1.0 / {height}.0, {width}, {height}) \n";
+
+ _edgeShaderPrograms = new int[_qualities.Length];
+ _blendShaderPrograms = new int[_qualities.Length];
+ _neighbourShaderPrograms = new int[_qualities.Length];
+
+ for (int i = 0; i < +_edgeShaderPrograms.Length; i++)
+ {
+ var presets = $"#version 430 core \n#define {_qualities[i]} 1 \n{pixelSizeDefine}#define SMAA_GLSL_4 1 \nlayout (local_size_x = 16, local_size_y = 16) in;\n{baseShader}";
+
+ var edgeShaderData = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_edge.glsl");
+ var blendShaderData = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_blend.glsl");
+ var neighbourShaderData = EmbeddedResources.ReadAllText("Ryujinx.Graphics.OpenGL/Effects/Shaders/smaa_neighbour.glsl");
+
+ var shaders = new string[] { presets, edgeShaderData };
+ var edgeProgram = ShaderHelper.CompileProgram(shaders, ShaderType.ComputeShader);
+
+ shaders[1] = blendShaderData;
+ var blendProgram = ShaderHelper.CompileProgram(shaders, ShaderType.ComputeShader);
+
+ shaders[1] = neighbourShaderData;
+ var neighbourProgram = ShaderHelper.CompileProgram(shaders, ShaderType.ComputeShader);
+
+ _edgeShaderPrograms[i] = edgeProgram;
+ _blendShaderPrograms[i] = blendProgram;
+ _neighbourShaderPrograms[i] = neighbourProgram;
+ }
+
+ _inputUniform = GL.GetUniformLocation(_edgeShaderPrograms[0], "inputTexture");
+ _outputUniform = GL.GetUniformLocation(_edgeShaderPrograms[0], "imgOutput");
+ _samplerAreaUniform = GL.GetUniformLocation(_blendShaderPrograms[0], "samplerArea");
+ _samplerSearchUniform = GL.GetUniformLocation(_blendShaderPrograms[0], "samplerSearch");
+ _samplerBlendUniform = GL.GetUniformLocation(_neighbourShaderPrograms[0], "samplerBlend");
+ _resolutionUniform = GL.GetUniformLocation(_edgeShaderPrograms[0], "invResolution");
+ }
+
+ private void Initialize()
+ {
+ var areaInfo = new TextureCreateInfo(AreaWidth,
+ AreaHeight,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ Format.R8G8Unorm,
+ DepthStencilMode.Depth,
+ Target.Texture2D,
+ SwizzleComponent.Red,
+ SwizzleComponent.Green,
+ SwizzleComponent.Blue,
+ SwizzleComponent.Alpha);
+
+ var searchInfo = new TextureCreateInfo(SearchWidth,
+ SearchHeight,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ Format.R8Unorm,
+ DepthStencilMode.Depth,
+ Target.Texture2D,
+ SwizzleComponent.Red,
+ SwizzleComponent.Green,
+ SwizzleComponent.Blue,
+ SwizzleComponent.Alpha);
+
+ _areaTexture = new TextureStorage(_renderer, areaInfo, 1);
+ _searchTexture = new TextureStorage(_renderer, searchInfo, 1);
+
+ var areaTexture = EmbeddedResources.Read("Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaAreaTexture.bin");
+ var searchTexture = EmbeddedResources.Read("Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaSearchTexture.bin");
+
+ var areaView = _areaTexture.CreateDefaultView();
+ var searchView = _searchTexture.CreateDefaultView();
+
+ areaView.SetData(areaTexture);
+ searchView.SetData(searchTexture);
+ }
+
+ public TextureView Run(TextureView view, int width, int height)
+ {
+ if (_outputTexture == null || _outputTexture.Info.Width != view.Width || _outputTexture.Info.Height != view.Height)
+ {
+ _outputTexture?.Dispose();
+ _outputTexture = new TextureStorage(_renderer, view.Info, view.ScaleFactor);
+ _outputTexture.CreateDefaultView();
+ _edgeOutputTexture = new TextureStorage(_renderer, view.Info, view.ScaleFactor);
+ _edgeOutputTexture.CreateDefaultView();
+ _blendOutputTexture = new TextureStorage(_renderer, view.Info, view.ScaleFactor);
+ _blendOutputTexture.CreateDefaultView();
+
+ DeleteShaders();
+
+ RecreateShaders(view.Width, view.Height);
+ }
+
+ var textureView = _outputTexture.CreateView(view.Info, 0, 0) as TextureView;
+ var edgeOutput = _edgeOutputTexture.DefaultView as TextureView;
+ var blendOutput = _blendOutputTexture.DefaultView as TextureView;
+ var areaTexture = _areaTexture.DefaultView as TextureView;
+ var searchTexture = _searchTexture.DefaultView as TextureView;
+
+ var previousFramebuffer = GL.GetInteger(GetPName.FramebufferBinding);
+ int previousUnit = GL.GetInteger(GetPName.ActiveTexture);
+ GL.ActiveTexture(TextureUnit.Texture0);
+ int previousTextureBinding0 = GL.GetInteger(GetPName.TextureBinding2D);
+ GL.ActiveTexture(TextureUnit.Texture1);
+ int previousTextureBinding1 = GL.GetInteger(GetPName.TextureBinding2D);
+ GL.ActiveTexture(TextureUnit.Texture2);
+ int previousTextureBinding2 = GL.GetInteger(GetPName.TextureBinding2D);
+
+ var framebuffer = new Framebuffer();
+ framebuffer.Bind();
+ framebuffer.AttachColor(0, edgeOutput);
+ GL.Clear(ClearBufferMask.ColorBufferBit);
+ GL.ClearColor(0, 0, 0, 0);
+ framebuffer.AttachColor(0, blendOutput);
+ GL.Clear(ClearBufferMask.ColorBufferBit);
+ GL.ClearColor(0, 0, 0, 0);
+
+ GL.BindFramebuffer(FramebufferTarget.Framebuffer, previousFramebuffer);
+
+ framebuffer.Dispose();
+
+ var dispatchX = BitUtils.DivRoundUp(view.Width, IPostProcessingEffect.LocalGroupSize);
+ var dispatchY = BitUtils.DivRoundUp(view.Height, IPostProcessingEffect.LocalGroupSize);
+
+ int previousProgram = GL.GetInteger(GetPName.CurrentProgram);
+ GL.BindImageTexture(0, edgeOutput.Handle, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ GL.UseProgram(_edgeShaderPrograms[Quality]);
+ view.Bind(0);
+ GL.Uniform1(_inputUniform, 0);
+ GL.Uniform1(_outputUniform, 0);
+ GL.Uniform2(_resolutionUniform, (float)view.Width, (float)view.Height);
+ GL.DispatchCompute(dispatchX, dispatchY, 1);
+ GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrierBit);
+
+ GL.BindImageTexture(0, blendOutput.Handle, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ GL.UseProgram(_blendShaderPrograms[Quality]);
+ edgeOutput.Bind(0);
+ areaTexture.Bind(1);
+ searchTexture.Bind(2);
+ GL.Uniform1(_inputUniform, 0);
+ GL.Uniform1(_outputUniform, 0);
+ GL.Uniform1(_samplerAreaUniform, 1);
+ GL.Uniform1(_samplerSearchUniform, 2);
+ GL.Uniform2(_resolutionUniform, (float)view.Width, (float)view.Height);
+ GL.DispatchCompute(dispatchX, dispatchY, 1);
+ GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrierBit);
+
+ GL.BindImageTexture(0, textureView.Handle, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ GL.UseProgram(_neighbourShaderPrograms[Quality]);
+ view.Bind(0);
+ blendOutput.Bind(1);
+ GL.Uniform1(_inputUniform, 0);
+ GL.Uniform1(_outputUniform, 0);
+ GL.Uniform1(_samplerBlendUniform, 1);
+ GL.Uniform2(_resolutionUniform, (float)view.Width, (float)view.Height);
+ GL.DispatchCompute(dispatchX, dispatchY, 1);
+ GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrierBit);
+
+ (_renderer.Pipeline as Pipeline).RestoreImages1And2();
+
+ GL.UseProgram(previousProgram);
+
+ GL.ActiveTexture(TextureUnit.Texture0);
+ GL.BindTexture(TextureTarget.Texture2D, previousTextureBinding0);
+ GL.ActiveTexture(TextureUnit.Texture1);
+ GL.BindTexture(TextureTarget.Texture2D, previousTextureBinding1);
+ GL.ActiveTexture(TextureUnit.Texture2);
+ GL.BindTexture(TextureTarget.Texture2D, previousTextureBinding2);
+
+ GL.ActiveTexture((TextureUnit)previousUnit);
+
+ return textureView;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaAreaTexture.bin b/src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaAreaTexture.bin
new file mode 100644
index 00000000..f4a7a1b4
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaAreaTexture.bin
Binary files differ
diff --git a/src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaSearchTexture.bin b/src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaSearchTexture.bin
new file mode 100644
index 00000000..db5bf73f
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Effects/Textures/SmaaSearchTexture.bin
Binary files differ
diff --git a/src/Ryujinx.Graphics.OpenGL/EnumConversion.cs b/src/Ryujinx.Graphics.OpenGL/EnumConversion.cs
new file mode 100644
index 00000000..c9a1eaa9
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/EnumConversion.cs
@@ -0,0 +1,675 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.Shader;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ static class EnumConversion
+ {
+ public static TextureWrapMode Convert(this AddressMode mode)
+ {
+ switch (mode)
+ {
+ case AddressMode.Clamp:
+ return TextureWrapMode.Clamp;
+ case AddressMode.Repeat:
+ return TextureWrapMode.Repeat;
+ case AddressMode.MirrorClamp:
+ return (TextureWrapMode)ExtTextureMirrorClamp.MirrorClampExt;
+ case AddressMode.MirrorClampToEdge:
+ return (TextureWrapMode)ExtTextureMirrorClamp.MirrorClampToEdgeExt;
+ case AddressMode.MirrorClampToBorder:
+ return (TextureWrapMode)ExtTextureMirrorClamp.MirrorClampToBorderExt;
+ case AddressMode.ClampToBorder:
+ return TextureWrapMode.ClampToBorder;
+ case AddressMode.MirroredRepeat:
+ return TextureWrapMode.MirroredRepeat;
+ case AddressMode.ClampToEdge:
+ return TextureWrapMode.ClampToEdge;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(AddressMode)} enum value: {mode}.");
+
+ return TextureWrapMode.Clamp;
+ }
+
+ public static NvBlendEquationAdvanced Convert(this AdvancedBlendOp op)
+ {
+ switch (op)
+ {
+ case AdvancedBlendOp.Zero:
+ return NvBlendEquationAdvanced.Zero;
+ case AdvancedBlendOp.Src:
+ return NvBlendEquationAdvanced.SrcNv;
+ case AdvancedBlendOp.Dst:
+ return NvBlendEquationAdvanced.DstNv;
+ case AdvancedBlendOp.SrcOver:
+ return NvBlendEquationAdvanced.SrcOverNv;
+ case AdvancedBlendOp.DstOver:
+ return NvBlendEquationAdvanced.DstOverNv;
+ case AdvancedBlendOp.SrcIn:
+ return NvBlendEquationAdvanced.SrcInNv;
+ case AdvancedBlendOp.DstIn:
+ return NvBlendEquationAdvanced.DstInNv;
+ case AdvancedBlendOp.SrcOut:
+ return NvBlendEquationAdvanced.SrcOutNv;
+ case AdvancedBlendOp.DstOut:
+ return NvBlendEquationAdvanced.DstOutNv;
+ case AdvancedBlendOp.SrcAtop:
+ return NvBlendEquationAdvanced.SrcAtopNv;
+ case AdvancedBlendOp.DstAtop:
+ return NvBlendEquationAdvanced.DstAtopNv;
+ case AdvancedBlendOp.Xor:
+ return NvBlendEquationAdvanced.XorNv;
+ case AdvancedBlendOp.Plus:
+ return NvBlendEquationAdvanced.PlusNv;
+ case AdvancedBlendOp.PlusClamped:
+ return NvBlendEquationAdvanced.PlusClampedNv;
+ case AdvancedBlendOp.PlusClampedAlpha:
+ return NvBlendEquationAdvanced.PlusClampedAlphaNv;
+ case AdvancedBlendOp.PlusDarker:
+ return NvBlendEquationAdvanced.PlusDarkerNv;
+ case AdvancedBlendOp.Multiply:
+ return NvBlendEquationAdvanced.MultiplyNv;
+ case AdvancedBlendOp.Screen:
+ return NvBlendEquationAdvanced.ScreenNv;
+ case AdvancedBlendOp.Overlay:
+ return NvBlendEquationAdvanced.OverlayNv;
+ case AdvancedBlendOp.Darken:
+ return NvBlendEquationAdvanced.DarkenNv;
+ case AdvancedBlendOp.Lighten:
+ return NvBlendEquationAdvanced.LightenNv;
+ case AdvancedBlendOp.ColorDodge:
+ return NvBlendEquationAdvanced.ColordodgeNv;
+ case AdvancedBlendOp.ColorBurn:
+ return NvBlendEquationAdvanced.ColorburnNv;
+ case AdvancedBlendOp.HardLight:
+ return NvBlendEquationAdvanced.HardlightNv;
+ case AdvancedBlendOp.SoftLight:
+ return NvBlendEquationAdvanced.SoftlightNv;
+ case AdvancedBlendOp.Difference:
+ return NvBlendEquationAdvanced.DifferenceNv;
+ case AdvancedBlendOp.Minus:
+ return NvBlendEquationAdvanced.MinusNv;
+ case AdvancedBlendOp.MinusClamped:
+ return NvBlendEquationAdvanced.MinusClampedNv;
+ case AdvancedBlendOp.Exclusion:
+ return NvBlendEquationAdvanced.ExclusionNv;
+ case AdvancedBlendOp.Contrast:
+ return NvBlendEquationAdvanced.ContrastNv;
+ case AdvancedBlendOp.Invert:
+ return NvBlendEquationAdvanced.Invert;
+ case AdvancedBlendOp.InvertRGB:
+ return NvBlendEquationAdvanced.InvertRgbNv;
+ case AdvancedBlendOp.InvertOvg:
+ return NvBlendEquationAdvanced.InvertOvgNv;
+ case AdvancedBlendOp.LinearDodge:
+ return NvBlendEquationAdvanced.LineardodgeNv;
+ case AdvancedBlendOp.LinearBurn:
+ return NvBlendEquationAdvanced.LinearburnNv;
+ case AdvancedBlendOp.VividLight:
+ return NvBlendEquationAdvanced.VividlightNv;
+ case AdvancedBlendOp.LinearLight:
+ return NvBlendEquationAdvanced.LinearlightNv;
+ case AdvancedBlendOp.PinLight:
+ return NvBlendEquationAdvanced.PinlightNv;
+ case AdvancedBlendOp.HardMix:
+ return NvBlendEquationAdvanced.HardmixNv;
+ case AdvancedBlendOp.Red:
+ return NvBlendEquationAdvanced.RedNv;
+ case AdvancedBlendOp.Green:
+ return NvBlendEquationAdvanced.GreenNv;
+ case AdvancedBlendOp.Blue:
+ return NvBlendEquationAdvanced.BlueNv;
+ case AdvancedBlendOp.HslHue:
+ return NvBlendEquationAdvanced.HslHueNv;
+ case AdvancedBlendOp.HslSaturation:
+ return NvBlendEquationAdvanced.HslSaturationNv;
+ case AdvancedBlendOp.HslColor:
+ return NvBlendEquationAdvanced.HslColorNv;
+ case AdvancedBlendOp.HslLuminosity:
+ return NvBlendEquationAdvanced.HslLuminosityNv;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(AdvancedBlendOp)} enum value: {op}.");
+
+ return NvBlendEquationAdvanced.Zero;
+ }
+
+ public static All Convert(this AdvancedBlendOverlap overlap)
+ {
+ switch (overlap)
+ {
+ case AdvancedBlendOverlap.Uncorrelated:
+ return All.UncorrelatedNv;
+ case AdvancedBlendOverlap.Disjoint:
+ return All.DisjointNv;
+ case AdvancedBlendOverlap.Conjoint:
+ return All.ConjointNv;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(AdvancedBlendOverlap)} enum value: {overlap}.");
+
+ return All.UncorrelatedNv;
+ }
+
+ public static All Convert(this BlendFactor factor)
+ {
+ switch (factor)
+ {
+ case BlendFactor.Zero:
+ case BlendFactor.ZeroGl:
+ return All.Zero;
+ case BlendFactor.One:
+ case BlendFactor.OneGl:
+ return All.One;
+ case BlendFactor.SrcColor:
+ case BlendFactor.SrcColorGl:
+ return All.SrcColor;
+ case BlendFactor.OneMinusSrcColor:
+ case BlendFactor.OneMinusSrcColorGl:
+ return All.OneMinusSrcColor;
+ case BlendFactor.SrcAlpha:
+ case BlendFactor.SrcAlphaGl:
+ return All.SrcAlpha;
+ case BlendFactor.OneMinusSrcAlpha:
+ case BlendFactor.OneMinusSrcAlphaGl:
+ return All.OneMinusSrcAlpha;
+ case BlendFactor.DstAlpha:
+ case BlendFactor.DstAlphaGl:
+ return All.DstAlpha;
+ case BlendFactor.OneMinusDstAlpha:
+ case BlendFactor.OneMinusDstAlphaGl:
+ return All.OneMinusDstAlpha;
+ case BlendFactor.DstColor:
+ case BlendFactor.DstColorGl:
+ return All.DstColor;
+ case BlendFactor.OneMinusDstColor:
+ case BlendFactor.OneMinusDstColorGl:
+ return All.OneMinusDstColor;
+ case BlendFactor.SrcAlphaSaturate:
+ case BlendFactor.SrcAlphaSaturateGl:
+ return All.SrcAlphaSaturate;
+ case BlendFactor.Src1Color:
+ case BlendFactor.Src1ColorGl:
+ return All.Src1Color;
+ case BlendFactor.OneMinusSrc1Color:
+ case BlendFactor.OneMinusSrc1ColorGl:
+ return All.OneMinusSrc1Color;
+ case BlendFactor.Src1Alpha:
+ case BlendFactor.Src1AlphaGl:
+ return All.Src1Alpha;
+ case BlendFactor.OneMinusSrc1Alpha:
+ case BlendFactor.OneMinusSrc1AlphaGl:
+ return All.OneMinusSrc1Alpha;
+ case BlendFactor.ConstantColor:
+ return All.ConstantColor;
+ case BlendFactor.OneMinusConstantColor:
+ return All.OneMinusConstantColor;
+ case BlendFactor.ConstantAlpha:
+ return All.ConstantAlpha;
+ case BlendFactor.OneMinusConstantAlpha:
+ return All.OneMinusConstantAlpha;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(BlendFactor)} enum value: {factor}.");
+
+ return All.Zero;
+ }
+
+ public static BlendEquationMode Convert(this BlendOp op)
+ {
+ switch (op)
+ {
+ case BlendOp.Add:
+ case BlendOp.AddGl:
+ return BlendEquationMode.FuncAdd;
+ case BlendOp.Minimum:
+ case BlendOp.MinimumGl:
+ return BlendEquationMode.Min;
+ case BlendOp.Maximum:
+ case BlendOp.MaximumGl:
+ return BlendEquationMode.Max;
+ case BlendOp.Subtract:
+ case BlendOp.SubtractGl:
+ return BlendEquationMode.FuncSubtract;
+ case BlendOp.ReverseSubtract:
+ case BlendOp.ReverseSubtractGl:
+ return BlendEquationMode.FuncReverseSubtract;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(BlendOp)} enum value: {op}.");
+
+ return BlendEquationMode.FuncAdd;
+ }
+
+ public static TextureCompareMode Convert(this CompareMode mode)
+ {
+ switch (mode)
+ {
+ case CompareMode.None:
+ return TextureCompareMode.None;
+ case CompareMode.CompareRToTexture:
+ return TextureCompareMode.CompareRToTexture;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(CompareMode)} enum value: {mode}.");
+
+ return TextureCompareMode.None;
+ }
+
+ public static All Convert(this CompareOp op)
+ {
+ switch (op)
+ {
+ case CompareOp.Never:
+ case CompareOp.NeverGl:
+ return All.Never;
+ case CompareOp.Less:
+ case CompareOp.LessGl:
+ return All.Less;
+ case CompareOp.Equal:
+ case CompareOp.EqualGl:
+ return All.Equal;
+ case CompareOp.LessOrEqual:
+ case CompareOp.LessOrEqualGl:
+ return All.Lequal;
+ case CompareOp.Greater:
+ case CompareOp.GreaterGl:
+ return All.Greater;
+ case CompareOp.NotEqual:
+ case CompareOp.NotEqualGl:
+ return All.Notequal;
+ case CompareOp.GreaterOrEqual:
+ case CompareOp.GreaterOrEqualGl:
+ return All.Gequal;
+ case CompareOp.Always:
+ case CompareOp.AlwaysGl:
+ return All.Always;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(CompareOp)} enum value: {op}.");
+
+ return All.Never;
+ }
+
+ public static ClipDepthMode Convert(this DepthMode mode)
+ {
+ switch (mode)
+ {
+ case DepthMode.MinusOneToOne:
+ return ClipDepthMode.NegativeOneToOne;
+ case DepthMode.ZeroToOne:
+ return ClipDepthMode.ZeroToOne;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(DepthMode)} enum value: {mode}.");
+
+ return ClipDepthMode.NegativeOneToOne;
+ }
+
+ public static All Convert(this DepthStencilMode mode)
+ {
+ switch (mode)
+ {
+ case DepthStencilMode.Depth:
+ return All.DepthComponent;
+ case DepthStencilMode.Stencil:
+ return All.StencilIndex;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(DepthStencilMode)} enum value: {mode}.");
+
+ return All.Depth;
+ }
+
+ public static CullFaceMode Convert(this Face face)
+ {
+ switch (face)
+ {
+ case Face.Back:
+ return CullFaceMode.Back;
+ case Face.Front:
+ return CullFaceMode.Front;
+ case Face.FrontAndBack:
+ return CullFaceMode.FrontAndBack;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(Face)} enum value: {face}.");
+
+ return CullFaceMode.Back;
+ }
+
+ public static FrontFaceDirection Convert(this FrontFace frontFace)
+ {
+ switch (frontFace)
+ {
+ case FrontFace.Clockwise:
+ return FrontFaceDirection.Cw;
+ case FrontFace.CounterClockwise:
+ return FrontFaceDirection.Ccw;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(FrontFace)} enum value: {frontFace}.");
+
+ return FrontFaceDirection.Cw;
+ }
+
+ public static DrawElementsType Convert(this IndexType type)
+ {
+ switch (type)
+ {
+ case IndexType.UByte:
+ return DrawElementsType.UnsignedByte;
+ case IndexType.UShort:
+ return DrawElementsType.UnsignedShort;
+ case IndexType.UInt:
+ return DrawElementsType.UnsignedInt;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(IndexType)} enum value: {type}.");
+
+ return DrawElementsType.UnsignedByte;
+ }
+
+ public static TextureMagFilter Convert(this MagFilter filter)
+ {
+ switch (filter)
+ {
+ case MagFilter.Nearest:
+ return TextureMagFilter.Nearest;
+ case MagFilter.Linear:
+ return TextureMagFilter.Linear;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(MagFilter)} enum value: {filter}.");
+
+ return TextureMagFilter.Nearest;
+ }
+
+ public static TextureMinFilter Convert(this MinFilter filter)
+ {
+ switch (filter)
+ {
+ case MinFilter.Nearest:
+ return TextureMinFilter.Nearest;
+ case MinFilter.Linear:
+ return TextureMinFilter.Linear;
+ case MinFilter.NearestMipmapNearest:
+ return TextureMinFilter.NearestMipmapNearest;
+ case MinFilter.LinearMipmapNearest:
+ return TextureMinFilter.LinearMipmapNearest;
+ case MinFilter.NearestMipmapLinear:
+ return TextureMinFilter.NearestMipmapLinear;
+ case MinFilter.LinearMipmapLinear:
+ return TextureMinFilter.LinearMipmapLinear;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(MinFilter)} enum value: {filter}.");
+
+ return TextureMinFilter.Nearest;
+ }
+
+ public static OpenTK.Graphics.OpenGL.PolygonMode Convert(this GAL.PolygonMode mode)
+ {
+ switch (mode)
+ {
+ case GAL.PolygonMode.Point:
+ return OpenTK.Graphics.OpenGL.PolygonMode.Point;
+ case GAL.PolygonMode.Line:
+ return OpenTK.Graphics.OpenGL.PolygonMode.Line;
+ case GAL.PolygonMode.Fill:
+ return OpenTK.Graphics.OpenGL.PolygonMode.Fill;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(GAL.PolygonMode)} enum value: {mode}.");
+
+ return OpenTK.Graphics.OpenGL.PolygonMode.Fill;
+ }
+
+ public static PrimitiveType Convert(this PrimitiveTopology topology)
+ {
+ switch (topology)
+ {
+ case PrimitiveTopology.Points:
+ return PrimitiveType.Points;
+ case PrimitiveTopology.Lines:
+ return PrimitiveType.Lines;
+ case PrimitiveTopology.LineLoop:
+ return PrimitiveType.LineLoop;
+ case PrimitiveTopology.LineStrip:
+ return PrimitiveType.LineStrip;
+ case PrimitiveTopology.Triangles:
+ return PrimitiveType.Triangles;
+ case PrimitiveTopology.TriangleStrip:
+ return PrimitiveType.TriangleStrip;
+ case PrimitiveTopology.TriangleFan:
+ return PrimitiveType.TriangleFan;
+ case PrimitiveTopology.Quads:
+ return PrimitiveType.Quads;
+ case PrimitiveTopology.QuadStrip:
+ return PrimitiveType.QuadStrip;
+ case PrimitiveTopology.Polygon:
+ return PrimitiveType.TriangleFan;
+ case PrimitiveTopology.LinesAdjacency:
+ return PrimitiveType.LinesAdjacency;
+ case PrimitiveTopology.LineStripAdjacency:
+ return PrimitiveType.LineStripAdjacency;
+ case PrimitiveTopology.TrianglesAdjacency:
+ return PrimitiveType.TrianglesAdjacency;
+ case PrimitiveTopology.TriangleStripAdjacency:
+ return PrimitiveType.TriangleStripAdjacency;
+ case PrimitiveTopology.Patches:
+ return PrimitiveType.Patches;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(PrimitiveTopology)} enum value: {topology}.");
+
+ return PrimitiveType.Points;
+ }
+
+ public static TransformFeedbackPrimitiveType ConvertToTfType(this PrimitiveTopology topology)
+ {
+ switch (topology)
+ {
+ case PrimitiveTopology.Points:
+ return TransformFeedbackPrimitiveType.Points;
+ case PrimitiveTopology.Lines:
+ case PrimitiveTopology.LineLoop:
+ case PrimitiveTopology.LineStrip:
+ case PrimitiveTopology.LinesAdjacency:
+ case PrimitiveTopology.LineStripAdjacency:
+ return TransformFeedbackPrimitiveType.Lines;
+ case PrimitiveTopology.Triangles:
+ case PrimitiveTopology.TriangleStrip:
+ case PrimitiveTopology.TriangleFan:
+ case PrimitiveTopology.TrianglesAdjacency:
+ case PrimitiveTopology.TriangleStripAdjacency:
+ return TransformFeedbackPrimitiveType.Triangles;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(PrimitiveTopology)} enum value: {topology}.");
+
+ return TransformFeedbackPrimitiveType.Points;
+ }
+
+ public static OpenTK.Graphics.OpenGL.StencilOp Convert(this GAL.StencilOp op)
+ {
+ switch (op)
+ {
+ case GAL.StencilOp.Keep:
+ case GAL.StencilOp.KeepGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.Keep;
+ case GAL.StencilOp.Zero:
+ case GAL.StencilOp.ZeroGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.Zero;
+ case GAL.StencilOp.Replace:
+ case GAL.StencilOp.ReplaceGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.Replace;
+ case GAL.StencilOp.IncrementAndClamp:
+ case GAL.StencilOp.IncrementAndClampGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.Incr;
+ case GAL.StencilOp.DecrementAndClamp:
+ case GAL.StencilOp.DecrementAndClampGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.Decr;
+ case GAL.StencilOp.Invert:
+ case GAL.StencilOp.InvertGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.Invert;
+ case GAL.StencilOp.IncrementAndWrap:
+ case GAL.StencilOp.IncrementAndWrapGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.IncrWrap;
+ case GAL.StencilOp.DecrementAndWrap:
+ case GAL.StencilOp.DecrementAndWrapGl:
+ return OpenTK.Graphics.OpenGL.StencilOp.DecrWrap;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(GAL.StencilOp)} enum value: {op}.");
+
+ return OpenTK.Graphics.OpenGL.StencilOp.Keep;
+ }
+
+ public static All Convert(this SwizzleComponent swizzleComponent)
+ {
+ switch (swizzleComponent)
+ {
+ case SwizzleComponent.Zero:
+ return All.Zero;
+ case SwizzleComponent.One:
+ return All.One;
+ case SwizzleComponent.Red:
+ return All.Red;
+ case SwizzleComponent.Green:
+ return All.Green;
+ case SwizzleComponent.Blue:
+ return All.Blue;
+ case SwizzleComponent.Alpha:
+ return All.Alpha;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(SwizzleComponent)} enum value: {swizzleComponent}.");
+
+ return All.Zero;
+ }
+
+ public static ImageTarget ConvertToImageTarget(this Target target)
+ {
+ return (ImageTarget)target.Convert();
+ }
+
+ public static TextureTarget Convert(this Target target)
+ {
+ switch (target)
+ {
+ case Target.Texture1D:
+ return TextureTarget.Texture1D;
+ case Target.Texture2D:
+ return TextureTarget.Texture2D;
+ case Target.Texture3D:
+ return TextureTarget.Texture3D;
+ case Target.Texture1DArray:
+ return TextureTarget.Texture1DArray;
+ case Target.Texture2DArray:
+ return TextureTarget.Texture2DArray;
+ case Target.Texture2DMultisample:
+ return TextureTarget.Texture2DMultisample;
+ case Target.Texture2DMultisampleArray:
+ return TextureTarget.Texture2DMultisampleArray;
+ case Target.Cubemap:
+ return TextureTarget.TextureCubeMap;
+ case Target.CubemapArray:
+ return TextureTarget.TextureCubeMapArray;
+ case Target.TextureBuffer:
+ return TextureTarget.TextureBuffer;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(Target)} enum value: {target}.");
+
+ return TextureTarget.Texture2D;
+ }
+
+ public static NvViewportSwizzle Convert(this ViewportSwizzle swizzle)
+ {
+ switch (swizzle)
+ {
+ case ViewportSwizzle.PositiveX:
+ return NvViewportSwizzle.ViewportSwizzlePositiveXNv;
+ case ViewportSwizzle.PositiveY:
+ return NvViewportSwizzle.ViewportSwizzlePositiveYNv;
+ case ViewportSwizzle.PositiveZ:
+ return NvViewportSwizzle.ViewportSwizzlePositiveZNv;
+ case ViewportSwizzle.PositiveW:
+ return NvViewportSwizzle.ViewportSwizzlePositiveWNv;
+ case ViewportSwizzle.NegativeX:
+ return NvViewportSwizzle.ViewportSwizzleNegativeXNv;
+ case ViewportSwizzle.NegativeY:
+ return NvViewportSwizzle.ViewportSwizzleNegativeYNv;
+ case ViewportSwizzle.NegativeZ:
+ return NvViewportSwizzle.ViewportSwizzleNegativeZNv;
+ case ViewportSwizzle.NegativeW:
+ return NvViewportSwizzle.ViewportSwizzleNegativeWNv;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(ViewportSwizzle)} enum value: {swizzle}.");
+
+ return NvViewportSwizzle.ViewportSwizzlePositiveXNv;
+ }
+
+ public static All Convert(this LogicalOp op)
+ {
+ switch (op)
+ {
+ case LogicalOp.Clear:
+ return All.Clear;
+ case LogicalOp.And:
+ return All.And;
+ case LogicalOp.AndReverse:
+ return All.AndReverse;
+ case LogicalOp.Copy:
+ return All.Copy;
+ case LogicalOp.AndInverted:
+ return All.AndInverted;
+ case LogicalOp.Noop:
+ return All.Noop;
+ case LogicalOp.Xor:
+ return All.Xor;
+ case LogicalOp.Or:
+ return All.Or;
+ case LogicalOp.Nor:
+ return All.Nor;
+ case LogicalOp.Equiv:
+ return All.Equiv;
+ case LogicalOp.Invert:
+ return All.Invert;
+ case LogicalOp.OrReverse:
+ return All.OrReverse;
+ case LogicalOp.CopyInverted:
+ return All.CopyInverted;
+ case LogicalOp.OrInverted:
+ return All.OrInverted;
+ case LogicalOp.Nand:
+ return All.Nand;
+ case LogicalOp.Set:
+ return All.Set;
+ }
+
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(LogicalOp)} enum value: {op}.");
+
+ return All.Never;
+ }
+
+ public static ShaderType Convert(this ShaderStage stage)
+ {
+ return stage switch
+ {
+ ShaderStage.Compute => ShaderType.ComputeShader,
+ ShaderStage.Vertex => ShaderType.VertexShader,
+ ShaderStage.TessellationControl => ShaderType.TessControlShader,
+ ShaderStage.TessellationEvaluation => ShaderType.TessEvaluationShader,
+ ShaderStage.Geometry => ShaderType.GeometryShader,
+ ShaderStage.Fragment => ShaderType.FragmentShader,
+ _ => ShaderType.VertexShader
+ };
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/FormatInfo.cs b/src/Ryujinx.Graphics.OpenGL/FormatInfo.cs
new file mode 100644
index 00000000..aef4dbad
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/FormatInfo.cs
@@ -0,0 +1,45 @@
+using OpenTK.Graphics.OpenGL;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ readonly struct FormatInfo
+ {
+ public int Components { get; }
+ public bool Normalized { get; }
+ public bool Scaled { get; }
+
+ public PixelInternalFormat PixelInternalFormat { get; }
+ public PixelFormat PixelFormat { get; }
+ public PixelType PixelType { get; }
+
+ public bool IsCompressed { get; }
+
+ public FormatInfo(
+ int components,
+ bool normalized,
+ bool scaled,
+ All pixelInternalFormat,
+ PixelFormat pixelFormat,
+ PixelType pixelType)
+ {
+ Components = components;
+ Normalized = normalized;
+ Scaled = scaled;
+ PixelInternalFormat = (PixelInternalFormat)pixelInternalFormat;
+ PixelFormat = pixelFormat;
+ PixelType = pixelType;
+ IsCompressed = false;
+ }
+
+ public FormatInfo(int components, bool normalized, bool scaled, All pixelFormat)
+ {
+ Components = components;
+ Normalized = normalized;
+ Scaled = scaled;
+ PixelInternalFormat = 0;
+ PixelFormat = (PixelFormat)pixelFormat;
+ PixelType = 0;
+ IsCompressed = true;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/FormatTable.cs b/src/Ryujinx.Graphics.OpenGL/FormatTable.cs
new file mode 100644
index 00000000..281f0004
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/FormatTable.cs
@@ -0,0 +1,225 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ struct FormatTable
+ {
+ private static FormatInfo[] _table;
+ private static SizedInternalFormat[] _tableImage;
+
+ static FormatTable()
+ {
+ int tableSize = Enum.GetNames<Format>().Length;
+
+ _table = new FormatInfo[tableSize];
+ _tableImage = new SizedInternalFormat[tableSize];
+
+ Add(Format.R8Unorm, new FormatInfo(1, true, false, All.R8, PixelFormat.Red, PixelType.UnsignedByte));
+ Add(Format.R8Snorm, new FormatInfo(1, true, false, All.R8Snorm, PixelFormat.Red, PixelType.Byte));
+ Add(Format.R8Uint, new FormatInfo(1, false, false, All.R8ui, PixelFormat.RedInteger, PixelType.UnsignedByte));
+ Add(Format.R8Sint, new FormatInfo(1, false, false, All.R8i, PixelFormat.RedInteger, PixelType.Byte));
+ Add(Format.R16Float, new FormatInfo(1, false, false, All.R16f, PixelFormat.Red, PixelType.HalfFloat));
+ Add(Format.R16Unorm, new FormatInfo(1, true, false, All.R16, PixelFormat.Red, PixelType.UnsignedShort));
+ Add(Format.R16Snorm, new FormatInfo(1, true, false, All.R16Snorm, PixelFormat.Red, PixelType.Short));
+ Add(Format.R16Uint, new FormatInfo(1, false, false, All.R16ui, PixelFormat.RedInteger, PixelType.UnsignedShort));
+ Add(Format.R16Sint, new FormatInfo(1, false, false, All.R16i, PixelFormat.RedInteger, PixelType.Short));
+ Add(Format.R32Float, new FormatInfo(1, false, false, All.R32f, PixelFormat.Red, PixelType.Float));
+ Add(Format.R32Uint, new FormatInfo(1, false, false, All.R32ui, PixelFormat.RedInteger, PixelType.UnsignedInt));
+ Add(Format.R32Sint, new FormatInfo(1, false, false, All.R32i, PixelFormat.RedInteger, PixelType.Int));
+ Add(Format.R8G8Unorm, new FormatInfo(2, true, false, All.Rg8, PixelFormat.Rg, PixelType.UnsignedByte));
+ Add(Format.R8G8Snorm, new FormatInfo(2, true, false, All.Rg8Snorm, PixelFormat.Rg, PixelType.Byte));
+ Add(Format.R8G8Uint, new FormatInfo(2, false, false, All.Rg8ui, PixelFormat.RgInteger, PixelType.UnsignedByte));
+ Add(Format.R8G8Sint, new FormatInfo(2, false, false, All.Rg8i, PixelFormat.RgInteger, PixelType.Byte));
+ Add(Format.R16G16Float, new FormatInfo(2, false, false, All.Rg16f, PixelFormat.Rg, PixelType.HalfFloat));
+ Add(Format.R16G16Unorm, new FormatInfo(2, true, false, All.Rg16, PixelFormat.Rg, PixelType.UnsignedShort));
+ Add(Format.R16G16Snorm, new FormatInfo(2, true, false, All.Rg16Snorm, PixelFormat.Rg, PixelType.Short));
+ Add(Format.R16G16Uint, new FormatInfo(2, false, false, All.Rg16ui, PixelFormat.RgInteger, PixelType.UnsignedShort));
+ Add(Format.R16G16Sint, new FormatInfo(2, false, false, All.Rg16i, PixelFormat.RgInteger, PixelType.Short));
+ Add(Format.R32G32Float, new FormatInfo(2, false, false, All.Rg32f, PixelFormat.Rg, PixelType.Float));
+ Add(Format.R32G32Uint, new FormatInfo(2, false, false, All.Rg32ui, PixelFormat.RgInteger, PixelType.UnsignedInt));
+ Add(Format.R32G32Sint, new FormatInfo(2, false, false, All.Rg32i, PixelFormat.RgInteger, PixelType.Int));
+ Add(Format.R8G8B8Unorm, new FormatInfo(3, true, false, All.Rgb8, PixelFormat.Rgb, PixelType.UnsignedByte));
+ Add(Format.R8G8B8Snorm, new FormatInfo(3, true, false, All.Rgb8Snorm, PixelFormat.Rgb, PixelType.Byte));
+ Add(Format.R8G8B8Uint, new FormatInfo(3, false, false, All.Rgb8ui, PixelFormat.RgbInteger, PixelType.UnsignedByte));
+ Add(Format.R8G8B8Sint, new FormatInfo(3, false, false, All.Rgb8i, PixelFormat.RgbInteger, PixelType.Byte));
+ Add(Format.R16G16B16Float, new FormatInfo(3, false, false, All.Rgb16f, PixelFormat.Rgb, PixelType.HalfFloat));
+ Add(Format.R16G16B16Unorm, new FormatInfo(3, true, false, All.Rgb16, PixelFormat.Rgb, PixelType.UnsignedShort));
+ Add(Format.R16G16B16Snorm, new FormatInfo(3, true, false, All.Rgb16Snorm, PixelFormat.Rgb, PixelType.Short));
+ Add(Format.R16G16B16Uint, new FormatInfo(3, false, false, All.Rgb16ui, PixelFormat.RgbInteger, PixelType.UnsignedShort));
+ Add(Format.R16G16B16Sint, new FormatInfo(3, false, false, All.Rgb16i, PixelFormat.RgbInteger, PixelType.Short));
+ Add(Format.R32G32B32Float, new FormatInfo(3, false, false, All.Rgb32f, PixelFormat.Rgb, PixelType.Float));
+ Add(Format.R32G32B32Uint, new FormatInfo(3, false, false, All.Rgb32ui, PixelFormat.RgbInteger, PixelType.UnsignedInt));
+ Add(Format.R32G32B32Sint, new FormatInfo(3, false, false, All.Rgb32i, PixelFormat.RgbInteger, PixelType.Int));
+ Add(Format.R8G8B8A8Unorm, new FormatInfo(4, true, false, All.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte));
+ Add(Format.R8G8B8A8Snorm, new FormatInfo(4, true, false, All.Rgba8Snorm, PixelFormat.Rgba, PixelType.Byte));
+ Add(Format.R8G8B8A8Uint, new FormatInfo(4, false, false, All.Rgba8ui, PixelFormat.RgbaInteger, PixelType.UnsignedByte));
+ Add(Format.R8G8B8A8Sint, new FormatInfo(4, false, false, All.Rgba8i, PixelFormat.RgbaInteger, PixelType.Byte));
+ Add(Format.R16G16B16A16Float, new FormatInfo(4, false, false, All.Rgba16f, PixelFormat.Rgba, PixelType.HalfFloat));
+ Add(Format.R16G16B16A16Unorm, new FormatInfo(4, true, false, All.Rgba16, PixelFormat.Rgba, PixelType.UnsignedShort));
+ Add(Format.R16G16B16A16Snorm, new FormatInfo(4, true, false, All.Rgba16Snorm, PixelFormat.Rgba, PixelType.Short));
+ Add(Format.R16G16B16A16Uint, new FormatInfo(4, false, false, All.Rgba16ui, PixelFormat.RgbaInteger, PixelType.UnsignedShort));
+ Add(Format.R16G16B16A16Sint, new FormatInfo(4, false, false, All.Rgba16i, PixelFormat.RgbaInteger, PixelType.Short));
+ Add(Format.R32G32B32A32Float, new FormatInfo(4, false, false, All.Rgba32f, PixelFormat.Rgba, PixelType.Float));
+ Add(Format.R32G32B32A32Uint, new FormatInfo(4, false, false, All.Rgba32ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt));
+ Add(Format.R32G32B32A32Sint, new FormatInfo(4, false, false, All.Rgba32i, PixelFormat.RgbaInteger, PixelType.Int));
+ Add(Format.S8Uint, new FormatInfo(1, false, false, All.StencilIndex8, PixelFormat.StencilIndex, PixelType.UnsignedByte));
+ Add(Format.D16Unorm, new FormatInfo(1, false, false, All.DepthComponent16, PixelFormat.DepthComponent, PixelType.UnsignedShort));
+ Add(Format.S8UintD24Unorm, new FormatInfo(1, false, false, All.Depth24Stencil8, PixelFormat.DepthStencil, PixelType.UnsignedInt248));
+ Add(Format.D32Float, new FormatInfo(1, false, false, All.DepthComponent32f, PixelFormat.DepthComponent, PixelType.Float));
+ Add(Format.D24UnormS8Uint, new FormatInfo(1, false, false, All.Depth24Stencil8, PixelFormat.DepthStencil, PixelType.UnsignedInt248));
+ Add(Format.D32FloatS8Uint, new FormatInfo(1, false, false, All.Depth32fStencil8, PixelFormat.DepthStencil, PixelType.Float32UnsignedInt248Rev));
+ Add(Format.R8G8B8A8Srgb, new FormatInfo(4, false, false, All.Srgb8Alpha8, PixelFormat.Rgba, PixelType.UnsignedByte));
+ Add(Format.R4G4B4A4Unorm, new FormatInfo(4, true, false, All.Rgba4, PixelFormat.Rgba, PixelType.UnsignedShort4444Reversed));
+ Add(Format.R5G5B5X1Unorm, new FormatInfo(4, true, false, All.Rgb5, PixelFormat.Rgb, PixelType.UnsignedShort1555Reversed));
+ Add(Format.R5G5B5A1Unorm, new FormatInfo(4, true, false, All.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort1555Reversed));
+ Add(Format.R5G6B5Unorm, new FormatInfo(3, true, false, All.Rgb565, PixelFormat.Rgb, PixelType.UnsignedShort565Reversed));
+ Add(Format.R10G10B10A2Unorm, new FormatInfo(4, true, false, All.Rgb10A2, PixelFormat.Rgba, PixelType.UnsignedInt2101010Reversed));
+ Add(Format.R10G10B10A2Uint, new FormatInfo(4, false, false, All.Rgb10A2ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt2101010Reversed));
+ Add(Format.R11G11B10Float, new FormatInfo(3, false, false, All.R11fG11fB10f, PixelFormat.Rgb, PixelType.UnsignedInt10F11F11FRev));
+ Add(Format.R9G9B9E5Float, new FormatInfo(3, false, false, All.Rgb9E5, PixelFormat.Rgb, PixelType.UnsignedInt5999Rev));
+ Add(Format.Bc1RgbaUnorm, new FormatInfo(4, true, false, All.CompressedRgbaS3tcDxt1Ext));
+ Add(Format.Bc2Unorm, new FormatInfo(4, true, false, All.CompressedRgbaS3tcDxt3Ext));
+ Add(Format.Bc3Unorm, new FormatInfo(4, true, false, All.CompressedRgbaS3tcDxt5Ext));
+ Add(Format.Bc1RgbaSrgb, new FormatInfo(4, true, false, All.CompressedSrgbAlphaS3tcDxt1Ext));
+ Add(Format.Bc2Srgb, new FormatInfo(4, false, false, All.CompressedSrgbAlphaS3tcDxt3Ext));
+ Add(Format.Bc3Srgb, new FormatInfo(4, false, false, All.CompressedSrgbAlphaS3tcDxt5Ext));
+ Add(Format.Bc4Unorm, new FormatInfo(1, true, false, All.CompressedRedRgtc1));
+ Add(Format.Bc4Snorm, new FormatInfo(1, true, false, All.CompressedSignedRedRgtc1));
+ Add(Format.Bc5Unorm, new FormatInfo(2, true, false, All.CompressedRgRgtc2));
+ Add(Format.Bc5Snorm, new FormatInfo(2, true, false, All.CompressedSignedRgRgtc2));
+ Add(Format.Bc7Unorm, new FormatInfo(4, true, false, All.CompressedRgbaBptcUnorm));
+ Add(Format.Bc7Srgb, new FormatInfo(4, false, false, All.CompressedSrgbAlphaBptcUnorm));
+ Add(Format.Bc6HSfloat, new FormatInfo(4, false, false, All.CompressedRgbBptcSignedFloat));
+ Add(Format.Bc6HUfloat, new FormatInfo(4, false, false, All.CompressedRgbBptcUnsignedFloat));
+ Add(Format.Etc2RgbUnorm, new FormatInfo(4, false, false, All.CompressedRgb8Etc2));
+ Add(Format.Etc2RgbaUnorm, new FormatInfo(4, false, false, All.CompressedRgba8Etc2Eac));
+ Add(Format.Etc2RgbPtaUnorm, new FormatInfo(4, false, false, All.CompressedRgb8PunchthroughAlpha1Etc2));
+ Add(Format.Etc2RgbSrgb, new FormatInfo(4, false, false, All.CompressedSrgb8Etc2));
+ Add(Format.Etc2RgbaSrgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Etc2Eac));
+ Add(Format.Etc2RgbPtaSrgb, new FormatInfo(4, false, false, All.CompressedSrgb8PunchthroughAlpha1Etc2));
+ Add(Format.R8Uscaled, new FormatInfo(1, false, true, All.R8ui, PixelFormat.RedInteger, PixelType.UnsignedByte));
+ Add(Format.R8Sscaled, new FormatInfo(1, false, true, All.R8i, PixelFormat.RedInteger, PixelType.Byte));
+ Add(Format.R16Uscaled, new FormatInfo(1, false, true, All.R16ui, PixelFormat.RedInteger, PixelType.UnsignedShort));
+ Add(Format.R16Sscaled, new FormatInfo(1, false, true, All.R16i, PixelFormat.RedInteger, PixelType.Short));
+ Add(Format.R32Uscaled, new FormatInfo(1, false, true, All.R32ui, PixelFormat.RedInteger, PixelType.UnsignedInt));
+ Add(Format.R32Sscaled, new FormatInfo(1, false, true, All.R32i, PixelFormat.RedInteger, PixelType.Int));
+ Add(Format.R8G8Uscaled, new FormatInfo(2, false, true, All.Rg8ui, PixelFormat.RgInteger, PixelType.UnsignedByte));
+ Add(Format.R8G8Sscaled, new FormatInfo(2, false, true, All.Rg8i, PixelFormat.RgInteger, PixelType.Byte));
+ Add(Format.R16G16Uscaled, new FormatInfo(2, false, true, All.Rg16ui, PixelFormat.RgInteger, PixelType.UnsignedShort));
+ Add(Format.R16G16Sscaled, new FormatInfo(2, false, true, All.Rg16i, PixelFormat.RgInteger, PixelType.Short));
+ Add(Format.R32G32Uscaled, new FormatInfo(2, false, true, All.Rg32ui, PixelFormat.RgInteger, PixelType.UnsignedInt));
+ Add(Format.R32G32Sscaled, new FormatInfo(2, false, true, All.Rg32i, PixelFormat.RgInteger, PixelType.Int));
+ Add(Format.R8G8B8Uscaled, new FormatInfo(3, false, true, All.Rgb8ui, PixelFormat.RgbInteger, PixelType.UnsignedByte));
+ Add(Format.R8G8B8Sscaled, new FormatInfo(3, false, true, All.Rgb8i, PixelFormat.RgbInteger, PixelType.Byte));
+ Add(Format.R16G16B16Uscaled, new FormatInfo(3, false, true, All.Rgb16ui, PixelFormat.RgbInteger, PixelType.UnsignedShort));
+ Add(Format.R16G16B16Sscaled, new FormatInfo(3, false, true, All.Rgb16i, PixelFormat.RgbInteger, PixelType.Short));
+ Add(Format.R32G32B32Uscaled, new FormatInfo(3, false, true, All.Rgb32ui, PixelFormat.RgbInteger, PixelType.UnsignedInt));
+ Add(Format.R32G32B32Sscaled, new FormatInfo(3, false, true, All.Rgb32i, PixelFormat.RgbInteger, PixelType.Int));
+ Add(Format.R8G8B8A8Uscaled, new FormatInfo(4, false, true, All.Rgba8ui, PixelFormat.RgbaInteger, PixelType.UnsignedByte));
+ Add(Format.R8G8B8A8Sscaled, new FormatInfo(4, false, true, All.Rgba8i, PixelFormat.RgbaInteger, PixelType.Byte));
+ Add(Format.R16G16B16A16Uscaled, new FormatInfo(4, false, true, All.Rgba16ui, PixelFormat.RgbaInteger, PixelType.UnsignedShort));
+ Add(Format.R16G16B16A16Sscaled, new FormatInfo(4, false, true, All.Rgba16i, PixelFormat.RgbaInteger, PixelType.Short));
+ Add(Format.R32G32B32A32Uscaled, new FormatInfo(4, false, true, All.Rgba32ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt));
+ Add(Format.R32G32B32A32Sscaled, new FormatInfo(4, false, true, All.Rgba32i, PixelFormat.RgbaInteger, PixelType.Int));
+ Add(Format.R10G10B10A2Snorm, new FormatInfo(4, true, false, All.Rgb10A2, PixelFormat.Rgba, (PixelType)All.Int2101010Rev));
+ Add(Format.R10G10B10A2Sint, new FormatInfo(4, false, false, All.Rgb10A2, PixelFormat.RgbaInteger, (PixelType)All.Int2101010Rev));
+ Add(Format.R10G10B10A2Uscaled, new FormatInfo(4, false, true, All.Rgb10A2ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt2101010Reversed));
+ Add(Format.R10G10B10A2Sscaled, new FormatInfo(4, false, true, All.Rgb10A2, PixelFormat.RgbaInteger, PixelType.UnsignedInt2101010Reversed));
+ Add(Format.Astc4x4Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc4X4Khr));
+ Add(Format.Astc5x4Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc5X4Khr));
+ Add(Format.Astc5x5Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc5X5Khr));
+ Add(Format.Astc6x5Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc6X5Khr));
+ Add(Format.Astc6x6Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc6X6Khr));
+ Add(Format.Astc8x5Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc8X5Khr));
+ Add(Format.Astc8x6Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc8X6Khr));
+ Add(Format.Astc8x8Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc8X8Khr));
+ Add(Format.Astc10x5Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc10X5Khr));
+ Add(Format.Astc10x6Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc10X6Khr));
+ Add(Format.Astc10x8Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc10X8Khr));
+ Add(Format.Astc10x10Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc10X10Khr));
+ Add(Format.Astc12x10Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc12X10Khr));
+ Add(Format.Astc12x12Unorm, new FormatInfo(4, true, false, All.CompressedRgbaAstc12X12Khr));
+ Add(Format.Astc4x4Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc4X4Khr));
+ Add(Format.Astc5x4Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc5X4Khr));
+ Add(Format.Astc5x5Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc5X5Khr));
+ Add(Format.Astc6x5Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc6X5Khr));
+ Add(Format.Astc6x6Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc6X6Khr));
+ Add(Format.Astc8x5Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc8X5Khr));
+ Add(Format.Astc8x6Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc8X6Khr));
+ Add(Format.Astc8x8Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc8X8Khr));
+ Add(Format.Astc10x5Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc10X5Khr));
+ Add(Format.Astc10x6Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc10X6Khr));
+ Add(Format.Astc10x8Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc10X8Khr));
+ Add(Format.Astc10x10Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc10X10Khr));
+ Add(Format.Astc12x10Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc12X10Khr));
+ Add(Format.Astc12x12Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc12X12Khr));
+ Add(Format.B5G6R5Unorm, new FormatInfo(3, true, false, All.Rgb565, PixelFormat.Rgb, PixelType.UnsignedShort565Reversed));
+ Add(Format.B5G5R5A1Unorm, new FormatInfo(4, true, false, All.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort1555Reversed));
+ Add(Format.A1B5G5R5Unorm, new FormatInfo(4, true, false, All.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort5551));
+ Add(Format.B8G8R8A8Unorm, new FormatInfo(4, true, false, All.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte));
+ Add(Format.B8G8R8A8Srgb, new FormatInfo(4, false, false, All.Srgb8Alpha8, PixelFormat.Rgba, PixelType.UnsignedByte));
+
+ Add(Format.R8Unorm, SizedInternalFormat.R8);
+ Add(Format.R8Uint, SizedInternalFormat.R8ui);
+ Add(Format.R8Sint, SizedInternalFormat.R8i);
+ Add(Format.R16Float, SizedInternalFormat.R16f);
+ Add(Format.R16Unorm, SizedInternalFormat.R16);
+ Add(Format.R16Snorm, (SizedInternalFormat)All.R16Snorm);
+ Add(Format.R16Uint, SizedInternalFormat.R16ui);
+ Add(Format.R16Sint, SizedInternalFormat.R16i);
+ Add(Format.R32Float, SizedInternalFormat.R32f);
+ Add(Format.R32Uint, SizedInternalFormat.R32ui);
+ Add(Format.R32Sint, SizedInternalFormat.R32i);
+ Add(Format.R8G8Unorm, SizedInternalFormat.Rg8);
+ Add(Format.R8G8Snorm, (SizedInternalFormat)All.Rg8Snorm);
+ Add(Format.R8G8Uint, SizedInternalFormat.Rg8ui);
+ Add(Format.R8G8Sint, SizedInternalFormat.Rg8i);
+ Add(Format.R16G16Float, SizedInternalFormat.Rg16f);
+ Add(Format.R16G16Unorm, SizedInternalFormat.Rg16);
+ Add(Format.R16G16Snorm, (SizedInternalFormat)All.Rg16Snorm);
+ Add(Format.R16G16Uint, SizedInternalFormat.Rg16ui);
+ Add(Format.R16G16Sint, SizedInternalFormat.Rg16i);
+ Add(Format.R32G32Float, SizedInternalFormat.Rg32f);
+ Add(Format.R32G32Uint, SizedInternalFormat.Rg32ui);
+ Add(Format.R32G32Sint, SizedInternalFormat.Rg32i);
+ Add(Format.R8G8B8A8Unorm, SizedInternalFormat.Rgba8);
+ Add(Format.R8G8B8A8Snorm, (SizedInternalFormat)All.Rgba8Snorm);
+ Add(Format.R8G8B8A8Uint, SizedInternalFormat.Rgba8ui);
+ Add(Format.R8G8B8A8Sint, SizedInternalFormat.Rgba8i);
+ Add(Format.R16G16B16A16Float, SizedInternalFormat.Rgba16f);
+ Add(Format.R16G16B16A16Unorm, SizedInternalFormat.Rgba16);
+ Add(Format.R16G16B16A16Snorm, (SizedInternalFormat)All.Rgba16Snorm);
+ Add(Format.R16G16B16A16Uint, SizedInternalFormat.Rgba16ui);
+ Add(Format.R16G16B16A16Sint, SizedInternalFormat.Rgba16i);
+ Add(Format.R32G32B32A32Float, SizedInternalFormat.Rgba32f);
+ Add(Format.R32G32B32A32Uint, SizedInternalFormat.Rgba32ui);
+ Add(Format.R32G32B32A32Sint, SizedInternalFormat.Rgba32i);
+ Add(Format.R8G8B8A8Srgb, SizedInternalFormat.Rgba8);
+ Add(Format.R10G10B10A2Unorm, (SizedInternalFormat)All.Rgb10A2);
+ Add(Format.R10G10B10A2Uint, (SizedInternalFormat)All.Rgb10A2ui);
+ Add(Format.R11G11B10Float, (SizedInternalFormat)All.R11fG11fB10f);
+ }
+
+ private static void Add(Format format, FormatInfo info)
+ {
+ _table[(int)format] = info;
+ }
+
+ private static void Add(Format format, SizedInternalFormat sif)
+ {
+ _tableImage[(int)format] = sif;
+ }
+
+ public static FormatInfo GetFormatInfo(Format format)
+ {
+ return _table[(int)format];
+ }
+
+ public static SizedInternalFormat GetImageFormat(Format format)
+ {
+ return _tableImage[(int)format];
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Framebuffer.cs b/src/Ryujinx.Graphics.OpenGL/Framebuffer.cs
new file mode 100644
index 00000000..b180b857
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Framebuffer.cs
@@ -0,0 +1,247 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class Framebuffer : IDisposable
+ {
+ public int Handle { get; private set; }
+ private int _clearFbHandle;
+ private bool _clearFbInitialized;
+
+ private FramebufferAttachment _lastDsAttachment;
+
+ private readonly TextureView[] _colors;
+ private TextureView _depthStencil;
+
+ private int _colorsCount;
+ private bool _dualSourceBlend;
+
+ public Framebuffer()
+ {
+ Handle = GL.GenFramebuffer();
+ _clearFbHandle = GL.GenFramebuffer();
+
+ _colors = new TextureView[8];
+ }
+
+ public int Bind()
+ {
+ GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
+ return Handle;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void AttachColor(int index, TextureView color)
+ {
+ if (_colors[index] == color)
+ {
+ return;
+ }
+
+ FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
+
+ GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
+
+ _colors[index] = color;
+ }
+
+ public void AttachDepthStencil(TextureView depthStencil)
+ {
+ // Detach the last depth/stencil buffer if there is any.
+ if (_lastDsAttachment != 0)
+ {
+ GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
+ }
+
+ if (depthStencil != null)
+ {
+ FramebufferAttachment attachment = GetAttachment(depthStencil.Format);
+
+ GL.FramebufferTexture(
+ FramebufferTarget.Framebuffer,
+ attachment,
+ depthStencil.Handle,
+ 0);
+
+ _lastDsAttachment = attachment;
+ }
+ else
+ {
+ _lastDsAttachment = 0;
+ }
+
+ _depthStencil = depthStencil;
+ }
+
+ public void SetDualSourceBlend(bool enable)
+ {
+ bool oldEnable = _dualSourceBlend;
+
+ _dualSourceBlend = enable;
+
+ // When dual source blend is used,
+ // we can only have one draw buffer.
+ if (enable)
+ {
+ GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
+ }
+ else if (oldEnable)
+ {
+ SetDrawBuffersImpl(_colorsCount);
+ }
+ }
+
+ public void SetDrawBuffers(int colorsCount)
+ {
+ if (_colorsCount != colorsCount && !_dualSourceBlend)
+ {
+ SetDrawBuffersImpl(colorsCount);
+ }
+
+ _colorsCount = colorsCount;
+ }
+
+ private void SetDrawBuffersImpl(int colorsCount)
+ {
+ DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
+
+ for (int index = 0; index < colorsCount; index++)
+ {
+ drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
+ }
+
+ GL.DrawBuffers(colorsCount, drawBuffers);
+ }
+
+ private static FramebufferAttachment GetAttachment(Format format)
+ {
+ if (IsPackedDepthStencilFormat(format))
+ {
+ return FramebufferAttachment.DepthStencilAttachment;
+ }
+ else if (IsDepthOnlyFormat(format))
+ {
+ return FramebufferAttachment.DepthAttachment;
+ }
+ else
+ {
+ return FramebufferAttachment.StencilAttachment;
+ }
+ }
+
+ private static bool IsPackedDepthStencilFormat(Format format)
+ {
+ return format == Format.D24UnormS8Uint ||
+ format == Format.D32FloatS8Uint ||
+ format == Format.S8UintD24Unorm;
+ }
+
+ private static bool IsDepthOnlyFormat(Format format)
+ {
+ return format == Format.D16Unorm || format == Format.D32Float;
+ }
+
+ public int GetColorLayerCount(int index)
+ {
+ return _colors[index]?.Info.GetDepthOrLayers() ?? 0;
+ }
+
+ public int GetDepthStencilLayerCount()
+ {
+ return _depthStencil?.Info.GetDepthOrLayers() ?? 0;
+ }
+
+ public void AttachColorLayerForClear(int index, int layer)
+ {
+ TextureView color = _colors[index];
+
+ if (!IsLayered(color))
+ {
+ return;
+ }
+
+ BindClearFb();
+ GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, color.Handle, 0, layer);
+ }
+
+ public void DetachColorLayerForClear(int index)
+ {
+ TextureView color = _colors[index];
+
+ if (!IsLayered(color))
+ {
+ return;
+ }
+
+ GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, 0, 0);
+ Bind();
+ }
+
+ public void AttachDepthStencilLayerForClear(int layer)
+ {
+ TextureView depthStencil = _depthStencil;
+
+ if (!IsLayered(depthStencil))
+ {
+ return;
+ }
+
+ BindClearFb();
+ GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), depthStencil.Handle, 0, layer);
+ }
+
+ public void DetachDepthStencilLayerForClear()
+ {
+ TextureView depthStencil = _depthStencil;
+
+ if (!IsLayered(depthStencil))
+ {
+ return;
+ }
+
+ GL.FramebufferTexture(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), 0, 0);
+ Bind();
+ }
+
+ private void BindClearFb()
+ {
+ GL.BindFramebuffer(FramebufferTarget.Framebuffer, _clearFbHandle);
+
+ if (!_clearFbInitialized)
+ {
+ SetDrawBuffersImpl(Constants.MaxRenderTargets);
+ _clearFbInitialized = true;
+ }
+ }
+
+ private static bool IsLayered(TextureView view)
+ {
+ return view != null &&
+ view.Target != Target.Texture1D &&
+ view.Target != Target.Texture2D &&
+ view.Target != Target.Texture2DMultisample &&
+ view.Target != Target.TextureBuffer;
+ }
+
+ public void Dispose()
+ {
+ if (Handle != 0)
+ {
+ GL.DeleteFramebuffer(Handle);
+
+ Handle = 0;
+ }
+
+ if (_clearFbHandle != 0)
+ {
+ GL.DeleteFramebuffer(_clearFbHandle);
+
+ _clearFbHandle = 0;
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Handle.cs b/src/Ryujinx.Graphics.OpenGL/Handle.cs
new file mode 100644
index 00000000..4b2f05e6
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Handle.cs
@@ -0,0 +1,23 @@
+using Ryujinx.Graphics.GAL;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ static class Handle
+ {
+ public static T FromInt32<T>(int handle) where T : unmanaged
+ {
+ Debug.Assert(Unsafe.SizeOf<T>() == sizeof(ulong));
+
+ ulong handle64 = (uint)handle;
+
+ return Unsafe.As<ulong, T>(ref handle64);
+ }
+
+ public static int ToInt32(this BufferHandle handle)
+ {
+ return (int)Unsafe.As<BufferHandle, ulong>(ref handle);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Helper/GLXHelper.cs b/src/Ryujinx.Graphics.OpenGL/Helper/GLXHelper.cs
new file mode 100644
index 00000000..96354946
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Helper/GLXHelper.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Runtime.Versioning;
+
+namespace Ryujinx.Graphics.OpenGL.Helper
+{
+ [SupportedOSPlatform("linux")]
+ internal static partial class GLXHelper
+ {
+ private const string LibraryName = "glx.dll";
+
+ static GLXHelper()
+ {
+ NativeLibrary.SetDllImportResolver(typeof(GLXHelper).Assembly, (name, assembly, path) =>
+ {
+ if (name != LibraryName)
+ {
+ return IntPtr.Zero;
+ }
+
+ if (!NativeLibrary.TryLoad("libGL.so.1", assembly, path, out IntPtr result))
+ {
+ if (!NativeLibrary.TryLoad("libGL.so", assembly, path, out result))
+ {
+ return IntPtr.Zero;
+ }
+ }
+
+ return result;
+ });
+ }
+
+ [LibraryImport(LibraryName, EntryPoint = "glXGetCurrentContext")]
+ public static partial IntPtr GetCurrentContext();
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Helper/WGLHelper.cs b/src/Ryujinx.Graphics.OpenGL/Helper/WGLHelper.cs
new file mode 100644
index 00000000..df497ae2
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Helper/WGLHelper.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Runtime.Versioning;
+
+namespace Ryujinx.Graphics.OpenGL.Helper
+{
+ [SupportedOSPlatform("windows")]
+ internal static partial class WGLHelper
+ {
+ private const string LibraryName = "OPENGL32.DLL";
+
+ [LibraryImport(LibraryName, EntryPoint = "wglGetCurrentContext")]
+ public static partial IntPtr GetCurrentContext();
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/HwCapabilities.cs b/src/Ryujinx.Graphics.OpenGL/HwCapabilities.cs
new file mode 100644
index 00000000..bf365b4d
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/HwCapabilities.cs
@@ -0,0 +1,142 @@
+using OpenTK.Graphics.OpenGL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ static class HwCapabilities
+ {
+ private static readonly Lazy<bool> _supportsAlphaToCoverageDitherControl = new Lazy<bool>(() => HasExtension("GL_NV_alpha_to_coverage_dither_control"));
+ private static readonly Lazy<bool> _supportsAstcCompression = new Lazy<bool>(() => HasExtension("GL_KHR_texture_compression_astc_ldr"));
+ private static readonly Lazy<bool> _supportsBlendEquationAdvanced = new Lazy<bool>(() => HasExtension("GL_NV_blend_equation_advanced"));
+ 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> _supportsGeometryShaderPassthrough = new Lazy<bool>(() => HasExtension("GL_NV_geometry_shader_passthrough"));
+ private static readonly Lazy<bool> _supportsImageLoadFormatted = new Lazy<bool>(() => HasExtension("GL_EXT_shader_image_load_formatted"));
+ private static readonly Lazy<bool> _supportsIndirectParameters = new Lazy<bool>(() => HasExtension("GL_ARB_indirect_parameters"));
+ private static readonly Lazy<bool> _supportsParallelShaderCompile = new Lazy<bool>(() => HasExtension("GL_ARB_parallel_shader_compile"));
+ private static readonly Lazy<bool> _supportsPolygonOffsetClamp = new Lazy<bool>(() => HasExtension("GL_EXT_polygon_offset_clamp"));
+ private static readonly Lazy<bool> _supportsQuads = new Lazy<bool>(SupportsQuadsCheck);
+ private static readonly Lazy<bool> _supportsSeamlessCubemapPerTexture = new Lazy<bool>(() => HasExtension("GL_ARB_seamless_cubemap_per_texture"));
+ private static readonly Lazy<bool> _supportsShaderBallot = new Lazy<bool>(() => HasExtension("GL_ARB_shader_ballot"));
+ private static readonly Lazy<bool> _supportsShaderViewportLayerArray = new Lazy<bool>(() => HasExtension("GL_ARB_shader_viewport_layer_array"));
+ private static readonly Lazy<bool> _supportsViewportArray2 = new Lazy<bool>(() => HasExtension("GL_NV_viewport_array2"));
+ private static readonly Lazy<bool> _supportsTextureCompressionBptc = new Lazy<bool>(() => HasExtension("GL_EXT_texture_compression_bptc"));
+ private static readonly Lazy<bool> _supportsTextureCompressionRgtc = new Lazy<bool>(() => HasExtension("GL_EXT_texture_compression_rgtc"));
+ private static readonly Lazy<bool> _supportsTextureCompressionS3tc = new Lazy<bool>(() => HasExtension("GL_EXT_texture_compression_s3tc"));
+ private static readonly Lazy<bool> _supportsTextureShadowLod = new Lazy<bool>(() => HasExtension("GL_EXT_texture_shadow_lod"));
+ private static readonly Lazy<bool> _supportsViewportSwizzle = new Lazy<bool>(() => HasExtension("GL_NV_viewport_swizzle"));
+
+ private static readonly Lazy<int> _maximumComputeSharedMemorySize = new Lazy<int>(() => GetLimit(All.MaxComputeSharedMemorySize));
+ private static readonly Lazy<int> _storageBufferOffsetAlignment = new Lazy<int>(() => GetLimit(All.ShaderStorageBufferOffsetAlignment));
+
+ public enum GpuVendor
+ {
+ Unknown,
+ AmdWindows,
+ AmdUnix,
+ IntelWindows,
+ IntelUnix,
+ Nvidia
+ }
+
+ private static readonly Lazy<GpuVendor> _gpuVendor = new Lazy<GpuVendor>(GetGpuVendor);
+
+ private static bool _isAMD => _gpuVendor.Value == GpuVendor.AmdWindows || _gpuVendor.Value == GpuVendor.AmdUnix;
+ private static bool _isIntel => _gpuVendor.Value == GpuVendor.IntelWindows || _gpuVendor.Value == GpuVendor.IntelUnix;
+
+ public static GpuVendor Vendor => _gpuVendor.Value;
+
+ private static Lazy<float> _maxSupportedAnisotropy = new Lazy<float>(GL.GetFloat((GetPName)All.MaxTextureMaxAnisotropy));
+
+ public static bool UsePersistentBufferForFlush => _gpuVendor.Value == GpuVendor.AmdWindows || _gpuVendor.Value == GpuVendor.Nvidia;
+
+ public static bool SupportsAlphaToCoverageDitherControl => _supportsAlphaToCoverageDitherControl.Value;
+ public static bool SupportsAstcCompression => _supportsAstcCompression.Value;
+ public static bool SupportsBlendEquationAdvanced => _supportsBlendEquationAdvanced.Value;
+ public static bool SupportsDrawTexture => _supportsDrawTexture.Value;
+ public static bool SupportsFragmentShaderInterlock => _supportsFragmentShaderInterlock.Value;
+ public static bool SupportsFragmentShaderOrdering => _supportsFragmentShaderOrdering.Value;
+ public static bool SupportsGeometryShaderPassthrough => _supportsGeometryShaderPassthrough.Value;
+ public static bool SupportsImageLoadFormatted => _supportsImageLoadFormatted.Value;
+ public static bool SupportsIndirectParameters => _supportsIndirectParameters.Value;
+ public static bool SupportsParallelShaderCompile => _supportsParallelShaderCompile.Value;
+ public static bool SupportsPolygonOffsetClamp => _supportsPolygonOffsetClamp.Value;
+ public static bool SupportsQuads => _supportsQuads.Value;
+ public static bool SupportsSeamlessCubemapPerTexture => _supportsSeamlessCubemapPerTexture.Value;
+ public static bool SupportsShaderBallot => _supportsShaderBallot.Value;
+ public static bool SupportsShaderViewportLayerArray => _supportsShaderViewportLayerArray.Value;
+ public static bool SupportsViewportArray2 => _supportsViewportArray2.Value;
+ public static bool SupportsTextureCompressionBptc => _supportsTextureCompressionBptc.Value;
+ public static bool SupportsTextureCompressionRgtc => _supportsTextureCompressionRgtc.Value;
+ public static bool SupportsTextureCompressionS3tc => _supportsTextureCompressionS3tc.Value;
+ public static bool SupportsTextureShadowLod => _supportsTextureShadowLod.Value;
+ public static bool SupportsViewportSwizzle => _supportsViewportSwizzle.Value;
+
+ public static bool SupportsMismatchingViewFormat => _gpuVendor.Value != GpuVendor.AmdWindows && _gpuVendor.Value != GpuVendor.IntelWindows;
+ public static bool SupportsNonConstantTextureOffset => _gpuVendor.Value == GpuVendor.Nvidia;
+ public static bool RequiresSyncFlush => _gpuVendor.Value == GpuVendor.AmdWindows || _isIntel;
+
+ public static int MaximumComputeSharedMemorySize => _maximumComputeSharedMemorySize.Value;
+ public static int StorageBufferOffsetAlignment => _storageBufferOffsetAlignment.Value;
+
+ public static float MaximumSupportedAnisotropy => _maxSupportedAnisotropy.Value;
+
+ private static bool HasExtension(string name)
+ {
+ int numExtensions = GL.GetInteger(GetPName.NumExtensions);
+
+ for (int extension = 0; extension < numExtensions; extension++)
+ {
+ if (GL.GetString(StringNameIndexed.Extensions, extension) == name)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static int GetLimit(All name)
+ {
+ return GL.GetInteger((GetPName)name);
+ }
+
+ private static GpuVendor GetGpuVendor()
+ {
+ string vendor = GL.GetString(StringName.Vendor).ToLower();
+
+ if (vendor == "nvidia corporation")
+ {
+ return GpuVendor.Nvidia;
+ }
+ else if (vendor == "intel")
+ {
+ string renderer = GL.GetString(StringName.Renderer).ToLower();
+
+ return renderer.Contains("mesa") ? GpuVendor.IntelUnix : GpuVendor.IntelWindows;
+ }
+ else if (vendor == "ati technologies inc." || vendor == "advanced micro devices, inc.")
+ {
+ return GpuVendor.AmdWindows;
+ }
+ else if (vendor == "amd" || vendor == "x.org")
+ {
+ return GpuVendor.AmdUnix;
+ }
+ else
+ {
+ return GpuVendor.Unknown;
+ }
+ }
+
+ private static bool SupportsQuadsCheck()
+ {
+ GL.GetError(); // Clear any existing error.
+ GL.Begin(PrimitiveType.Quads);
+ GL.End();
+
+ return GL.GetError() == ErrorCode.NoError;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/IOpenGLContext.cs b/src/Ryujinx.Graphics.OpenGL/IOpenGLContext.cs
new file mode 100644
index 00000000..b1f6d72d
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/IOpenGLContext.cs
@@ -0,0 +1,27 @@
+using Ryujinx.Graphics.OpenGL.Helper;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ public interface IOpenGLContext : IDisposable
+ {
+ void MakeCurrent();
+
+ // TODO: Support more APIs per platform.
+ static bool HasContext()
+ {
+ if (OperatingSystem.IsWindows())
+ {
+ return WGLHelper.GetCurrentContext() != IntPtr.Zero;
+ }
+ else if (OperatingSystem.IsLinux())
+ {
+ return GLXHelper.GetCurrentContext() != IntPtr.Zero;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/FormatConverter.cs b/src/Ryujinx.Graphics.OpenGL/Image/FormatConverter.cs
new file mode 100644
index 00000000..c4bbf745
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/FormatConverter.cs
@@ -0,0 +1,149 @@
+using System;
+using System.Numerics;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Runtime.Intrinsics.X86;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ static class FormatConverter
+ {
+ public unsafe static byte[] ConvertS8D24ToD24S8(ReadOnlySpan<byte> data)
+ {
+ byte[] output = new byte[data.Length];
+
+ int start = 0;
+
+ if (Avx2.IsSupported)
+ {
+ var mask = Vector256.Create(
+ (byte)3, (byte)0, (byte)1, (byte)2,
+ (byte)7, (byte)4, (byte)5, (byte)6,
+ (byte)11, (byte)8, (byte)9, (byte)10,
+ (byte)15, (byte)12, (byte)13, (byte)14,
+ (byte)19, (byte)16, (byte)17, (byte)18,
+ (byte)23, (byte)20, (byte)21, (byte)22,
+ (byte)27, (byte)24, (byte)25, (byte)26,
+ (byte)31, (byte)28, (byte)29, (byte)30);
+
+ int sizeAligned = data.Length & ~31;
+
+ fixed (byte* pInput = data, pOutput = output)
+ {
+ for (uint i = 0; i < sizeAligned; i += 32)
+ {
+ var dataVec = Avx.LoadVector256(pInput + i);
+
+ dataVec = Avx2.Shuffle(dataVec, mask);
+
+ Avx.Store(pOutput + i, dataVec);
+ }
+ }
+
+ start = sizeAligned;
+ }
+ else if (Ssse3.IsSupported)
+ {
+ var mask = Vector128.Create(
+ (byte)3, (byte)0, (byte)1, (byte)2,
+ (byte)7, (byte)4, (byte)5, (byte)6,
+ (byte)11, (byte)8, (byte)9, (byte)10,
+ (byte)15, (byte)12, (byte)13, (byte)14);
+
+ int sizeAligned = data.Length & ~15;
+
+ fixed (byte* pInput = data, pOutput = output)
+ {
+ for (uint i = 0; i < sizeAligned; i += 16)
+ {
+ var dataVec = Sse2.LoadVector128(pInput + i);
+
+ dataVec = Ssse3.Shuffle(dataVec, mask);
+
+ Sse2.Store(pOutput + i, dataVec);
+ }
+ }
+
+ start = sizeAligned;
+ }
+
+ var outSpan = MemoryMarshal.Cast<byte, uint>(output);
+ var dataSpan = MemoryMarshal.Cast<byte, uint>(data);
+ for (int i = start / sizeof(uint); i < dataSpan.Length; i++)
+ {
+ outSpan[i] = BitOperations.RotateLeft(dataSpan[i], 8);
+ }
+
+ return output;
+ }
+
+ public unsafe static byte[] ConvertD24S8ToS8D24(ReadOnlySpan<byte> data)
+ {
+ byte[] output = new byte[data.Length];
+
+ int start = 0;
+
+ if (Avx2.IsSupported)
+ {
+ var mask = Vector256.Create(
+ (byte)1, (byte)2, (byte)3, (byte)0,
+ (byte)5, (byte)6, (byte)7, (byte)4,
+ (byte)9, (byte)10, (byte)11, (byte)8,
+ (byte)13, (byte)14, (byte)15, (byte)12,
+ (byte)17, (byte)18, (byte)19, (byte)16,
+ (byte)21, (byte)22, (byte)23, (byte)20,
+ (byte)25, (byte)26, (byte)27, (byte)24,
+ (byte)29, (byte)30, (byte)31, (byte)28);
+
+ int sizeAligned = data.Length & ~31;
+
+ fixed (byte* pInput = data, pOutput = output)
+ {
+ for (uint i = 0; i < sizeAligned; i += 32)
+ {
+ var dataVec = Avx.LoadVector256(pInput + i);
+
+ dataVec = Avx2.Shuffle(dataVec, mask);
+
+ Avx.Store(pOutput + i, dataVec);
+ }
+ }
+
+ start = sizeAligned;
+ }
+ else if (Ssse3.IsSupported)
+ {
+ var mask = Vector128.Create(
+ (byte)1, (byte)2, (byte)3, (byte)0,
+ (byte)5, (byte)6, (byte)7, (byte)4,
+ (byte)9, (byte)10, (byte)11, (byte)8,
+ (byte)13, (byte)14, (byte)15, (byte)12);
+
+ int sizeAligned = data.Length & ~15;
+
+ fixed (byte* pInput = data, pOutput = output)
+ {
+ for (uint i = 0; i < sizeAligned; i += 16)
+ {
+ var dataVec = Sse2.LoadVector128(pInput + i);
+
+ dataVec = Ssse3.Shuffle(dataVec, mask);
+
+ Sse2.Store(pOutput + i, dataVec);
+ }
+ }
+
+ start = sizeAligned;
+ }
+
+ var outSpan = MemoryMarshal.Cast<byte, uint>(output);
+ var dataSpan = MemoryMarshal.Cast<byte, uint>(data);
+ for (int i = start / sizeof(uint); i < dataSpan.Length; i++)
+ {
+ outSpan[i] = BitOperations.RotateRight(dataSpan[i], 8);
+ }
+
+ return output;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs b/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs
new file mode 100644
index 00000000..4c8d7fef
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs
@@ -0,0 +1,14 @@
+using Ryujinx.Graphics.GAL;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ interface ITextureInfo
+ {
+ ITextureInfo Storage { get; }
+ int Handle { get; }
+ int FirstLayer => 0;
+ int FirstLevel => 0;
+
+ TextureCreateInfo Info { get; }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/IntermmediatePool.cs b/src/Ryujinx.Graphics.OpenGL/Image/IntermmediatePool.cs
new file mode 100644
index 00000000..4f167e89
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/IntermmediatePool.cs
@@ -0,0 +1,103 @@
+using Ryujinx.Graphics.GAL;
+using System;
+using System.Collections.Generic;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class IntermediatePool : IDisposable
+ {
+ private readonly OpenGLRenderer _renderer;
+ private readonly List<TextureView> _entries;
+
+ public IntermediatePool(OpenGLRenderer renderer)
+ {
+ _renderer = renderer;
+ _entries = new List<TextureView>();
+ }
+
+ public TextureView GetOrCreateWithAtLeast(
+ Target target,
+ int blockWidth,
+ int blockHeight,
+ int bytesPerPixel,
+ Format format,
+ int width,
+ int height,
+ int depth,
+ int levels,
+ int samples)
+ {
+ TextureView entry;
+
+ for (int i = 0; i < _entries.Count; i++)
+ {
+ entry = _entries[i];
+
+ if (entry.Target == target && entry.Format == format && entry.Info.Samples == samples)
+ {
+ if (entry.Width < width ||
+ entry.Height < height ||
+ entry.Info.Depth < depth ||
+ entry.Info.Levels < levels)
+ {
+ width = Math.Max(width, entry.Width);
+ height = Math.Max(height, entry.Height);
+ depth = Math.Max(depth, entry.Info.Depth);
+ levels = Math.Max(levels, entry.Info.Levels);
+
+ entry.Dispose();
+ entry = CreateNew(target, blockWidth, blockHeight, bytesPerPixel, format, width, height, depth, levels, samples);
+ _entries[i] = entry;
+ }
+
+ return entry;
+ }
+ }
+
+ entry = CreateNew(target, blockWidth, blockHeight, bytesPerPixel, format, width, height, depth, levels, samples);
+ _entries.Add(entry);
+
+ return entry;
+ }
+
+ private TextureView CreateNew(
+ Target target,
+ int blockWidth,
+ int blockHeight,
+ int bytesPerPixel,
+ Format format,
+ int width,
+ int height,
+ int depth,
+ int levels,
+ int samples)
+ {
+ return (TextureView)_renderer.CreateTexture(new TextureCreateInfo(
+ width,
+ height,
+ depth,
+ levels,
+ samples,
+ blockWidth,
+ blockHeight,
+ bytesPerPixel,
+ format,
+ DepthStencilMode.Depth,
+ target,
+ SwizzleComponent.Red,
+ SwizzleComponent.Green,
+ SwizzleComponent.Blue,
+ SwizzleComponent.Alpha), 1f);
+ }
+
+ public void Dispose()
+ {
+ foreach (TextureView entry in _entries)
+ {
+ entry.Dispose();
+ }
+
+ _entries.Clear();
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/Sampler.cs b/src/Ryujinx.Graphics.OpenGL/Image/Sampler.cs
new file mode 100644
index 00000000..f705aa3e
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/Sampler.cs
@@ -0,0 +1,64 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class Sampler : ISampler
+ {
+ public int Handle { get; private set; }
+
+ public Sampler(SamplerCreateInfo info)
+ {
+ Handle = GL.GenSampler();
+
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureMinFilter, (int)info.MinFilter.Convert());
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureMagFilter, (int)info.MagFilter.Convert());
+
+ if (HwCapabilities.SupportsSeamlessCubemapPerTexture)
+ {
+ GL.SamplerParameter(Handle, (SamplerParameterName)ArbSeamlessCubemapPerTexture.TextureCubeMapSeamless, info.SeamlessCubemap ? 1 : 0);
+ }
+
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureWrapS, (int)info.AddressU.Convert());
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureWrapT, (int)info.AddressV.Convert());
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureWrapR, (int)info.AddressP.Convert());
+
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureCompareMode, (int)info.CompareMode.Convert());
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureCompareFunc, (int)info.CompareOp.Convert());
+
+ unsafe
+ {
+ float* borderColor = stackalloc float[4]
+ {
+ info.BorderColor.Red,
+ info.BorderColor.Green,
+ info.BorderColor.Blue,
+ info.BorderColor.Alpha
+ };
+
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureBorderColor, borderColor);
+ }
+
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureMinLod, info.MinLod);
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureMaxLod, info.MaxLod);
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureLodBias, info.MipLodBias);
+
+ GL.SamplerParameter(Handle, SamplerParameterName.TextureMaxAnisotropyExt, info.MaxAnisotropy);
+ }
+
+ public void Bind(int unit)
+ {
+ GL.BindSampler(unit, Handle);
+ }
+
+ public void Dispose()
+ {
+ if (Handle != 0)
+ {
+ GL.DeleteSampler(Handle);
+
+ Handle = 0;
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureBase.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureBase.cs
new file mode 100644
index 00000000..2ab9dffb
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureBase.cs
@@ -0,0 +1,44 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureBase
+ {
+ public int Handle { get; protected set; }
+
+ public TextureCreateInfo Info { get; }
+
+ public int Width => Info.Width;
+ public int Height => Info.Height;
+ public float ScaleFactor { get; }
+
+ public Target Target => Info.Target;
+ public Format Format => Info.Format;
+
+ public TextureBase(TextureCreateInfo info, float scaleFactor = 1f)
+ {
+ Info = info;
+ ScaleFactor = scaleFactor;
+
+ Handle = GL.GenTexture();
+ }
+
+ public void Bind(int unit)
+ {
+ Bind(Target.Convert(), unit);
+ }
+
+ protected void Bind(TextureTarget target, int unit)
+ {
+ GL.ActiveTexture(TextureUnit.Texture0 + unit);
+ GL.BindTexture(target, Handle);
+ }
+
+ public static void ClearBinding(int unit)
+ {
+ GL.ActiveTexture(TextureUnit.Texture0 + unit);
+ GL.BindTextureUnit(unit, 0);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureBuffer.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureBuffer.cs
new file mode 100644
index 00000000..1e9e4d6b
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureBuffer.cs
@@ -0,0 +1,108 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Memory;
+using Ryujinx.Graphics.GAL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureBuffer : TextureBase, ITexture
+ {
+ private OpenGLRenderer _renderer;
+ private int _bufferOffset;
+ private int _bufferSize;
+ private int _bufferCount;
+
+ private BufferHandle _buffer;
+
+ public TextureBuffer(OpenGLRenderer renderer, TextureCreateInfo info) : base(info)
+ {
+ _renderer = renderer;
+ }
+
+ public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
+ {
+ throw new NotSupportedException();
+ }
+
+ public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
+ {
+ throw new NotSupportedException();
+ }
+
+ public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
+ {
+ throw new NotSupportedException();
+ }
+
+ public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
+ {
+ throw new NotSupportedException();
+ }
+
+ public PinnedSpan<byte> GetData()
+ {
+ return Buffer.GetData(_renderer, _buffer, _bufferOffset, _bufferSize);
+ }
+
+ public PinnedSpan<byte> GetData(int layer, int level)
+ {
+ return GetData();
+ }
+
+ public void SetData(SpanOrArray<byte> data)
+ {
+ var dataSpan = data.AsSpan();
+
+ Buffer.SetData(_buffer, _bufferOffset, dataSpan.Slice(0, Math.Min(dataSpan.Length, _bufferSize)));
+ }
+
+ public void SetData(SpanOrArray<byte> data, int layer, int level)
+ {
+ throw new NotSupportedException();
+ }
+
+ public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
+ {
+ throw new NotSupportedException();
+ }
+
+ public void SetStorage(BufferRange buffer)
+ {
+ if (_buffer != BufferHandle.Null &&
+ _buffer == buffer.Handle &&
+ buffer.Offset == _bufferOffset &&
+ buffer.Size == _bufferSize &&
+ _renderer.BufferCount == _bufferCount)
+ {
+ // Only rebind the buffer when more have been created.
+ return;
+ }
+
+ _buffer = buffer.Handle;
+ _bufferOffset = buffer.Offset;
+ _bufferSize = buffer.Size;
+ _bufferCount = _renderer.BufferCount;
+
+ Bind(0);
+
+ SizedInternalFormat format = (SizedInternalFormat)FormatTable.GetFormatInfo(Info.Format).PixelInternalFormat;
+
+ GL.TexBufferRange(TextureBufferTarget.TextureBuffer, format, _buffer.ToInt32(), (IntPtr)buffer.Offset, buffer.Size);
+ }
+
+ public void Dispose()
+ {
+ if (Handle != 0)
+ {
+ GL.DeleteTexture(Handle);
+
+ Handle = 0;
+ }
+ }
+
+ public void Release()
+ {
+ Dispose();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs
new file mode 100644
index 00000000..a4b08787
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs
@@ -0,0 +1,524 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common;
+using Ryujinx.Graphics.GAL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureCopy : IDisposable
+ {
+ private readonly OpenGLRenderer _renderer;
+
+ private int _srcFramebuffer;
+ private int _dstFramebuffer;
+
+ private int _copyPboHandle;
+ private int _copyPboSize;
+
+ public IntermediatePool IntermediatePool { get; }
+
+ public TextureCopy(OpenGLRenderer renderer)
+ {
+ _renderer = renderer;
+ IntermediatePool = new IntermediatePool(renderer);
+ }
+
+ public void Copy(
+ TextureView src,
+ TextureView dst,
+ Extents2D srcRegion,
+ Extents2D dstRegion,
+ bool linearFilter,
+ int srcLayer = 0,
+ int dstLayer = 0,
+ int srcLevel = 0,
+ int dstLevel = 0)
+ {
+ int levels = Math.Min(src.Info.Levels - srcLevel, dst.Info.Levels - dstLevel);
+ int layers = Math.Min(src.Info.GetLayers() - srcLayer, dst.Info.GetLayers() - dstLayer);
+
+ Copy(src, dst, srcRegion, dstRegion, linearFilter, srcLayer, dstLayer, srcLevel, dstLevel, layers, levels);
+ }
+
+ public void Copy(
+ TextureView src,
+ TextureView dst,
+ Extents2D srcRegion,
+ Extents2D dstRegion,
+ bool linearFilter,
+ int srcLayer,
+ int dstLayer,
+ int srcLevel,
+ int dstLevel,
+ int layers,
+ int levels)
+ {
+ TextureView srcConverted = src.Format.IsBgr() != dst.Format.IsBgr() ? BgraSwap(src) : src;
+
+ (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
+
+ GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
+ GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());
+
+ if (srcLevel != 0)
+ {
+ srcRegion = srcRegion.Reduce(srcLevel);
+ }
+
+ if (dstLevel != 0)
+ {
+ dstRegion = dstRegion.Reduce(dstLevel);
+ }
+
+ for (int level = 0; level < levels; level++)
+ {
+ for (int layer = 0; layer < layers; layer++)
+ {
+ if ((srcLayer | dstLayer) != 0 || layers > 1)
+ {
+ Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle, srcLevel + level, srcLayer + layer);
+ Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle, dstLevel + level, dstLayer + layer);
+ }
+ else
+ {
+ Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle, srcLevel + level);
+ Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle, dstLevel + level);
+ }
+
+ ClearBufferMask mask = GetMask(src.Format);
+
+ if ((mask & (ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit)) != 0 || src.Format.IsInteger())
+ {
+ linearFilter = false;
+ }
+
+ BlitFramebufferFilter filter = linearFilter
+ ? BlitFramebufferFilter.Linear
+ : BlitFramebufferFilter.Nearest;
+
+ GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
+ GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
+
+ GL.Disable(EnableCap.RasterizerDiscard);
+ GL.Disable(IndexedEnableCap.ScissorTest, 0);
+
+ GL.BlitFramebuffer(
+ srcRegion.X1,
+ srcRegion.Y1,
+ srcRegion.X2,
+ srcRegion.Y2,
+ dstRegion.X1,
+ dstRegion.Y1,
+ dstRegion.X2,
+ dstRegion.Y2,
+ mask,
+ filter);
+ }
+
+ if (level < levels - 1)
+ {
+ srcRegion = srcRegion.Reduce(1);
+ dstRegion = dstRegion.Reduce(1);
+ }
+ }
+
+ Attach(FramebufferTarget.ReadFramebuffer, src.Format, 0);
+ Attach(FramebufferTarget.DrawFramebuffer, dst.Format, 0);
+
+ GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
+ GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
+
+ ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
+ ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
+
+ if (srcConverted != src)
+ {
+ srcConverted.Dispose();
+ }
+ }
+
+ public void CopyUnscaled(
+ ITextureInfo src,
+ ITextureInfo dst,
+ int srcLayer,
+ int dstLayer,
+ int srcLevel,
+ int dstLevel)
+ {
+ TextureCreateInfo srcInfo = src.Info;
+ TextureCreateInfo dstInfo = dst.Info;
+
+ int srcDepth = srcInfo.GetDepthOrLayers();
+ int srcLevels = srcInfo.Levels;
+
+ int dstDepth = dstInfo.GetDepthOrLayers();
+ int dstLevels = dstInfo.Levels;
+
+ if (dstInfo.Target == Target.Texture3D)
+ {
+ dstDepth = Math.Max(1, dstDepth >> dstLevel);
+ }
+
+ int depth = Math.Min(srcDepth, dstDepth);
+ int levels = Math.Min(srcLevels, dstLevels);
+
+ CopyUnscaled(src, dst, srcLayer, dstLayer, srcLevel, dstLevel, depth, levels);
+ }
+
+ public void CopyUnscaled(
+ ITextureInfo src,
+ ITextureInfo dst,
+ int srcLayer,
+ int dstLayer,
+ int srcLevel,
+ int dstLevel,
+ int depth,
+ int levels)
+ {
+ TextureCreateInfo srcInfo = src.Info;
+ TextureCreateInfo dstInfo = dst.Info;
+
+ int srcHandle = src.Handle;
+ int dstHandle = dst.Handle;
+
+ int srcWidth = srcInfo.Width;
+ int srcHeight = srcInfo.Height;
+
+ int dstWidth = dstInfo.Width;
+ int dstHeight = dstInfo.Height;
+
+ srcWidth = Math.Max(1, srcWidth >> srcLevel);
+ srcHeight = Math.Max(1, srcHeight >> srcLevel);
+
+ dstWidth = Math.Max(1, dstWidth >> dstLevel);
+ dstHeight = Math.Max(1, dstHeight >> dstLevel);
+
+ int blockWidth = 1;
+ int blockHeight = 1;
+ bool sizeInBlocks = false;
+
+ // When copying from a compressed to a non-compressed format,
+ // the non-compressed texture will have the size of the texture
+ // in blocks (not in texels), so we must adjust that size to
+ // match the size in texels of the compressed texture.
+ if (!srcInfo.IsCompressed && dstInfo.IsCompressed)
+ {
+ srcWidth *= dstInfo.BlockWidth;
+ srcHeight *= dstInfo.BlockHeight;
+ blockWidth = dstInfo.BlockWidth;
+ blockHeight = dstInfo.BlockHeight;
+
+ sizeInBlocks = true;
+ }
+ else if (srcInfo.IsCompressed && !dstInfo.IsCompressed)
+ {
+ dstWidth *= srcInfo.BlockWidth;
+ dstHeight *= srcInfo.BlockHeight;
+ blockWidth = srcInfo.BlockWidth;
+ blockHeight = srcInfo.BlockHeight;
+ }
+
+ int width = Math.Min(srcWidth, dstWidth);
+ int height = Math.Min(srcHeight, dstHeight);
+
+ for (int level = 0; level < levels; level++)
+ {
+ // Stop copy if we are already out of the levels range.
+ if (level >= srcInfo.Levels || dstLevel + level >= dstInfo.Levels)
+ {
+ break;
+ }
+
+ if ((width % blockWidth != 0 || height % blockHeight != 0) && src is TextureView srcView && dst is TextureView dstView)
+ {
+ PboCopy(srcView, dstView, srcLayer, dstLayer, srcLevel + level, dstLevel + level, width, height);
+ }
+ else
+ {
+ int copyWidth = sizeInBlocks ? BitUtils.DivRoundUp(width, blockWidth) : width;
+ int copyHeight = sizeInBlocks ? BitUtils.DivRoundUp(height, blockHeight) : height;
+
+ if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows)
+ {
+ GL.CopyImageSubData(
+ src.Storage.Handle,
+ src.Storage.Info.Target.ConvertToImageTarget(),
+ src.FirstLevel + srcLevel + level,
+ 0,
+ 0,
+ src.FirstLayer + srcLayer,
+ dst.Storage.Handle,
+ dst.Storage.Info.Target.ConvertToImageTarget(),
+ dst.FirstLevel + dstLevel + level,
+ 0,
+ 0,
+ dst.FirstLayer + dstLayer,
+ copyWidth,
+ copyHeight,
+ depth);
+ }
+ else
+ {
+ GL.CopyImageSubData(
+ srcHandle,
+ srcInfo.Target.ConvertToImageTarget(),
+ srcLevel + level,
+ 0,
+ 0,
+ srcLayer,
+ dstHandle,
+ dstInfo.Target.ConvertToImageTarget(),
+ dstLevel + level,
+ 0,
+ 0,
+ dstLayer,
+ copyWidth,
+ copyHeight,
+ depth);
+ }
+ }
+
+ width = Math.Max(1, width >> 1);
+ height = Math.Max(1, height >> 1);
+
+ if (srcInfo.Target == Target.Texture3D)
+ {
+ depth = Math.Max(1, depth >> 1);
+ }
+ }
+ }
+
+ private static FramebufferAttachment AttachmentForFormat(Format format)
+ {
+ if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
+ {
+ return FramebufferAttachment.DepthStencilAttachment;
+ }
+ else if (IsDepthOnly(format))
+ {
+ return FramebufferAttachment.DepthAttachment;
+ }
+ else if (format == Format.S8Uint)
+ {
+ return FramebufferAttachment.StencilAttachment;
+ }
+ else
+ {
+ return FramebufferAttachment.ColorAttachment0;
+ }
+ }
+
+ private static void Attach(FramebufferTarget target, Format format, int handle, int level = 0)
+ {
+ FramebufferAttachment attachment = AttachmentForFormat(format);
+
+ GL.FramebufferTexture(target, attachment, handle, level);
+ }
+
+ private static void Attach(FramebufferTarget target, Format format, int handle, int level, int layer)
+ {
+ FramebufferAttachment attachment = AttachmentForFormat(format);
+
+ GL.FramebufferTextureLayer(target, attachment, handle, level, layer);
+ }
+
+ private static ClearBufferMask GetMask(Format format)
+ {
+ if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint || format == Format.S8UintD24Unorm)
+ {
+ return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
+ }
+ else if (IsDepthOnly(format))
+ {
+ return ClearBufferMask.DepthBufferBit;
+ }
+ else if (format == Format.S8Uint)
+ {
+ return ClearBufferMask.StencilBufferBit;
+ }
+ else
+ {
+ return ClearBufferMask.ColorBufferBit;
+ }
+ }
+
+ private static bool IsDepthOnly(Format format)
+ {
+ return format == Format.D16Unorm || format == Format.D32Float;
+ }
+
+ public TextureView BgraSwap(TextureView from)
+ {
+ TextureView to = (TextureView)_renderer.CreateTexture(from.Info, from.ScaleFactor);
+
+ EnsurePbo(from);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
+
+ from.WriteToPbo(0, forceBgra: true);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
+ GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
+
+ to.ReadFromPbo(0, _copyPboSize);
+
+ GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
+
+ return to;
+ }
+
+ private TextureView PboCopy(TextureView from, TextureView to, int srcLayer, int dstLayer, int srcLevel, int dstLevel, int width, int height)
+ {
+ int dstWidth = width;
+ int dstHeight = height;
+
+ // The size of the source texture.
+ int unpackWidth = from.Width;
+ int unpackHeight = from.Height;
+
+ if (from.Info.IsCompressed != to.Info.IsCompressed)
+ {
+ if (from.Info.IsCompressed)
+ {
+ // Dest size is in pixels, but should be in blocks
+ dstWidth = BitUtils.DivRoundUp(width, from.Info.BlockWidth);
+ dstHeight = BitUtils.DivRoundUp(height, from.Info.BlockHeight);
+
+ // When copying from a compressed texture, the source size must be taken in blocks for unpacking to the uncompressed block texture.
+ unpackWidth = BitUtils.DivRoundUp(from.Info.Width, from.Info.BlockWidth);
+ unpackHeight = BitUtils.DivRoundUp(from.Info.Height, from.Info.BlockHeight);
+ }
+ else
+ {
+ // When copying to a compressed texture, the source size must be scaled by the block width for unpacking on the compressed target.
+ unpackWidth = from.Info.Width * to.Info.BlockWidth;
+ unpackHeight = from.Info.Height * to.Info.BlockHeight;
+ }
+ }
+
+ EnsurePbo(from);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
+
+ // The source texture is written out in full, then the destination is taken as a slice from the data using unpack params.
+ // The offset points to the base at which the requested layer is at.
+
+ int offset = from.WriteToPbo2D(0, srcLayer, srcLevel);
+
+ // If the destination size is not an exact match for the source unpack parameters, we need to set them to slice the data correctly.
+
+ bool slice = (unpackWidth != dstWidth || unpackHeight != dstHeight);
+
+ if (slice)
+ {
+ // Set unpack parameters to take a slice of width/height:
+ GL.PixelStore(PixelStoreParameter.UnpackRowLength, unpackWidth);
+ GL.PixelStore(PixelStoreParameter.UnpackImageHeight, unpackHeight);
+
+ if (to.Info.IsCompressed)
+ {
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, to.Info.BlockWidth);
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, to.Info.BlockHeight);
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 1);
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, to.Info.BytesPerPixel);
+ }
+ }
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
+ GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
+
+ to.ReadFromPbo2D(offset, dstLayer, dstLevel, dstWidth, dstHeight);
+
+ if (slice)
+ {
+ // Reset unpack parameters
+ GL.PixelStore(PixelStoreParameter.UnpackRowLength, 0);
+ GL.PixelStore(PixelStoreParameter.UnpackImageHeight, 0);
+
+ if (to.Info.IsCompressed)
+ {
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, 0);
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, 0);
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 0);
+ GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, 0);
+ }
+ }
+
+ GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
+
+ return to;
+ }
+
+ private void EnsurePbo(TextureView view)
+ {
+ int requiredSize = 0;
+
+ for (int level = 0; level < view.Info.Levels; level++)
+ {
+ requiredSize += view.Info.GetMipSize(level);
+ }
+
+ if (_copyPboSize < requiredSize && _copyPboHandle != 0)
+ {
+ GL.DeleteBuffer(_copyPboHandle);
+
+ _copyPboHandle = 0;
+ }
+
+ if (_copyPboHandle == 0)
+ {
+ _copyPboHandle = GL.GenBuffer();
+ _copyPboSize = requiredSize;
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
+ GL.BufferData(BufferTarget.PixelPackBuffer, requiredSize, IntPtr.Zero, BufferUsageHint.DynamicCopy);
+ }
+ }
+
+ private int GetSrcFramebufferLazy()
+ {
+ if (_srcFramebuffer == 0)
+ {
+ _srcFramebuffer = GL.GenFramebuffer();
+ }
+
+ return _srcFramebuffer;
+ }
+
+ private int GetDstFramebufferLazy()
+ {
+ if (_dstFramebuffer == 0)
+ {
+ _dstFramebuffer = GL.GenFramebuffer();
+ }
+
+ return _dstFramebuffer;
+ }
+
+ public void Dispose()
+ {
+ if (_srcFramebuffer != 0)
+ {
+ GL.DeleteFramebuffer(_srcFramebuffer);
+
+ _srcFramebuffer = 0;
+ }
+
+ if (_dstFramebuffer != 0)
+ {
+ GL.DeleteFramebuffer(_dstFramebuffer);
+
+ _dstFramebuffer = 0;
+ }
+
+ if (_copyPboHandle != 0)
+ {
+ GL.DeleteBuffer(_copyPboHandle);
+
+ _copyPboHandle = 0;
+ }
+
+ IntermediatePool.Dispose();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs
new file mode 100644
index 00000000..c8fbfbc6
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs
@@ -0,0 +1,252 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Numerics;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureCopyIncompatible
+ {
+ private const string ComputeShaderShortening = @"#version 450 core
+
+layout (binding = 0, $SRC_FORMAT$) uniform uimage2D src;
+layout (binding = 1, $DST_FORMAT$) uniform uimage2D dst;
+
+layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
+
+void main()
+{
+ uvec2 coords = gl_GlobalInvocationID.xy;
+ ivec2 imageSz = imageSize(src);
+
+ if (int(coords.x) >= imageSz.x || int(coords.y) >= imageSz.y)
+ {
+ return;
+ }
+
+ uint coordsShifted = coords.x << $RATIO_LOG2$;
+
+ uvec2 dstCoords0 = uvec2(coordsShifted, coords.y);
+ uvec2 dstCoords1 = uvec2(coordsShifted + 1, coords.y);
+ uvec2 dstCoords2 = uvec2(coordsShifted + 2, coords.y);
+ uvec2 dstCoords3 = uvec2(coordsShifted + 3, coords.y);
+
+ uvec4 rgba = imageLoad(src, ivec2(coords));
+
+ imageStore(dst, ivec2(dstCoords0), rgba.rrrr);
+ imageStore(dst, ivec2(dstCoords1), rgba.gggg);
+ imageStore(dst, ivec2(dstCoords2), rgba.bbbb);
+ imageStore(dst, ivec2(dstCoords3), rgba.aaaa);
+}";
+
+ private const string ComputeShaderWidening = @"#version 450 core
+
+layout (binding = 0, $SRC_FORMAT$) uniform uimage2D src;
+layout (binding = 1, $DST_FORMAT$) uniform uimage2D dst;
+
+layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
+
+void main()
+{
+ uvec2 coords = gl_GlobalInvocationID.xy;
+ ivec2 imageSz = imageSize(dst);
+
+ if (int(coords.x) >= imageSz.x || int(coords.y) >= imageSz.y)
+ {
+ return;
+ }
+
+ uvec2 srcCoords = uvec2(coords.x << $RATIO_LOG2$, coords.y);
+
+ uint r = imageLoad(src, ivec2(srcCoords) + ivec2(0, 0)).r;
+ uint g = imageLoad(src, ivec2(srcCoords) + ivec2(1, 0)).r;
+ uint b = imageLoad(src, ivec2(srcCoords) + ivec2(2, 0)).r;
+ uint a = imageLoad(src, ivec2(srcCoords) + ivec2(3, 0)).r;
+
+ imageStore(dst, ivec2(coords), uvec4(r, g, b, a));
+}";
+
+ private readonly OpenGLRenderer _renderer;
+ private readonly Dictionary<int, int> _shorteningProgramHandles;
+ private readonly Dictionary<int, int> _wideningProgramHandles;
+
+ public TextureCopyIncompatible(OpenGLRenderer renderer)
+ {
+ _renderer = renderer;
+ _shorteningProgramHandles = new Dictionary<int, int>();
+ _wideningProgramHandles = new Dictionary<int, int>();
+ }
+
+ public void CopyIncompatibleFormats(ITextureInfo src, ITextureInfo dst, int srcLayer, int dstLayer, int srcLevel, int dstLevel, int depth, int levels)
+ {
+ TextureCreateInfo srcInfo = src.Info;
+ TextureCreateInfo dstInfo = dst.Info;
+
+ int srcBpp = src.Info.BytesPerPixel;
+ int dstBpp = dst.Info.BytesPerPixel;
+
+ // Calculate ideal component size, given our constraints:
+ // - Component size must not exceed bytes per pixel of source and destination image formats.
+ // - Maximum component size is 4 (R32).
+ int componentSize = Math.Min(Math.Min(srcBpp, dstBpp), 4);
+
+ int srcComponentsCount = srcBpp / componentSize;
+ int dstComponentsCount = dstBpp / componentSize;
+
+ var srcFormat = GetFormat(componentSize, srcComponentsCount);
+ var dstFormat = GetFormat(componentSize, dstComponentsCount);
+
+ GL.UseProgram(srcBpp < dstBpp
+ ? GetWideningShader(componentSize, srcComponentsCount, dstComponentsCount)
+ : GetShorteningShader(componentSize, srcComponentsCount, dstComponentsCount));
+
+ for (int l = 0; l < levels; l++)
+ {
+ int srcWidth = Math.Max(1, src.Info.Width >> l);
+ int srcHeight = Math.Max(1, src.Info.Height >> l);
+
+ int dstWidth = Math.Max(1, dst.Info.Width >> l);
+ int dstHeight = Math.Max(1, dst.Info.Height >> l);
+
+ int width = Math.Min(srcWidth, dstWidth);
+ int height = Math.Min(srcHeight, dstHeight);
+
+ for (int z = 0; z < depth; z++)
+ {
+ GL.BindImageTexture(0, src.Handle, srcLevel + l, false, srcLayer + z, TextureAccess.ReadOnly, srcFormat);
+ GL.BindImageTexture(1, dst.Handle, dstLevel + l, false, dstLayer + z, TextureAccess.WriteOnly, dstFormat);
+
+ GL.DispatchCompute((width + 31) / 32, (height + 31) / 32, 1);
+ }
+ }
+
+ Pipeline pipeline = (Pipeline)_renderer.Pipeline;
+
+ pipeline.RestoreProgram();
+ pipeline.RestoreImages1And2();
+ }
+
+ private static SizedInternalFormat GetFormat(int componentSize, int componentsCount)
+ {
+ if (componentSize == 1)
+ {
+ return componentsCount switch
+ {
+ 1 => SizedInternalFormat.R8ui,
+ 2 => SizedInternalFormat.Rg8ui,
+ 4 => SizedInternalFormat.Rgba8ui,
+ _ => throw new ArgumentException($"Invalid components count {componentsCount}.")
+ };
+ }
+ else if (componentSize == 2)
+ {
+ return componentsCount switch
+ {
+ 1 => SizedInternalFormat.R16ui,
+ 2 => SizedInternalFormat.Rg16ui,
+ 4 => SizedInternalFormat.Rgba16ui,
+ _ => throw new ArgumentException($"Invalid components count {componentsCount}.")
+ };
+ }
+ else if (componentSize == 4)
+ {
+ return componentsCount switch
+ {
+ 1 => SizedInternalFormat.R32ui,
+ 2 => SizedInternalFormat.Rg32ui,
+ 4 => SizedInternalFormat.Rgba32ui,
+ _ => throw new ArgumentException($"Invalid components count {componentsCount}.")
+ };
+ }
+ else
+ {
+ throw new ArgumentException($"Invalid component size {componentSize}.");
+ }
+ }
+
+ private int GetShorteningShader(int componentSize, int srcComponentsCount, int dstComponentsCount)
+ {
+ return GetShader(ComputeShaderShortening, _shorteningProgramHandles, componentSize, srcComponentsCount, dstComponentsCount);
+ }
+
+ private int GetWideningShader(int componentSize, int srcComponentsCount, int dstComponentsCount)
+ {
+ return GetShader(ComputeShaderWidening, _wideningProgramHandles, componentSize, srcComponentsCount, dstComponentsCount);
+ }
+
+ private int GetShader(
+ string code,
+ Dictionary<int, int> programHandles,
+ int componentSize,
+ int srcComponentsCount,
+ int dstComponentsCount)
+ {
+ int componentSizeLog2 = BitOperations.Log2((uint)componentSize);
+
+ int srcIndex = componentSizeLog2 + BitOperations.Log2((uint)srcComponentsCount) * 3;
+ int dstIndex = componentSizeLog2 + BitOperations.Log2((uint)dstComponentsCount) * 3;
+
+ int key = srcIndex | (dstIndex << 8);
+
+ if (!programHandles.TryGetValue(key, out int programHandle))
+ {
+ int csHandle = GL.CreateShader(ShaderType.ComputeShader);
+
+ string[] formatTable = new[] { "r8ui", "r16ui", "r32ui", "rg8ui", "rg16ui", "rg32ui", "rgba8ui", "rgba16ui", "rgba32ui" };
+
+ string srcFormat = formatTable[srcIndex];
+ string dstFormat = formatTable[dstIndex];
+
+ int srcBpp = srcComponentsCount * componentSize;
+ int dstBpp = dstComponentsCount * componentSize;
+
+ int ratio = srcBpp < dstBpp ? dstBpp / srcBpp : srcBpp / dstBpp;
+ int ratioLog2 = BitOperations.Log2((uint)ratio);
+
+ GL.ShaderSource(csHandle, code
+ .Replace("$SRC_FORMAT$", srcFormat)
+ .Replace("$DST_FORMAT$", dstFormat)
+ .Replace("$RATIO_LOG2$", ratioLog2.ToString(CultureInfo.InvariantCulture)));
+
+ GL.CompileShader(csHandle);
+
+ programHandle = GL.CreateProgram();
+
+ GL.AttachShader(programHandle, csHandle);
+ GL.LinkProgram(programHandle);
+ GL.DetachShader(programHandle, csHandle);
+ GL.DeleteShader(csHandle);
+
+ GL.GetProgram(programHandle, GetProgramParameterName.LinkStatus, out int status);
+
+ if (status == 0)
+ {
+ throw new Exception(GL.GetProgramInfoLog(programHandle));
+ }
+
+ programHandles.Add(key, programHandle);
+ }
+
+ return programHandle;
+ }
+
+ public void Dispose()
+ {
+ foreach (int handle in _shorteningProgramHandles.Values)
+ {
+ GL.DeleteProgram(handle);
+ }
+
+ _shorteningProgramHandles.Clear();
+
+ foreach (int handle in _wideningProgramHandles.Values)
+ {
+ GL.DeleteProgram(handle);
+ }
+
+ _wideningProgramHandles.Clear();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs
new file mode 100644
index 00000000..9963dc66
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs
@@ -0,0 +1,276 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+using System.Numerics;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureCopyMS
+ {
+ private const string ComputeShaderMSToNonMS = @"#version 450 core
+
+layout (binding = 0, $FORMAT$) uniform uimage2DMS imgIn;
+layout (binding = 1, $FORMAT$) uniform uimage2D imgOut;
+
+layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
+
+void main()
+{
+ uvec2 coords = gl_GlobalInvocationID.xy;
+ ivec2 imageSz = imageSize(imgOut);
+ if (int(coords.x) >= imageSz.x || int(coords.y) >= imageSz.y)
+ {
+ return;
+ }
+ int inSamples = imageSamples(imgIn);
+ int samplesInXLog2 = 0;
+ int samplesInYLog2 = 0;
+ switch (inSamples)
+ {
+ case 2:
+ samplesInXLog2 = 1;
+ break;
+ case 4:
+ samplesInXLog2 = 1;
+ samplesInYLog2 = 1;
+ break;
+ case 8:
+ samplesInXLog2 = 2;
+ samplesInYLog2 = 1;
+ break;
+ case 16:
+ samplesInXLog2 = 2;
+ samplesInYLog2 = 2;
+ break;
+ }
+ int samplesInX = 1 << samplesInXLog2;
+ int samplesInY = 1 << samplesInYLog2;
+ int sampleIdx = (int(coords.x) & (samplesInX - 1)) | ((int(coords.y) & (samplesInY - 1)) << samplesInXLog2);
+ uvec4 value = imageLoad(imgIn, ivec2(int(coords.x) >> samplesInXLog2, int(coords.y) >> samplesInYLog2), sampleIdx);
+ imageStore(imgOut, ivec2(coords), value);
+}";
+
+ private const string ComputeShaderNonMSToMS = @"#version 450 core
+
+layout (binding = 0, $FORMAT$) uniform uimage2D imgIn;
+layout (binding = 1, $FORMAT$) uniform uimage2DMS imgOut;
+
+layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in;
+
+void main()
+{
+ uvec2 coords = gl_GlobalInvocationID.xy;
+ ivec2 imageSz = imageSize(imgIn);
+ if (int(coords.x) >= imageSz.x || int(coords.y) >= imageSz.y)
+ {
+ return;
+ }
+ int outSamples = imageSamples(imgOut);
+ int samplesInXLog2 = 0;
+ int samplesInYLog2 = 0;
+ switch (outSamples)
+ {
+ case 2:
+ samplesInXLog2 = 1;
+ break;
+ case 4:
+ samplesInXLog2 = 1;
+ samplesInYLog2 = 1;
+ break;
+ case 8:
+ samplesInXLog2 = 2;
+ samplesInYLog2 = 1;
+ break;
+ case 16:
+ samplesInXLog2 = 2;
+ samplesInYLog2 = 2;
+ break;
+ }
+ int samplesInX = 1 << samplesInXLog2;
+ int samplesInY = 1 << samplesInYLog2;
+ int sampleIdx = (int(coords.x) & (samplesInX - 1)) | ((int(coords.y) & (samplesInY - 1)) << samplesInXLog2);
+ uvec4 value = imageLoad(imgIn, ivec2(coords));
+ imageStore(imgOut, ivec2(int(coords.x) >> samplesInXLog2, int(coords.y) >> samplesInYLog2), sampleIdx, value);
+}";
+
+ private readonly OpenGLRenderer _renderer;
+ private int[] _msToNonMSProgramHandles;
+ private int[] _nonMSToMSProgramHandles;
+
+ public TextureCopyMS(OpenGLRenderer renderer)
+ {
+ _renderer = renderer;
+ _msToNonMSProgramHandles = new int[5];
+ _nonMSToMSProgramHandles = new int[5];
+ }
+
+ public void CopyMSToNonMS(ITextureInfo src, ITextureInfo dst, int srcLayer, int dstLayer, int depth)
+ {
+ TextureCreateInfo srcInfo = src.Info;
+ TextureCreateInfo dstInfo = dst.Info;
+
+ int srcHandle = CreateViewIfNeeded(src);
+ int dstHandle = CreateViewIfNeeded(dst);
+
+ int dstWidth = dstInfo.Width;
+ int dstHeight = dstInfo.Height;
+
+ GL.UseProgram(GetMSToNonMSShader(srcInfo.BytesPerPixel));
+
+ for (int z = 0; z < depth; z++)
+ {
+ GL.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, TextureAccess.ReadOnly, GetFormat(srcInfo.BytesPerPixel));
+ GL.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, TextureAccess.WriteOnly, GetFormat(dstInfo.BytesPerPixel));
+
+ GL.DispatchCompute((dstWidth + 31) / 32, (dstHeight + 31) / 32, 1);
+ }
+
+ Pipeline pipeline = (Pipeline)_renderer.Pipeline;
+
+ pipeline.RestoreProgram();
+ pipeline.RestoreImages1And2();
+
+ DestroyViewIfNeeded(src, srcHandle);
+ DestroyViewIfNeeded(dst, dstHandle);
+ }
+
+ public void CopyNonMSToMS(ITextureInfo src, ITextureInfo dst, int srcLayer, int dstLayer, int depth)
+ {
+ TextureCreateInfo srcInfo = src.Info;
+ TextureCreateInfo dstInfo = dst.Info;
+
+ int srcHandle = CreateViewIfNeeded(src);
+ int dstHandle = CreateViewIfNeeded(dst);
+
+ int srcWidth = srcInfo.Width;
+ int srcHeight = srcInfo.Height;
+
+ GL.UseProgram(GetNonMSToMSShader(srcInfo.BytesPerPixel));
+
+ for (int z = 0; z < depth; z++)
+ {
+ GL.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, TextureAccess.ReadOnly, GetFormat(srcInfo.BytesPerPixel));
+ GL.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, TextureAccess.WriteOnly, GetFormat(dstInfo.BytesPerPixel));
+
+ GL.DispatchCompute((srcWidth + 31) / 32, (srcHeight + 31) / 32, 1);
+ }
+
+ Pipeline pipeline = (Pipeline)_renderer.Pipeline;
+
+ pipeline.RestoreProgram();
+ pipeline.RestoreImages1And2();
+
+ DestroyViewIfNeeded(src, srcHandle);
+ DestroyViewIfNeeded(dst, dstHandle);
+ }
+
+ private static SizedInternalFormat GetFormat(int bytesPerPixel)
+ {
+ return bytesPerPixel switch
+ {
+ 1 => SizedInternalFormat.R8ui,
+ 2 => SizedInternalFormat.R16ui,
+ 4 => SizedInternalFormat.R32ui,
+ 8 => SizedInternalFormat.Rg32ui,
+ 16 => SizedInternalFormat.Rgba32ui,
+ _ => throw new ArgumentException($"Invalid bytes per pixel {bytesPerPixel}.")
+ };
+ }
+
+ private static int CreateViewIfNeeded(ITextureInfo texture)
+ {
+ // Binding sRGB textures as images doesn't work on NVIDIA,
+ // we need to create and bind a RGBA view for it to work.
+ if (texture.Info.Format == Format.R8G8B8A8Srgb)
+ {
+ int handle = GL.GenTexture();
+
+ GL.TextureView(
+ handle,
+ texture.Info.Target.Convert(),
+ texture.Storage.Handle,
+ PixelInternalFormat.Rgba8,
+ texture.FirstLevel,
+ 1,
+ texture.FirstLayer,
+ texture.Info.GetLayers());
+
+ return handle;
+ }
+
+ return texture.Handle;
+ }
+
+ private static void DestroyViewIfNeeded(ITextureInfo info, int handle)
+ {
+ if (info.Handle != handle)
+ {
+ GL.DeleteTexture(handle);
+ }
+ }
+
+ private int GetMSToNonMSShader(int bytesPerPixel)
+ {
+ return GetShader(ComputeShaderMSToNonMS, _msToNonMSProgramHandles, bytesPerPixel);
+ }
+
+ private int GetNonMSToMSShader(int bytesPerPixel)
+ {
+ return GetShader(ComputeShaderNonMSToMS, _nonMSToMSProgramHandles, bytesPerPixel);
+ }
+
+ private int GetShader(string code, int[] programHandles, int bytesPerPixel)
+ {
+ int index = BitOperations.Log2((uint)bytesPerPixel);
+
+ if (programHandles[index] == 0)
+ {
+ int csHandle = GL.CreateShader(ShaderType.ComputeShader);
+
+ string format = new[] { "r8ui", "r16ui", "r32ui", "rg32ui", "rgba32ui" }[index];
+
+ GL.ShaderSource(csHandle, code.Replace("$FORMAT$", format));
+ GL.CompileShader(csHandle);
+
+ int programHandle = GL.CreateProgram();
+
+ GL.AttachShader(programHandle, csHandle);
+ GL.LinkProgram(programHandle);
+ GL.DetachShader(programHandle, csHandle);
+ GL.DeleteShader(csHandle);
+
+ GL.GetProgram(programHandle, GetProgramParameterName.LinkStatus, out int status);
+
+ if (status == 0)
+ {
+ throw new Exception(GL.GetProgramInfoLog(programHandle));
+ }
+
+ programHandles[index] = programHandle;
+ }
+
+ return programHandles[index];
+ }
+
+ public void Dispose()
+ {
+ for (int i = 0; i < _msToNonMSProgramHandles.Length; i++)
+ {
+ if (_msToNonMSProgramHandles[i] != 0)
+ {
+ GL.DeleteProgram(_msToNonMSProgramHandles[i]);
+ _msToNonMSProgramHandles[i] = 0;
+ }
+ }
+
+ for (int i = 0; i < _nonMSToMSProgramHandles.Length; i++)
+ {
+ if (_nonMSToMSProgramHandles[i] != 0)
+ {
+ GL.DeleteProgram(_nonMSToMSProgramHandles[i]);
+ _nonMSToMSProgramHandles[i] = 0;
+ }
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs
new file mode 100644
index 00000000..c058ac88
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs
@@ -0,0 +1,212 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using Ryujinx.Graphics.GAL;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureStorage : ITextureInfo
+ {
+ public ITextureInfo Storage => this;
+ public int Handle { get; private set; }
+ public float ScaleFactor { get; private set; }
+
+ public TextureCreateInfo Info { get; }
+
+ private readonly OpenGLRenderer _renderer;
+
+ private int _viewsCount;
+
+ internal ITexture DefaultView { get; private set; }
+
+ public TextureStorage(OpenGLRenderer renderer, TextureCreateInfo info, float scaleFactor)
+ {
+ _renderer = renderer;
+ Info = info;
+
+ Handle = GL.GenTexture();
+ ScaleFactor = scaleFactor;
+
+ CreateImmutableStorage();
+ }
+
+ private void CreateImmutableStorage()
+ {
+ TextureTarget target = Info.Target.Convert();
+
+ GL.ActiveTexture(TextureUnit.Texture0);
+
+ GL.BindTexture(target, Handle);
+
+ FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
+
+ SizedInternalFormat internalFormat;
+
+ if (format.IsCompressed)
+ {
+ internalFormat = (SizedInternalFormat)format.PixelFormat;
+ }
+ else
+ {
+ internalFormat = (SizedInternalFormat)format.PixelInternalFormat;
+ }
+
+ int levels = Info.GetLevelsClamped();
+
+ switch (Info.Target)
+ {
+ case Target.Texture1D:
+ GL.TexStorage1D(
+ TextureTarget1d.Texture1D,
+ levels,
+ internalFormat,
+ Info.Width);
+ break;
+
+ case Target.Texture1DArray:
+ GL.TexStorage2D(
+ TextureTarget2d.Texture1DArray,
+ levels,
+ internalFormat,
+ Info.Width,
+ Info.Height);
+ break;
+
+ case Target.Texture2D:
+ GL.TexStorage2D(
+ TextureTarget2d.Texture2D,
+ levels,
+ internalFormat,
+ Info.Width,
+ Info.Height);
+ break;
+
+ case Target.Texture2DArray:
+ GL.TexStorage3D(
+ TextureTarget3d.Texture2DArray,
+ levels,
+ internalFormat,
+ Info.Width,
+ Info.Height,
+ Info.Depth);
+ break;
+
+ case Target.Texture2DMultisample:
+ GL.TexStorage2DMultisample(
+ TextureTargetMultisample2d.Texture2DMultisample,
+ Info.Samples,
+ internalFormat,
+ Info.Width,
+ Info.Height,
+ true);
+ break;
+
+ case Target.Texture2DMultisampleArray:
+ GL.TexStorage3DMultisample(
+ TextureTargetMultisample3d.Texture2DMultisampleArray,
+ Info.Samples,
+ internalFormat,
+ Info.Width,
+ Info.Height,
+ Info.Depth,
+ true);
+ break;
+
+ case Target.Texture3D:
+ GL.TexStorage3D(
+ TextureTarget3d.Texture3D,
+ levels,
+ internalFormat,
+ Info.Width,
+ Info.Height,
+ Info.Depth);
+ break;
+
+ case Target.Cubemap:
+ GL.TexStorage2D(
+ TextureTarget2d.TextureCubeMap,
+ levels,
+ internalFormat,
+ Info.Width,
+ Info.Height);
+ break;
+
+ case Target.CubemapArray:
+ GL.TexStorage3D(
+ (TextureTarget3d)All.TextureCubeMapArray,
+ levels,
+ internalFormat,
+ Info.Width,
+ Info.Height,
+ Info.Depth);
+ break;
+
+ default:
+ Logger.Debug?.Print(LogClass.Gpu, $"Invalid or unsupported texture target: {target}.");
+ break;
+ }
+ }
+
+ public ITexture CreateDefaultView()
+ {
+ DefaultView = CreateView(Info, 0, 0);
+
+ return DefaultView;
+ }
+
+ public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
+ {
+ IncrementViewsCount();
+
+ return new TextureView(_renderer, this, info, firstLayer, firstLevel);
+ }
+
+ private void IncrementViewsCount()
+ {
+ _viewsCount++;
+ }
+
+ public void DecrementViewsCount()
+ {
+ // If we don't have any views, then the storage is now useless, delete it.
+ if (--_viewsCount == 0)
+ {
+ if (DefaultView == null)
+ {
+ Dispose();
+ }
+ else
+ {
+ // If the default view still exists, we can put it into the resource pool.
+ Release();
+ }
+ }
+ }
+
+ /// <summary>
+ /// Release the TextureStorage to the resource pool without disposing its handle.
+ /// </summary>
+ public void Release()
+ {
+ _viewsCount = 1; // When we are used again, we will have the default view.
+
+ _renderer.ResourcePool.AddTexture((TextureView)DefaultView);
+ }
+
+ public void DeleteDefault()
+ {
+ DefaultView = null;
+ }
+
+ public void Dispose()
+ {
+ DefaultView = null;
+
+ if (Handle != 0)
+ {
+ GL.DeleteTexture(Handle);
+
+ Handle = 0;
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureView.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureView.cs
new file mode 100644
index 00000000..804b3b03
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureView.cs
@@ -0,0 +1,867 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common;
+using Ryujinx.Common.Memory;
+using Ryujinx.Graphics.GAL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Image
+{
+ class TextureView : TextureBase, ITexture, ITextureInfo
+ {
+ private readonly OpenGLRenderer _renderer;
+
+ private readonly TextureStorage _parent;
+
+ public ITextureInfo Storage => _parent;
+
+ public int FirstLayer { get; private set; }
+ public int FirstLevel { get; private set; }
+
+ public TextureView(
+ OpenGLRenderer renderer,
+ TextureStorage parent,
+ TextureCreateInfo info,
+ int firstLayer,
+ int firstLevel) : base(info, parent.ScaleFactor)
+ {
+ _renderer = renderer;
+ _parent = parent;
+
+ FirstLayer = firstLayer;
+ FirstLevel = firstLevel;
+
+ CreateView();
+ }
+
+ private void CreateView()
+ {
+ TextureTarget target = Target.Convert();
+
+ FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
+
+ PixelInternalFormat pixelInternalFormat;
+
+ if (format.IsCompressed)
+ {
+ pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
+ }
+ else
+ {
+ pixelInternalFormat = format.PixelInternalFormat;
+ }
+
+ int levels = Info.GetLevelsClamped();
+
+ GL.TextureView(
+ Handle,
+ target,
+ _parent.Handle,
+ pixelInternalFormat,
+ FirstLevel,
+ levels,
+ FirstLayer,
+ Info.GetLayers());
+
+ GL.ActiveTexture(TextureUnit.Texture0);
+
+ GL.BindTexture(target, Handle);
+
+ int[] swizzleRgba = new int[]
+ {
+ (int)Info.SwizzleR.Convert(),
+ (int)Info.SwizzleG.Convert(),
+ (int)Info.SwizzleB.Convert(),
+ (int)Info.SwizzleA.Convert()
+ };
+
+ if (Info.Format == Format.A1B5G5R5Unorm)
+ {
+ int temp = swizzleRgba[0];
+ int temp2 = swizzleRgba[1];
+ swizzleRgba[0] = swizzleRgba[3];
+ swizzleRgba[1] = swizzleRgba[2];
+ swizzleRgba[2] = temp2;
+ swizzleRgba[3] = temp;
+ }
+ else if (Info.Format.IsBgr())
+ {
+ // Swap B <-> R for BGRA formats, as OpenGL has no support for them
+ // and we need to manually swap the components on read/write on the GPU.
+ int temp = swizzleRgba[0];
+ swizzleRgba[0] = swizzleRgba[2];
+ swizzleRgba[2] = temp;
+ }
+
+ GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);
+
+ int maxLevel = levels - 1;
+
+ if (maxLevel < 0)
+ {
+ maxLevel = 0;
+ }
+
+ GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);
+ GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)Info.DepthStencilMode.Convert());
+ }
+
+ public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
+ {
+ firstLayer += FirstLayer;
+ firstLevel += FirstLevel;
+
+ return _parent.CreateView(info, firstLayer, firstLevel);
+ }
+
+ public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
+ {
+ TextureView destinationView = (TextureView)destination;
+
+ bool srcIsMultisample = Target.IsMultisample();
+ bool dstIsMultisample = destinationView.Target.IsMultisample();
+
+ if (dstIsMultisample != srcIsMultisample && Info.Format.IsDepthOrStencil())
+ {
+ int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
+ CopyWithBlitForDepthMS(destinationView, 0, firstLayer, layers);
+ }
+ else if (!dstIsMultisample && srcIsMultisample)
+ {
+ int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
+ _renderer.TextureCopyMS.CopyMSToNonMS(this, destinationView, 0, firstLayer, layers);
+ }
+ else if (dstIsMultisample && !srcIsMultisample)
+ {
+ int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
+ _renderer.TextureCopyMS.CopyNonMSToMS(this, destinationView, 0, firstLayer, layers);
+ }
+ else if (destinationView.Info.BytesPerPixel != Info.BytesPerPixel)
+ {
+ int layers = Math.Min(Info.GetLayers(), destinationView.Info.GetLayers() - firstLayer);
+ int levels = Math.Min(Info.Levels, destinationView.Info.Levels - firstLevel);
+ _renderer.TextureCopyIncompatible.CopyIncompatibleFormats(this, destinationView, 0, firstLayer, 0, firstLevel, layers, levels);
+ }
+ else
+ {
+ _renderer.TextureCopy.CopyUnscaled(this, destinationView, 0, firstLayer, 0, firstLevel);
+ }
+ }
+
+ public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
+ {
+ TextureView destinationView = (TextureView)destination;
+
+ bool srcIsMultisample = Target.IsMultisample();
+ bool dstIsMultisample = destinationView.Target.IsMultisample();
+
+ if (dstIsMultisample != srcIsMultisample && Info.Format.IsDepthOrStencil())
+ {
+ CopyWithBlitForDepthMS(destinationView, srcLayer, dstLayer, 1);
+ }
+ else if (!dstIsMultisample && srcIsMultisample)
+ {
+ _renderer.TextureCopyMS.CopyMSToNonMS(this, destinationView, srcLayer, dstLayer, 1);
+ }
+ else if (dstIsMultisample && !srcIsMultisample)
+ {
+ _renderer.TextureCopyMS.CopyNonMSToMS(this, destinationView, srcLayer, dstLayer, 1);
+ }
+ else if (destinationView.Info.BytesPerPixel != Info.BytesPerPixel)
+ {
+ _renderer.TextureCopyIncompatible.CopyIncompatibleFormats(this, destinationView, srcLayer, dstLayer, srcLevel, dstLevel, 1, 1);
+ }
+ else
+ {
+ _renderer.TextureCopy.CopyUnscaled(this, destinationView, srcLayer, dstLayer, srcLevel, dstLevel, 1, 1);
+ }
+ }
+
+ private void CopyWithBlitForDepthMS(TextureView destinationView, int srcLayer, int dstLayer, int layers)
+ {
+ // This is currently used for multisample <-> non-multisample copies.
+ // We can't do that with compute because it's not possible to write depth textures on compute.
+ // It can be done with draws, but we don't have support for saving and restoring the OpenGL state
+ // for a draw with different state right now.
+ // This approach uses blit, which causes a resolution loss since some samples will be lost
+ // in the process.
+
+ Extents2D srcRegion = new Extents2D(0, 0, Width, Height);
+ Extents2D dstRegion = new Extents2D(0, 0, destinationView.Width, destinationView.Height);
+
+ if (destinationView.Target.IsMultisample())
+ {
+ TextureView intermmediate = _renderer.TextureCopy.IntermediatePool.GetOrCreateWithAtLeast(
+ Info.Target,
+ Info.BlockWidth,
+ Info.BlockHeight,
+ Info.BytesPerPixel,
+ Format,
+ destinationView.Width,
+ destinationView.Height,
+ Info.Depth,
+ 1,
+ 1);
+
+ _renderer.TextureCopy.Copy(this, intermmediate, srcRegion, dstRegion, false);
+ _renderer.TextureCopy.Copy(intermmediate, destinationView, dstRegion, dstRegion, false, srcLayer, dstLayer, 0, 0, layers, 1);
+ }
+ else
+ {
+ Target target = Target switch
+ {
+ Target.Texture2DMultisample => Target.Texture2D,
+ Target.Texture2DMultisampleArray => Target.Texture2DArray,
+ _ => Target
+ };
+
+ TextureView intermmediate = _renderer.TextureCopy.IntermediatePool.GetOrCreateWithAtLeast(
+ target,
+ Info.BlockWidth,
+ Info.BlockHeight,
+ Info.BytesPerPixel,
+ Format,
+ Width,
+ Height,
+ Info.Depth,
+ 1,
+ 1);
+
+ _renderer.TextureCopy.Copy(this, intermmediate, srcRegion, srcRegion, false);
+ _renderer.TextureCopy.Copy(intermmediate, destinationView, srcRegion, dstRegion, false, srcLayer, dstLayer, 0, 0, layers, 1);
+ }
+ }
+
+ public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
+ {
+ _renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
+ }
+
+ public unsafe PinnedSpan<byte> GetData()
+ {
+ int size = 0;
+ int levels = Info.GetLevelsClamped();
+
+ for (int level = 0; level < levels; level++)
+ {
+ size += Info.GetMipSize(level);
+ }
+
+ ReadOnlySpan<byte> data;
+
+ if (HwCapabilities.UsePersistentBufferForFlush)
+ {
+ data = _renderer.PersistentBuffers.Default.GetTextureData(this, size);
+ }
+ else
+ {
+ IntPtr target = _renderer.PersistentBuffers.Default.GetHostArray(size);
+
+ WriteTo(target);
+
+ data = new ReadOnlySpan<byte>(target.ToPointer(), size);
+ }
+
+ if (Format == Format.S8UintD24Unorm)
+ {
+ data = FormatConverter.ConvertD24S8ToS8D24(data);
+ }
+
+ return PinnedSpan<byte>.UnsafeFromSpan(data);
+ }
+
+ public unsafe PinnedSpan<byte> GetData(int layer, int level)
+ {
+ int size = Info.GetMipSize(level);
+
+ if (HwCapabilities.UsePersistentBufferForFlush)
+ {
+ return PinnedSpan<byte>.UnsafeFromSpan(_renderer.PersistentBuffers.Default.GetTextureData(this, size, layer, level));
+ }
+ else
+ {
+ IntPtr target = _renderer.PersistentBuffers.Default.GetHostArray(size);
+
+ int offset = WriteTo2D(target, layer, level);
+
+ return new PinnedSpan<byte>((byte*)target.ToPointer() + offset, size);
+ }
+ }
+
+ public void WriteToPbo(int offset, bool forceBgra)
+ {
+ WriteTo(IntPtr.Zero + offset, forceBgra);
+ }
+
+ public int WriteToPbo2D(int offset, int layer, int level)
+ {
+ return WriteTo2D(IntPtr.Zero + offset, layer, level);
+ }
+
+ private int WriteTo2D(IntPtr data, int layer, int level)
+ {
+ TextureTarget target = Target.Convert();
+
+ Bind(target, 0);
+
+ FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
+
+ PixelFormat pixelFormat = format.PixelFormat;
+ PixelType pixelType = format.PixelType;
+
+ if (target == TextureTarget.TextureCubeMap || target == TextureTarget.TextureCubeMapArray)
+ {
+ target = TextureTarget.TextureCubeMapPositiveX + (layer % 6);
+ }
+
+ int mipSize = Info.GetMipSize2D(level);
+
+ if (format.IsCompressed)
+ {
+ GL.GetCompressedTextureSubImage(Handle, level, 0, 0, layer, Math.Max(1, Info.Width >> level), Math.Max(1, Info.Height >> level), 1, mipSize, data);
+ }
+ else if (format.PixelFormat != PixelFormat.DepthStencil)
+ {
+ GL.GetTextureSubImage(Handle, level, 0, 0, layer, Math.Max(1, Info.Width >> level), Math.Max(1, Info.Height >> level), 1, pixelFormat, pixelType, mipSize, data);
+ }
+ else
+ {
+ GL.GetTexImage(target, level, pixelFormat, pixelType, data);
+
+ // The GL function returns all layers. Must return the offset of the layer we're interested in.
+ return target switch
+ {
+ TextureTarget.TextureCubeMapArray => (layer / 6) * mipSize,
+ TextureTarget.Texture1DArray => layer * mipSize,
+ TextureTarget.Texture2DArray => layer * mipSize,
+ _ => 0
+ };
+ }
+
+ return 0;
+ }
+
+ private void WriteTo(IntPtr data, bool forceBgra = false)
+ {
+ TextureTarget target = Target.Convert();
+
+ Bind(target, 0);
+
+ FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
+
+ PixelFormat pixelFormat = format.PixelFormat;
+ PixelType pixelType = format.PixelType;
+
+ if (forceBgra)
+ {
+ if (pixelType == PixelType.UnsignedShort565)
+ {
+ pixelType = PixelType.UnsignedShort565Reversed;
+ }
+ else if (pixelType == PixelType.UnsignedShort565Reversed)
+ {
+ pixelType = PixelType.UnsignedShort565;
+ }
+ else
+ {
+ pixelFormat = PixelFormat.Bgra;
+ }
+ }
+
+ int faces = 1;
+
+ if (target == TextureTarget.TextureCubeMap)
+ {
+ target = TextureTarget.TextureCubeMapPositiveX;
+
+ faces = 6;
+ }
+
+ int levels = Info.GetLevelsClamped();
+
+ for (int level = 0; level < levels; level++)
+ {
+ for (int face = 0; face < faces; face++)
+ {
+ int faceOffset = face * Info.GetMipSize2D(level);
+
+ if (format.IsCompressed)
+ {
+ GL.GetCompressedTexImage(target + face, level, data + faceOffset);
+ }
+ else
+ {
+ GL.GetTexImage(target + face, level, pixelFormat, pixelType, data + faceOffset);
+ }
+ }
+
+ data += Info.GetMipSize(level);
+ }
+ }
+
+ public void SetData(SpanOrArray<byte> data)
+ {
+ var dataSpan = data.AsSpan();
+
+ if (Format == Format.S8UintD24Unorm)
+ {
+ dataSpan = FormatConverter.ConvertS8D24ToD24S8(dataSpan);
+ }
+
+ unsafe
+ {
+ fixed (byte* ptr = dataSpan)
+ {
+ ReadFrom((IntPtr)ptr, dataSpan.Length);
+ }
+ }
+ }
+
+ public void SetData(SpanOrArray<byte> data, int layer, int level)
+ {
+ var dataSpan = data.AsSpan();
+
+ if (Format == Format.S8UintD24Unorm)
+ {
+ dataSpan = FormatConverter.ConvertS8D24ToD24S8(dataSpan);
+ }
+
+ unsafe
+ {
+ fixed (byte* ptr = dataSpan)
+ {
+ int width = Math.Max(Info.Width >> level, 1);
+ int height = Math.Max(Info.Height >> level, 1);
+
+ ReadFrom2D((IntPtr)ptr, layer, level, 0, 0, width, height);
+ }
+ }
+ }
+
+ public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
+ {
+ var dataSpan = data.AsSpan();
+
+ if (Format == Format.S8UintD24Unorm)
+ {
+ dataSpan = FormatConverter.ConvertS8D24ToD24S8(dataSpan);
+ }
+
+ int wInBlocks = BitUtils.DivRoundUp(region.Width, Info.BlockWidth);
+ int hInBlocks = BitUtils.DivRoundUp(region.Height, Info.BlockHeight);
+
+ unsafe
+ {
+ fixed (byte* ptr = dataSpan)
+ {
+ ReadFrom2D(
+ (IntPtr)ptr,
+ layer,
+ level,
+ region.X,
+ region.Y,
+ region.Width,
+ region.Height,
+ BitUtils.AlignUp(wInBlocks * Info.BytesPerPixel, 4) * hInBlocks);
+ }
+ }
+ }
+
+ public void ReadFromPbo(int offset, int size)
+ {
+ ReadFrom(IntPtr.Zero + offset, size);
+ }
+
+ public void ReadFromPbo2D(int offset, int layer, int level, int width, int height)
+ {
+ ReadFrom2D(IntPtr.Zero + offset, layer, level, 0, 0, width, height);
+ }
+
+ private void ReadFrom2D(IntPtr data, int layer, int level, int x, int y, int width, int height)
+ {
+ int mipSize = Info.GetMipSize2D(level);
+
+ ReadFrom2D(data, layer, level, x, y, width, height, mipSize);
+ }
+
+ private void ReadFrom2D(IntPtr data, int layer, int level, int x, int y, int width, int height, int mipSize)
+ {
+ TextureTarget target = Target.Convert();
+
+ Bind(target, 0);
+
+ FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
+
+ switch (Target)
+ {
+ case Target.Texture1D:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage1D(
+ target,
+ level,
+ x,
+ width,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage1D(
+ target,
+ level,
+ x,
+ width,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Texture1DArray:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage2D(
+ target,
+ level,
+ x,
+ layer,
+ width,
+ 1,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage2D(
+ target,
+ level,
+ x,
+ layer,
+ width,
+ 1,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Texture2D:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage2D(
+ target,
+ level,
+ x,
+ y,
+ width,
+ height,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage2D(
+ target,
+ level,
+ x,
+ y,
+ width,
+ height,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Texture2DArray:
+ case Target.Texture3D:
+ case Target.CubemapArray:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage3D(
+ target,
+ level,
+ x,
+ y,
+ layer,
+ width,
+ height,
+ 1,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage3D(
+ target,
+ level,
+ x,
+ y,
+ layer,
+ width,
+ height,
+ 1,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Cubemap:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage2D(
+ TextureTarget.TextureCubeMapPositiveX + layer,
+ level,
+ x,
+ y,
+ width,
+ height,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage2D(
+ TextureTarget.TextureCubeMapPositiveX + layer,
+ level,
+ x,
+ y,
+ width,
+ height,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+ }
+ }
+
+ private void ReadFrom(IntPtr data, int size)
+ {
+ TextureTarget target = Target.Convert();
+ int baseLevel = 0;
+
+ // glTexSubImage on cubemap views is broken on Intel, we have to use the storage instead.
+ if (Target == Target.Cubemap && HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows)
+ {
+ GL.ActiveTexture(TextureUnit.Texture0);
+ GL.BindTexture(target, Storage.Handle);
+ baseLevel = FirstLevel;
+ }
+ else
+ {
+ Bind(target, 0);
+ }
+
+ FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
+
+ int width = Info.Width;
+ int height = Info.Height;
+ int depth = Info.Depth;
+ int levels = Info.GetLevelsClamped();
+
+ int offset = 0;
+
+ for (int level = 0; level < levels; level++)
+ {
+ int mipSize = Info.GetMipSize(level);
+
+ int endOffset = offset + mipSize;
+
+ if ((uint)endOffset > (uint)size)
+ {
+ return;
+ }
+
+ switch (Target)
+ {
+ case Target.Texture1D:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage1D(
+ target,
+ level,
+ 0,
+ width,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage1D(
+ target,
+ level,
+ 0,
+ width,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Texture1DArray:
+ case Target.Texture2D:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage2D(
+ target,
+ level,
+ 0,
+ 0,
+ width,
+ height,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage2D(
+ target,
+ level,
+ 0,
+ 0,
+ width,
+ height,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Texture2DArray:
+ case Target.Texture3D:
+ case Target.CubemapArray:
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage3D(
+ target,
+ level,
+ 0,
+ 0,
+ 0,
+ width,
+ height,
+ depth,
+ format.PixelFormat,
+ mipSize,
+ data);
+ }
+ else
+ {
+ GL.TexSubImage3D(
+ target,
+ level,
+ 0,
+ 0,
+ 0,
+ width,
+ height,
+ depth,
+ format.PixelFormat,
+ format.PixelType,
+ data);
+ }
+ break;
+
+ case Target.Cubemap:
+ int faceOffset = 0;
+
+ for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
+ {
+ if (format.IsCompressed)
+ {
+ GL.CompressedTexSubImage2D(
+ TextureTarget.TextureCubeMapPositiveX + face,
+ baseLevel + level,
+ 0,
+ 0,
+ width,
+ height,
+ format.PixelFormat,
+ mipSize / 6,
+ data + faceOffset);
+ }
+ else
+ {
+ GL.TexSubImage2D(
+ TextureTarget.TextureCubeMapPositiveX + face,
+ baseLevel + level,
+ 0,
+ 0,
+ width,
+ height,
+ format.PixelFormat,
+ format.PixelType,
+ data + faceOffset);
+ }
+ }
+ break;
+ }
+
+ data += mipSize;
+ offset += mipSize;
+
+ width = Math.Max(1, width >> 1);
+ height = Math.Max(1, height >> 1);
+
+ if (Target == Target.Texture3D)
+ {
+ depth = Math.Max(1, depth >> 1);
+ }
+ }
+ }
+
+ public void SetStorage(BufferRange buffer)
+ {
+ throw new NotSupportedException();
+ }
+
+ private void DisposeHandles()
+ {
+ if (Handle != 0)
+ {
+ GL.DeleteTexture(Handle);
+
+ Handle = 0;
+ }
+ }
+
+ /// <summary>
+ /// Release the view without necessarily disposing the parent if we are the default view.
+ /// This allows it to be added to the resource pool and reused later.
+ /// </summary>
+ public void Release()
+ {
+ bool hadHandle = Handle != 0;
+
+ if (_parent.DefaultView != this)
+ {
+ DisposeHandles();
+ }
+
+ if (hadHandle)
+ {
+ _parent.DecrementViewsCount();
+ }
+ }
+
+ public void Dispose()
+ {
+ if (_parent.DefaultView == this)
+ {
+ // Remove the default view (us), so that the texture cannot be released to the cache.
+ _parent.DeleteDefault();
+ }
+
+ Release();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/OpenGLRenderer.cs b/src/Ryujinx.Graphics.OpenGL/OpenGLRenderer.cs
new file mode 100644
index 00000000..3903b4d4
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/OpenGLRenderer.cs
@@ -0,0 +1,276 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Configuration;
+using Ryujinx.Common.Logging;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using Ryujinx.Graphics.OpenGL.Queries;
+using Ryujinx.Graphics.Shader.Translation;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ public sealed class OpenGLRenderer : IRenderer
+ {
+ private readonly Pipeline _pipeline;
+
+ public IPipeline Pipeline => _pipeline;
+
+ private readonly Counters _counters;
+
+ private readonly Window _window;
+
+ public IWindow Window => _window;
+
+ private TextureCopy _textureCopy;
+ private TextureCopy _backgroundTextureCopy;
+ internal TextureCopy TextureCopy => BackgroundContextWorker.InBackground ? _backgroundTextureCopy : _textureCopy;
+ internal TextureCopyIncompatible TextureCopyIncompatible { get; }
+ internal TextureCopyMS TextureCopyMS { get; }
+
+ private Sync _sync;
+
+ public event EventHandler<ScreenCaptureImageInfo> ScreenCaptured;
+
+ internal PersistentBuffers PersistentBuffers { get; }
+
+ internal ResourcePool ResourcePool { get; }
+
+ internal int BufferCount { get; private set; }
+
+ public string GpuVendor { get; private set; }
+ public string GpuRenderer { get; private set; }
+ public string GpuVersion { get; private set; }
+
+ public bool PreferThreading => true;
+
+ public OpenGLRenderer()
+ {
+ _pipeline = new Pipeline();
+ _counters = new Counters();
+ _window = new Window(this);
+ _textureCopy = new TextureCopy(this);
+ _backgroundTextureCopy = new TextureCopy(this);
+ TextureCopyIncompatible = new TextureCopyIncompatible(this);
+ TextureCopyMS = new TextureCopyMS(this);
+ _sync = new Sync();
+ PersistentBuffers = new PersistentBuffers();
+ ResourcePool = new ResourcePool();
+ }
+
+ public BufferHandle CreateBuffer(int size, BufferHandle storageHint)
+ {
+ BufferCount++;
+
+ return Buffer.Create(size);
+ }
+
+ public IProgram CreateProgram(ShaderSource[] shaders, ShaderInfo info)
+ {
+ return new Program(shaders, info.FragmentOutputMap);
+ }
+
+ public ISampler CreateSampler(SamplerCreateInfo info)
+ {
+ return new Sampler(info);
+ }
+
+ public ITexture CreateTexture(TextureCreateInfo info, float scaleFactor)
+ {
+ if (info.Target == Target.TextureBuffer)
+ {
+ return new TextureBuffer(this, info);
+ }
+ else
+ {
+ return ResourcePool.GetTextureOrNull(info, scaleFactor) ?? new TextureStorage(this, info, scaleFactor).CreateDefaultView();
+ }
+ }
+
+ public void DeleteBuffer(BufferHandle buffer)
+ {
+ Buffer.Delete(buffer);
+ }
+
+ public HardwareInfo GetHardwareInfo()
+ {
+ return new HardwareInfo(GpuVendor, GpuRenderer);
+ }
+
+ public PinnedSpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
+ {
+ return Buffer.GetData(this, buffer, offset, size);
+ }
+
+ public Capabilities GetCapabilities()
+ {
+ bool intelWindows = HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows;
+ bool amdWindows = HwCapabilities.Vendor == HwCapabilities.GpuVendor.AmdWindows;
+
+ return new Capabilities(
+ api: TargetApi.OpenGL,
+ vendorName: GpuVendor,
+ hasFrontFacingBug: intelWindows,
+ hasVectorIndexingBug: amdWindows,
+ needsFragmentOutputSpecialization: false,
+ reduceShaderPrecision: false,
+ supportsAstcCompression: HwCapabilities.SupportsAstcCompression,
+ supportsBc123Compression: HwCapabilities.SupportsTextureCompressionS3tc,
+ supportsBc45Compression: HwCapabilities.SupportsTextureCompressionRgtc,
+ supportsBc67Compression: true, // Should check BPTC extension, but for some reason NVIDIA is not exposing the extension.
+ supportsEtc2Compression: true,
+ supports3DTextureCompression: false,
+ supportsBgraFormat: false,
+ supportsR4G4Format: false,
+ supportsR4G4B4A4Format: true,
+ supportsSnormBufferTextureFormat: false,
+ supports5BitComponentFormat: true,
+ supportsBlendEquationAdvanced: HwCapabilities.SupportsBlendEquationAdvanced,
+ supportsFragmentShaderInterlock: HwCapabilities.SupportsFragmentShaderInterlock,
+ supportsFragmentShaderOrderingIntel: HwCapabilities.SupportsFragmentShaderOrdering,
+ supportsGeometryShader: true,
+ supportsGeometryShaderPassthrough: HwCapabilities.SupportsGeometryShaderPassthrough,
+ supportsImageLoadFormatted: HwCapabilities.SupportsImageLoadFormatted,
+ supportsLayerVertexTessellation: HwCapabilities.SupportsShaderViewportLayerArray,
+ supportsMismatchingViewFormat: HwCapabilities.SupportsMismatchingViewFormat,
+ supportsCubemapView: true,
+ supportsNonConstantTextureOffset: HwCapabilities.SupportsNonConstantTextureOffset,
+ supportsShaderBallot: HwCapabilities.SupportsShaderBallot,
+ supportsTextureShadowLod: HwCapabilities.SupportsTextureShadowLod,
+ supportsViewportIndexVertexTessellation: HwCapabilities.SupportsShaderViewportLayerArray,
+ supportsViewportMask: HwCapabilities.SupportsViewportArray2,
+ supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
+ supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
+ maximumUniformBuffersPerStage: 13, // TODO: Avoid hardcoding those limits here and get from driver?
+ maximumStorageBuffersPerStage: 16,
+ maximumTexturesPerStage: 32,
+ maximumImagesPerStage: 8,
+ maximumComputeSharedMemorySize: HwCapabilities.MaximumComputeSharedMemorySize,
+ maximumSupportedAnisotropy: HwCapabilities.MaximumSupportedAnisotropy,
+ storageBufferOffsetAlignment: HwCapabilities.StorageBufferOffsetAlignment,
+ gatherBiasPrecision: intelWindows || amdWindows ? 8 : 0); // Precision is 8 for these vendors on Vulkan.
+ }
+
+ public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
+ {
+ Buffer.SetData(buffer, offset, data);
+ }
+
+ public void UpdateCounters()
+ {
+ _counters.Update();
+ }
+
+ public void PreFrame()
+ {
+ _sync.Cleanup();
+ ResourcePool.Tick();
+ }
+
+ public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
+ {
+ return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount, hostReserved);
+ }
+
+ public void Initialize(GraphicsDebugLevel glLogLevel)
+ {
+ Debugger.Initialize(glLogLevel);
+
+ PrintGpuInformation();
+
+ if (HwCapabilities.SupportsParallelShaderCompile)
+ {
+ GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
+ }
+
+ _pipeline.Initialize(this);
+ _counters.Initialize(_pipeline);
+
+ // This is required to disable [0, 1] clamping for SNorm outputs on compatibility profiles.
+ // This call is expected to fail if we're running with a core profile,
+ // as this clamp target was deprecated, but that's fine as a core profile
+ // should already have the desired behaviour were outputs are not clamped.
+ GL.ClampColor(ClampColorTarget.ClampFragmentColor, ClampColorMode.False);
+ }
+
+ private void PrintGpuInformation()
+ {
+ GpuVendor = GL.GetString(StringName.Vendor);
+ GpuRenderer = GL.GetString(StringName.Renderer);
+ GpuVersion = GL.GetString(StringName.Version);
+
+ Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
+ }
+
+ public void ResetCounter(CounterType type)
+ {
+ _counters.QueueReset(type);
+ }
+
+ public void BackgroundContextAction(Action action, bool alwaysBackground = false)
+ {
+ // alwaysBackground is ignored, since we cannot switch from the current context.
+
+ if (IOpenGLContext.HasContext())
+ {
+ action(); // We have a context already - use that (assuming it is the main one).
+ }
+ else
+ {
+ _window.BackgroundContext.Invoke(action);
+ }
+ }
+
+ public void InitializeBackgroundContext(IOpenGLContext baseContext)
+ {
+ _window.InitializeBackgroundContext(baseContext);
+ }
+
+ public void Dispose()
+ {
+ _textureCopy.Dispose();
+ _backgroundTextureCopy.Dispose();
+ TextureCopyMS.Dispose();
+ PersistentBuffers.Dispose();
+ ResourcePool.Dispose();
+ _pipeline.Dispose();
+ _window.Dispose();
+ _counters.Dispose();
+ _sync.Dispose();
+ }
+
+ public IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info)
+ {
+ return new Program(programBinary, hasFragmentShader, info.FragmentOutputMap);
+ }
+
+ public void CreateSync(ulong id, bool strict)
+ {
+ _sync.Create(id);
+ }
+
+ public void WaitSync(ulong id)
+ {
+ _sync.Wait(id);
+ }
+
+ public ulong GetCurrentSync()
+ {
+ return _sync.GetCurrent();
+ }
+
+ public void SetInterruptAction(Action<Action> interruptAction)
+ {
+ // Currently no need for an interrupt action.
+ }
+
+ public void Screenshot()
+ {
+ _window.ScreenCaptureRequested = true;
+ }
+
+ public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
+ {
+ ScreenCaptured?.Invoke(this, bitmap);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/PersistentBuffers.cs b/src/Ryujinx.Graphics.OpenGL/PersistentBuffers.cs
new file mode 100644
index 00000000..654e25b9
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/PersistentBuffers.cs
@@ -0,0 +1,136 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class PersistentBuffers : IDisposable
+ {
+ private PersistentBuffer _main = new PersistentBuffer();
+ private PersistentBuffer _background = new PersistentBuffer();
+
+ public PersistentBuffer Default => BackgroundContextWorker.InBackground ? _background : _main;
+
+ public void Dispose()
+ {
+ _main?.Dispose();
+ _background?.Dispose();
+ }
+ }
+
+ class PersistentBuffer : IDisposable
+ {
+ private IntPtr _bufferMap;
+ private int _copyBufferHandle;
+ private int _copyBufferSize;
+
+ private byte[] _data;
+ private IntPtr _dataMap;
+
+ private void EnsureBuffer(int requiredSize)
+ {
+ if (_copyBufferSize < requiredSize && _copyBufferHandle != 0)
+ {
+ GL.DeleteBuffer(_copyBufferHandle);
+
+ _copyBufferHandle = 0;
+ }
+
+ if (_copyBufferHandle == 0)
+ {
+ _copyBufferHandle = GL.GenBuffer();
+ _copyBufferSize = requiredSize;
+
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, _copyBufferHandle);
+ GL.BufferStorage(BufferTarget.CopyWriteBuffer, requiredSize, IntPtr.Zero, BufferStorageFlags.MapReadBit | BufferStorageFlags.MapPersistentBit);
+
+ _bufferMap = GL.MapBufferRange(BufferTarget.CopyWriteBuffer, IntPtr.Zero, requiredSize, BufferAccessMask.MapReadBit | BufferAccessMask.MapPersistentBit);
+ }
+ }
+
+ public unsafe IntPtr GetHostArray(int requiredSize)
+ {
+ if (_data == null || _data.Length < requiredSize)
+ {
+ _data = GC.AllocateUninitializedArray<byte>(requiredSize, true);
+
+ _dataMap = (IntPtr)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(_data));
+ }
+
+ return _dataMap;
+ }
+
+ private void Sync()
+ {
+ GL.MemoryBarrier(MemoryBarrierFlags.ClientMappedBufferBarrierBit);
+
+ IntPtr sync = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
+ WaitSyncStatus syncResult = GL.ClientWaitSync(sync, ClientWaitSyncFlags.SyncFlushCommandsBit, 1000000000);
+
+ if (syncResult == WaitSyncStatus.TimeoutExpired)
+ {
+ Logger.Error?.PrintMsg(LogClass.Gpu, $"Failed to sync persistent buffer state within 1000ms. Continuing...");
+ }
+
+ GL.DeleteSync(sync);
+ }
+
+ public unsafe ReadOnlySpan<byte> GetTextureData(TextureView view, int size)
+ {
+ EnsureBuffer(size);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyBufferHandle);
+
+ view.WriteToPbo(0, false);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
+
+ Sync();
+
+ return new ReadOnlySpan<byte>(_bufferMap.ToPointer(), size);
+ }
+
+ public unsafe ReadOnlySpan<byte> GetTextureData(TextureView view, int size, int layer, int level)
+ {
+ EnsureBuffer(size);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyBufferHandle);
+
+ int offset = view.WriteToPbo2D(0, layer, level);
+
+ GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
+
+ Sync();
+
+ return new ReadOnlySpan<byte>(_bufferMap.ToPointer(), size).Slice(offset);
+ }
+
+ public unsafe ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
+ {
+ EnsureBuffer(size);
+
+ GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, _copyBufferHandle);
+
+ GL.CopyBufferSubData(BufferTarget.CopyReadBuffer, BufferTarget.CopyWriteBuffer, (IntPtr)offset, IntPtr.Zero, size);
+
+ GL.BindBuffer(BufferTarget.CopyWriteBuffer, 0);
+
+ Sync();
+
+ return new ReadOnlySpan<byte>(_bufferMap.ToPointer(), size);
+ }
+
+ public void Dispose()
+ {
+ if (_copyBufferHandle != 0)
+ {
+ GL.DeleteBuffer(_copyBufferHandle);
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Pipeline.cs b/src/Ryujinx.Graphics.OpenGL/Pipeline.cs
new file mode 100644
index 00000000..6b6d0289
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Pipeline.cs
@@ -0,0 +1,1770 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using Ryujinx.Graphics.OpenGL.Queries;
+using Ryujinx.Graphics.Shader;
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class Pipeline : IPipeline, IDisposable
+ {
+ private const int SavedImages = 2;
+
+ private readonly DrawTextureEmulation _drawTexture;
+
+ internal ulong DrawCount { get; private set; }
+
+ private Program _program;
+
+ private bool _rasterizerDiscard;
+
+ private VertexArray _vertexArray;
+ private Framebuffer _framebuffer;
+
+ private IntPtr _indexBaseOffset;
+
+ private DrawElementsType _elementsType;
+
+ private PrimitiveType _primitiveType;
+
+ 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;
+
+ private CounterQueueEvent _activeConditionalRender;
+
+ private Vector4<int>[] _fpIsBgra = new Vector4<int>[SupportBuffer.FragmentIsBgraCount];
+ private Vector4<float>[] _renderScale = new Vector4<float>[73];
+ private int _fragmentScaleCount;
+
+ private (TextureBase, Format)[] _images;
+ private TextureBase _unit0Texture;
+ private Sampler _unit0Sampler;
+
+ private FrontFaceDirection _frontFace;
+ private ClipOrigin _clipOrigin;
+ private ClipDepthMode _clipDepthMode;
+
+ private uint _fragmentOutputMap;
+ private uint _componentMasks;
+ private uint _currentComponentMasks;
+ private bool _advancedBlendEnable;
+
+ private uint _scissorEnables;
+
+ private bool _tfEnabled;
+ private TransformFeedbackPrimitiveType _tfTopology;
+
+ private SupportBufferUpdater _supportBuffer;
+ private readonly BufferHandle[] _tfbs;
+ private readonly BufferRange[] _tfbTargets;
+
+ private ColorF _blendConstant;
+
+ internal Pipeline()
+ {
+ _drawTexture = new DrawTextureEmulation();
+ _rasterizerDiscard = false;
+ _clipOrigin = ClipOrigin.LowerLeft;
+ _clipDepthMode = ClipDepthMode.NegativeOneToOne;
+
+ _fragmentOutputMap = uint.MaxValue;
+ _componentMasks = uint.MaxValue;
+
+ _images = new (TextureBase, Format)[SavedImages];
+
+ var defaultScale = new Vector4<float> { X = 1f, Y = 0f, Z = 0f, W = 0f };
+ new Span<Vector4<float>>(_renderScale).Fill(defaultScale);
+
+ _tfbs = new BufferHandle[Constants.MaxTransformFeedbackBuffers];
+ _tfbTargets = new BufferRange[Constants.MaxTransformFeedbackBuffers];
+ }
+
+ public void Initialize(OpenGLRenderer renderer)
+ {
+ _supportBuffer = new SupportBufferUpdater(renderer);
+ GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, Unsafe.As<BufferHandle, int>(ref _supportBuffer.Handle));
+
+ _supportBuffer.UpdateFragmentIsBgra(_fpIsBgra, 0, SupportBuffer.FragmentIsBgraCount);
+ _supportBuffer.UpdateRenderScale(_renderScale, 0, SupportBuffer.RenderScaleMaxCount);
+ }
+
+ public void Barrier()
+ {
+ GL.MemoryBarrier(MemoryBarrierFlags.AllBarrierBits);
+ }
+
+ public void BeginTransformFeedback(PrimitiveTopology topology)
+ {
+ GL.BeginTransformFeedback(_tfTopology = topology.ConvertToTfType());
+ _tfEnabled = true;
+ }
+
+ public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
+ {
+ Buffer.Clear(destination, offset, size, value);
+ }
+
+ public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
+ {
+ EnsureFramebuffer();
+
+ GL.ColorMask(
+ index,
+ (componentMask & 1) != 0,
+ (componentMask & 2) != 0,
+ (componentMask & 4) != 0,
+ (componentMask & 8) != 0);
+
+ float[] colors = new float[] { color.Red, color.Green, color.Blue, color.Alpha };
+
+ if (layer != 0 || layerCount != _framebuffer.GetColorLayerCount(index))
+ {
+ for (int l = layer; l < layer + layerCount; l++)
+ {
+ _framebuffer.AttachColorLayerForClear(index, l);
+
+ GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Color, index, colors);
+ }
+
+ _framebuffer.DetachColorLayerForClear(index);
+ }
+ else
+ {
+ GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Color, index, colors);
+ }
+
+ RestoreComponentMask(index);
+ }
+
+ public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue, int stencilMask)
+ {
+ EnsureFramebuffer();
+
+ bool stencilMaskChanged =
+ stencilMask != 0 &&
+ stencilMask != _stencilFrontMask;
+
+ bool depthMaskChanged = depthMask && depthMask != _depthMask;
+
+ if (stencilMaskChanged)
+ {
+ GL.StencilMaskSeparate(StencilFace.Front, stencilMask);
+ }
+
+ if (depthMaskChanged)
+ {
+ GL.DepthMask(depthMask);
+ }
+
+ if (layer != 0 || layerCount != _framebuffer.GetDepthStencilLayerCount())
+ {
+ for (int l = layer; l < layer + layerCount; l++)
+ {
+ _framebuffer.AttachDepthStencilLayerForClear(l);
+
+ ClearDepthStencil(depthValue, depthMask, stencilValue, stencilMask);
+ }
+
+ _framebuffer.DetachDepthStencilLayerForClear();
+ }
+ else
+ {
+ ClearDepthStencil(depthValue, depthMask, stencilValue, stencilMask);
+ }
+
+ if (stencilMaskChanged)
+ {
+ GL.StencilMaskSeparate(StencilFace.Front, _stencilFrontMask);
+ }
+
+ if (depthMaskChanged)
+ {
+ GL.DepthMask(_depthMask);
+ }
+ }
+
+ private static void ClearDepthStencil(float depthValue, bool depthMask, int stencilValue, int stencilMask)
+ {
+ if (depthMask && stencilMask != 0)
+ {
+ GL.ClearBuffer(ClearBufferCombined.DepthStencil, 0, depthValue, stencilValue);
+ }
+ else if (depthMask)
+ {
+ GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Depth, 0, ref depthValue);
+ }
+ else if (stencilMask != 0)
+ {
+ GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Stencil, 0, ref stencilValue);
+ }
+ }
+
+ public void CommandBufferBarrier()
+ {
+ GL.MemoryBarrier(MemoryBarrierFlags.CommandBarrierBit);
+ }
+
+ public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
+ {
+ Buffer.Copy(source, destination, srcOffset, dstOffset, size);
+ }
+
+ public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Dispatch error, shader not linked.");
+ return;
+ }
+
+ PrepareForDispatch();
+
+ GL.DispatchCompute(groupsX, groupsY, groupsZ);
+ }
+
+ public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
+ return;
+ }
+
+ PreDraw(vertexCount);
+
+ if (_primitiveType == PrimitiveType.Quads && !HwCapabilities.SupportsQuads)
+ {
+ DrawQuadsImpl(vertexCount, instanceCount, firstVertex, firstInstance);
+ }
+ else if (_primitiveType == PrimitiveType.QuadStrip && !HwCapabilities.SupportsQuads)
+ {
+ DrawQuadStripImpl(vertexCount, instanceCount, firstVertex, firstInstance);
+ }
+ else
+ {
+ DrawImpl(vertexCount, instanceCount, firstVertex, firstInstance);
+ }
+
+ PostDraw();
+ }
+
+ private void DrawQuadsImpl(
+ int vertexCount,
+ int instanceCount,
+ int firstVertex,
+ int firstInstance)
+ {
+ // TODO: Instanced rendering.
+ int quadsCount = vertexCount / 4;
+
+ int[] firsts = new int[quadsCount];
+ int[] counts = new int[quadsCount];
+
+ for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
+ {
+ firsts[quadIndex] = firstVertex + quadIndex * 4;
+ counts[quadIndex] = 4;
+ }
+
+ GL.MultiDrawArrays(
+ PrimitiveType.TriangleFan,
+ firsts,
+ counts,
+ quadsCount);
+ }
+
+ private void DrawQuadStripImpl(
+ int vertexCount,
+ int instanceCount,
+ int firstVertex,
+ int firstInstance)
+ {
+ int quadsCount = (vertexCount - 2) / 2;
+
+ if (firstInstance != 0 || instanceCount != 1)
+ {
+ for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
+ {
+ GL.DrawArraysInstancedBaseInstance(PrimitiveType.TriangleFan, firstVertex + quadIndex * 2, 4, instanceCount, firstInstance);
+ }
+ }
+ else
+ {
+ int[] firsts = new int[quadsCount];
+ int[] counts = new int[quadsCount];
+
+ firsts[0] = firstVertex;
+ counts[0] = 4;
+
+ for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
+ {
+ firsts[quadIndex] = firstVertex + quadIndex * 2;
+ counts[quadIndex] = 4;
+ }
+
+ GL.MultiDrawArrays(
+ PrimitiveType.TriangleFan,
+ firsts,
+ counts,
+ quadsCount);
+ }
+ }
+
+ private void DrawImpl(
+ int vertexCount,
+ int instanceCount,
+ int firstVertex,
+ int firstInstance)
+ {
+ if (firstInstance == 0 && instanceCount == 1)
+ {
+ GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
+ }
+ else if (firstInstance == 0)
+ {
+ GL.DrawArraysInstanced(_primitiveType, firstVertex, vertexCount, instanceCount);
+ }
+ else
+ {
+ GL.DrawArraysInstancedBaseInstance(
+ _primitiveType,
+ firstVertex,
+ vertexCount,
+ instanceCount,
+ firstInstance);
+ }
+ }
+
+ public void DrawIndexed(
+ int indexCount,
+ int instanceCount,
+ int firstIndex,
+ int firstVertex,
+ int firstInstance)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
+ return;
+ }
+
+ PreDrawVbUnbounded();
+
+ int indexElemSize = 1;
+
+ switch (_elementsType)
+ {
+ case DrawElementsType.UnsignedShort: indexElemSize = 2; break;
+ case DrawElementsType.UnsignedInt: indexElemSize = 4; break;
+ }
+
+ IntPtr indexBaseOffset = _indexBaseOffset + firstIndex * indexElemSize;
+
+ if (_primitiveType == PrimitiveType.Quads && !HwCapabilities.SupportsQuads)
+ {
+ DrawQuadsIndexedImpl(
+ indexCount,
+ instanceCount,
+ indexBaseOffset,
+ indexElemSize,
+ firstVertex,
+ firstInstance);
+ }
+ else if (_primitiveType == PrimitiveType.QuadStrip && !HwCapabilities.SupportsQuads)
+ {
+ DrawQuadStripIndexedImpl(
+ indexCount,
+ instanceCount,
+ indexBaseOffset,
+ indexElemSize,
+ firstVertex,
+ firstInstance);
+ }
+ else
+ {
+ DrawIndexedImpl(
+ indexCount,
+ instanceCount,
+ indexBaseOffset,
+ firstVertex,
+ firstInstance);
+ }
+
+ PostDraw();
+ }
+
+ private void DrawQuadsIndexedImpl(
+ int indexCount,
+ int instanceCount,
+ IntPtr indexBaseOffset,
+ int indexElemSize,
+ int firstVertex,
+ int firstInstance)
+ {
+ int quadsCount = indexCount / 4;
+
+ if (firstInstance != 0 || instanceCount != 1)
+ {
+ if (firstVertex != 0 && firstInstance != 0)
+ {
+ for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
+ {
+ GL.DrawElementsInstancedBaseVertexBaseInstance(
+ PrimitiveType.TriangleFan,
+ 4,
+ _elementsType,
+ indexBaseOffset + quadIndex * 4 * indexElemSize,
+ instanceCount,
+ firstVertex,
+ firstInstance);
+ }
+ }
+ else if (firstInstance != 0)
+ {
+ for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
+ {
+ GL.DrawElementsInstancedBaseInstance(
+ PrimitiveType.TriangleFan,
+ 4,
+ _elementsType,
+ indexBaseOffset + quadIndex * 4 * indexElemSize,
+ instanceCount,
+ firstInstance);
+ }
+ }
+ else
+ {
+ for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
+ {
+ GL.DrawElementsInstanced(
+ PrimitiveType.TriangleFan,
+ 4,
+ _elementsType,
+ indexBaseOffset + quadIndex * 4 * indexElemSize,
+ instanceCount);
+ }
+ }
+ }
+ else
+ {
+ IntPtr[] indices = new IntPtr[quadsCount];
+
+ int[] counts = new int[quadsCount];
+
+ int[] baseVertices = new int[quadsCount];
+
+ for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
+ {
+ indices[quadIndex] = indexBaseOffset + quadIndex * 4 * indexElemSize;
+
+ counts[quadIndex] = 4;
+
+ baseVertices[quadIndex] = firstVertex;
+ }
+
+ GL.MultiDrawElementsBaseVertex(
+ PrimitiveType.TriangleFan,
+ counts,
+ _elementsType,
+ indices,
+ quadsCount,
+ baseVertices);
+ }
+ }
+
+ private void DrawQuadStripIndexedImpl(
+ int indexCount,
+ int instanceCount,
+ IntPtr indexBaseOffset,
+ int indexElemSize,
+ int firstVertex,
+ int firstInstance)
+ {
+ // TODO: Instanced rendering.
+ int quadsCount = (indexCount - 2) / 2;
+
+ IntPtr[] indices = new IntPtr[quadsCount];
+
+ int[] counts = new int[quadsCount];
+
+ int[] baseVertices = new int[quadsCount];
+
+ indices[0] = indexBaseOffset;
+
+ counts[0] = 4;
+
+ baseVertices[0] = firstVertex;
+
+ for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
+ {
+ indices[quadIndex] = indexBaseOffset + quadIndex * 2 * indexElemSize;
+
+ counts[quadIndex] = 4;
+
+ baseVertices[quadIndex] = firstVertex;
+ }
+
+ GL.MultiDrawElementsBaseVertex(
+ PrimitiveType.TriangleFan,
+ counts,
+ _elementsType,
+ indices,
+ quadsCount,
+ baseVertices);
+ }
+
+ private void DrawIndexedImpl(
+ int indexCount,
+ int instanceCount,
+ IntPtr indexBaseOffset,
+ int firstVertex,
+ int firstInstance)
+ {
+ if (firstInstance == 0 && firstVertex == 0 && instanceCount == 1)
+ {
+ GL.DrawElements(_primitiveType, indexCount, _elementsType, indexBaseOffset);
+ }
+ else if (firstInstance == 0 && instanceCount == 1)
+ {
+ GL.DrawElementsBaseVertex(
+ _primitiveType,
+ indexCount,
+ _elementsType,
+ indexBaseOffset,
+ firstVertex);
+ }
+ else if (firstInstance == 0 && firstVertex == 0)
+ {
+ GL.DrawElementsInstanced(
+ _primitiveType,
+ indexCount,
+ _elementsType,
+ indexBaseOffset,
+ instanceCount);
+ }
+ else if (firstInstance == 0)
+ {
+ GL.DrawElementsInstancedBaseVertex(
+ _primitiveType,
+ indexCount,
+ _elementsType,
+ indexBaseOffset,
+ instanceCount,
+ firstVertex);
+ }
+ else if (firstVertex == 0)
+ {
+ GL.DrawElementsInstancedBaseInstance(
+ _primitiveType,
+ indexCount,
+ _elementsType,
+ indexBaseOffset,
+ instanceCount,
+ firstInstance);
+ }
+ else
+ {
+ GL.DrawElementsInstancedBaseVertexBaseInstance(
+ _primitiveType,
+ indexCount,
+ _elementsType,
+ indexBaseOffset,
+ instanceCount,
+ firstVertex,
+ firstInstance);
+ }
+ }
+
+ public void DrawIndexedIndirect(BufferRange indirectBuffer)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
+ return;
+ }
+
+ PreDrawVbUnbounded();
+
+ _vertexArray.SetRangeOfIndexBuffer();
+
+ GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
+
+ GL.DrawElementsIndirect(_primitiveType, _elementsType, (IntPtr)indirectBuffer.Offset);
+
+ _vertexArray.RestoreIndexBuffer();
+
+ PostDraw();
+ }
+
+ public void DrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
+ return;
+ }
+
+ PreDrawVbUnbounded();
+
+ _vertexArray.SetRangeOfIndexBuffer();
+
+ GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
+ GL.BindBuffer((BufferTarget)All.ParameterBuffer, parameterBuffer.Handle.ToInt32());
+
+ GL.MultiDrawElementsIndirectCount(
+ _primitiveType,
+ (All)_elementsType,
+ (IntPtr)indirectBuffer.Offset,
+ (IntPtr)parameterBuffer.Offset,
+ maxDrawCount,
+ stride);
+
+ _vertexArray.RestoreIndexBuffer();
+
+ PostDraw();
+ }
+
+ public void DrawIndirect(BufferRange indirectBuffer)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
+ return;
+ }
+
+ PreDrawVbUnbounded();
+
+ GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
+
+ GL.DrawArraysIndirect(_primitiveType, (IntPtr)indirectBuffer.Offset);
+
+ PostDraw();
+ }
+
+ public void DrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
+ {
+ if (!_program.IsLinked)
+ {
+ Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
+ return;
+ }
+
+ PreDrawVbUnbounded();
+
+ GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
+ GL.BindBuffer((BufferTarget)All.ParameterBuffer, parameterBuffer.Handle.ToInt32());
+
+ GL.MultiDrawArraysIndirectCount(
+ _primitiveType,
+ (IntPtr)indirectBuffer.Offset,
+ (IntPtr)parameterBuffer.Offset,
+ maxDrawCount,
+ stride);
+
+ PostDraw();
+ }
+
+ public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
+ {
+ if (texture is TextureView view && sampler is Sampler samp)
+ {
+ _supportBuffer.Commit();
+
+ 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();
+ }
+
+ GL.ClipControl(ClipOrigin.UpperLeft, ClipDepthMode.NegativeOneToOne);
+
+ _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);
+
+ RestoreViewport0();
+
+ Enable(EnableCap.CullFace, _cullEnable);
+ Enable(EnableCap.StencilTest, _stencilTestEnable);
+ Enable(EnableCap.DepthTest, _depthTestEnable);
+
+ if (_depthMask)
+ {
+ GL.DepthMask(true);
+ }
+
+ if (_tfEnabled)
+ {
+ GL.BeginTransformFeedback(_tfTopology);
+ }
+
+ RestoreClipControl();
+ }
+ }
+ }
+
+ public void EndTransformFeedback()
+ {
+ GL.EndTransformFeedback();
+ _tfEnabled = false;
+ }
+
+ public double GetCounterDivisor(CounterType type)
+ {
+ if (type == CounterType.SamplesPassed)
+ {
+ return _renderScale[0].X * _renderScale[0].X;
+ }
+
+ return 1;
+ }
+
+ public void SetAlphaTest(bool enable, float reference, CompareOp op)
+ {
+ if (!enable)
+ {
+ GL.Disable(EnableCap.AlphaTest);
+ return;
+ }
+
+ GL.AlphaFunc((AlphaFunction)op.Convert(), reference);
+ GL.Enable(EnableCap.AlphaTest);
+ }
+
+ public void SetBlendState(AdvancedBlendDescriptor blend)
+ {
+ if (HwCapabilities.SupportsBlendEquationAdvanced)
+ {
+ GL.BlendEquation((BlendEquationMode)blend.Op.Convert());
+ GL.NV.BlendParameter(NvBlendEquationAdvanced.BlendOverlapNv, (int)blend.Overlap.Convert());
+ GL.NV.BlendParameter(NvBlendEquationAdvanced.BlendPremultipliedSrcNv, blend.SrcPreMultiplied ? 1 : 0);
+ GL.Enable(EnableCap.Blend);
+ _advancedBlendEnable = true;
+ }
+ }
+
+ public void SetBlendState(int index, BlendDescriptor blend)
+ {
+ if (_advancedBlendEnable)
+ {
+ GL.Disable(EnableCap.Blend);
+ _advancedBlendEnable = false;
+ }
+
+ if (!blend.Enable)
+ {
+ GL.Disable(IndexedEnableCap.Blend, index);
+ return;
+ }
+
+ GL.BlendEquationSeparate(
+ index,
+ blend.ColorOp.Convert(),
+ blend.AlphaOp.Convert());
+
+ GL.BlendFuncSeparate(
+ index,
+ (BlendingFactorSrc)blend.ColorSrcFactor.Convert(),
+ (BlendingFactorDest)blend.ColorDstFactor.Convert(),
+ (BlendingFactorSrc)blend.AlphaSrcFactor.Convert(),
+ (BlendingFactorDest)blend.AlphaDstFactor.Convert());
+
+ EnsureFramebuffer();
+
+ _framebuffer.SetDualSourceBlend(
+ blend.ColorSrcFactor.IsDualSource() ||
+ blend.ColorDstFactor.IsDualSource() ||
+ blend.AlphaSrcFactor.IsDualSource() ||
+ blend.AlphaDstFactor.IsDualSource());
+
+ if (_blendConstant != blend.BlendConstant)
+ {
+ _blendConstant = blend.BlendConstant;
+
+ GL.BlendColor(
+ blend.BlendConstant.Red,
+ blend.BlendConstant.Green,
+ blend.BlendConstant.Blue,
+ blend.BlendConstant.Alpha);
+ }
+
+ GL.Enable(IndexedEnableCap.Blend, index);
+ }
+
+ public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
+ {
+ if ((enables & PolygonModeMask.Point) != 0)
+ {
+ GL.Enable(EnableCap.PolygonOffsetPoint);
+ }
+ else
+ {
+ GL.Disable(EnableCap.PolygonOffsetPoint);
+ }
+
+ if ((enables & PolygonModeMask.Line) != 0)
+ {
+ GL.Enable(EnableCap.PolygonOffsetLine);
+ }
+ else
+ {
+ GL.Disable(EnableCap.PolygonOffsetLine);
+ }
+
+ if ((enables & PolygonModeMask.Fill) != 0)
+ {
+ GL.Enable(EnableCap.PolygonOffsetFill);
+ }
+ else
+ {
+ GL.Disable(EnableCap.PolygonOffsetFill);
+ }
+
+ if (enables == 0)
+ {
+ return;
+ }
+
+ if (HwCapabilities.SupportsPolygonOffsetClamp)
+ {
+ GL.PolygonOffsetClamp(factor, units, clamp);
+ }
+ else
+ {
+ GL.PolygonOffset(factor, units);
+ }
+ }
+
+ public void SetDepthClamp(bool clamp)
+ {
+ if (!clamp)
+ {
+ GL.Disable(EnableCap.DepthClamp);
+ return;
+ }
+
+ GL.Enable(EnableCap.DepthClamp);
+ }
+
+ public void SetDepthMode(DepthMode mode)
+ {
+ ClipDepthMode depthMode = mode.Convert();
+
+ if (_clipDepthMode != depthMode)
+ {
+ _clipDepthMode = depthMode;
+
+ GL.ClipControl(_clipOrigin, depthMode);
+ }
+ }
+
+ public void SetDepthTest(DepthTestDescriptor depthTest)
+ {
+ if (depthTest.TestEnable)
+ {
+ GL.Enable(EnableCap.DepthTest);
+ GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
+ }
+ else
+ {
+ GL.Disable(EnableCap.DepthTest);
+ }
+
+ 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);
+ return;
+ }
+
+ GL.CullFace(face.Convert());
+
+ GL.Enable(EnableCap.CullFace);
+ }
+
+ public void SetFrontFace(FrontFace frontFace)
+ {
+ SetFrontFace(_frontFace = frontFace.Convert());
+ }
+
+ public void SetImage(int binding, ITexture texture, Format imageFormat)
+ {
+ if ((uint)binding < SavedImages)
+ {
+ _images[binding] = (texture as TextureBase, imageFormat);
+ }
+
+ if (texture == null)
+ {
+ GL.BindImageTexture(binding, 0, 0, true, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ return;
+ }
+
+ TextureBase texBase = (TextureBase)texture;
+
+ SizedInternalFormat format = FormatTable.GetImageFormat(imageFormat);
+
+ if (format != 0)
+ {
+ GL.BindImageTexture(binding, texBase.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
+ }
+ }
+
+ public void SetIndexBuffer(BufferRange buffer, IndexType type)
+ {
+ _elementsType = type.Convert();
+
+ _indexBaseOffset = (IntPtr)buffer.Offset;
+
+ EnsureVertexArray();
+
+ _vertexArray.SetIndexBuffer(buffer);
+ }
+
+ public void SetLogicOpState(bool enable, LogicalOp op)
+ {
+ if (enable)
+ {
+ GL.Enable(EnableCap.ColorLogicOp);
+
+ GL.LogicOp((LogicOp)op.Convert());
+ }
+ else
+ {
+ GL.Disable(EnableCap.ColorLogicOp);
+ }
+ }
+
+ public void SetMultisampleState(MultisampleDescriptor multisample)
+ {
+ if (multisample.AlphaToCoverageEnable)
+ {
+ GL.Enable(EnableCap.SampleAlphaToCoverage);
+
+ if (multisample.AlphaToOneEnable)
+ {
+ GL.Enable(EnableCap.SampleAlphaToOne);
+ }
+ else
+ {
+ GL.Disable(EnableCap.SampleAlphaToOne);
+ }
+
+ if (HwCapabilities.SupportsAlphaToCoverageDitherControl)
+ {
+ GL.NV.AlphaToCoverageDitherControl(multisample.AlphaToCoverageDitherEnable
+ ? NvAlphaToCoverageDitherControl.AlphaToCoverageDitherEnableNv
+ : NvAlphaToCoverageDitherControl.AlphaToCoverageDitherDisableNv);
+ }
+ }
+ else
+ {
+ GL.Disable(EnableCap.SampleAlphaToCoverage);
+ }
+ }
+
+ public void SetLineParameters(float width, bool smooth)
+ {
+ if (smooth)
+ {
+ GL.Enable(EnableCap.LineSmooth);
+ }
+ else
+ {
+ GL.Disable(EnableCap.LineSmooth);
+ }
+
+ GL.LineWidth(width);
+ }
+
+ public unsafe void SetPatchParameters(int vertices, ReadOnlySpan<float> defaultOuterLevel, ReadOnlySpan<float> defaultInnerLevel)
+ {
+ GL.PatchParameter(PatchParameterInt.PatchVertices, vertices);
+
+ fixed (float* pOuterLevel = defaultOuterLevel)
+ {
+ GL.PatchParameter(PatchParameterFloat.PatchDefaultOuterLevel, pOuterLevel);
+ }
+
+ fixed (float* pInnerLevel = defaultInnerLevel)
+ {
+ GL.PatchParameter(PatchParameterFloat.PatchDefaultInnerLevel, pInnerLevel);
+ }
+ }
+
+ public void SetPointParameters(float size, bool isProgramPointSize, bool enablePointSprite, Origin origin)
+ {
+ // GL_POINT_SPRITE was deprecated in core profile 3.2+ and causes GL_INVALID_ENUM when set.
+ // As we don't know if the current context is core or compat, it's safer to keep this code.
+ if (enablePointSprite)
+ {
+ GL.Enable(EnableCap.PointSprite);
+ }
+ else
+ {
+ GL.Disable(EnableCap.PointSprite);
+ }
+
+ if (isProgramPointSize)
+ {
+ GL.Enable(EnableCap.ProgramPointSize);
+ }
+ else
+ {
+ GL.Disable(EnableCap.ProgramPointSize);
+ }
+
+ GL.PointParameter(origin == Origin.LowerLeft
+ ? PointSpriteCoordOriginParameter.LowerLeft
+ : PointSpriteCoordOriginParameter.UpperLeft);
+
+ // Games seem to set point size to 0 which generates a GL_INVALID_VALUE
+ // From the spec, GL_INVALID_VALUE is generated if size is less than or equal to 0.
+ GL.PointSize(Math.Max(float.Epsilon, size));
+ }
+
+ public void SetPolygonMode(GAL.PolygonMode frontMode, GAL.PolygonMode backMode)
+ {
+ if (frontMode == backMode)
+ {
+ GL.PolygonMode(MaterialFace.FrontAndBack, frontMode.Convert());
+ }
+ else
+ {
+ GL.PolygonMode(MaterialFace.Front, frontMode.Convert());
+ GL.PolygonMode(MaterialFace.Back, backMode.Convert());
+ }
+ }
+
+ public void SetPrimitiveRestart(bool enable, int index)
+ {
+ if (!enable)
+ {
+ GL.Disable(EnableCap.PrimitiveRestart);
+ return;
+ }
+
+ GL.PrimitiveRestartIndex(index);
+
+ GL.Enable(EnableCap.PrimitiveRestart);
+ }
+
+ public void SetPrimitiveTopology(PrimitiveTopology topology)
+ {
+ _primitiveType = topology.Convert();
+ }
+
+ public void SetProgram(IProgram program)
+ {
+ Program prg = (Program)program;
+
+ if (_tfEnabled)
+ {
+ GL.EndTransformFeedback();
+ prg.Bind();
+ GL.BeginTransformFeedback(_tfTopology);
+ }
+ else
+ {
+ prg.Bind();
+ }
+
+ if (prg.HasFragmentShader && _fragmentOutputMap != (uint)prg.FragmentOutputMap)
+ {
+ _fragmentOutputMap = (uint)prg.FragmentOutputMap;
+
+ for (int index = 0; index < Constants.MaxRenderTargets; index++)
+ {
+ RestoreComponentMask(index, force: false);
+ }
+ }
+
+ _program = prg;
+ }
+
+ public void SetRasterizerDiscard(bool discard)
+ {
+ if (discard)
+ {
+ GL.Enable(EnableCap.RasterizerDiscard);
+ }
+ else
+ {
+ GL.Disable(EnableCap.RasterizerDiscard);
+ }
+
+ _rasterizerDiscard = discard;
+ }
+
+ public void SetRenderTargetScale(float scale)
+ {
+ _renderScale[0].X = scale;
+ _supportBuffer.UpdateRenderScale(_renderScale, 0, 1); // Just the first element.
+ }
+
+ public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMasks)
+ {
+ _componentMasks = 0;
+
+ for (int index = 0; index < componentMasks.Length; index++)
+ {
+ _componentMasks |= componentMasks[index] << (index * 4);
+
+ RestoreComponentMask(index, force: false);
+ }
+ }
+
+ public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
+ {
+ EnsureFramebuffer();
+
+ bool isBgraChanged = false;
+
+ for (int index = 0; index < colors.Length; index++)
+ {
+ TextureView color = (TextureView)colors[index];
+
+ _framebuffer.AttachColor(index, color);
+
+ if (color != null)
+ {
+ int isBgra = color.Format.IsBgr() ? 1 : 0;
+
+ if (_fpIsBgra[index].X != isBgra)
+ {
+ _fpIsBgra[index].X = isBgra;
+ isBgraChanged = true;
+
+ RestoreComponentMask(index);
+ }
+ }
+ }
+
+ if (isBgraChanged)
+ {
+ _supportBuffer.UpdateFragmentIsBgra(_fpIsBgra, 0, SupportBuffer.FragmentIsBgraCount);
+ }
+
+ TextureView depthStencilView = (TextureView)depthStencil;
+
+ _framebuffer.AttachDepthStencil(depthStencilView);
+ _framebuffer.SetDrawBuffers(colors.Length);
+ }
+
+ public void SetScissors(ReadOnlySpan<Rectangle<int>> regions)
+ {
+ int count = Math.Min(regions.Length, Constants.MaxViewports);
+
+ Span<int> v = stackalloc int[count * 4];
+
+ for (int index = 0; index < count; index++)
+ {
+ int vIndex = index * 4;
+
+ var region = regions[index];
+
+ bool enabled = (region.X | region.Y) != 0 || region.Width != 0xffff || region.Height != 0xffff;
+ uint mask = 1u << index;
+
+ if (enabled)
+ {
+ v[vIndex] = region.X;
+ v[vIndex + 1] = region.Y;
+ v[vIndex + 2] = region.Width;
+ v[vIndex + 3] = region.Height;
+
+ if ((_scissorEnables & mask) == 0)
+ {
+ _scissorEnables |= mask;
+ GL.Enable(IndexedEnableCap.ScissorTest, index);
+ }
+ }
+ else
+ {
+ if ((_scissorEnables & mask) != 0)
+ {
+ _scissorEnables &= ~mask;
+ GL.Disable(IndexedEnableCap.ScissorTest, index);
+ }
+ }
+ }
+
+ GL.ScissorArray(0, count, ref v[0]);
+ }
+
+ public void SetStencilTest(StencilTestDescriptor stencilTest)
+ {
+ _stencilTestEnable = stencilTest.TestEnable;
+
+ if (!stencilTest.TestEnable)
+ {
+ GL.Disable(EnableCap.StencilTest);
+ return;
+ }
+
+ GL.StencilOpSeparate(
+ StencilFace.Front,
+ stencilTest.FrontSFail.Convert(),
+ stencilTest.FrontDpFail.Convert(),
+ stencilTest.FrontDpPass.Convert());
+
+ GL.StencilFuncSeparate(
+ StencilFace.Front,
+ (StencilFunction)stencilTest.FrontFunc.Convert(),
+ stencilTest.FrontFuncRef,
+ stencilTest.FrontFuncMask);
+
+ GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
+
+ GL.StencilOpSeparate(
+ StencilFace.Back,
+ stencilTest.BackSFail.Convert(),
+ stencilTest.BackDpFail.Convert(),
+ stencilTest.BackDpPass.Convert());
+
+ GL.StencilFuncSeparate(
+ StencilFace.Back,
+ (StencilFunction)stencilTest.BackFunc.Convert(),
+ stencilTest.BackFuncRef,
+ stencilTest.BackFuncMask);
+
+ GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
+
+ GL.Enable(EnableCap.StencilTest);
+
+ _stencilFrontMask = stencilTest.FrontMask;
+ }
+
+ public void SetStorageBuffers(ReadOnlySpan<BufferAssignment> buffers)
+ {
+ SetBuffers(buffers, isStorage: true);
+ }
+
+ public void SetTextureAndSampler(ShaderStage stage, int binding, ITexture texture, ISampler sampler)
+ {
+ if (texture != null)
+ {
+ if (binding == 0)
+ {
+ _unit0Texture = (TextureBase)texture;
+ }
+ else
+ {
+ ((TextureBase)texture).Bind(binding);
+ }
+ }
+ else
+ {
+ TextureBase.ClearBinding(binding);
+ }
+
+ Sampler glSampler = (Sampler)sampler;
+
+ glSampler?.Bind(binding);
+
+ if (binding == 0)
+ {
+ _unit0Sampler = glSampler;
+ }
+ }
+
+
+ public void SetTransformFeedbackBuffers(ReadOnlySpan<BufferRange> buffers)
+ {
+ if (_tfEnabled)
+ {
+ GL.EndTransformFeedback();
+ }
+
+ int count = Math.Min(buffers.Length, Constants.MaxTransformFeedbackBuffers);
+
+ for (int i = 0; i < count; i++)
+ {
+ BufferRange buffer = buffers[i];
+ _tfbTargets[i] = buffer;
+
+ if (buffer.Handle == BufferHandle.Null)
+ {
+ GL.BindBufferBase(BufferRangeTarget.TransformFeedbackBuffer, i, 0);
+ continue;
+ }
+
+ if (_tfbs[i] == BufferHandle.Null)
+ {
+ _tfbs[i] = Buffer.Create();
+ }
+
+ Buffer.Resize(_tfbs[i], buffer.Size);
+ Buffer.Copy(buffer.Handle, _tfbs[i], buffer.Offset, 0, buffer.Size);
+ GL.BindBufferBase(BufferRangeTarget.TransformFeedbackBuffer, i, _tfbs[i].ToInt32());
+ }
+
+ if (_tfEnabled)
+ {
+ GL.BeginTransformFeedback(_tfTopology);
+ }
+ }
+
+ public void SetUniformBuffers(ReadOnlySpan<BufferAssignment> buffers)
+ {
+ SetBuffers(buffers, isStorage: false);
+ }
+
+ public void SetUserClipDistance(int index, bool enableClip)
+ {
+ if (!enableClip)
+ {
+ GL.Disable(EnableCap.ClipDistance0 + index);
+ return;
+ }
+
+ GL.Enable(EnableCap.ClipDistance0 + index);
+ }
+
+ public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
+ {
+ EnsureVertexArray();
+
+ _vertexArray.SetVertexAttributes(vertexAttribs);
+ }
+
+ public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
+ {
+ EnsureVertexArray();
+
+ _vertexArray.SetVertexBuffers(vertexBuffers);
+ }
+
+ public void SetViewports(ReadOnlySpan<Viewport> viewports, bool disableTransform)
+ {
+ Array.Resize(ref _viewportArray, viewports.Length * 4);
+ Array.Resize(ref _depthRangeArray, viewports.Length * 2);
+
+ float[] viewportArray = _viewportArray;
+ double[] depthRangeArray = _depthRangeArray;
+
+ for (int index = 0; index < viewports.Length; index++)
+ {
+ int viewportElemIndex = index * 4;
+
+ Viewport viewport = viewports[index];
+
+ viewportArray[viewportElemIndex + 0] = viewport.Region.X;
+ viewportArray[viewportElemIndex + 1] = viewport.Region.Y + (viewport.Region.Height < 0 ? viewport.Region.Height : 0);
+ viewportArray[viewportElemIndex + 2] = viewport.Region.Width;
+ viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
+
+ if (HwCapabilities.SupportsViewportSwizzle)
+ {
+ GL.NV.ViewportSwizzle(
+ index,
+ viewport.SwizzleX.Convert(),
+ viewport.SwizzleY.Convert(),
+ viewport.SwizzleZ.Convert(),
+ viewport.SwizzleW.Convert());
+ }
+
+ depthRangeArray[index * 2 + 0] = viewport.DepthNear;
+ depthRangeArray[index * 2 + 1] = viewport.DepthFar;
+ }
+
+ bool flipY = viewports.Length != 0 && viewports[0].Region.Height < 0;
+
+ SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
+
+ GL.ViewportArray(0, viewports.Length, viewportArray);
+ GL.DepthRangeArray(0, viewports.Length, depthRangeArray);
+
+ float disableTransformF = disableTransform ? 1.0f : 0.0f;
+ if (_supportBuffer.Data.ViewportInverse.W != disableTransformF || disableTransform)
+ {
+ float scale = _renderScale[0].X;
+ _supportBuffer.UpdateViewportInverse(new Vector4<float>
+ {
+ X = scale * 2f / viewports[0].Region.Width,
+ Y = scale * 2f / viewports[0].Region.Height,
+ Z = 1,
+ W = disableTransformF
+ });
+ }
+ }
+
+ public void TextureBarrier()
+ {
+ GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
+ }
+
+ public void TextureBarrierTiled()
+ {
+ GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
+ }
+
+ private void SetBuffers(ReadOnlySpan<BufferAssignment> buffers, bool isStorage)
+ {
+ BufferRangeTarget target = isStorage ? BufferRangeTarget.ShaderStorageBuffer : BufferRangeTarget.UniformBuffer;
+
+ for (int index = 0; index < buffers.Length; index++)
+ {
+ BufferAssignment assignment = buffers[index];
+ BufferRange buffer = assignment.Range;
+
+ if (buffer.Handle == BufferHandle.Null)
+ {
+ GL.BindBufferRange(target, assignment.Binding, 0, IntPtr.Zero, 0);
+ continue;
+ }
+
+ GL.BindBufferRange(target, assignment.Binding, buffer.Handle.ToInt32(), (IntPtr)buffer.Offset, buffer.Size);
+ }
+ }
+
+ private void SetOrigin(ClipOrigin origin)
+ {
+ if (_clipOrigin != origin)
+ {
+ _clipOrigin = origin;
+
+ GL.ClipControl(origin, _clipDepthMode);
+
+ SetFrontFace(_frontFace);
+ }
+ }
+
+ private void SetFrontFace(FrontFaceDirection frontFace)
+ {
+ // Changing clip origin will also change the front face to compensate
+ // for the flipped viewport, we flip it again here to compensate as
+ // this effect is undesirable for us.
+ if (_clipOrigin == ClipOrigin.UpperLeft)
+ {
+ frontFace = frontFace == FrontFaceDirection.Ccw ? FrontFaceDirection.Cw : FrontFaceDirection.Ccw;
+ }
+
+ GL.FrontFace(frontFace);
+ }
+
+ private void EnsureVertexArray()
+ {
+ if (_vertexArray == null)
+ {
+ _vertexArray = new VertexArray();
+
+ _vertexArray.Bind();
+ }
+ }
+
+ private void EnsureFramebuffer()
+ {
+ if (_framebuffer == null)
+ {
+ _framebuffer = new Framebuffer();
+
+ int boundHandle = _framebuffer.Bind();
+ _boundDrawFramebuffer = _boundReadFramebuffer = boundHandle;
+
+ GL.Enable(EnableCap.FramebufferSrgb);
+ }
+ }
+
+ internal (int drawHandle, int readHandle) GetBoundFramebuffers()
+ {
+ if (BackgroundContextWorker.InBackground)
+ {
+ return (0, 0);
+ }
+
+ return (_boundDrawFramebuffer, _boundReadFramebuffer);
+ }
+
+ public void UpdateRenderScale(ReadOnlySpan<float> scales, int totalCount, int fragmentCount)
+ {
+ bool changed = false;
+
+ for (int index = 0; index < totalCount; index++)
+ {
+ if (_renderScale[1 + index].X != scales[index])
+ {
+ _renderScale[1 + index].X = scales[index];
+ changed = true;
+ }
+ }
+
+ // Only update fragment count if there are scales after it for the vertex stage.
+ if (fragmentCount != totalCount && fragmentCount != _fragmentScaleCount)
+ {
+ _fragmentScaleCount = fragmentCount;
+ _supportBuffer.UpdateFragmentRenderScaleCount(_fragmentScaleCount);
+ }
+
+ if (changed)
+ {
+ _supportBuffer.UpdateRenderScale(_renderScale, 0, 1 + totalCount);
+ }
+ }
+
+ private void PrepareForDispatch()
+ {
+ _unit0Texture?.Bind(0);
+ _supportBuffer.Commit();
+ }
+
+ private void PreDraw(int vertexCount)
+ {
+ _vertexArray.PreDraw(vertexCount);
+ PreDraw();
+ }
+
+ private void PreDrawVbUnbounded()
+ {
+ _vertexArray.PreDrawVbUnbounded();
+ PreDraw();
+ }
+
+ private void PreDraw()
+ {
+ DrawCount++;
+
+ _unit0Texture?.Bind(0);
+ _supportBuffer.Commit();
+ }
+
+ private void PostDraw()
+ {
+ if (_tfEnabled)
+ {
+ for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
+ {
+ if (_tfbTargets[i].Handle != BufferHandle.Null)
+ {
+ Buffer.Copy(_tfbs[i], _tfbTargets[i].Handle, 0, _tfbTargets[i].Offset, _tfbTargets[i].Size);
+ }
+ }
+ }
+ }
+
+ public void RestoreComponentMask(int index, bool force = true)
+ {
+ // If the bound render target is bgra, swap the red and blue masks.
+ uint redMask = _fpIsBgra[index].X == 0 ? 1u : 4u;
+ uint blueMask = _fpIsBgra[index].X == 0 ? 4u : 1u;
+
+ int shift = index * 4;
+ uint componentMask = _componentMasks & _fragmentOutputMap;
+ uint checkMask = 0xfu << shift;
+ uint componentMaskAtIndex = componentMask & checkMask;
+
+ if (!force && componentMaskAtIndex == (_currentComponentMasks & checkMask))
+ {
+ return;
+ }
+
+ componentMask >>= shift;
+ componentMask &= 0xfu;
+
+ GL.ColorMask(
+ index,
+ (componentMask & redMask) != 0,
+ (componentMask & 2u) != 0,
+ (componentMask & blueMask) != 0,
+ (componentMask & 8u) != 0);
+
+ _currentComponentMasks &= ~checkMask;
+ _currentComponentMasks |= componentMaskAtIndex;
+ }
+
+ public void RestoreClipControl()
+ {
+ GL.ClipControl(_clipOrigin, _clipDepthMode);
+ }
+
+ public void RestoreScissor0Enable()
+ {
+ if ((_scissorEnables & 1u) != 0)
+ {
+ GL.Enable(IndexedEnableCap.ScissorTest, 0);
+ }
+ }
+
+ public void RestoreRasterizerDiscard()
+ {
+ if (_rasterizerDiscard)
+ {
+ GL.Enable(EnableCap.RasterizerDiscard);
+ }
+ }
+
+ public void RestoreViewport0()
+ {
+ if (_viewportArray.Length > 0)
+ {
+ GL.ViewportArray(0, 1, _viewportArray);
+ }
+ }
+
+ public void RestoreProgram()
+ {
+ _program?.Bind();
+ }
+
+ public void RestoreImages1And2()
+ {
+ for (int i = 0; i < SavedImages; i++)
+ {
+ (TextureBase texBase, Format imageFormat) = _images[i];
+
+ if (texBase != null)
+ {
+ SizedInternalFormat format = FormatTable.GetImageFormat(imageFormat);
+
+ if (format != 0)
+ {
+ GL.BindImageTexture(i, texBase.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
+ continue;
+ }
+ }
+
+ GL.BindImageTexture(i, 0, 0, true, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
+ }
+ }
+
+ public bool TryHostConditionalRendering(ICounterEvent value, ulong compare, bool isEqual)
+ {
+ if (value is CounterQueueEvent)
+ {
+ // Compare an event and a constant value.
+ CounterQueueEvent evt = (CounterQueueEvent)value;
+
+ // Easy host conditional rendering when the check matches what GL can do:
+ // - Event is of type samples passed.
+ // - Result is not a combination of multiple queries.
+ // - Comparing against 0.
+ // - Event has not already been flushed.
+
+ if (compare == 0 && evt.Type == QueryTarget.SamplesPassed && evt.ClearCounter)
+ {
+ if (!value.ReserveForHostAccess())
+ {
+ // If the event has been flushed, then just use the values on the CPU.
+ // The query object may already be repurposed for another draw (eg. begin + end).
+ return false;
+ }
+
+ GL.BeginConditionalRender(evt.Query, isEqual ? ConditionalRenderType.QueryNoWaitInverted : ConditionalRenderType.QueryNoWait);
+ _activeConditionalRender = evt;
+
+ return true;
+ }
+ }
+
+ // The GPU will flush the queries to CPU and evaluate the condition there instead.
+
+ GL.Flush(); // The thread will be stalled manually flushing the counter, so flush GL commands now.
+ return false;
+ }
+
+ public bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual)
+ {
+ GL.Flush(); // The GPU thread will be stalled manually flushing the counter, so flush GL commands now.
+ return false; // We don't currently have a way to compare two counters for conditional rendering.
+ }
+
+ public void EndHostConditionalRendering()
+ {
+ GL.EndConditionalRender();
+
+ _activeConditionalRender?.ReleaseHostAccess();
+ _activeConditionalRender = null;
+ }
+
+ public void Dispose()
+ {
+ _supportBuffer?.Dispose();
+
+ for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
+ {
+ if (_tfbs[i] != BufferHandle.Null)
+ {
+ Buffer.Delete(_tfbs[i]);
+ _tfbs[i] = BufferHandle.Null;
+ }
+ }
+
+ _activeConditionalRender?.ReleaseHostAccess();
+ _framebuffer?.Dispose();
+ _vertexArray?.Dispose();
+ _drawTexture.Dispose();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Program.cs b/src/Ryujinx.Graphics.OpenGL/Program.cs
new file mode 100644
index 00000000..a6009108
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Program.cs
@@ -0,0 +1,177 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.Shader;
+using Ryujinx.Graphics.Shader.Translation;
+using System;
+using System.Buffers.Binary;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class Program : IProgram
+ {
+ private const int MaxShaderLogLength = 2048;
+
+ public int Handle { get; private set; }
+
+ public bool IsLinked
+ {
+ get
+ {
+ if (_status == ProgramLinkStatus.Incomplete)
+ {
+ CheckProgramLink(true);
+ }
+
+ return _status == ProgramLinkStatus.Success;
+ }
+ }
+
+ private ProgramLinkStatus _status = ProgramLinkStatus.Incomplete;
+ private int[] _shaderHandles;
+
+ public bool HasFragmentShader;
+ public int FragmentOutputMap { get; }
+
+ public Program(ShaderSource[] shaders, int fragmentOutputMap)
+ {
+ Handle = GL.CreateProgram();
+
+ GL.ProgramParameter(Handle, ProgramParameterName.ProgramBinaryRetrievableHint, 1);
+
+ _shaderHandles = new int[shaders.Length];
+
+ for (int index = 0; index < shaders.Length; index++)
+ {
+ ShaderSource shader = shaders[index];
+
+ if (shader.Stage == ShaderStage.Fragment)
+ {
+ HasFragmentShader = true;
+ }
+
+ int shaderHandle = GL.CreateShader(shader.Stage.Convert());
+
+ switch (shader.Language)
+ {
+ case TargetLanguage.Glsl:
+ GL.ShaderSource(shaderHandle, shader.Code);
+ GL.CompileShader(shaderHandle);
+ break;
+ case TargetLanguage.Spirv:
+ GL.ShaderBinary(1, ref shaderHandle, (BinaryFormat)All.ShaderBinaryFormatSpirVArb, shader.BinaryCode, shader.BinaryCode.Length);
+ GL.SpecializeShader(shaderHandle, "main", 0, (int[])null, (int[])null);
+ break;
+ }
+
+ GL.AttachShader(Handle, shaderHandle);
+
+ _shaderHandles[index] = shaderHandle;
+ }
+
+ GL.LinkProgram(Handle);
+
+ FragmentOutputMap = fragmentOutputMap;
+ }
+
+ public Program(ReadOnlySpan<byte> code, bool hasFragmentShader, int fragmentOutputMap)
+ {
+ Handle = GL.CreateProgram();
+
+ if (code.Length >= 4)
+ {
+ BinaryFormat binaryFormat = (BinaryFormat)BinaryPrimitives.ReadInt32LittleEndian(code.Slice(code.Length - 4, 4));
+
+ unsafe
+ {
+ fixed (byte* ptr = code)
+ {
+ GL.ProgramBinary(Handle, binaryFormat, (IntPtr)ptr, code.Length - 4);
+ }
+ }
+ }
+
+ HasFragmentShader = hasFragmentShader;
+ FragmentOutputMap = fragmentOutputMap;
+ }
+
+ public void Bind()
+ {
+ GL.UseProgram(Handle);
+ }
+
+ public ProgramLinkStatus CheckProgramLink(bool blocking)
+ {
+ if (!blocking && HwCapabilities.SupportsParallelShaderCompile)
+ {
+ GL.GetProgram(Handle, (GetProgramParameterName)ArbParallelShaderCompile.CompletionStatusArb, out int completed);
+
+ if (completed == 0)
+ {
+ return ProgramLinkStatus.Incomplete;
+ }
+ }
+
+ GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out int status);
+ DeleteShaders();
+
+ if (status == 0)
+ {
+ _status = ProgramLinkStatus.Failure;
+
+ string log = GL.GetProgramInfoLog(Handle);
+
+ if (log.Length > MaxShaderLogLength)
+ {
+ log = log.Substring(0, MaxShaderLogLength) + "...";
+ }
+
+ Logger.Warning?.Print(LogClass.Gpu, $"Shader linking failed: \n{log}");
+ }
+ else
+ {
+ _status = ProgramLinkStatus.Success;
+ }
+
+ return _status;
+ }
+
+ public byte[] GetBinary()
+ {
+ GL.GetProgram(Handle, (GetProgramParameterName)All.ProgramBinaryLength, out int size);
+
+ byte[] data = new byte[size + 4];
+
+ GL.GetProgramBinary(Handle, size, out _, out BinaryFormat binFormat, data);
+
+ BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(size, 4), (int)binFormat);
+
+ return data;
+ }
+
+ private void DeleteShaders()
+ {
+ if (_shaderHandles != null)
+ {
+ foreach (int shaderHandle in _shaderHandles)
+ {
+ GL.DetachShader(Handle, shaderHandle);
+ GL.DeleteShader(shaderHandle);
+ }
+
+ _shaderHandles = null;
+ }
+ }
+
+ public void Dispose()
+ {
+ if (Handle != 0)
+ {
+ DeleteShaders();
+ GL.DeleteProgram(Handle);
+
+ Handle = 0;
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Queries/BufferedQuery.cs b/src/Ryujinx.Graphics.OpenGL/Queries/BufferedQuery.cs
new file mode 100644
index 00000000..9d43f6c3
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Queries/BufferedQuery.cs
@@ -0,0 +1,120 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+namespace Ryujinx.Graphics.OpenGL.Queries
+{
+ class BufferedQuery : IDisposable
+ {
+ private const int MaxQueryRetries = 5000;
+ private const long DefaultValue = -1;
+ private const ulong HighMask = 0xFFFFFFFF00000000;
+
+ public int Query { get; }
+
+ private int _buffer;
+ private IntPtr _bufferMap;
+ private QueryTarget _type;
+
+ public BufferedQuery(QueryTarget type)
+ {
+ _buffer = GL.GenBuffer();
+ Query = GL.GenQuery();
+ _type = type;
+
+ GL.BindBuffer(BufferTarget.QueryBuffer, _buffer);
+
+ unsafe
+ {
+ long defaultValue = DefaultValue;
+ GL.BufferStorage(BufferTarget.QueryBuffer, sizeof(long), (IntPtr)(&defaultValue), BufferStorageFlags.MapReadBit | BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit);
+ }
+ _bufferMap = GL.MapBufferRange(BufferTarget.QueryBuffer, IntPtr.Zero, sizeof(long), BufferAccessMask.MapReadBit | BufferAccessMask.MapWriteBit | BufferAccessMask.MapPersistentBit);
+ }
+
+ public void Reset()
+ {
+ GL.EndQuery(_type);
+ GL.BeginQuery(_type, Query);
+ }
+
+ public void Begin()
+ {
+ GL.BeginQuery(_type, Query);
+ }
+
+ public unsafe void End(bool withResult)
+ {
+ GL.EndQuery(_type);
+
+ if (withResult)
+ {
+ GL.BindBuffer(BufferTarget.QueryBuffer, _buffer);
+
+ Marshal.WriteInt64(_bufferMap, -1L);
+ GL.GetQueryObject(Query, GetQueryObjectParam.QueryResult, (long*)0);
+ GL.MemoryBarrier(MemoryBarrierFlags.QueryBufferBarrierBit | MemoryBarrierFlags.ClientMappedBufferBarrierBit);
+ }
+ else
+ {
+ // Dummy result, just return 0.
+ Marshal.WriteInt64(_bufferMap, 0L);
+ }
+ }
+
+ private bool WaitingForValue(long data)
+ {
+ return data == DefaultValue ||
+ ((ulong)data & HighMask) == (unchecked((ulong)DefaultValue) & HighMask);
+ }
+
+ public bool TryGetResult(out long result)
+ {
+ result = Marshal.ReadInt64(_bufferMap);
+
+ return !WaitingForValue(result);
+ }
+
+ public long AwaitResult(AutoResetEvent wakeSignal = null)
+ {
+ long data = DefaultValue;
+
+ if (wakeSignal == null)
+ {
+ while (WaitingForValue(data))
+ {
+ data = Marshal.ReadInt64(_bufferMap);
+ }
+ }
+ else
+ {
+ int iterations = 0;
+ while (WaitingForValue(data) && iterations++ < MaxQueryRetries)
+ {
+ data = Marshal.ReadInt64(_bufferMap);
+ if (WaitingForValue(data))
+ {
+ wakeSignal.WaitOne(1);
+ }
+ }
+
+ if (iterations >= MaxQueryRetries)
+ {
+ Logger.Error?.Print(LogClass.Gpu, $"Error: Query result timed out. Took more than {MaxQueryRetries} tries.");
+ }
+ }
+
+ return data;
+ }
+
+ public void Dispose()
+ {
+ GL.BindBuffer(BufferTarget.QueryBuffer, _buffer);
+ GL.UnmapBuffer(BufferTarget.QueryBuffer);
+ GL.DeleteBuffer(_buffer);
+ GL.DeleteQuery(Query);
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs b/src/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs
new file mode 100644
index 00000000..e0aafa6f
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Queries/CounterQueue.cs
@@ -0,0 +1,229 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+using System.Collections.Generic;
+using System.Threading;
+
+namespace Ryujinx.Graphics.OpenGL.Queries
+{
+ class CounterQueue : IDisposable
+ {
+ private const int QueryPoolInitialSize = 100;
+
+ public CounterType Type { get; }
+ public bool Disposed { get; private set; }
+
+ private readonly Pipeline _pipeline;
+
+ private Queue<CounterQueueEvent> _events = new Queue<CounterQueueEvent>();
+ private CounterQueueEvent _current;
+
+ private ulong _accumulatedCounter;
+ private int _waiterCount;
+
+ private object _lock = new object();
+
+ private Queue<BufferedQuery> _queryPool;
+ private AutoResetEvent _queuedEvent = new AutoResetEvent(false);
+ private AutoResetEvent _wakeSignal = new AutoResetEvent(false);
+ private AutoResetEvent _eventConsumed = new AutoResetEvent(false);
+
+ private Thread _consumerThread;
+
+ internal CounterQueue(Pipeline pipeline, CounterType type)
+ {
+ Type = type;
+
+ _pipeline = pipeline;
+
+ QueryTarget glType = GetTarget(Type);
+
+ _queryPool = new Queue<BufferedQuery>(QueryPoolInitialSize);
+ for (int i = 0; i < QueryPoolInitialSize; i++)
+ {
+ _queryPool.Enqueue(new BufferedQuery(glType));
+ }
+
+ _current = new CounterQueueEvent(this, glType, 0);
+
+ _consumerThread = new Thread(EventConsumer);
+ _consumerThread.Start();
+ }
+
+ private void EventConsumer()
+ {
+ while (!Disposed)
+ {
+ CounterQueueEvent evt = null;
+ lock (_lock)
+ {
+ if (_events.Count > 0)
+ {
+ evt = _events.Dequeue();
+ }
+ }
+
+ if (evt == null)
+ {
+ _queuedEvent.WaitOne(); // No more events to go through, wait for more.
+ }
+ else
+ {
+ // Spin-wait rather than sleeping if there are any waiters, by passing null instead of the wake signal.
+ evt.TryConsume(ref _accumulatedCounter, true, _waiterCount == 0 ? _wakeSignal : null);
+ }
+
+ if (_waiterCount > 0)
+ {
+ _eventConsumed.Set();
+ }
+ }
+ }
+
+ internal BufferedQuery GetQueryObject()
+ {
+ // Creating/disposing query objects on a context we're sharing with will cause issues.
+ // So instead, make a lot of query objects on the main thread and reuse them.
+
+ lock (_lock)
+ {
+ if (_queryPool.Count > 0)
+ {
+ BufferedQuery result = _queryPool.Dequeue();
+ return result;
+ }
+ else
+ {
+ return new BufferedQuery(GetTarget(Type));
+ }
+ }
+ }
+
+ internal void ReturnQueryObject(BufferedQuery query)
+ {
+ lock (_lock)
+ {
+ _queryPool.Enqueue(query);
+ }
+ }
+
+ public CounterQueueEvent QueueReport(EventHandler<ulong> resultHandler, ulong lastDrawIndex, bool hostReserved)
+ {
+ CounterQueueEvent result;
+ ulong draws = lastDrawIndex - _current.DrawIndex;
+
+ lock (_lock)
+ {
+ // A query's result only matters if more than one draw was performed during it.
+ // Otherwise, dummy it out and return 0 immediately.
+
+ if (hostReserved)
+ {
+ // This counter event is guaranteed to be available for host conditional rendering.
+ _current.ReserveForHostAccess();
+ }
+
+ _current.Complete(draws > 0, _pipeline.GetCounterDivisor(Type));
+ _events.Enqueue(_current);
+
+ _current.OnResult += resultHandler;
+
+ result = _current;
+
+ _current = new CounterQueueEvent(this, GetTarget(Type), lastDrawIndex);
+ }
+
+ _queuedEvent.Set();
+
+ return result;
+ }
+
+ public void QueueReset()
+ {
+ lock (_lock)
+ {
+ _current.Clear();
+ }
+ }
+
+ private static QueryTarget GetTarget(CounterType type)
+ {
+ switch (type)
+ {
+ case CounterType.SamplesPassed: return QueryTarget.SamplesPassed;
+ case CounterType.PrimitivesGenerated: return QueryTarget.PrimitivesGenerated;
+ case CounterType.TransformFeedbackPrimitivesWritten: return QueryTarget.TransformFeedbackPrimitivesWritten;
+ }
+
+ return QueryTarget.SamplesPassed;
+ }
+
+ public void Flush(bool blocking)
+ {
+ if (!blocking)
+ {
+ // Just wake the consumer thread - it will update the queries.
+ _wakeSignal.Set();
+ return;
+ }
+
+ lock (_lock)
+ {
+ // Tell the queue to process all events.
+ while (_events.Count > 0)
+ {
+ CounterQueueEvent flush = _events.Peek();
+ if (!flush.TryConsume(ref _accumulatedCounter, true))
+ {
+ return; // If not blocking, then return when we encounter an event that is not ready yet.
+ }
+ _events.Dequeue();
+ }
+ }
+ }
+
+ public void FlushTo(CounterQueueEvent evt)
+ {
+ // Flush the counter queue on the main thread.
+
+ Interlocked.Increment(ref _waiterCount);
+
+ _wakeSignal.Set();
+
+ while (!evt.Disposed)
+ {
+ _eventConsumed.WaitOne(1);
+ }
+
+ Interlocked.Decrement(ref _waiterCount);
+ }
+
+ public void Dispose()
+ {
+ lock (_lock)
+ {
+ while (_events.Count > 0)
+ {
+ CounterQueueEvent evt = _events.Dequeue();
+
+ evt.Dispose();
+ }
+
+ Disposed = true;
+ }
+
+ _queuedEvent.Set();
+
+ _consumerThread.Join();
+
+ foreach (BufferedQuery query in _queryPool)
+ {
+ query.Dispose();
+ }
+
+ _queuedEvent.Dispose();
+ _wakeSignal.Dispose();
+ _eventConsumed.Dispose();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Queries/CounterQueueEvent.cs b/src/Ryujinx.Graphics.OpenGL/Queries/CounterQueueEvent.cs
new file mode 100644
index 00000000..7297baab
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Queries/CounterQueueEvent.cs
@@ -0,0 +1,163 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+using System.Threading;
+
+namespace Ryujinx.Graphics.OpenGL.Queries
+{
+ class CounterQueueEvent : ICounterEvent
+ {
+ public event EventHandler<ulong> OnResult;
+
+ public QueryTarget Type { get; }
+ public bool ClearCounter { get; private set; }
+ public int Query => _counter.Query;
+
+ public bool Disposed { get; private set; }
+ public bool Invalid { get; set; }
+
+ public ulong DrawIndex { get; }
+
+ private CounterQueue _queue;
+ private BufferedQuery _counter;
+
+ private bool _hostAccessReserved = false;
+ private int _refCount = 1; // Starts with a reference from the counter queue.
+
+ private object _lock = new object();
+ private ulong _result = ulong.MaxValue;
+ private double _divisor = 1f;
+
+ public CounterQueueEvent(CounterQueue queue, QueryTarget type, ulong drawIndex)
+ {
+ _queue = queue;
+
+ _counter = queue.GetQueryObject();
+ Type = type;
+
+ DrawIndex = drawIndex;
+
+ _counter.Begin();
+ }
+
+ internal void Clear()
+ {
+ _counter.Reset();
+ ClearCounter = true;
+ }
+
+ internal void Complete(bool withResult, double divisor)
+ {
+ _counter.End(withResult);
+
+ _divisor = divisor;
+ }
+
+ internal bool TryConsume(ref ulong result, bool block, AutoResetEvent wakeSignal = null)
+ {
+ lock (_lock)
+ {
+ if (Disposed)
+ {
+ return true;
+ }
+
+ if (ClearCounter || Type == QueryTarget.Timestamp)
+ {
+ result = 0;
+ }
+
+ long queryResult;
+
+ if (block)
+ {
+ queryResult = _counter.AwaitResult(wakeSignal);
+ }
+ else
+ {
+ if (!_counter.TryGetResult(out queryResult))
+ {
+ return false;
+ }
+ }
+
+ result += _divisor == 1 ? (ulong)queryResult : (ulong)Math.Ceiling(queryResult / _divisor);
+
+ _result = result;
+
+ OnResult?.Invoke(this, result);
+
+ Dispose(); // Return the our resources to the pool.
+
+ return true;
+ }
+ }
+
+ public void Flush()
+ {
+ if (Disposed)
+ {
+ return;
+ }
+
+ // Tell the queue to process all events up to this one.
+ _queue.FlushTo(this);
+ }
+
+ public void DecrementRefCount()
+ {
+ if (Interlocked.Decrement(ref _refCount) == 0)
+ {
+ DisposeInternal();
+ }
+ }
+
+ public bool ReserveForHostAccess()
+ {
+ if (_hostAccessReserved)
+ {
+ return true;
+ }
+
+ if (IsValueAvailable())
+ {
+ return false;
+ }
+
+ if (Interlocked.Increment(ref _refCount) == 1)
+ {
+ Interlocked.Decrement(ref _refCount);
+
+ return false;
+ }
+
+ _hostAccessReserved = true;
+
+ return true;
+ }
+
+ public void ReleaseHostAccess()
+ {
+ _hostAccessReserved = false;
+
+ DecrementRefCount();
+ }
+
+ private void DisposeInternal()
+ {
+ _queue.ReturnQueryObject(_counter);
+ }
+
+ private bool IsValueAvailable()
+ {
+ return _result != ulong.MaxValue || _counter.TryGetResult(out _);
+ }
+
+ public void Dispose()
+ {
+ Disposed = true;
+
+ DecrementRefCount();
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Queries/Counters.cs b/src/Ryujinx.Graphics.OpenGL/Queries/Counters.cs
new file mode 100644
index 00000000..e6c7ab2b
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Queries/Counters.cs
@@ -0,0 +1,57 @@
+using Ryujinx.Graphics.GAL;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL.Queries
+{
+ class Counters : IDisposable
+ {
+ private CounterQueue[] _counterQueues;
+
+ public Counters()
+ {
+ int count = Enum.GetNames<CounterType>().Length;
+
+ _counterQueues = new CounterQueue[count];
+ }
+
+ public void Initialize(Pipeline pipeline)
+ {
+ for (int index = 0; index < _counterQueues.Length; index++)
+ {
+ CounterType type = (CounterType)index;
+ _counterQueues[index] = new CounterQueue(pipeline, type);
+ }
+ }
+
+ public CounterQueueEvent QueueReport(CounterType type, EventHandler<ulong> resultHandler, ulong lastDrawIndex, bool hostReserved)
+ {
+ return _counterQueues[(int)type].QueueReport(resultHandler, lastDrawIndex, hostReserved);
+ }
+
+ public void QueueReset(CounterType type)
+ {
+ _counterQueues[(int)type].QueueReset();
+ }
+
+ public void Update()
+ {
+ foreach (var queue in _counterQueues)
+ {
+ queue.Flush(false);
+ }
+ }
+
+ public void Flush(CounterType type)
+ {
+ _counterQueues[(int)type].Flush(true);
+ }
+
+ public void Dispose()
+ {
+ foreach (var queue in _counterQueues)
+ {
+ queue.Dispose();
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Ryujinx.Graphics.OpenGL/ResourcePool.cs b/src/Ryujinx.Graphics.OpenGL/ResourcePool.cs
new file mode 100644
index 00000000..57231cd6
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/ResourcePool.cs
@@ -0,0 +1,122 @@
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+using System.Collections.Generic;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class DisposedTexture
+ {
+ public TextureCreateInfo Info;
+ public TextureView View;
+ public float ScaleFactor;
+ public int RemainingFrames;
+ }
+
+ /// <summary>
+ /// A structure for pooling resources that can be reused without recreation, such as textures.
+ /// </summary>
+ class ResourcePool : IDisposable
+ {
+ private const int DisposedLiveFrames = 2;
+
+ private readonly object _lock = new object();
+ private readonly Dictionary<TextureCreateInfo, List<DisposedTexture>> _textures = new Dictionary<TextureCreateInfo, List<DisposedTexture>>();
+
+ /// <summary>
+ /// Add a texture that is not being used anymore to the resource pool to be used later.
+ /// Both the texture's view and storage should be completely unused.
+ /// </summary>
+ /// <param name="view">The texture's view</param>
+ public void AddTexture(TextureView view)
+ {
+ lock (_lock)
+ {
+ List<DisposedTexture> list;
+ if (!_textures.TryGetValue(view.Info, out list))
+ {
+ list = new List<DisposedTexture>();
+ _textures.Add(view.Info, list);
+ }
+
+ list.Add(new DisposedTexture()
+ {
+ Info = view.Info,
+ View = view,
+ ScaleFactor = view.ScaleFactor,
+ RemainingFrames = DisposedLiveFrames
+ });
+ }
+ }
+
+ /// <summary>
+ /// Attempt to obtain a texture from the resource cache with the desired parameters.
+ /// </summary>
+ /// <param name="info">The creation info for the desired texture</param>
+ /// <param name="scaleFactor">The scale factor for the desired texture</param>
+ /// <returns>A TextureView with the description specified, or null if one was not found.</returns>
+ public TextureView GetTextureOrNull(TextureCreateInfo info, float scaleFactor)
+ {
+ lock (_lock)
+ {
+ List<DisposedTexture> list;
+ if (!_textures.TryGetValue(info, out list))
+ {
+ return null;
+ }
+
+ foreach (DisposedTexture texture in list)
+ {
+ if (scaleFactor == texture.ScaleFactor)
+ {
+ list.Remove(texture);
+ return texture.View;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /// <summary>
+ /// Update the pool, removing any resources that have expired.
+ /// </summary>
+ public void Tick()
+ {
+ lock (_lock)
+ {
+ foreach (List<DisposedTexture> list in _textures.Values)
+ {
+ for (int i = 0; i < list.Count; i++)
+ {
+ DisposedTexture tex = list[i];
+
+ if (--tex.RemainingFrames < 0)
+ {
+ tex.View.Dispose();
+ list.RemoveAt(i--);
+ }
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Disposes the resource pool.
+ /// </summary>
+ public void Dispose()
+ {
+ lock (_lock)
+ {
+ foreach (List<DisposedTexture> list in _textures.Values)
+ {
+ foreach (DisposedTexture texture in list)
+ {
+ texture.View.Dispose();
+ }
+ }
+ _textures.Clear();
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Ryujinx.Graphics.OpenGL.csproj b/src/Ryujinx.Graphics.OpenGL/Ryujinx.Graphics.OpenGL.csproj
new file mode 100644
index 00000000..2313cc68
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Ryujinx.Graphics.OpenGL.csproj
@@ -0,0 +1,32 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net7.0</TargetFramework>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="OpenTK.Graphics" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <EmbeddedResource Include="Effects\Textures\SmaaAreaTexture.bin" />
+ <EmbeddedResource Include="Effects\Textures\SmaaSearchTexture.bin" />
+ <EmbeddedResource Include="Effects\Shaders\fsr_sharpening.glsl" />
+ <EmbeddedResource Include="Effects\Shaders\fxaa.glsl" />
+ <EmbeddedResource Include="Effects\Shaders\smaa.hlsl" />
+ <EmbeddedResource Include="Effects\Shaders\smaa_blend.glsl" />
+ <EmbeddedResource Include="Effects\Shaders\smaa_edge.glsl" />
+ <EmbeddedResource Include="Effects\Shaders\smaa_neighbour.glsl" />
+ <EmbeddedResource Include="Effects\Shaders\ffx_fsr1.h" />
+ <EmbeddedResource Include="Effects\Shaders\ffx_a.h" />
+ <EmbeddedResource Include="Effects\Shaders\fsr_scaling.glsl" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\Ryujinx.Common\Ryujinx.Common.csproj" />
+ <ProjectReference Include="..\Ryujinx.Graphics.GAL\Ryujinx.Graphics.GAL.csproj" />
+ <ProjectReference Include="..\Ryujinx.Graphics.Shader\Ryujinx.Graphics.Shader.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/src/Ryujinx.Graphics.OpenGL/Sync.cs b/src/Ryujinx.Graphics.OpenGL/Sync.cs
new file mode 100644
index 00000000..58818e6a
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Sync.cs
@@ -0,0 +1,168 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Common.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class Sync : IDisposable
+ {
+ private class SyncHandle
+ {
+ public ulong ID;
+ public IntPtr Handle;
+ }
+
+ private ulong _firstHandle = 0;
+ private ClientWaitSyncFlags _syncFlags => HwCapabilities.RequiresSyncFlush ? ClientWaitSyncFlags.None : ClientWaitSyncFlags.SyncFlushCommandsBit;
+
+ private List<SyncHandle> _handles = new List<SyncHandle>();
+
+ public void Create(ulong id)
+ {
+ SyncHandle handle = new SyncHandle
+ {
+ ID = id,
+ Handle = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None)
+ };
+
+
+ if (HwCapabilities.RequiresSyncFlush)
+ {
+ // Force commands to flush up to the syncpoint.
+ GL.ClientWaitSync(handle.Handle, ClientWaitSyncFlags.SyncFlushCommandsBit, 0);
+ }
+
+ lock (_handles)
+ {
+ _handles.Add(handle);
+ }
+ }
+
+ public ulong GetCurrent()
+ {
+ lock (_handles)
+ {
+ ulong lastHandle = _firstHandle;
+
+ foreach (SyncHandle handle in _handles)
+ {
+ lock (handle)
+ {
+ if (handle.Handle == IntPtr.Zero)
+ {
+ continue;
+ }
+
+ if (handle.ID > lastHandle)
+ {
+ WaitSyncStatus syncResult = GL.ClientWaitSync(handle.Handle, _syncFlags, 0);
+
+ if (syncResult == WaitSyncStatus.AlreadySignaled)
+ {
+ lastHandle = handle.ID;
+ }
+ }
+ }
+ }
+
+ return lastHandle;
+ }
+ }
+
+ public void Wait(ulong id)
+ {
+ SyncHandle result = null;
+
+ lock (_handles)
+ {
+ if ((long)(_firstHandle - id) > 0)
+ {
+ return; // The handle has already been signalled or deleted.
+ }
+
+ foreach (SyncHandle handle in _handles)
+ {
+ if (handle.ID == id)
+ {
+ result = handle;
+ break;
+ }
+ }
+ }
+
+ if (result != null)
+ {
+ lock (result)
+ {
+ if (result.Handle == IntPtr.Zero)
+ {
+ return;
+ }
+
+ WaitSyncStatus syncResult = GL.ClientWaitSync(result.Handle, _syncFlags, 1000000000);
+
+ if (syncResult == WaitSyncStatus.TimeoutExpired)
+ {
+ Logger.Error?.PrintMsg(LogClass.Gpu, $"GL Sync Object {result.ID} failed to signal within 1000ms. Continuing...");
+ }
+ }
+ }
+ }
+
+ public void Cleanup()
+ {
+ // Iterate through handles and remove any that have already been signalled.
+
+ while (true)
+ {
+ SyncHandle first = null;
+ lock (_handles)
+ {
+ first = _handles.FirstOrDefault();
+ }
+
+ if (first == null) break;
+
+ WaitSyncStatus syncResult = GL.ClientWaitSync(first.Handle, _syncFlags, 0);
+
+ if (syncResult == WaitSyncStatus.AlreadySignaled)
+ {
+ // Delete the sync object.
+ lock (_handles)
+ {
+ lock (first)
+ {
+ _firstHandle = first.ID + 1;
+ _handles.RemoveAt(0);
+ GL.DeleteSync(first.Handle);
+ first.Handle = IntPtr.Zero;
+ }
+ }
+ } else
+ {
+ // This sync handle and any following have not been reached yet.
+ break;
+ }
+ }
+ }
+
+ public void Dispose()
+ {
+ lock (_handles)
+ {
+ foreach (SyncHandle handle in _handles)
+ {
+ lock (handle)
+ {
+ GL.DeleteSync(handle.Handle);
+ handle.Handle = IntPtr.Zero;
+ }
+ }
+
+ _handles.Clear();
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/VertexArray.cs b/src/Ryujinx.Graphics.OpenGL/VertexArray.cs
new file mode 100644
index 00000000..7d22033e
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/VertexArray.cs
@@ -0,0 +1,280 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using System;
+using System.Numerics;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class VertexArray : IDisposable
+ {
+ public int Handle { get; private set; }
+
+ private readonly VertexAttribDescriptor[] _vertexAttribs;
+ private readonly VertexBufferDescriptor[] _vertexBuffers;
+
+ private int _minVertexCount;
+
+ private uint _vertexAttribsInUse;
+ private uint _vertexBuffersInUse;
+ private uint _vertexBuffersLimited;
+
+ private BufferRange _indexBuffer;
+ private BufferHandle _tempIndexBuffer;
+ private BufferHandle _tempVertexBuffer;
+ private int _tempVertexBufferSize;
+
+ public VertexArray()
+ {
+ Handle = GL.GenVertexArray();
+
+ _vertexAttribs = new VertexAttribDescriptor[Constants.MaxVertexAttribs];
+ _vertexBuffers = new VertexBufferDescriptor[Constants.MaxVertexBuffers];
+
+ _tempIndexBuffer = Buffer.Create();
+ }
+
+ public void Bind()
+ {
+ GL.BindVertexArray(Handle);
+ }
+
+ public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
+ {
+ int minVertexCount = int.MaxValue;
+
+ int bindingIndex;
+ for (bindingIndex = 0; bindingIndex < vertexBuffers.Length; bindingIndex++)
+ {
+ VertexBufferDescriptor vb = vertexBuffers[bindingIndex];
+
+ if (vb.Buffer.Handle != BufferHandle.Null)
+ {
+ int vertexCount = vb.Stride <= 0 ? 0 : vb.Buffer.Size / vb.Stride;
+ if (minVertexCount > vertexCount)
+ {
+ minVertexCount = vertexCount;
+ }
+
+ GL.BindVertexBuffer(bindingIndex, vb.Buffer.Handle.ToInt32(), (IntPtr)vb.Buffer.Offset, vb.Stride);
+ GL.VertexBindingDivisor(bindingIndex, vb.Divisor);
+ _vertexBuffersInUse |= 1u << bindingIndex;
+ }
+ else
+ {
+ if ((_vertexBuffersInUse & (1u << bindingIndex)) != 0)
+ {
+ GL.BindVertexBuffer(bindingIndex, 0, IntPtr.Zero, 0);
+ _vertexBuffersInUse &= ~(1u << bindingIndex);
+ }
+ }
+
+ _vertexBuffers[bindingIndex] = vb;
+ }
+
+ _minVertexCount = minVertexCount;
+ }
+
+ public void SetVertexAttributes(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
+ {
+ int index = 0;
+
+ for (; index < vertexAttribs.Length; index++)
+ {
+ VertexAttribDescriptor attrib = vertexAttribs[index];
+
+ if (attrib.Equals(_vertexAttribs[index]))
+ {
+ continue;
+ }
+
+ FormatInfo fmtInfo = FormatTable.GetFormatInfo(attrib.Format);
+
+ if (attrib.IsZero)
+ {
+ // Disabling the attribute causes the shader to read a constant value.
+ // We currently set the constant to (0, 0, 0, 0).
+ DisableVertexAttrib(index);
+ }
+ else
+ {
+ EnableVertexAttrib(index);
+ }
+
+ int offset = attrib.Offset;
+ int size = fmtInfo.Components;
+
+ bool isFloat = fmtInfo.PixelType == PixelType.Float ||
+ fmtInfo.PixelType == PixelType.HalfFloat;
+
+ if (isFloat || fmtInfo.Normalized || fmtInfo.Scaled)
+ {
+ VertexAttribType type = (VertexAttribType)fmtInfo.PixelType;
+
+ GL.VertexAttribFormat(index, size, type, fmtInfo.Normalized, offset);
+ }
+ else
+ {
+ VertexAttribIntegerType type = (VertexAttribIntegerType)fmtInfo.PixelType;
+
+ GL.VertexAttribIFormat(index, size, type, offset);
+ }
+
+ GL.VertexAttribBinding(index, attrib.BufferIndex);
+
+ _vertexAttribs[index] = attrib;
+ }
+
+ for (; index < Constants.MaxVertexAttribs; index++)
+ {
+ DisableVertexAttrib(index);
+ }
+ }
+
+ public void SetIndexBuffer(BufferRange range)
+ {
+ _indexBuffer = range;
+ GL.BindBuffer(BufferTarget.ElementArrayBuffer, range.Handle.ToInt32());
+ }
+
+ public void SetRangeOfIndexBuffer()
+ {
+ Buffer.Resize(_tempIndexBuffer, _indexBuffer.Size);
+ Buffer.Copy(_indexBuffer.Handle, _tempIndexBuffer, _indexBuffer.Offset, 0, _indexBuffer.Size);
+ GL.BindBuffer(BufferTarget.ElementArrayBuffer, _tempIndexBuffer.ToInt32());
+ }
+
+ public void RestoreIndexBuffer()
+ {
+ GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBuffer.Handle.ToInt32());
+ }
+
+ public void PreDraw(int vertexCount)
+ {
+ LimitVertexBuffers(vertexCount);
+ }
+
+ public void PreDrawVbUnbounded()
+ {
+ UnlimitVertexBuffers();
+ }
+
+ public void LimitVertexBuffers(int vertexCount)
+ {
+ // Is it possible for the draw to fetch outside the bounds of any vertex buffer currently bound?
+
+ if (vertexCount <= _minVertexCount)
+ {
+ return;
+ }
+
+ // If the draw can fetch out of bounds, let's ensure that it will only fetch zeros rather than memory garbage.
+
+ int currentTempVbOffset = 0;
+ uint buffersInUse = _vertexBuffersInUse;
+
+ while (buffersInUse != 0)
+ {
+ int vbIndex = BitOperations.TrailingZeroCount(buffersInUse);
+
+ ref var vb = ref _vertexBuffers[vbIndex];
+
+ int requiredSize = vertexCount * vb.Stride;
+
+ if (vb.Buffer.Size < requiredSize)
+ {
+ BufferHandle tempVertexBuffer = EnsureTempVertexBufferSize(currentTempVbOffset + requiredSize);
+
+ Buffer.Copy(vb.Buffer.Handle, tempVertexBuffer, vb.Buffer.Offset, currentTempVbOffset, vb.Buffer.Size);
+ Buffer.Clear(tempVertexBuffer, currentTempVbOffset + vb.Buffer.Size, requiredSize - vb.Buffer.Size, 0);
+
+ GL.BindVertexBuffer(vbIndex, tempVertexBuffer.ToInt32(), (IntPtr)currentTempVbOffset, vb.Stride);
+
+ currentTempVbOffset += requiredSize;
+ _vertexBuffersLimited |= 1u << vbIndex;
+ }
+
+ buffersInUse &= ~(1u << vbIndex);
+ }
+ }
+
+ private BufferHandle EnsureTempVertexBufferSize(int size)
+ {
+ BufferHandle tempVertexBuffer = _tempVertexBuffer;
+
+ if (_tempVertexBufferSize < size)
+ {
+ _tempVertexBufferSize = size;
+
+ if (tempVertexBuffer == BufferHandle.Null)
+ {
+ tempVertexBuffer = Buffer.Create(size);
+ _tempVertexBuffer = tempVertexBuffer;
+ return tempVertexBuffer;
+ }
+
+ Buffer.Resize(_tempVertexBuffer, size);
+ }
+
+ return tempVertexBuffer;
+ }
+
+ public void UnlimitVertexBuffers()
+ {
+ uint buffersLimited = _vertexBuffersLimited;
+
+ if (buffersLimited == 0)
+ {
+ return;
+ }
+
+ while (buffersLimited != 0)
+ {
+ int vbIndex = BitOperations.TrailingZeroCount(buffersLimited);
+
+ ref var vb = ref _vertexBuffers[vbIndex];
+
+ GL.BindVertexBuffer(vbIndex, vb.Buffer.Handle.ToInt32(), (IntPtr)vb.Buffer.Offset, vb.Stride);
+
+ buffersLimited &= ~(1u << vbIndex);
+ }
+
+ _vertexBuffersLimited = 0;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private void EnableVertexAttrib(int index)
+ {
+ uint mask = 1u << index;
+
+ if ((_vertexAttribsInUse & mask) == 0)
+ {
+ _vertexAttribsInUse |= mask;
+ GL.EnableVertexAttribArray(index);
+ }
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private void DisableVertexAttrib(int index)
+ {
+ uint mask = 1u << index;
+
+ if ((_vertexAttribsInUse & mask) != 0)
+ {
+ _vertexAttribsInUse &= ~mask;
+ GL.DisableVertexAttribArray(index);
+ GL.VertexAttrib4(index, 0f, 0f, 0f, 1f);
+ }
+ }
+
+ public void Dispose()
+ {
+ if (Handle != 0)
+ {
+ GL.DeleteVertexArray(Handle);
+
+ Handle = 0;
+ }
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.OpenGL/Window.cs b/src/Ryujinx.Graphics.OpenGL/Window.cs
new file mode 100644
index 00000000..b37ec375
--- /dev/null
+++ b/src/Ryujinx.Graphics.OpenGL/Window.cs
@@ -0,0 +1,420 @@
+using OpenTK.Graphics.OpenGL;
+using Ryujinx.Graphics.GAL;
+using Ryujinx.Graphics.OpenGL.Effects;
+using Ryujinx.Graphics.OpenGL.Effects.Smaa;
+using Ryujinx.Graphics.OpenGL.Image;
+using System;
+
+namespace Ryujinx.Graphics.OpenGL
+{
+ class Window : IWindow, IDisposable
+ {
+ private readonly OpenGLRenderer _renderer;
+
+ private bool _initialized;
+
+ private int _width;
+ private int _height;
+ private bool _updateSize;
+ private int _copyFramebufferHandle;
+ private IPostProcessingEffect _antiAliasing;
+ private IScalingFilter _scalingFilter;
+ private bool _isLinear;
+ private AntiAliasing _currentAntiAliasing;
+ private bool _updateEffect;
+ private ScalingFilter _currentScalingFilter;
+ private float _scalingFilterLevel;
+ private bool _updateScalingFilter;
+ private bool _isBgra;
+ private TextureView _upscaledTexture;
+
+ internal BackgroundContextWorker BackgroundContext { get; private set; }
+
+ internal bool ScreenCaptureRequested { get; set; }
+
+ public Window(OpenGLRenderer renderer)
+ {
+ _renderer = renderer;
+ }
+
+ public void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
+ {
+ GL.Disable(EnableCap.FramebufferSrgb);
+
+ (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
+
+ CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop, swapBuffersCallback);
+
+ GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
+ GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
+
+ GL.Enable(EnableCap.FramebufferSrgb);
+
+ // Restore unpack alignment to 4, as performance overlays such as RTSS may change this to load their resources.
+ GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
+ }
+
+ public void ChangeVSyncMode(bool vsyncEnabled) { }
+
+ public void SetSize(int width, int height)
+ {
+ _width = width;
+ _height = height;
+
+ _updateSize = true;
+ }
+
+ private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop, Action swapBuffersCallback)
+ {
+ GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
+ GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
+
+ TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;
+
+ UpdateEffect();
+
+ if (_antiAliasing != null)
+ {
+ var oldView = viewConverted;
+
+ viewConverted = _antiAliasing.Run(viewConverted, _width, _height);
+
+ if (viewConverted.Format.IsBgr())
+ {
+ var swappedView = _renderer.TextureCopy.BgraSwap(viewConverted);
+
+ viewConverted?.Dispose();
+
+ viewConverted = swappedView;
+ }
+
+ if (viewConverted != oldView && oldView != view)
+ {
+ oldView.Dispose();
+ }
+ }
+
+ GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
+ GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
+
+ GL.FramebufferTexture(
+ FramebufferTarget.ReadFramebuffer,
+ FramebufferAttachment.ColorAttachment0,
+ viewConverted.Handle,
+ 0);
+
+ GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
+
+ GL.Disable(EnableCap.RasterizerDiscard);
+ GL.Disable(IndexedEnableCap.ScissorTest, 0);
+
+ GL.Clear(ClearBufferMask.ColorBufferBit);
+
+ int srcX0, srcX1, srcY0, srcY1;
+ float scale = viewConverted.ScaleFactor;
+
+ if (crop.Left == 0 && crop.Right == 0)
+ {
+ srcX0 = 0;
+ srcX1 = (int)(viewConverted.Width / scale);
+ }
+ else
+ {
+ srcX0 = crop.Left;
+ srcX1 = crop.Right;
+ }
+
+ if (crop.Top == 0 && crop.Bottom == 0)
+ {
+ srcY0 = 0;
+ srcY1 = (int)(viewConverted.Height / scale);
+ }
+ else
+ {
+ srcY0 = crop.Top;
+ srcY1 = crop.Bottom;
+ }
+
+ if (scale != 1f)
+ {
+ srcX0 = (int)(srcX0 * scale);
+ srcY0 = (int)(srcY0 * scale);
+ srcX1 = (int)Math.Ceiling(srcX1 * scale);
+ srcY1 = (int)Math.Ceiling(srcY1 * scale);
+ }
+
+ float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
+ float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
+
+ int dstWidth = (int)(_width * ratioX);
+ int dstHeight = (int)(_height * ratioY);
+
+ int dstPaddingX = (_width - dstWidth) / 2;
+ int dstPaddingY = (_height - dstHeight) / 2;
+
+ int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
+ int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
+
+ int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
+ int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
+
+ if (ScreenCaptureRequested)
+ {
+ CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
+
+ ScreenCaptureRequested = false;
+ }
+
+ if (_scalingFilter != null)
+ {
+ if (viewConverted.Format.IsBgr() && !_isBgra)
+ {
+ RecreateUpscalingTexture(true);
+ }
+
+ _scalingFilter.Run(
+ viewConverted,
+ _upscaledTexture,
+ _width,
+ _height,
+ new Extents2D(
+ srcX0,
+ srcY0,
+ srcX1,
+ srcY1),
+ new Extents2D(
+ dstX0,
+ dstY0,
+ dstX1,
+ dstY1)
+ );
+
+ srcX0 = dstX0;
+ srcY0 = dstY0;
+ srcX1 = dstX1;
+ srcY1 = dstY1;
+
+ GL.FramebufferTexture(
+ FramebufferTarget.ReadFramebuffer,
+ FramebufferAttachment.ColorAttachment0,
+ _upscaledTexture.Handle,
+ 0);
+ }
+
+ GL.BlitFramebuffer(
+ srcX0,
+ srcY0,
+ srcX1,
+ srcY1,
+ dstX0,
+ dstY0,
+ dstX1,
+ dstY1,
+ ClearBufferMask.ColorBufferBit,
+ _isLinear ? BlitFramebufferFilter.Linear : BlitFramebufferFilter.Nearest);
+
+ // Remove Alpha channel
+ GL.ColorMask(false, false, false, true);
+ GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ GL.Clear(ClearBufferMask.ColorBufferBit);
+
+ for (int i = 0; i < Constants.MaxRenderTargets; i++)
+ {
+ ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
+ }
+
+ // Set clip control, viewport and the framebuffer to the output to placate overlays and OBS capture.
+ GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.NegativeOneToOne);
+ GL.Viewport(0, 0, _width, _height);
+ GL.BindFramebuffer(FramebufferTarget.Framebuffer, drawFramebuffer);
+
+ swapBuffersCallback();
+
+ ((Pipeline)_renderer.Pipeline).RestoreClipControl();
+ ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
+ ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
+ ((Pipeline)_renderer.Pipeline).RestoreViewport0();
+
+ if (viewConverted != view)
+ {
+ viewConverted.Dispose();
+ }
+ }
+
+ private int GetCopyFramebufferHandleLazy()
+ {
+ int handle = _copyFramebufferHandle;
+
+ if (handle == 0)
+ {
+ handle = GL.GenFramebuffer();
+
+ _copyFramebufferHandle = handle;
+ }
+
+ return handle;
+ }
+
+ public void InitializeBackgroundContext(IOpenGLContext baseContext)
+ {
+ BackgroundContext = new BackgroundContextWorker(baseContext);
+ _initialized = true;
+ }
+
+ public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
+ {
+ long size = Math.Abs(4 * width * height);
+ byte[] bitmap = new byte[size];
+
+ GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
+
+ _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
+ }
+
+ public void Dispose()
+ {
+ if (!_initialized)
+ {
+ return;
+ }
+
+ BackgroundContext.Dispose();
+
+ if (_copyFramebufferHandle != 0)
+ {
+ GL.DeleteFramebuffer(_copyFramebufferHandle);
+
+ _copyFramebufferHandle = 0;
+ }
+
+ _antiAliasing?.Dispose();
+ _scalingFilter?.Dispose();
+ _upscaledTexture?.Dispose();
+ }
+
+ public void SetAntiAliasing(AntiAliasing effect)
+ {
+ if (_currentAntiAliasing == effect && _antiAliasing != null)
+ {
+ return;
+ }
+
+ _currentAntiAliasing = effect;
+
+ _updateEffect = true;
+ }
+
+ public void SetScalingFilter(ScalingFilter type)
+ {
+ if (_currentScalingFilter == type && _antiAliasing != null)
+ {
+ return;
+ }
+
+ _currentScalingFilter = type;
+
+ _updateScalingFilter = true;
+ }
+
+ private void UpdateEffect()
+ {
+ if (_updateEffect)
+ {
+ _updateEffect = false;
+
+ switch (_currentAntiAliasing)
+ {
+ case AntiAliasing.Fxaa:
+ _antiAliasing?.Dispose();
+ _antiAliasing = new FxaaPostProcessingEffect(_renderer);
+ break;
+ case AntiAliasing.None:
+ _antiAliasing?.Dispose();
+ _antiAliasing = null;
+ break;
+ case AntiAliasing.SmaaLow:
+ case AntiAliasing.SmaaMedium:
+ case AntiAliasing.SmaaHigh:
+ case AntiAliasing.SmaaUltra:
+ var quality = _currentAntiAliasing - AntiAliasing.SmaaLow;
+ if (_antiAliasing is SmaaPostProcessingEffect smaa)
+ {
+ smaa.Quality = quality;
+ }
+ else
+ {
+ _antiAliasing?.Dispose();
+ _antiAliasing = new SmaaPostProcessingEffect(_renderer, quality);
+ }
+ break;
+ }
+ }
+
+ if (_updateSize && !_updateScalingFilter)
+ {
+ RecreateUpscalingTexture();
+ }
+
+ _updateSize = false;
+
+ if (_updateScalingFilter)
+ {
+ _updateScalingFilter = false;
+
+ switch (_currentScalingFilter)
+ {
+ case ScalingFilter.Bilinear:
+ case ScalingFilter.Nearest:
+ _scalingFilter?.Dispose();
+ _scalingFilter = null;
+ _isLinear = _currentScalingFilter == ScalingFilter.Bilinear;
+ _upscaledTexture?.Dispose();
+ _upscaledTexture = null;
+ break;
+ case ScalingFilter.Fsr:
+ if (_scalingFilter is not FsrScalingFilter)
+ {
+ _scalingFilter?.Dispose();
+ _scalingFilter = new FsrScalingFilter(_renderer, _antiAliasing);
+ }
+ _isLinear = false;
+ _scalingFilter.Level = _scalingFilterLevel;
+
+ RecreateUpscalingTexture();
+ break;
+ }
+ }
+ }
+
+ private void RecreateUpscalingTexture(bool forceBgra = false)
+ {
+ _upscaledTexture?.Dispose();
+
+ var info = new TextureCreateInfo(
+ _width,
+ _height,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ Format.R8G8B8A8Unorm,
+ DepthStencilMode.Depth,
+ Target.Texture2D,
+ forceBgra ? SwizzleComponent.Blue : SwizzleComponent.Red,
+ SwizzleComponent.Green,
+ forceBgra ? SwizzleComponent.Red : SwizzleComponent.Blue,
+ SwizzleComponent.Alpha);
+
+ _isBgra = forceBgra;
+ _upscaledTexture = _renderer.CreateTexture(info, 1) as TextureView;
+ }
+
+ public void SetScalingFilterLevel(float level)
+ {
+ _scalingFilterLevel = level;
+ _updateScalingFilter = true;
+ }
+ }
+} \ No newline at end of file