Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Metal backend #1287

Open
wants to merge 188 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
188 commits
Select commit Hold shift + click to select a range
2477bad
start metal backend
SamoZ256 Jul 25, 2024
9837026
initialize Metal
SamoZ256 Jul 25, 2024
6db893c
implement texture view & fix: crashes
SamoZ256 Jul 25, 2024
9b127be
create and present drawable
SamoZ256 Jul 25, 2024
46981d7
implement pixel formats and texture copy
SamoZ256 Jul 26, 2024
6959716
start the shader decompiler
SamoZ256 Jul 26, 2024
aac9b12
improve shadergen & create shaders
SamoZ256 Jul 26, 2024
af3ce80
fix: shader errors and shader crashes
SamoZ256 Jul 26, 2024
4976ff3
fix: sampling & capture boundaries
SamoZ256 Jul 27, 2024
4c78c6a
implement texture decoding
SamoZ256 Jul 27, 2024
e539527
implement render passes
SamoZ256 Jul 27, 2024
cb525b2
implement vertex descriptors & draw
SamoZ256 Jul 27, 2024
7ea18d8
bind resources to shaders
SamoZ256 Jul 28, 2024
35eea12
bind default sampler
SamoZ256 Jul 28, 2024
7ae29a7
present
SamoZ256 Jul 28, 2024
8800de0
update CMakeLists.txt
SamoZ256 Jul 29, 2024
89a2c23
Merge branch 'cemu-project:main' into metal
SamoZ256 Jul 29, 2024
f011300
rework command encoder system
SamoZ256 Jul 29, 2024
0cb83d4
fix: support buffer data & fix: depth
SamoZ256 Jul 29, 2024
be8a560
optimize index buffers
SamoZ256 Jul 30, 2024
1fbd6ad
set viewport and scissor
SamoZ256 Jul 31, 2024
d4564c1
fix: viewport and scissor
SamoZ256 Aug 1, 2024
e4abb30
implement blending
SamoZ256 Aug 1, 2024
a2d1585
fix: shader errors
SamoZ256 Aug 2, 2024
4173675
implement depth state & texture clears
SamoZ256 Aug 2, 2024
4022755
implement proper bit cast in shaders
SamoZ256 Aug 2, 2024
db709c3
fix: present issue
SamoZ256 Aug 3, 2024
c6f6616
fix: viewport
SamoZ256 Aug 3, 2024
d7411e2
implement render pipeline cache
SamoZ256 Aug 3, 2024
406a856
implement depth stencil cache & fix: textureSize
SamoZ256 Aug 3, 2024
fe3b84b
fix: buffer allocator
SamoZ256 Aug 3, 2024
fa53af5
fix: memory leaks
SamoZ256 Aug 3, 2024
d2edc41
fix: present issues
SamoZ256 Aug 3, 2024
ce6d4ca
implement sampler states
SamoZ256 Aug 3, 2024
763d57d
implement texture copy and clear
SamoZ256 Aug 4, 2024
5030a2e
implement color buffer clear
SamoZ256 Aug 4, 2024
f11526a
fix: texture bindings
SamoZ256 Aug 5, 2024
d64e64e
fix: texture updates and buffer bindings
SamoZ256 Aug 6, 2024
0a7f30c
implement texture swizzle
SamoZ256 Aug 6, 2024
5fc4540
fix: texture swizzle
SamoZ256 Aug 6, 2024
99ff282
implement more primitive types & warn about vertex stride
SamoZ256 Aug 6, 2024
82dcbd9
prepare for vertex stride workaround
SamoZ256 Aug 6, 2024
ac651eb
implement vertex stride workaround
SamoZ256 Aug 6, 2024
d7e9aff
implement triangle fan reindexing
SamoZ256 Aug 7, 2024
e2ec602
specialize fragment shader output types & fix: shader errors
SamoZ256 Aug 7, 2024
d3249dc
implement texture readback
SamoZ256 Aug 7, 2024
1bcdade
set stencil state
SamoZ256 Aug 7, 2024
a50ce99
fix: writing to depth from a fragment shader
SamoZ256 Aug 7, 2024
e0791c3
set stencil reference value
SamoZ256 Aug 8, 2024
5c246d5
implement transform feedback
SamoZ256 Aug 8, 2024
a38ddb5
fix: shadows
SamoZ256 Aug 8, 2024
6d34d24
fix: missing color attachments & bind some other state
SamoZ256 Aug 11, 2024
caba20d
apply gamma correction
SamoZ256 Aug 11, 2024
c6ab45a
fix: vertex buffer leaks
SamoZ256 Aug 11, 2024
eb573fc
fix: color write mask and unpackHalf2x16
SamoZ256 Aug 11, 2024
8316cee
prepare for surface copy
SamoZ256 Aug 11, 2024
5bc9913
implement surface copy
SamoZ256 Aug 11, 2024
7fb3e1b
support cubemap arrays
SamoZ256 Aug 12, 2024
8496fc1
Merge branch 'main' into metal
SamoZ256 Aug 12, 2024
5e748cd
support surface copy parameters
SamoZ256 Aug 12, 2024
34d8076
fix: srgb
SamoZ256 Aug 12, 2024
0c73ff8
use multiple command buffers per frame
SamoZ256 Aug 12, 2024
3f52f3a
fix: cubemap sampling
SamoZ256 Aug 13, 2024
4b7c01e
fix: 3D texture copies & fix: present filter
SamoZ256 Aug 13, 2024
5e9537c
fix: render pass mess
SamoZ256 Aug 13, 2024
27925a4
do vertex buffer restride in a void vertex function
SamoZ256 Aug 13, 2024
bba2bbc
use buffer allocator for restrided vertex buffers
SamoZ256 Aug 13, 2024
13834ca
correct comment
SamoZ256 Aug 14, 2024
53efb9e
prepare for shader cache
SamoZ256 Aug 14, 2024
3c1a747
save pipelines to a binary archive
SamoZ256 Aug 14, 2024
52062bd
Merge branch 'main' into metal
SamoZ256 Aug 14, 2024
d48de5b
fix: buffer cache crash on shutdown
SamoZ256 Aug 14, 2024
c4688e1
stringify utility shader source
SamoZ256 Aug 14, 2024
0bb99aa
set depth bias
SamoZ256 Aug 14, 2024
be98734
fix: incorrect roundEven implementation
SamoZ256 Aug 14, 2024
ed7354f
reset depth bias
SamoZ256 Aug 14, 2024
9a215e0
don't bind textures and buffers which are already bound
SamoZ256 Aug 15, 2024
9982ac7
remove more unnecessary rebinds
SamoZ256 Aug 15, 2024
d79d7fe
implement sampler cache
SamoZ256 Aug 15, 2024
0d0152d
fix: present
SamoZ256 Aug 16, 2024
502d5b8
include debug information
SamoZ256 Aug 16, 2024
83a08b2
fix: some Metal validation errors
SamoZ256 Aug 17, 2024
b8e9bb3
fix: GPU hangs (hack)
SamoZ256 Aug 17, 2024
07989d8
fix: performance regression
SamoZ256 Aug 17, 2024
2657857
rework buffer allocator
SamoZ256 Aug 18, 2024
269e072
move buffer allocators to separate file & fix: high memory usage
SamoZ256 Aug 18, 2024
485a652
use managed storage mode when dedicated memory
SamoZ256 Aug 18, 2024
be0a69a
do texture load on GPU if no unified memory
SamoZ256 Aug 18, 2024
9bb7479
use more efficient cpu cache mode when possible
SamoZ256 Aug 18, 2024
9aa72e6
report vram usage
SamoZ256 Aug 18, 2024
e2f66b8
fix: streamout
SamoZ256 Aug 19, 2024
b59dbfc
hack: increase xfb ring buffer size
SamoZ256 Aug 19, 2024
69a3624
do GPU texture loading on non-apple GPUs
SamoZ256 Aug 19, 2024
ca256eb
check for pixel format support
SamoZ256 Aug 19, 2024
6b13604
emit object shaders
SamoZ256 Aug 20, 2024
b10bcd4
emit mesh shaders
SamoZ256 Aug 20, 2024
46269c0
fix: mesh shader errors
SamoZ256 Aug 20, 2024
9679c6b
set primitive count in mesh shaders
SamoZ256 Aug 20, 2024
2f4ceb3
patch object and mesh shaders
SamoZ256 Aug 20, 2024
97f441e
draw with geometry shaders
SamoZ256 Aug 20, 2024
76cf3de
disable vertex buffer restride for object shaders
SamoZ256 Aug 20, 2024
7500a54
fix: geometry shaders
SamoZ256 Aug 21, 2024
a832bc2
support rect primitive emulation
SamoZ256 Aug 21, 2024
339af5c
support formats with X components
SamoZ256 Aug 21, 2024
4f7288d
fix: rect mesh shader errors
SamoZ256 Aug 21, 2024
3d0055a
end render pass if attachment is being read
SamoZ256 Aug 21, 2024
67a64c9
rework the binding system
SamoZ256 Aug 22, 2024
c05b2d0
don't stringify shader source & move vertex buffer bindings by 1
SamoZ256 Aug 22, 2024
b332d63
Merge pull request #1 from SamoZ256/metal-geo-shaders
SamoZ256 Aug 22, 2024
8e87b96
fix: missing lod parameter
SamoZ256 Aug 22, 2024
28aef85
fix: texture readback
SamoZ256 Aug 23, 2024
6bb1912
simplify texture readback
SamoZ256 Aug 23, 2024
d4a1074
rework the present system
SamoZ256 Aug 23, 2024
7499c3f
add: ImGui support
SamoZ256 Aug 24, 2024
c4a26d4
report total vram correctly
SamoZ256 Aug 24, 2024
6bf3406
implement IsPadWindowActive
SamoZ256 Aug 24, 2024
b105a38
add: basic debug overlay
SamoZ256 Aug 24, 2024
c905399
fix: ImGui issues
SamoZ256 Aug 25, 2024
96d6168
add: additional debug info
SamoZ256 Aug 25, 2024
a693bf5
fix: issues on Intel Macs
SamoZ256 Aug 25, 2024
6c8947d
fix: temporary buffer allocator
SamoZ256 Aug 26, 2024
b7f88d0
implement buffer locking system
SamoZ256 Aug 26, 2024
cd8b74b
fix: buffer allocator leaks
SamoZ256 Aug 26, 2024
3439b32
uncomment drc swap buffer
SamoZ256 Aug 27, 2024
b7a1ade
clip mode
SamoZ256 Aug 27, 2024
a1b20fd
Merge pull request #2 from SamoZ256/metal-intel-macs
SamoZ256 Aug 27, 2024
4572f90
fix: infinite command buffer wait
SamoZ256 Aug 27, 2024
0e61471
skip draws when pipeline is invalid
SamoZ256 Aug 27, 2024
366be04
fix: validation errors
SamoZ256 Aug 28, 2024
163eeea
use texture views for surface copies
SamoZ256 Aug 28, 2024
1b44269
simplify flush
SamoZ256 Aug 28, 2024
be76dad
do surface copy properly
SamoZ256 Aug 28, 2024
35740c5
always do texture copies on gpu
SamoZ256 Aug 28, 2024
a6c8d83
release unused buffers
SamoZ256 Aug 28, 2024
bbed007
hack: don't attempt to compile shaders with errors
SamoZ256 Aug 28, 2024
64610c1
limit buffer allocation size
SamoZ256 Aug 28, 2024
b011d75
adjust texture swizzle
SamoZ256 Aug 29, 2024
7a28985
properly implement 0 stride vertex buffers
SamoZ256 Aug 29, 2024
074f9f6
align buffer stride after setting to min stride
SamoZ256 Aug 29, 2024
ccd72bf
implement gather sampling
SamoZ256 Aug 29, 2024
a4ff855
do buffer copying in a void vertex function
SamoZ256 Aug 29, 2024
2403cf9
always use blit commands for buffer copies on non-apple GPUs
SamoZ256 Aug 29, 2024
1cfb841
do buffer cache uploading on the GPU
SamoZ256 Aug 29, 2024
2f822d0
support game pad view in a separate window
SamoZ256 Aug 30, 2024
41ee2e7
Merge branch 'main' into metal
SamoZ256 Aug 30, 2024
1412d1e
enable triangle fan support
SamoZ256 Aug 30, 2024
9c29acc
synchronize buffer copying correctly
SamoZ256 Aug 30, 2024
cda4799
disable writes for color attachments which are not in the active FBO
SamoZ256 Aug 30, 2024
a9a4d7b
disable depth write if active FBO doesn't have a depth attachment
SamoZ256 Aug 30, 2024
5eb3026
skip unnecessary draws
SamoZ256 Aug 31, 2024
f9f6260
monitor clears
SamoZ256 Aug 31, 2024
5d07d11
emulate unsupported shadow sampler types
SamoZ256 Sep 1, 2024
491ac69
fix: object shader error when used with rect primitive
SamoZ256 Sep 1, 2024
45737e8
disable shader write usage on textures
SamoZ256 Sep 2, 2024
c4eb195
make all textures be at least 1x1x1
SamoZ256 Sep 2, 2024
953975f
don't jit compile vertex shaders
SamoZ256 Sep 3, 2024
2ee92e5
use the correct fetch shader
SamoZ256 Sep 3, 2024
b13ba58
correct the object shader hash
SamoZ256 Sep 4, 2024
8a74445
don't compile fragment shaders just-in-time
SamoZ256 Sep 4, 2024
cd72ad8
include color format data types in ps hash
SamoZ256 Sep 5, 2024
4251f3f
check for invalid color formats
SamoZ256 Sep 6, 2024
6a3bdd4
refactor pixel format support
SamoZ256 Sep 6, 2024
548ffb6
add: todo notices
SamoZ256 Sep 6, 2024
5e50592
Merge pull request #3 from SamoZ256/metal-shaders
SamoZ256 Sep 6, 2024
3dc233f
support rasterization kill
SamoZ256 Sep 7, 2024
cf56024
optimize rasterization kill
SamoZ256 Sep 7, 2024
8404981
make binary archives device and os version specific
SamoZ256 Sep 7, 2024
e8f726e
use lcr
SamoZ256 Sep 7, 2024
395cd1c
handle rasterization kill for mesh shaders
SamoZ256 Sep 11, 2024
950f04d
support instancing for mesh shaders
SamoZ256 Sep 11, 2024
e7f8f0e
simplify uniform names
SamoZ256 Sep 11, 2024
a328c5e
use uchar for index type
SamoZ256 Sep 11, 2024
4cce369
put query object into a separate file
SamoZ256 Sep 12, 2024
eb7c10e
implement occlusion queries
SamoZ256 Sep 13, 2024
fd16488
improve command buffer sync
SamoZ256 Sep 13, 2024
934b1f8
handle occlusion queries with no draws
SamoZ256 Sep 13, 2024
e89efed
request soon commit instead of committing directly
SamoZ256 Sep 13, 2024
e5dcd93
Merge pull request #4 from SamoZ256/metal-occlusion-queries
SamoZ256 Sep 14, 2024
b5954d8
release command buffers properly
SamoZ256 Sep 14, 2024
25a1d7e
Merge branch 'cemu-project:main' into metal
SamoZ256 Sep 14, 2024
02254d4
set pixel format view usage for textures
SamoZ256 Sep 14, 2024
008c11c
allocate special buffers only when needed
SamoZ256 Sep 14, 2024
2961151
correct comment
SamoZ256 Sep 14, 2024
358567a
make a workaround for streamout with no fbo
SamoZ256 Sep 14, 2024
8ac90ce
use correct stages for buffer sync
SamoZ256 Sep 16, 2024
535107e
fix: invalid surface copy depth
SamoZ256 Sep 16, 2024
aa81070
refactor pixel formats
SamoZ256 Sep 16, 2024
f03c0a2
only set buffer offset if needed
SamoZ256 Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rework buffer allocator
  • Loading branch information
