Skip to content

Commit

Permalink
Merge pull request #18 from SamoZ256/metal-air-cache
Browse files Browse the repository at this point in the history
AIR cache
  • Loading branch information
SamoZ256 authored Jan 17, 2025
2 parents 0bf245b + 7700635 commit 21e7466
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 30 deletions.
32 changes: 17 additions & 15 deletions src/Cafe/HW/Latte/Core/LatteShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ void LatteSHRC_UpdateVSBaseHash(uint8* vertexShaderPtr, uint32 vertexShaderSize,
if (LatteGPUState.contextNew.PA_CL_CLIP_CNTL.get_DX_CLIP_SPACE_DEF())
vsHash += 0x1537;

#if ENABLE_METAL
if (g_renderer->GetType() == RendererAPI::Metal)
{
if (usesGeometryShader || _activeFetchShader->mtlFetchVertexManually)
Expand All @@ -542,27 +543,28 @@ void LatteSHRC_UpdateVSBaseHash(uint8* vertexShaderPtr, uint32 vertexShaderSize,

if (!usesGeometryShader)
{
// Rasterization
bool rasterizationEnabled = !LatteGPUState.contextNew.PA_CL_CLIP_CNTL.get_DX_RASTERIZATION_KILL();
// Rasterization
bool rasterizationEnabled = !LatteGPUState.contextNew.PA_CL_CLIP_CNTL.get_DX_RASTERIZATION_KILL();

// HACK
if (!LatteGPUState.contextNew.PA_CL_VTE_CNTL.get_VPORT_X_OFFSET_ENA())
rasterizationEnabled = true;
// HACK
if (!LatteGPUState.contextNew.PA_CL_VTE_CNTL.get_VPORT_X_OFFSET_ENA())
rasterizationEnabled = true;

const auto& polygonControlReg = LatteGPUState.contextNew.PA_SU_SC_MODE_CNTL;
uint32 cullFront = polygonControlReg.get_CULL_FRONT();
uint32 cullBack = polygonControlReg.get_CULL_BACK();
if (cullFront && cullBack)
rasterizationEnabled = false;
const auto& polygonControlReg = LatteGPUState.contextNew.PA_SU_SC_MODE_CNTL;
uint32 cullFront = polygonControlReg.get_CULL_FRONT();
uint32 cullBack = polygonControlReg.get_CULL_BACK();
if (cullFront && cullBack)
rasterizationEnabled = false;

if (rasterizationEnabled)
vsHash += 51ULL;
if (rasterizationEnabled)
vsHash += 51ULL;

// Vertex fetch
if (_activeFetchShader->mtlFetchVertexManually)
vsHash += 349ULL;
// Vertex fetch
if (_activeFetchShader->mtlFetchVertexManually)
vsHash += 349ULL;
}
}
#endif

_shaderBaseHash_vs = vsHash;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Cafe/HW/Latte/Renderer/Metal/LatteTextureMtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
#include "Cafe/HW/Latte/Renderer/Metal/LatteTextureViewMtl.h"
#include "Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h"
#include "Cafe/HW/Latte/Renderer/Metal/LatteToMtl.h"
#include "Common/precompiled.h"
#include "Metal/MTLResource.hpp"
#include "Metal/MTLTexture.hpp"

LatteTextureMtl::LatteTextureMtl(class MetalRenderer* mtlRenderer, Latte::E_DIM dim, MPTR physAddress, MPTR physMipAddress, Latte::E_GX2SURFFMT format, uint32 width, uint32 height, uint32 depth, uint32 pitch, uint32 mipLevels, uint32 swizzle,
Latte::E_HWTILEMODE tileMode, bool isDepth)
: LatteTexture(dim, physAddress, physMipAddress, format, width, height, depth, pitch, mipLevels, swizzle, tileMode, isDepth), m_mtlr(mtlRenderer)
{
MTL::TextureDescriptor* desc = MTL::TextureDescriptor::alloc()->init();
desc->setStorageMode(MTL::StorageModePrivate);
desc->setCpuCacheMode(MTL::CPUCacheModeWriteCombined);
//desc->setCpuCacheMode(MTL::CPUCacheModeWriteCombined);

sint32 effectiveBaseWidth = width;
sint32 effectiveBaseHeight = height;
Expand Down
76 changes: 76 additions & 0 deletions src/Cafe/HW/Latte/Renderer/Metal/MetalCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,79 @@ inline bool FormatIsRenderable(Latte::E_GX2SURFFMT format)
{
return !Latte::IsCompressedFormat(format);
}

template <typename... T>
inline bool executeCommand(fmt::format_string<T...> fmt, T&&... args) {
std::string command = fmt::format(fmt, std::forward<T>(args)...);
int res = system(command.c_str());
if (res != 0)
{
cemuLog_log(LogType::Force, "command \"{}\" failed with exit code {}", command, res);
return false;
}

return true;
}

class MemoryMappedFile
{
public:
MemoryMappedFile(const std::string& filePath)
{
// Open the file
m_fd = open(filePath.c_str(), O_RDONLY);
if (m_fd == -1) {
cemuLog_log(LogType::Force, "failed to open file: {}", filePath);
return;
}

// Get the file size
// Use a loop to handle the case where the file size is 0 (more of a safety net)
struct stat fileStat;
while (true)
{
if (fstat(m_fd, &fileStat) == -1)
{
close(m_fd);
cemuLog_log(LogType::Force, "failed to get file size: {}", filePath);
return;
}
m_fileSize = fileStat.st_size;

if (m_fileSize == 0)
{
cemuLog_logOnce(LogType::Force, "file size is 0: {}", filePath);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}

break;
}

// Memory map the file
m_data = mmap(nullptr, m_fileSize, PROT_READ, MAP_PRIVATE, m_fd, 0);
if (m_data == MAP_FAILED)
{
close(m_fd);
cemuLog_log(LogType::Force, "failed to memory map file: {}", filePath);
return;
}
}

~MemoryMappedFile()
{
if (m_data && m_data != MAP_FAILED)
munmap(m_data, m_fileSize);

if (m_fd != -1)
close(m_fd);
}

uint8* data() const { return static_cast<uint8*>(m_data); }
size_t size() const { return m_fileSize; }

private:
int m_fd = -1;
void* m_data = nullptr;
size_t m_fileSize = 0;
};
Loading

0 comments on commit 21e7466

Please sign in to comment.