Latte: Cleanup some endian handling code

This commit is contained in:
Exzap 2026-02-20 19:19:44 +01:00
parent 18dd75c1f5
commit 2096c434de
5 changed files with 46 additions and 52 deletions

View File

@ -105,7 +105,7 @@ void gx2Export_GX2CopyColorBufferToScanBuffer(PPCInterpreter_t* hCPU)
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.pitch);
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.tileMode.value());
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.swizzle);
gx2WriteGather_submitU32AsBE(_swapEndianU32(colorBuffer->viewFirstSlice));
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->viewFirstSlice);
gx2WriteGather_submitU32AsBE((uint32)colorBuffer->surface.format.value());
gx2WriteGather_submitU32AsBE(hCPU->gpr[4]);
@ -161,9 +161,9 @@ void _GX2InitScanBuffer(GX2ColorBuffer* colorBuffer, sint32 width, sint32 height
colorBuffer->surface.resFlag = GX2_RESFLAG_USAGE_TEXTURE | GX2_RESFLAG_USAGE_COLOR_BUFFER;
colorBuffer->surface.width = width;
colorBuffer->surface.height = height;
colorBuffer->viewFirstSlice = _swapEndianU32(0);
colorBuffer->viewNumSlices = _swapEndianU32(1);
colorBuffer->viewMip = _swapEndianU32(0);
colorBuffer->viewFirstSlice = 0;
colorBuffer->viewNumSlices = 1;
colorBuffer->viewMip = 0;
colorBuffer->surface.numLevels = 1;
colorBuffer->surface.dim = Latte::E_DIM::DIM_2D;
colorBuffer->surface.swizzle = 0;

View File

@ -13,8 +13,8 @@ namespace GX2
void GX2SetClearDepthStencil(GX2DepthBuffer* depthBuffer, float depthClearValue, uint8 stencilClearValue)
{
GX2ReserveCmdSpace(4);
*(uint32*)&depthBuffer->clearDepth = _swapEndianU32(*(uint32*)&depthClearValue);
depthBuffer->clearStencil = _swapEndianU32(stencilClearValue);
depthBuffer->clearDepth = depthClearValue;
depthBuffer->clearStencil = stencilClearValue;
Latte::LATTE_DB_STENCIL_CLEAR stencilClearReg;
stencilClearReg.set_clearValue(stencilClearValue);
Latte::LATTE_DB_DEPTH_CLEAR depthClearReg;
@ -28,7 +28,7 @@ namespace GX2
void GX2SetClearDepth(GX2DepthBuffer* depthBuffer, float depthClearValue)
{
GX2ReserveCmdSpace(3);
*(uint32*)&depthBuffer->clearDepth = _swapEndianU32(*(uint32*)&depthClearValue);
depthBuffer->clearDepth = depthClearValue;
Latte::LATTE_DB_DEPTH_CLEAR depthClearReg;
depthClearReg.set_clearValue(depthClearValue);
gx2WriteGather_submit(pm4HeaderType3(IT_SET_CONTEXT_REG, 1 + 1),
@ -40,7 +40,7 @@ namespace GX2
void GX2SetClearStencil(GX2DepthBuffer* depthBuffer, uint8 stencilClearValue)
{
GX2ReserveCmdSpace(3);
depthBuffer->clearStencil = _swapEndianU32(stencilClearValue);
depthBuffer->clearStencil = stencilClearValue;
Latte::LATTE_DB_STENCIL_CLEAR stencilClearReg;
stencilClearReg.set_clearValue(stencilClearValue);
gx2WriteGather_submit(pm4HeaderType3(IT_SET_CONTEXT_REG, 1 + 1),
@ -109,8 +109,8 @@ namespace GX2
colorWidth = colorBuffer->surface.width;
colorHeight = colorBuffer->surface.height;
colorPitch = colorBuffer->surface.pitch;
colorFirstSlice = _swapEndianU32(colorBuffer->viewFirstSlice);
colorNumSlices = _swapEndianU32(colorBuffer->viewNumSlices);
colorFirstSlice = colorBuffer->viewFirstSlice;
colorNumSlices = colorBuffer->viewNumSlices;
}
// depth buffer
MPTR depthPhysAddr = MPTR_NULL;
@ -129,8 +129,8 @@ namespace GX2
depthWidth = depthBuffer->surface.width;
depthHeight = depthBuffer->surface.height;
depthPitch = depthBuffer->surface.pitch;
depthFirstSlice = _swapEndianU32(depthBuffer->viewFirstSlice);
depthNumSlices = _swapEndianU32(depthBuffer->viewNumSlices);
depthFirstSlice = depthBuffer->viewFirstSlice;
depthNumSlices = depthBuffer->viewNumSlices;
}
gx2WriteGather_submit(pm4HeaderType3(IT_HLE_CLEAR_COLOR_DEPTH_STENCIL, 23),
hleClearFlags,

View File

@ -17,12 +17,12 @@ void gx2Export_GX2InitColorBufferRegs(PPCInterpreter_t* hCPU)
ppcDefineParamStructPtr(colorBuffer, GX2ColorBuffer, 0);
LatteAddrLib::AddrSurfaceInfo_OUT surfaceInfo;
LatteAddrLib::GX2CalculateSurfaceInfo(colorBuffer->surface.format, colorBuffer->surface.width, colorBuffer->surface.height, colorBuffer->surface.depth, colorBuffer->surface.dim, colorBuffer->surface.tileMode, colorBuffer->surface.aa, _swapEndianU32(colorBuffer->viewMip), &surfaceInfo);
LatteAddrLib::GX2CalculateSurfaceInfo(colorBuffer->surface.format, colorBuffer->surface.width, colorBuffer->surface.height, colorBuffer->surface.depth, colorBuffer->surface.dim, colorBuffer->surface.tileMode, colorBuffer->surface.aa, colorBuffer->viewMip, &surfaceInfo);
uint32 pitchHeight = (surfaceInfo.height * surfaceInfo.pitch) >> 6;
#ifdef CEMU_DEBUG_ASSERT
if (colorBuffer->viewNumSlices != _swapEndianU32(1))
cemuLog_logDebug(LogType::Force, "GX2InitColorBufferRegs(): With unsupported slice count {}", _swapEndianU32(colorBuffer->viewNumSlices));
if (colorBuffer->viewNumSlices != 1)
cemuLog_logDebug(LogType::Force, "GX2InitColorBufferRegs(): With unsupported slice count {}", (uint32)colorBuffer->viewNumSlices);
if (surfaceInfo.pitch < 7)
cemuLog_logDebug(LogType::Force, "GX2InitColorBufferRegs(): Pitch too small (pitch = {})", surfaceInfo.pitch);
if ((surfaceInfo.pitch & 7) != 0)
@ -101,8 +101,8 @@ void gx2Export_GX2InitColorBufferRegs(PPCInterpreter_t* hCPU)
uint32 regView = 0;
if (colorBuffer->surface.tileMode != Latte::E_GX2TILEMODE::TM_LINEAR_SPECIAL)
{
regView |= (_swapEndianU32(colorBuffer->viewFirstSlice) & 0x7FF);
regView |= (((_swapEndianU32(colorBuffer->viewNumSlices) + _swapEndianU32(colorBuffer->viewFirstSlice) - 1) & 0x7FF) << 13);
regView |= ((uint32)colorBuffer->viewFirstSlice & 0x7FF);
regView |= ((((uint32)colorBuffer->viewNumSlices + (uint32)colorBuffer->viewFirstSlice - 1) & 0x7FF) << 13);
}
colorBuffer->reg_view = regView;
colorBuffer->reg_mask = 0;
@ -118,7 +118,7 @@ void gx2Export_GX2InitDepthBufferRegs(PPCInterpreter_t* hCPU)
ppcDefineParamStructPtr(depthBuffer, GX2DepthBuffer, 0);
LatteAddrLib::AddrSurfaceInfo_OUT surfaceInfo;
LatteAddrLib::GX2CalculateSurfaceInfo(depthBuffer->surface.format, depthBuffer->surface.width, depthBuffer->surface.height, depthBuffer->surface.depth, depthBuffer->surface.dim, depthBuffer->surface.tileMode, depthBuffer->surface.aa, _swapEndianU32(depthBuffer->viewMip), &surfaceInfo);
LatteAddrLib::GX2CalculateSurfaceInfo(depthBuffer->surface.format, depthBuffer->surface.width, depthBuffer->surface.height, depthBuffer->surface.depth, depthBuffer->surface.dim, depthBuffer->surface.tileMode, depthBuffer->surface.aa, depthBuffer->viewMip, &surfaceInfo);
cemu_assert_debug(depthBuffer->viewNumSlices != 0);
@ -140,18 +140,12 @@ void gx2Export_GX2SetColorBuffer(PPCInterpreter_t* hCPU)
GX2ColorBuffer* colorBufferBE = (GX2ColorBuffer*)memory_getPointerFromVirtualOffset(hCPU->gpr[3]);
#ifdef CEMU_DEBUG_ASSERT
cemuLog_log(LogType::GX2, "ColorBuffer tileMode {:01x} PhysAddr {:08x} fmt {:04x} res {}x{} Mip {} Slice {}", (uint32)colorBufferBE->surface.tileMode.value(), (uint32)colorBufferBE->surface.imagePtr, (uint32)colorBufferBE->surface.format.value(), (uint32)colorBufferBE->surface.width, (uint32)colorBufferBE->surface.height, _swapEndianU32(colorBufferBE->viewMip), _swapEndianU32(colorBufferBE->viewFirstSlice));
cemuLog_log(LogType::GX2, "ColorBuffer tileMode {:01x} PhysAddr {:08x} fmt {:04x} res {}x{} Mip {} Slice {}", (uint32)colorBufferBE->surface.tileMode.value(), (uint32)colorBufferBE->surface.imagePtr, (uint32)colorBufferBE->surface.format.value(), (uint32)colorBufferBE->surface.width, (uint32)colorBufferBE->surface.height, (uint32)colorBufferBE->viewMip, (uint32)colorBufferBE->viewFirstSlice);
#endif
// regs[0] = mmCB_COLOR0_SIZE
// regs[1] = mmCB_COLOR0_INFO
// regs[2] = mmCB_COLOR0_VIEW
// regs[3] = mmCB_COLOR0_MASK
// regs[4] = mmCB_COLOR0_TILE
uint32 targetIndex = hCPU->gpr[4];
uint32 viewMip = _swapEndianU32(colorBufferBE->viewMip);
uint32 viewMip = colorBufferBE->viewMip;
uint32 colorBufferBase = memory_virtualToPhysical(colorBufferBE->surface.imagePtr);
if( viewMip != 0 )
@ -165,7 +159,7 @@ void gx2Export_GX2SetColorBuffer(PPCInterpreter_t* hCPU)
}
Latte::E_GX2TILEMODE tileMode = colorBufferBE->surface.tileMode;
uint32 viewMipIndex = _swapEndianU32(colorBufferBE->viewMip);
uint32 viewMipIndex = colorBufferBE->viewMip;
uint32 swizzle = colorBufferBE->surface.swizzle;
if (Latte::TM_IsMacroTiled(tileMode) && viewMipIndex < ((swizzle >> 16) & 0xFF))
@ -210,7 +204,7 @@ void gx2Export_GX2SetDepthBuffer(PPCInterpreter_t* hCPU)
cemuLog_log(LogType::GX2, "DepthBuffer tileMode {:01x} PhysAddr {:08x} fmt {:04x} res {}x{}", (uint32)depthBufferBE->surface.tileMode.value(), (uint32)depthBufferBE->surface.imagePtr, (uint32)depthBufferBE->surface.format.value(), (uint32)depthBufferBE->surface.width, (uint32)depthBufferBE->surface.height);
uint32 viewMip = _swapEndianU32(depthBufferBE->viewMip);
uint32 viewMip = depthBufferBE->viewMip;
// todo: current code for the PM4 packets is a hack, replace with proper implementation
@ -256,8 +250,8 @@ void gx2Export_GX2SetDepthBuffer(PPCInterpreter_t* hCPU)
// set DB_DEPTH_VIEW
uint32 db_view = 0;
db_view |= (_swapEndianU32(depthBufferBE->viewFirstSlice)&0x7FF);
db_view |= (((_swapEndianU32(depthBufferBE->viewNumSlices)+_swapEndianU32(depthBufferBE->viewFirstSlice)-1)&0x7FF)<<13);
db_view |= ((uint32)depthBufferBE->viewFirstSlice&0x7FF);
db_view |= ((((uint32)depthBufferBE->viewNumSlices+(uint32)depthBufferBE->viewFirstSlice-1)&0x7FF)<<13);
gx2WriteGather_submitU32AsBE(pm4HeaderType3(IT_SET_CONTEXT_REG, 2));
gx2WriteGather_submitU32AsBE(mmDB_DEPTH_VIEW - 0xA000);
gx2WriteGather_submitU32AsBE(db_view);

View File

@ -33,17 +33,17 @@ static_assert(sizeof(GX2Surface) == 0x74);
struct GX2ColorBuffer
{
/* +0x00 */ GX2Surface surface;
/* +0x74 */ uint32 viewMip;
/* +0x78 */ uint32 viewFirstSlice;
/* +0x7C */ uint32 viewNumSlices;
/* +0x80 */ MPTR auxData;
/* +0x84 */ uint32 auxSize;
/* +0x88 */ uint32be reg_size; // CB_COLOR*_SIZE
/* +0x8C */ uint32be reg_info; // CB_COLOR*_INFO
/* +0x90 */ uint32be reg_view; // CB_COLOR*_VIEW
/* +0x94 */ uint32be reg_mask; // CB_COLOR*_MASK
/* +0x98 */ uint32be reg4; // ?
/* +0x00 */ GX2Surface surface;
/* +0x74 */ uint32be viewMip;
/* +0x78 */ uint32be viewFirstSlice;
/* +0x7C */ uint32be viewNumSlices;
/* +0x80 */ MEMPTR<void> auxData;
/* +0x84 */ uint32be auxSize2;
/* +0x88 */ uint32be reg_size; // CB_COLOR*_SIZE
/* +0x8C */ uint32be reg_info; // CB_COLOR*_INFO
/* +0x90 */ uint32be reg_view; // CB_COLOR*_VIEW
/* +0x94 */ uint32be reg_mask; // CB_COLOR*_MASK
/* +0x98 */ uint32be reg4; // ?
};
static_assert(sizeof(GX2ColorBuffer) == 0x9C);
@ -51,13 +51,13 @@ static_assert(sizeof(GX2ColorBuffer) == 0x9C);
struct GX2DepthBuffer
{
/* +0x00 */ GX2Surface surface;
/* +0x74 */ uint32 viewMip;
/* +0x78 */ uint32 viewFirstSlice;
/* +0x7C */ uint32 viewNumSlices;
/* +0x80 */ MPTR hiZPtr;
/* +0x84 */ uint32 hiZSize;
/* +0x88 */ float clearDepth;
/* +0x8C */ uint32 clearStencil;
/* +0x74 */ uint32be viewMip;
/* +0x78 */ uint32be viewFirstSlice;
/* +0x7C */ uint32be viewNumSlices;
/* +0x80 */ MEMPTR<void> hiZPtr;
/* +0x84 */ uint32be hiZSize;
/* +0x88 */ float32be clearDepth;
/* +0x8C */ uint32be clearStencil;
/* +0x90 */ uint32be reg_size;
/* +0x94 */ uint32be reg_view;
/* +0x98 */ uint32be reg_base;

View File

@ -435,13 +435,13 @@ void gx2Export_GX2ResolveAAColorBuffer(PPCInterpreter_t* hCPU)
GX2ColorBuffer* srcColorBuffer = (GX2ColorBuffer*)memory_getPointerFromVirtualOffset(hCPU->gpr[3]);
GX2Surface* srcSurface = &srcColorBuffer->surface;
GX2Surface* dstSurface = (GX2Surface*)memory_getPointerFromVirtualOffset(hCPU->gpr[4]);
uint32 srcMip = _swapEndianU32(srcColorBuffer->viewMip);
uint32 srcMip = srcColorBuffer->viewMip;
uint32 dstMip = hCPU->gpr[5];
uint32 srcSlice = _swapEndianU32(srcColorBuffer->viewFirstSlice);
uint32 srcSlice = srcColorBuffer->viewFirstSlice;
uint32 dstSlice = hCPU->gpr[6];
#ifdef CEMU_DEBUG_ASSERT
if( _swapEndianU32(srcColorBuffer->viewMip) != 0 || _swapEndianU32(srcColorBuffer->viewFirstSlice) != 0 )
if( srcColorBuffer->viewMip != 0 || srcColorBuffer->viewFirstSlice != 0 )
assert_dbg();
#endif
@ -618,7 +618,7 @@ void gx2Export_GX2ConvertDepthBufferToTextureSurface(PPCInterpreter_t* hCPU)
sint32 srcMip = 0;
uint32 numSlices = std::max<uint32>(_swapEndianU32(depthBuffer->viewNumSlices), 1);
uint32 numSlices = std::max<uint32>(depthBuffer->viewNumSlices, 1);
GX2::GX2ReserveCmdSpace((1 + 13 * 2) * numSlices);
for (uint32 subSliceIndex = 0; subSliceIndex < numSlices; subSliceIndex++)
{