SamoZ256 committed Aug 18, 2024
commit 265785772aa07da9c1bdbca3fa7c1ba5910b12ed
83 changes: 9 additions & 74 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,8 @@
#include "Cafe/HW/Latte/Renderer/Metal/MetalCommon.h"
#include "Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h"
#include "Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h"
#include "Cafe/HW/Latte/Renderer/Metal/MetalHybridComputePipeline.h"
#include "Common/precompiled.h"

MetalBufferAllocator::~MetalBufferAllocator()
{
for (auto buffer : m_buffers)
{
buffer->release();
}
}

MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size)
{
// Align the size
size = Align(size, 16);

// First, try to find a free range
for (uint32 i = 0; i < m_freeBufferRanges.size(); i++)
{
auto& range = m_freeBufferRanges[i];
if (size <= range.size)
{
MetalBufferAllocation allocation;
allocation.bufferIndex = range.bufferIndex;
allocation.bufferOffset = range.offset;
allocation.data = (uint8*)m_buffers[range.bufferIndex]->contents() + range.offset;

range.offset += size;
range.size -= size;

if (range.size == 0)
{
m_freeBufferRanges.erase(m_freeBufferRanges.begin() + i);
}

return allocation;
}
}

// If no free range was found, allocate a new buffer
m_allocationSize = std::max(m_allocationSize, size);
MTL::Buffer* buffer = m_mtlr->GetDevice()->newBuffer(m_allocationSize, MTL::ResourceStorageModeShared);
#ifdef CEMU_DEBUG_ASSERT
buffer->setLabel(GetLabel("Buffer from buffer allocator", buffer));
#endif

MetalBufferAllocation allocation;
allocation.bufferIndex = m_buffers.size();
allocation.bufferOffset = 0;
allocation.data = buffer->contents();

m_buffers.push_back(buffer);

// If the buffer is larger than the requested size, add the remaining space to the free buffer ranges
if (size < m_allocationSize)
{
MetalBufferRange range;
range.bufferIndex = allocation.bufferIndex;
range.offset = size;
range.size = m_allocationSize - size;

m_freeBufferRanges.push_back(range);
}

// Increase the allocation size for the next buffer
if (m_allocationSize < 128 * 1024 * 1024)
m_allocationSize *= 2;

return allocation;
}

