Skip to content

Commit

Permalink
Metal: Better Bindings (#29)
Browse files Browse the repository at this point in the history
* Tell GAL to use Vk model (and break everything)

* ResourceBindingSegments

* Set information on backend caps

* Get ready to break everything

* Refactor EncoderStateManager

* Remove padding from helper shaders

* Fix ref array sizes

* Seperate vert & frag buffers

* Shader-side changes

* Fixes

* Fix some helper shader resource layouts

* Sort by binding id

* Fix helper shader layouts

* Don’t do inline vertex buffer updates

* Check for null storage
  • Loading branch information
IsaacMarovitz authored and GreemDev committed Dec 24, 2024
1 parent 549938e commit 60722a1
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 424 deletions.
8 changes: 4 additions & 4 deletions src/Ryujinx.Graphics.Gpu/Shader/GpuAccessorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SetBindingPair CreateConstantBufferBinding(int index)
{
int binding;

if (_context.Capabilities.Api == TargetApi.Vulkan)
if (_context.Capabilities.Api != TargetApi.OpenGL)
{
binding = GetBindingFromIndex(index, _context.Capabilities.MaximumUniformBuffersPerStage, "Uniform buffer");
}
Expand All @@ -71,7 +71,7 @@ public SetBindingPair CreateImageBinding(int count, bool isBuffer)
{
int binding;

if (_context.Capabilities.Api == TargetApi.Vulkan)
if (_context.Capabilities.Api != TargetApi.OpenGL)
{
if (count == 1)
{
Expand Down Expand Up @@ -103,7 +103,7 @@ public SetBindingPair CreateStorageBufferBinding(int index)
{
int binding;

if (_context.Capabilities.Api == TargetApi.Vulkan)
if (_context.Capabilities.Api != TargetApi.OpenGL)
{
binding = GetBindingFromIndex(index, _context.Capabilities.MaximumStorageBuffersPerStage, "Storage buffer");
}
Expand All @@ -119,7 +119,7 @@ public SetBindingPair CreateTextureBinding(int count, bool isBuffer)
{
int binding;

if (_context.Capabilities.Api == TargetApi.Vulkan)
if (_context.Capabilities.Api != TargetApi.OpenGL)
{
if (count == 1)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Ryujinx.Graphics.Metal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ static class Constants
public const int MaxUniformBuffersPerStage = 18;
public const int MaxStorageBuffersPerStage = 16;
public const int MaxTexturesPerStage = 64;
public const int MaxUniformBufferBindings = MaxUniformBuffersPerStage * MaxShaderStages;
public const int MaxStorageBufferBindings = MaxStorageBuffersPerStage * MaxShaderStages;
public const int MaxTextureBindings = MaxTexturesPerStage * MaxShaderStages;
public const int MaxColorAttachments = 8;
// TODO: Check this value
Expand All @@ -18,9 +20,11 @@ static class Constants
public const int MinResourceAlignment = 16;

// Must match constants set in shader generation
public const uint ZeroBufferIndex = 18;

public const uint ConstantBuffersIndex = 20;
public const uint StorageBuffersIndex = 21;
public const uint ZeroBufferIndex = 18;
public const uint TexturesIndex = 22;
public const uint ImagessIndex = 23;
}
}
41 changes: 24 additions & 17 deletions src/Ryujinx.Graphics.Metal/EncoderState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Metal.State;
using Ryujinx.Graphics.Shader;
using SharpMetal.Metal;
using System;
using System.Linq;
Expand All @@ -22,13 +23,13 @@ enum DirtyFlags
StencilRef = 1 << 7,
Viewports = 1 << 8,
Scissors = 1 << 9,
Buffers = 1 << 10,
VertexTextures = 1 << 11,
FragmentTextures = 1 << 12,
ComputeTextures = 1 << 13,
Uniforms = 1 << 10,
Storages = 1 << 11,
Textures = 1 << 12,
Images = 1 << 13,

RenderAll = RenderPipeline | DepthStencil | DepthClamp | DepthBias | CullMode | FrontFace | StencilRef | Viewports | Scissors | Buffers | VertexTextures | FragmentTextures,
ComputeAll = ComputePipeline | Buffers | ComputeTextures,
RenderAll = RenderPipeline | DepthStencil | DepthClamp | DepthBias | CullMode | FrontFace | StencilRef | Viewports | Scissors | Uniforms | Storages | Textures | Images,
ComputeAll = ComputePipeline | Uniforms | Storages | Textures | Images,
All = RenderAll | ComputeAll,
}

Expand All @@ -49,6 +50,20 @@ public BufferRef(Auto<DisposableBuffer> buffer, ref BufferRange range)
}
}

record struct TextureRef
{
public ShaderStage Stage;
public Texture Storage;
public Sampler Sampler;

public TextureRef(ShaderStage stage, Texture storage, Sampler sampler)
{
Stage = stage;
Storage = storage;
Sampler = sampler;
}
}

struct PredrawState
{
public MTLCullMode CullMode;
Expand All @@ -73,17 +88,9 @@ class EncoderState
public PipelineState Pipeline;
public DepthStencilUid DepthStencilUid;

public TextureBase[] FragmentTextures = new TextureBase[Constants.MaxTexturesPerStage];
public MTLSamplerState[] FragmentSamplers = new MTLSamplerState[Constants.MaxTexturesPerStage];

public TextureBase[] VertexTextures = new TextureBase[Constants.MaxTexturesPerStage];
public MTLSamplerState[] VertexSamplers = new MTLSamplerState[Constants.MaxTexturesPerStage];

public TextureBase[] ComputeTextures = new TextureBase[Constants.MaxTexturesPerStage];
public MTLSamplerState[] ComputeSamplers = new MTLSamplerState[Constants.MaxTexturesPerStage];

public BufferRef[] UniformBuffers = new BufferRef[Constants.MaxUniformBuffersPerStage];
public BufferRef[] StorageBuffers = new BufferRef[Constants.MaxStorageBuffersPerStage];
public readonly BufferRef[] UniformBufferRefs = new BufferRef[Constants.MaxUniformBufferBindings];
public readonly BufferRef[] StorageBufferRefs = new BufferRef[Constants.MaxStorageBufferBindings];
public readonly TextureRef[] TextureRefs = new TextureRef[Constants.MaxTextureBindings];

public Auto<DisposableBuffer> IndexBuffer = default;
public MTLIndexType IndexType = MTLIndexType.UInt16;
Expand Down
Loading

0 comments on commit 60722a1

Please sign in to comment.