Seizure my beloved is working

This commit is contained in:
Isaac Marovitz 2023-08-02 19:56:59 -04:00 committed by Isaac Marovitz
parent 6d722d83ba
commit aaa140e510
5 changed files with 109 additions and 39 deletions

View file

@ -4,6 +4,7 @@ using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Shader;
using SharpMetal.Foundation;
using SharpMetal.Metal;
using SharpMetal.QuartzCore;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
@ -28,8 +29,10 @@ namespace Ryujinx.Graphics.Metal
private MTLBuffer _indexBuffer;
private MTLIndexType _indexType;
private ulong _indexBufferOffset;
private MTLClearColor _clearColor = new() { alpha = 1.0f };
private int frameCount = 0;
public Pipeline(MTLDevice device, MTLCommandQueue commandQueue)
public Pipeline(MTLDevice device, MTLCommandQueue commandQueue, CAMetalLayer metalLayer)
{
_device = device;
_mtlCommandQueue = commandQueue;
@ -49,13 +52,17 @@ namespace Ryujinx.Graphics.Metal
// TODO: Recreate descriptor and encoder state as needed
var renderPipelineDescriptor = new MTLRenderPipelineDescriptor();
renderPipelineDescriptor.VertexFunction = vertexFunction;
renderPipelineDescriptor.FragmentFunction = fragmentFunction;
// renderPipelineDescriptor.FragmentFunction = fragmentFunction;
// TODO: This should not be hardcoded, but a bug in SharpMetal prevents me from doing this correctly
renderPipelineDescriptor.ColorAttachments.Object(0).PixelFormat = MTLPixelFormat.BGRA8Unorm;
_renderEncoderState = new(_device.NewRenderPipelineState(renderPipelineDescriptor, ref error), _device);
var renderPipelineState = _device.NewRenderPipelineState(renderPipelineDescriptor, ref error);
if (error != IntPtr.Zero)
{
Logger.Error?.PrintMsg(LogClass.Gpu, $"Failed to create Render Pipeline State: {StringHelper.String(error.LocalizedDescription)}");
}
_renderEncoderState = new RenderEncoderState(renderPipelineState, _device);
//
_commandBuffer = _mtlCommandQueue.CommandBuffer();
@ -68,6 +75,7 @@ namespace Ryujinx.Graphics.Metal
_currentEncoder.EndEncoding();
_currentEncoder = null;
}
Logger.Warning?.Print(LogClass.Gpu, "Current pass ended");
}
public MTLRenderCommandEncoder BeginRenderPass()
@ -77,6 +85,7 @@ namespace Ryujinx.Graphics.Metal
var descriptor = new MTLRenderPassDescriptor();
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
_renderEncoderState.SetEncoderState(renderCommandEncoder);
Logger.Warning?.Print(LogClass.Gpu, "Began render pass");
_currentEncoder = renderCommandEncoder;
return renderCommandEncoder;
@ -88,6 +97,7 @@ namespace Ryujinx.Graphics.Metal
var descriptor = new MTLBlitPassDescriptor();
var blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
Logger.Warning?.Print(LogClass.Gpu, "Began blit pass");
_currentEncoder = blitCommandEncoder;
return blitCommandEncoder;
@ -99,18 +109,43 @@ namespace Ryujinx.Graphics.Metal
var descriptor = new MTLComputePassDescriptor();
var computeCommandEncoder = _commandBuffer.ComputeCommandEncoder(descriptor);
Logger.Warning?.Print(LogClass.Gpu, "Began compute pass");
_currentEncoder = computeCommandEncoder;
return computeCommandEncoder;
}
public void Present()
public void Present(CAMetalDrawable drawable)
{
EndCurrentPass();
// TODO: Give command buffer a valid MTLDrawable
// _commandBuffer.PresentDrawable();
// _commandBuffer.Commit();
var descriptor = new MTLRenderPassDescriptor();
descriptor.ColorAttachments.Object(0).Texture = drawable.Texture;
descriptor.ColorAttachments.Object(0).LoadAction = MTLLoadAction.Clear;
descriptor.ColorAttachments.Object(0).ClearColor = _clearColor;
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
Logger.Warning?.Print(LogClass.Gpu, "Began present");
_renderEncoderState.SetEncoderState(renderCommandEncoder);
// Barry goes here
// renderCommandEncoder.SetFragmentTexture(_renderTarget, 0);
renderCommandEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 6);
renderCommandEncoder.EndEncoding();
_commandBuffer.PresentDrawable(drawable);
_commandBuffer.Commit();
Logger.Warning?.Print(LogClass.Gpu, "Presented");
frameCount++;
if (frameCount >= 5)
{
MTLCaptureManager.SharedCaptureManager().StopCapture();
Logger.Warning?.Print(LogClass.Gpu, "Trace ended!");
}
_commandBuffer = _mtlCommandQueue.CommandBuffer();
}
@ -147,7 +182,7 @@ namespace Ryujinx.Graphics.Metal
public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
{
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
_clearColor = new MTLClearColor { red = color.Red, green = color.Green, blue = color.Blue, alpha = color.Alpha};
}
public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue,