2024-05-18 18:54:55 -04:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Shader;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using SharpMetal.Metal;
|
|
|
|
using System;
|
2024-05-24 09:28:16 -04:00
|
|
|
using System.Collections.Generic;
|
2024-05-18 18:54:55 -04:00
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Metal
|
|
|
|
{
|
|
|
|
[SupportedOSPlatform("macos")]
|
|
|
|
class HelperShader : IDisposable
|
|
|
|
{
|
|
|
|
private const string ShadersSourcePath = "/Ryujinx.Graphics.Metal/Shaders";
|
2024-05-18 20:40:37 -04:00
|
|
|
private readonly Pipeline _pipeline;
|
2024-05-18 18:54:55 -04:00
|
|
|
private MTLDevice _device;
|
|
|
|
|
2024-05-27 09:47:50 -04:00
|
|
|
private readonly ISampler _samplerLinear;
|
|
|
|
private readonly ISampler _samplerNearest;
|
2024-05-18 18:54:55 -04:00
|
|
|
private readonly IProgram _programColorBlit;
|
2024-05-24 09:28:16 -04:00
|
|
|
private readonly List<IProgram> _programsColorClear = new();
|
2024-05-18 18:54:55 -04:00
|
|
|
private readonly IProgram _programDepthStencilClear;
|
|
|
|
|
|
|
|
public HelperShader(MTLDevice device, Pipeline pipeline)
|
|
|
|
{
|
|
|
|
_device = device;
|
|
|
|
_pipeline = pipeline;
|
|
|
|
|
2024-05-27 09:47:50 -04:00
|
|
|
_samplerNearest = new Sampler(_device, SamplerCreateInfo.Create(MinFilter.Nearest, MagFilter.Nearest));
|
|
|
|
_samplerLinear = new Sampler(_device, SamplerCreateInfo.Create(MinFilter.Linear, MagFilter.Linear));
|
|
|
|
|
2024-05-18 18:54:55 -04:00
|
|
|
var blitSource = ReadMsl("Blit.metal");
|
|
|
|
_programColorBlit = new Program(
|
|
|
|
[
|
|
|
|
new ShaderSource(blitSource, ShaderStage.Fragment, TargetLanguage.Msl),
|
|
|
|
new ShaderSource(blitSource, ShaderStage.Vertex, TargetLanguage.Msl)
|
|
|
|
], device);
|
|
|
|
|
2024-05-22 17:21:44 -04:00
|
|
|
var colorClearSource = ReadMsl("ColorClear.metal");
|
2024-05-24 09:28:16 -04:00
|
|
|
for (int i = 0; i < Constants.MaxColorAttachments; i++)
|
|
|
|
{
|
|
|
|
var crntSource = colorClearSource.Replace("COLOR_ATTACHMENT_INDEX", i.ToString());
|
|
|
|
_programsColorClear.Add(new Program(
|
|
|
|
[
|
|
|
|
new ShaderSource(crntSource, ShaderStage.Fragment, TargetLanguage.Msl),
|
|
|
|
new ShaderSource(crntSource, ShaderStage.Vertex, TargetLanguage.Msl)
|
|
|
|
], device));
|
|
|
|
}
|
2024-05-22 17:21:44 -04:00
|
|
|
|
2024-05-22 20:26:54 -04:00
|
|
|
var depthStencilClearSource = ReadMsl("DepthStencilClear.metal");
|
|
|
|
_programDepthStencilClear = new Program(
|
|
|
|
[
|
|
|
|
new ShaderSource(depthStencilClearSource, ShaderStage.Fragment, TargetLanguage.Msl),
|
|
|
|
new ShaderSource(depthStencilClearSource, ShaderStage.Vertex, TargetLanguage.Msl)
|
|
|
|
], device);
|
2024-05-18 18:54:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static string ReadMsl(string fileName)
|
|
|
|
{
|
|
|
|
return EmbeddedResources.ReadAllText(string.Join('/', ShadersSourcePath, fileName));
|
|
|
|
}
|
|
|
|
|
2024-05-27 09:47:50 -04:00
|
|
|
public unsafe void BlitColor(
|
|
|
|
ITexture src,
|
|
|
|
ITexture dst,
|
|
|
|
Extents2D srcRegion,
|
|
|
|
Extents2D dstRegion,
|
|
|
|
bool linearFilter)
|
2024-05-18 18:54:55 -04:00
|
|
|
{
|
2024-05-27 09:47:50 -04:00
|
|
|
const int RegionBufferSize = 16;
|
|
|
|
|
|
|
|
var sampler = linearFilter ? _samplerLinear : _samplerNearest;
|
|
|
|
|
|
|
|
Span<float> region = stackalloc float[RegionBufferSize / sizeof(float)];
|
|
|
|
|
2024-05-27 16:54:26 -04:00
|
|
|
region[0] = srcRegion.X1 / (float)src.Width;
|
|
|
|
region[1] = srcRegion.X2 / (float)src.Width;
|
|
|
|
region[2] = srcRegion.Y1 / (float)src.Height;
|
|
|
|
region[3] = srcRegion.Y2 / (float)src.Height;
|
2024-05-27 09:47:50 -04:00
|
|
|
|
|
|
|
if (dstRegion.X1 > dstRegion.X2)
|
2024-05-18 18:54:55 -04:00
|
|
|
{
|
2024-05-27 09:47:50 -04:00
|
|
|
(region[0], region[1]) = (region[1], region[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dstRegion.Y1 > dstRegion.Y2)
|
|
|
|
{
|
|
|
|
(region[2], region[3]) = (region[3], region[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var rect = new Rectangle<float>(
|
|
|
|
MathF.Min(dstRegion.X1, dstRegion.X2),
|
|
|
|
MathF.Min(dstRegion.Y1, dstRegion.Y2),
|
|
|
|
MathF.Abs(dstRegion.X2 - dstRegion.X1),
|
|
|
|
MathF.Abs(dstRegion.Y2 - dstRegion.Y1));
|
|
|
|
|
|
|
|
Span<Viewport> viewports = stackalloc Viewport[1];
|
|
|
|
|
|
|
|
viewports[0] = new Viewport(
|
|
|
|
rect,
|
|
|
|
ViewportSwizzle.PositiveX,
|
|
|
|
ViewportSwizzle.PositiveY,
|
|
|
|
ViewportSwizzle.PositiveZ,
|
|
|
|
ViewportSwizzle.PositiveW,
|
|
|
|
0f,
|
|
|
|
1f);
|
|
|
|
|
|
|
|
int dstWidth = dst.Width;
|
|
|
|
int dstHeight = dst.Height;
|
2024-05-18 18:54:55 -04:00
|
|
|
|
2024-05-24 09:09:06 -04:00
|
|
|
// Save current state
|
2024-05-27 07:58:03 -04:00
|
|
|
_pipeline.SaveAndResetState();
|
2024-05-24 09:09:06 -04:00
|
|
|
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.SetProgram(_programColorBlit);
|
2024-05-27 09:47:50 -04:00
|
|
|
_pipeline.SetViewports(viewports);
|
|
|
|
_pipeline.SetScissors(stackalloc Rectangle<int>[] { new Rectangle<int>(0, 0, dstWidth, dstHeight) });
|
|
|
|
_pipeline.SetRenderTargets([dst], null);
|
|
|
|
_pipeline.SetClearLoadAction(true);
|
|
|
|
_pipeline.SetTextureAndSampler(ShaderStage.Fragment, 0, src, sampler);
|
|
|
|
_pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
|
|
|
|
|
|
|
|
fixed (float* ptr = region)
|
|
|
|
{
|
|
|
|
_pipeline.GetOrCreateRenderEncoder().SetVertexBytes((IntPtr)ptr, RegionBufferSize, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_pipeline.Draw(4, 1, 0, 0);
|
|
|
|
|
|
|
|
// Restore previous state
|
|
|
|
_pipeline.RestoreState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public unsafe void DrawTexture(
|
|
|
|
ITexture src,
|
|
|
|
ISampler srcSampler,
|
|
|
|
Extents2DF srcRegion,
|
|
|
|
Extents2DF dstRegion)
|
|
|
|
{
|
|
|
|
const int RegionBufferSize = 16;
|
|
|
|
|
|
|
|
Span<float> region = stackalloc float[RegionBufferSize / sizeof(float)];
|
|
|
|
|
|
|
|
region[0] = srcRegion.X1 / src.Width;
|
|
|
|
region[1] = srcRegion.X2 / src.Width;
|
|
|
|
region[2] = srcRegion.Y1 / src.Height;
|
|
|
|
region[3] = srcRegion.Y2 / src.Height;
|
|
|
|
|
|
|
|
if (dstRegion.X1 > dstRegion.X2)
|
|
|
|
{
|
|
|
|
(region[0], region[1]) = (region[1], region[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dstRegion.Y1 > dstRegion.Y2)
|
|
|
|
{
|
|
|
|
(region[2], region[3]) = (region[3], region[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Span<Viewport> viewports = stackalloc Viewport[1];
|
|
|
|
Span<Rectangle<int>> scissors = stackalloc Rectangle<int>[1];
|
|
|
|
|
|
|
|
var rect = new Rectangle<float>(
|
|
|
|
MathF.Min(dstRegion.X1, dstRegion.X2),
|
|
|
|
MathF.Min(dstRegion.Y1, dstRegion.Y2),
|
|
|
|
MathF.Abs(dstRegion.X2 - dstRegion.X1),
|
|
|
|
MathF.Abs(dstRegion.Y2 - dstRegion.Y1));
|
|
|
|
|
|
|
|
viewports[0] = new Viewport(
|
|
|
|
rect,
|
|
|
|
ViewportSwizzle.PositiveX,
|
|
|
|
ViewportSwizzle.PositiveY,
|
|
|
|
ViewportSwizzle.PositiveZ,
|
|
|
|
ViewportSwizzle.PositiveW,
|
|
|
|
0f,
|
|
|
|
1f);
|
|
|
|
|
|
|
|
scissors[0] = new Rectangle<int>(0, 0, 0xFFFF, 0xFFFF);
|
|
|
|
|
|
|
|
// Save current state
|
|
|
|
_pipeline.SaveState();
|
|
|
|
|
|
|
|
_pipeline.SetProgram(_programColorBlit);
|
|
|
|
_pipeline.SetViewports(viewports);
|
|
|
|
_pipeline.SetScissors(scissors);
|
|
|
|
_pipeline.SetTextureAndSampler(ShaderStage.Fragment, 0, src, srcSampler);
|
|
|
|
_pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
|
|
|
|
_pipeline.SetFaceCulling(false, Face.FrontAndBack);
|
|
|
|
// For some reason this results in a SIGSEGV
|
|
|
|
// _pipeline.SetStencilTest(CreateStencilTestDescriptor(false));
|
|
|
|
_pipeline.SetDepthTest(new DepthTestDescriptor(false, false, CompareOp.Always));
|
|
|
|
|
|
|
|
fixed (float* ptr = region)
|
|
|
|
{
|
|
|
|
_pipeline.GetOrCreateRenderEncoder().SetVertexBytes((IntPtr)ptr, RegionBufferSize, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_pipeline.Draw(4, 1, 0, 0);
|
2024-05-24 09:09:06 -04:00
|
|
|
|
|
|
|
// Restore previous state
|
|
|
|
_pipeline.RestoreState();
|
2024-05-18 18:54:55 -04:00
|
|
|
}
|
|
|
|
|
2024-05-22 17:21:44 -04:00
|
|
|
public unsafe void ClearColor(
|
2024-05-24 09:28:16 -04:00
|
|
|
int index,
|
2024-05-30 08:20:16 -04:00
|
|
|
ReadOnlySpan<float> clearColor,
|
|
|
|
uint componentMask,
|
|
|
|
int dstWidth,
|
|
|
|
int dstHeight)
|
2024-05-18 18:54:55 -04:00
|
|
|
{
|
2024-05-22 17:21:44 -04:00
|
|
|
const int ClearColorBufferSize = 16;
|
2024-05-18 18:54:55 -04:00
|
|
|
|
2024-05-24 09:09:06 -04:00
|
|
|
// Save current state
|
|
|
|
_pipeline.SaveState();
|
|
|
|
|
2024-05-30 08:20:16 -04:00
|
|
|
Span<Viewport> viewports = stackalloc Viewport[1];
|
|
|
|
|
|
|
|
// TODO: Set exact viewport!
|
|
|
|
viewports[0] = new Viewport(
|
|
|
|
new Rectangle<float>(0, 0, dstWidth, dstHeight),
|
|
|
|
ViewportSwizzle.PositiveX,
|
|
|
|
ViewportSwizzle.PositiveY,
|
|
|
|
ViewportSwizzle.PositiveZ,
|
|
|
|
ViewportSwizzle.PositiveW,
|
|
|
|
0f,
|
|
|
|
1f);
|
|
|
|
|
2024-05-24 09:28:16 -04:00
|
|
|
_pipeline.SetProgram(_programsColorClear[index]);
|
2024-05-27 07:58:03 -04:00
|
|
|
_pipeline.SetBlendState(index, new BlendDescriptor(false, new ColorF(0f, 0f, 0f, 1f), BlendOp.Add, BlendFactor.One, BlendFactor.Zero, BlendOp.Add, BlendFactor.One, BlendFactor.Zero));
|
2024-05-25 03:51:56 -04:00
|
|
|
_pipeline.SetFaceCulling(false, Face.Front);
|
|
|
|
_pipeline.SetDepthTest(new DepthTestDescriptor(false, false, CompareOp.Always));
|
2024-05-30 08:20:16 -04:00
|
|
|
_pipeline.SetRenderTargetColorMasks([componentMask]);
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
|
2024-05-30 08:20:16 -04:00
|
|
|
_pipeline.SetViewports(viewports);
|
2024-05-27 07:58:03 -04:00
|
|
|
|
|
|
|
fixed (float* ptr = clearColor)
|
|
|
|
{
|
|
|
|
_pipeline.GetOrCreateRenderEncoder().SetFragmentBytes((IntPtr)ptr, ClearColorBufferSize, 0);
|
|
|
|
}
|
|
|
|
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.Draw(4, 1, 0, 0);
|
2024-05-24 09:09:06 -04:00
|
|
|
|
|
|
|
// Restore previous state
|
|
|
|
_pipeline.RestoreState();
|
2024-05-18 18:54:55 -04:00
|
|
|
}
|
|
|
|
|
2024-05-22 20:26:54 -04:00
|
|
|
public unsafe void ClearDepthStencil(
|
2024-05-27 07:58:03 -04:00
|
|
|
float depthValue,
|
2024-05-18 18:54:55 -04:00
|
|
|
bool depthMask,
|
|
|
|
int stencilValue,
|
2024-05-30 08:20:16 -04:00
|
|
|
int stencilMask,
|
|
|
|
int dstWidth,
|
|
|
|
int dstHeight)
|
2024-05-18 18:54:55 -04:00
|
|
|
{
|
2024-05-27 07:58:03 -04:00
|
|
|
const int ClearDepthBufferSize = 4;
|
2024-05-22 20:26:54 -04:00
|
|
|
|
2024-05-27 07:58:03 -04:00
|
|
|
IntPtr ptr = new(&depthValue);
|
2024-05-22 20:26:54 -04:00
|
|
|
|
2024-05-24 09:09:06 -04:00
|
|
|
// Save current state
|
|
|
|
_pipeline.SaveState();
|
|
|
|
|
2024-05-30 08:20:16 -04:00
|
|
|
Span<Viewport> viewports = stackalloc Viewport[1];
|
|
|
|
|
|
|
|
viewports[0] = new Viewport(
|
|
|
|
new Rectangle<float>(0, 0, dstWidth, dstHeight),
|
|
|
|
ViewportSwizzle.PositiveX,
|
|
|
|
ViewportSwizzle.PositiveY,
|
|
|
|
ViewportSwizzle.PositiveZ,
|
|
|
|
ViewportSwizzle.PositiveW,
|
|
|
|
0f,
|
|
|
|
1f);
|
|
|
|
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.SetProgram(_programDepthStencilClear);
|
2024-05-25 03:51:56 -04:00
|
|
|
_pipeline.SetFaceCulling(false, Face.Front);
|
|
|
|
_pipeline.SetDepthTest(new DepthTestDescriptor(false, false, CompareOp.Always));
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
|
2024-05-30 08:20:16 -04:00
|
|
|
_pipeline.SetViewports(viewports);
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.SetDepthTest(new DepthTestDescriptor(true, depthMask, CompareOp.Always));
|
2024-05-22 20:26:54 -04:00
|
|
|
// _pipeline.SetStencilTest(CreateStencilTestDescriptor(stencilMask != 0, stencilValue, 0xFF, stencilMask));
|
2024-05-27 07:58:03 -04:00
|
|
|
_pipeline.GetOrCreateRenderEncoder().SetFragmentBytes(ptr, ClearDepthBufferSize, 0);
|
2024-05-18 18:54:55 -04:00
|
|
|
_pipeline.Draw(4, 1, 0, 0);
|
2024-05-24 09:09:06 -04:00
|
|
|
|
|
|
|
// Restore previous state
|
|
|
|
_pipeline.RestoreState();
|
2024-05-18 18:54:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static StencilTestDescriptor CreateStencilTestDescriptor(
|
|
|
|
bool enabled,
|
|
|
|
int refValue = 0,
|
|
|
|
int compareMask = 0xff,
|
|
|
|
int writeMask = 0xff)
|
|
|
|
{
|
|
|
|
return new StencilTestDescriptor(
|
|
|
|
enabled,
|
|
|
|
CompareOp.Always,
|
|
|
|
StencilOp.Replace,
|
|
|
|
StencilOp.Replace,
|
|
|
|
StencilOp.Replace,
|
|
|
|
refValue,
|
|
|
|
compareMask,
|
|
|
|
writeMask,
|
|
|
|
CompareOp.Always,
|
|
|
|
StencilOp.Replace,
|
|
|
|
StencilOp.Replace,
|
|
|
|
StencilOp.Replace,
|
|
|
|
refValue,
|
|
|
|
compareMask,
|
|
|
|
writeMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_programColorBlit.Dispose();
|
2024-05-24 09:28:16 -04:00
|
|
|
foreach (var programColorClear in _programsColorClear)
|
|
|
|
{
|
|
|
|
programColorClear.Dispose();
|
|
|
|
}
|
2024-05-18 18:54:55 -04:00
|
|
|
_programDepthStencilClear.Dispose();
|
|
|
|
_pipeline.Dispose();
|
2024-05-27 09:47:50 -04:00
|
|
|
_samplerLinear.Dispose();
|
|
|
|
_samplerNearest.Dispose();
|
2024-05-18 18:54:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|