From 0993658c82e89dbda35d29b36a69d1e9e3d47678 Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Sun, 10 Mar 2024 01:21:04 +0100 Subject: [PATCH] GX2: Rework GX2Set*UniformReg - Use cafeExportRegister() instead of legacy export - Submit as a single PM4 packet - Add logging for the special case of the size parameter (not sure if this is used by any game?) - Add some extra validation and logging which may be helpful to homebrew devs --- src/Cafe/OS/libs/gx2/GX2.cpp | 3 -- src/Cafe/OS/libs/gx2/GX2.h | 2 -- src/Cafe/OS/libs/gx2/GX2_Shader.cpp | 34 +++++++++++++++++++++ src/Cafe/OS/libs/gx2/GX2_shader_legacy.cpp | 35 ---------------------- src/Cafe/OS/libs/nn_act/nn_act.cpp | 2 -- 5 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/Cafe/OS/libs/gx2/GX2.cpp b/src/Cafe/OS/libs/gx2/GX2.cpp index 82aef164..c2ea34a4 100644 --- a/src/Cafe/OS/libs/gx2/GX2.cpp +++ b/src/Cafe/OS/libs/gx2/GX2.cpp @@ -396,16 +396,13 @@ void gx2_load() osLib_addFunction("gx2", "GX2GetCurrentScanBuffer", gx2Export_GX2GetCurrentScanBuffer); // shader stuff - //osLib_addFunction("gx2", "GX2SetVertexShader", gx2Export_GX2SetVertexShader); osLib_addFunction("gx2", "GX2SetPixelShader", gx2Export_GX2SetPixelShader); osLib_addFunction("gx2", "GX2SetGeometryShader", gx2Export_GX2SetGeometryShader); osLib_addFunction("gx2", "GX2SetComputeShader", gx2Export_GX2SetComputeShader); - osLib_addFunction("gx2", "GX2SetVertexUniformReg", gx2Export_GX2SetVertexUniformReg); osLib_addFunction("gx2", "GX2SetVertexUniformBlock", gx2Export_GX2SetVertexUniformBlock); osLib_addFunction("gx2", "GX2RSetVertexUniformBlock", gx2Export_GX2RSetVertexUniformBlock); osLib_addFunction("gx2", "GX2SetPixelUniformBlock", gx2Export_GX2SetPixelUniformBlock); - osLib_addFunction("gx2", "GX2SetPixelUniformReg", gx2Export_GX2SetPixelUniformReg); osLib_addFunction("gx2", "GX2SetGeometryUniformBlock", gx2Export_GX2SetGeometryUniformBlock); osLib_addFunction("gx2", "GX2SetShaderModeEx", gx2Export_GX2SetShaderModeEx); diff --git a/src/Cafe/OS/libs/gx2/GX2.h b/src/Cafe/OS/libs/gx2/GX2.h index 58d98191..a22719f4 100644 --- a/src/Cafe/OS/libs/gx2/GX2.h +++ b/src/Cafe/OS/libs/gx2/GX2.h @@ -18,11 +18,9 @@ void gx2_load(); void gx2Export_GX2SetPixelShader(PPCInterpreter_t* hCPU); void gx2Export_GX2SetGeometryShader(PPCInterpreter_t* hCPU); void gx2Export_GX2SetComputeShader(PPCInterpreter_t* hCPU); -void gx2Export_GX2SetVertexUniformReg(PPCInterpreter_t* hCPU); void gx2Export_GX2SetVertexUniformBlock(PPCInterpreter_t* hCPU); void gx2Export_GX2RSetVertexUniformBlock(PPCInterpreter_t* hCPU); void gx2Export_GX2SetPixelUniformBlock(PPCInterpreter_t* hCPU); -void gx2Export_GX2SetPixelUniformReg(PPCInterpreter_t* hCPU); void gx2Export_GX2SetGeometryUniformBlock(PPCInterpreter_t* hCPU); void gx2Export_GX2SetShaderModeEx(PPCInterpreter_t* hCPU); void gx2Export_GX2CalcGeometryShaderInputRingBufferSize(PPCInterpreter_t* hCPU); diff --git a/src/Cafe/OS/libs/gx2/GX2_Shader.cpp b/src/Cafe/OS/libs/gx2/GX2_Shader.cpp index ad17dc49..d004288b 100644 --- a/src/Cafe/OS/libs/gx2/GX2_Shader.cpp +++ b/src/Cafe/OS/libs/gx2/GX2_Shader.cpp @@ -417,6 +417,37 @@ namespace GX2 } } + void _GX2SubmitUniformReg(uint32 offsetRegBase, uint32 aluRegisterOffset, uint32be* dataWords, uint32 sizeInU32s) + { + if(aluRegisterOffset&0x8000) + { + cemuLog_logDebug(LogType::Force, "_GX2SubmitUniformReg(): Unhandled loop const special case or invalid offset"); + return; + } + if((aluRegisterOffset+sizeInU32s) > 0x400) + { + cemuLog_logOnce(LogType::APIErrors, "GX2SetVertexUniformReg values are out of range (offset {} + size {} must be equal or smaller than 0x400)", aluRegisterOffset, sizeInU32s); + } + if( (sizeInU32s&3) != 0) + { + cemuLog_logOnce(LogType::APIErrors, "GX2Set*UniformReg must be called with a size that is a multiple of 4 (size: {:})", sizeInU32s); + sizeInU32s &= ~3; + } + GX2ReserveCmdSpace(2 + sizeInU32s); + gx2WriteGather_submit(pm4HeaderType3(IT_SET_ALU_CONST, 1 + sizeInU32s), offsetRegBase + aluRegisterOffset); + gx2WriteGather_submitU32AsLEArray((uint32*)dataWords, sizeInU32s); + } + + void GX2SetVertexUniformReg(uint32 offset, uint32 sizeInU32s, uint32be* values) + { + _GX2SubmitUniformReg(0x400, offset, values, sizeInU32s); + } + + void GX2SetPixelUniformReg(uint32 offset, uint32 sizeInU32s, uint32be* values) + { + _GX2SubmitUniformReg(0, offset, values, sizeInU32s); + } + void GX2ShaderInit() { cafeExportRegister("gx2", GX2CalcFetchShaderSizeEx, LogType::GX2); @@ -428,5 +459,8 @@ namespace GX2 cafeExportRegister("gx2", GX2GetPixelShaderStackEntries, LogType::GX2); cafeExportRegister("gx2", GX2SetFetchShader, LogType::GX2); cafeExportRegister("gx2", GX2SetVertexShader, LogType::GX2); + + cafeExportRegister("gx2", GX2SetVertexUniformReg, LogType::GX2); + cafeExportRegister("gx2", GX2SetPixelUniformReg, LogType::GX2); } } \ No newline at end of file diff --git a/src/Cafe/OS/libs/gx2/GX2_shader_legacy.cpp b/src/Cafe/OS/libs/gx2/GX2_shader_legacy.cpp index 1cb61a7e..b0a5d2fa 100644 --- a/src/Cafe/OS/libs/gx2/GX2_shader_legacy.cpp +++ b/src/Cafe/OS/libs/gx2/GX2_shader_legacy.cpp @@ -270,41 +270,6 @@ void gx2Export_GX2SetComputeShader(PPCInterpreter_t* hCPU) osLib_returnFromFunction(hCPU, 0); } -void _GX2SubmitUniformReg(uint32 aluRegisterOffset, MPTR virtualAddress, uint32 count) -{ - uint32* dataWords = (uint32*)memory_getPointerFromVirtualOffset(virtualAddress); - GX2ReserveCmdSpace(2 + (count / 0xFF) * 2 + count); - // write PM4 command(s) - uint32 currentRegisterOffset = aluRegisterOffset; - while (count > 0) - { - uint32 subCount = std::min(count, 0xFFu); // a single command can write at most 0xFF values - gx2WriteGather_submit(pm4HeaderType3(IT_SET_ALU_CONST, 1 + subCount), - currentRegisterOffset); - gx2WriteGather_submitU32AsLEArray(dataWords, subCount); - - dataWords += subCount; - count -= subCount; - currentRegisterOffset += subCount; - } -} - -void gx2Export_GX2SetVertexUniformReg(PPCInterpreter_t* hCPU) -{ - cemuLog_log(LogType::GX2, "GX2SetVertexUniformReg(0x{:08x},0x{:x},0x{:08x})", hCPU->gpr[3], hCPU->gpr[4], hCPU->gpr[5]); - _GX2SubmitUniformReg(hCPU->gpr[3] + 0x400, hCPU->gpr[5], hCPU->gpr[4]); - cemu_assert_debug((hCPU->gpr[3] + hCPU->gpr[4]) <= 0x400); - osLib_returnFromFunction(hCPU, 0); -} - -void gx2Export_GX2SetPixelUniformReg(PPCInterpreter_t* hCPU) -{ - cemuLog_log(LogType::GX2, "GX2SetPixelUniformReg(0x{:08x},0x{:x},0x{:08x})", hCPU->gpr[3], hCPU->gpr[4], hCPU->gpr[5]); - _GX2SubmitUniformReg(hCPU->gpr[3], hCPU->gpr[5], hCPU->gpr[4]); - cemu_assert_debug((hCPU->gpr[3] + hCPU->gpr[4]) <= 0x400); - osLib_returnFromFunction(hCPU, 0); -} - void _GX2SubmitUniformBlock(uint32 registerBase, uint32 index, MPTR virtualAddress, uint32 size) { GX2ReserveCmdSpace(9); diff --git a/src/Cafe/OS/libs/nn_act/nn_act.cpp b/src/Cafe/OS/libs/nn_act/nn_act.cpp index 0fd9df5a..2a9f61bc 100644 --- a/src/Cafe/OS/libs/nn_act/nn_act.cpp +++ b/src/Cafe/OS/libs/nn_act/nn_act.cpp @@ -543,8 +543,6 @@ void nnActExport_GetDefaultAccount(PPCInterpreter_t* hCPU) void nnActExport_GetSlotNo(PPCInterpreter_t* hCPU) { // id of active account - // uint8 GetSlotNo(void); - cemuLog_logDebug(LogType::Force, "nn_act.GetSlotNo()"); osLib_returnFromFunction(hCPU, 1); // 1 is the first slot (0 is invalid) }