Skip to content

Commit

Permalink
fix: issues on Intel Macs
Browse files Browse the repository at this point in the history
  • Loading branch information
SamoZ256 committed Aug 25, 2024
1 parent 96d6168 commit a693bf5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MetalBufferAllocator
MetalBufferAllocation GetBufferAllocation(size_t size)
{
// Align the size
size = Align(size, 16);
size = Align(size, 128);

// First, try to find a free range
for (uint32 i = 0; i < m_freeBufferRanges.size(); i++)
Expand Down
19 changes: 12 additions & 7 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ MetalRenderer::MetalRenderer()
presentFragmentFunction->release();

error = nullptr;
renderPipelineDescriptor->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
renderPipelineDescriptor->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatBGRA8Unorm);
#ifdef CEMU_DEBUG_ASSERT
renderPipelineDescriptor->setLabel(GetLabel("Present pipeline linear", renderPipelineDescriptor));
#endif
Expand All @@ -138,7 +138,7 @@ MetalRenderer::MetalRenderer()
}

error = nullptr;
renderPipelineDescriptor->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatRGBA8Unorm_sRGB);
renderPipelineDescriptor->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatBGRA8Unorm_sRGB);
#ifdef CEMU_DEBUG_ASSERT
renderPipelineDescriptor->setLabel(GetLabel("Present pipeline sRGB", renderPipelineDescriptor));
#endif
Expand Down Expand Up @@ -185,7 +185,9 @@ MetalRenderer::~MetalRenderer()

void MetalRenderer::InitializeLayer(const Vector2i& size, bool mainWindow)
{
GetLayer(mainWindow) = MetalLayerHandle(m_device, size);
auto& layer = GetLayer(mainWindow);
layer = MetalLayerHandle(m_device, size);
layer.GetLayer()->setPixelFormat(MTL::PixelFormatBGRA8Unorm);
}

void MetalRenderer::ResizeLayer(const Vector2i& size, bool mainWindow)
Expand Down Expand Up @@ -381,7 +383,7 @@ ImTextureID MetalRenderer::GenerateTexture(const std::vector<uint8>& data, const
desc->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
desc->setWidth(size.x);
desc->setHeight(size.y);
desc->setStorageMode(MTL::StorageModeShared);
desc->setStorageMode(m_isAppleGPU ? MTL::StorageModeShared : MTL::StorageModeManaged);
desc->setUsage(MTL::TextureUsageShaderRead);

MTL::Texture* texture = m_device->newTexture(desc);
Expand Down Expand Up @@ -507,11 +509,14 @@ void MetalRenderer::texture_loadSlice(LatteTexture* hostTexture, sint32 width, s
auto blitCommandEncoder = GetBlitCommandEncoder();

// Allocate a temporary buffer
auto allocation = m_memoryManager->GetTemporaryBufferAllocator().GetBufferAllocation(compressedImageSize);
auto buffer = m_memoryManager->GetTemporaryBufferAllocator().GetBuffer(allocation.bufferIndex);
// HACK: use the persistent buffer allocator so as to avoid any issues
auto& bufferAllocator = m_memoryManager->GetBufferAllocator();
auto allocation = bufferAllocator.GetBufferAllocation(compressedImageSize);
auto buffer = bufferAllocator.GetBuffer(allocation.bufferIndex);

// Copy the data to the temporary buffer
memcpy(allocation.data, pixelData, compressedImageSize);
buffer->didModifyRange(NS::Range(allocation.offset, allocation.size));

// Copy the data from the temporary buffer to the texture
blitCommandEncoder->copyFromBuffer(buffer, allocation.offset, bytesPerRow, 0, MTL::Size(width, height, 1), textureMtl->GetTexture(), sliceIndex, mipIndex, MTL::Origin(0, 0, offsetZ));
Expand Down Expand Up @@ -1474,7 +1479,7 @@ bool MetalRenderer::AcquireDrawable(bool mainWindow)
const bool latteBufferUsesSRGB = mainWindow ? LatteGPUState.tvBufferUsesSRGB : LatteGPUState.drcBufferUsesSRGB;
if (latteBufferUsesSRGB != m_state.m_usesSRGB)
{
layer.GetLayer()->setPixelFormat(latteBufferUsesSRGB ? MTL::PixelFormatRGBA8Unorm_sRGB : MTL::PixelFormatRGBA8Unorm);
layer.GetLayer()->setPixelFormat(latteBufferUsesSRGB ? MTL::PixelFormatBGRA8Unorm_sRGB : MTL::PixelFormatBGRA8Unorm);
m_state.m_usesSRGB = latteBufferUsesSRGB;
}

Expand Down

4 comments on commit a693bf5

@niekvlessert
Copy link

@niekvlessert niekvlessert commented on a693bf5 Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for the hard work on this, amazing that you can pull of stuff like this! I would like to report that the binaries you created won't work, they depend on your directory structure.

dyld[2330]: Library not loaded: /Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib
Referenced from: /Applications/Cemu.app/Contents/MacOS/Cemu_release
Reason: tried: '/Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib' (no such file), '/Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib' (no such file)

@SamoZ256
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for the hard work on this, amazing that you can pull of stuff like this! I would like to report that the binaries you created won't work, they depend on your directory structure.

dyld[2330]: Library not loaded: /Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib Referenced from: /Applications/Cemu.app/Contents/MacOS/Cemu_release Reason: tried: '/Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib' (no such file), '/Users/samuliak/Documents/Cemu/build/vcpkg_installed/x64-osx/lib/libusb-1.0.0.dylib' (no such file)

Thanks for the feedback! I am aware of this issue, but the releases have been made just for testing purposes and are fairly outdated too. If you'd like a more recent build, you could try this one: https://github.com/SamoZ256/Cemu/actions/runs/11651034910/artifacts/2137808074.

@niekvlessert
Copy link

@niekvlessert niekvlessert commented on a693bf5 Nov 3, 2024 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamoZ256
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that one is working! I'm trying Batman Armored Edition, framerate is great! Some issues on the menu in the beginning (no borders) and the shader compilation takes longer when selecting Metal backend. However there's a weird issue on my Intel Mac, both with Vulkan and Metal; sometimes it just freezes to zero frames per second basically. Can take a minute to get out of or it will never. I'll try to debug, not very familiar with doing that... Regards

I have noticed the long shader loading times as well, though I am not sure what's the cause of it yet. As for the Batman game issue, I cannot help you much since I don't have it. But thanks for the report! If you have any other questions or suggestions, feel free to join the Cemu discord server, as chatting on a random git commit is a bit weird ;)

Please sign in to comment.