MetalVertexBufferCache::~MetalVertexBufferCache()
{
}
Expand All @@ -87,13 +18,13 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
return {bufferCache, vertexBufferRange.offset};
}

auto buffer = m_bufferAllocator->GetBuffer(restrideInfo.allocation.bufferIndex);
MTL::Buffer* buffer;
if (restrideInfo.memoryInvalidated || stride != restrideInfo.lastStride)
{
size_t newStride = Align(stride, 4);
size_t newSize = vertexBufferRange.size / stride * newStride;
restrideInfo.allocation = m_bufferAllocator->GetBufferAllocation(newSize);
buffer = m_bufferAllocator->GetBuffer(restrideInfo.allocation.bufferIndex);
restrideInfo.allocation = m_bufferAllocator.GetBufferAllocation(newSize);
buffer = m_bufferAllocator.GetBuffer(restrideInfo.allocation.bufferIndex);

//uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset;
//uint8* newPtr = (uint8*)buffer->contents() + restrideInfo.allocation.bufferOffset;
Expand All @@ -112,7 +43,7 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
m_mtlr->GetEncoderState().m_renderPipelineState = m_restrideBufferPipeline->GetRenderPipelineState();

MTL::Buffer* buffers[] = {bufferCache, buffer};
size_t offsets[] = {vertexBufferRange.offset, restrideInfo.allocation.bufferOffset};
size_t offsets[] = {vertexBufferRange.offset, restrideInfo.allocation.offset};
renderCommandEncoder->setVertexBuffers(buffers, offsets, NS::Range(GET_HELPER_BUFFER_BINDING(0), 2));
m_mtlr->GetEncoderState().m_uniformBufferOffsets[METAL_SHADER_TYPE_VERTEX][GET_HELPER_BUFFER_BINDING(0)] = INVALID_OFFSET;
m_mtlr->GetEncoderState().m_uniformBufferOffsets[METAL_SHADER_TYPE_VERTEX][GET_HELPER_BUFFER_BINDING(1)] = INVALID_OFFSET;
Expand Down Expand Up @@ -149,8 +80,12 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
restrideInfo.memoryInvalidated = false;
restrideInfo.lastStride = newStride;
}
else
{
buffer = m_bufferAllocator.GetBuffer(restrideInfo.allocation.bufferIndex);
}

return {buffer, restrideInfo.allocation.bufferOffset};
return {buffer, restrideInfo.allocation.offset};
}

void MetalVertexBufferCache::MemoryRangeChanged(size_t offset, size_t size)
Expand Down
Loading