diff --git a/.gitmodules b/.gitmodules index 2cd57e65..802dd963 100644 --- a/.gitmodules +++ b/.gitmodules @@ -79,4 +79,4 @@ url = https://github.com/Panda3DS-emu/xbyak [submodule "third_party/dynarmic"] path = third_party/dynarmic - url = https://github.com/azahar-emu/dynarmic.git + url = https://github.com/gagarinyury/dynarmic.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f4d9df2..8e93a479 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,7 +177,10 @@ if(ENABLE_DISCORD_RPC AND NOT ANDROID) include_directories(third_party/discord-rpc/include) endif() -if (NOT ANDROID) +if (EMSCRIPTEN) + target_compile_options(AlberCore PUBLIC "-sUSE_SDL=2") + target_link_options(AlberCore PUBLIC "-sUSE_SDL=2") +elseif (NOT ANDROID) if (USE_SYSTEM_SDL2) find_package(SDL2 CONFIG REQUIRED) target_link_libraries(AlberCore PUBLIC SDL2::SDL2) @@ -215,7 +218,7 @@ if(ANDROID) target_link_libraries(AlberCore PRIVATE EGL log) endif() -if(NOT APPLE AND (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")) +if(EMSCRIPTEN OR (NOT APPLE AND (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"))) set(CRYPTOPP_OPT_DISABLE_ASM ON CACHE BOOL "" FORCE) endif() @@ -332,7 +335,7 @@ if(ENABLE_RENDERDOC_API) add_compile_definitions(PANDA3DS_ENABLE_RENDERDOC) endif() -if(HOST_X64 OR HOST_ARM64) +if(HOST_X64 OR HOST_ARM64 OR EMSCRIPTEN) set(DYNARMIC_TESTS OFF) set(DYNARMIC_FRONTENDS "A32" CACHE STRING "") add_subdirectory(third_party/dynarmic) @@ -755,6 +758,23 @@ else() target_compile_definitions(AlberCore PUBLIC "PANDA3DS_FRONTEND_SDL=1") endif() +# panda3ds-wasm: headless driver used to test the WebAssembly CPU backend under Node +if(EMSCRIPTEN AND EXISTS "${CMAKE_SOURCE_DIR}/../headless/headless.cpp") + add_executable(AlberHeadless "${CMAKE_SOURCE_DIR}/../headless/headless.cpp") + target_link_libraries(AlberHeadless PRIVATE AlberCore) + target_compile_options(AlberHeadless PRIVATE -fexceptions) + target_link_options(AlberHeadless PRIVATE -fexceptions -sNODERAWFS=1 -sENVIRONMENT=node -sALLOW_MEMORY_GROWTH=1 -sSTACK_SIZE=2097152 -sEXIT_RUNTIME=1 --profiling-funcs) +endif() + +# panda3ds-wasm: browser build (JS drives the frame loop and reads the two screens) +if(EMSCRIPTEN AND EXISTS "${CMAKE_SOURCE_DIR}/../web/web_main.cpp") + add_executable(AlberWeb "${CMAKE_SOURCE_DIR}/../web/web_main.cpp") + target_link_libraries(AlberWeb PRIVATE AlberCore) + target_link_options(AlberWeb PRIVATE -sENVIRONMENT=web -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sMAXIMUM_MEMORY=2147483648 -sSTACK_SIZE=2097152 -sFORCE_FILESYSTEM=1 + -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sGL_TRACK_ERRORS=0 -lidbfs.js + "-sEXPORTED_FUNCTIONS=_main,_web_init,_web_load,_web_frame,_web_screen,_web_key,_web_touch,_web_circle,_web_layout,_web_layout_rect" "-sEXPORTED_RUNTIME_METHODS=FS,ccall,cwrap,HEAPU8,HEAPF32,stringToNewUTF8,ENV" --profiling-funcs) +endif() + if(NOT BUILD_HYDRA_CORE AND NOT BUILD_LIBRETRO_CORE) add_executable(Alber) diff --git a/include/PICA/gpu.hpp b/include/PICA/gpu.hpp index e62f41b3..dd594ee8 100644 --- a/include/PICA/gpu.hpp +++ b/include/PICA/gpu.hpp @@ -99,9 +99,10 @@ class GPU { // Used to prevent uploading the lighting_lut on every draw call // Set to true when the CPU writes to the lighting_lut // Set to false by the renderer when the lighting_lut is uploaded ot the GPU - bool lightingLUTDirty = false; + bool lightingLUTDirty = true; + u32 lightingLUTDirtyMask = (1u << PICA::Lights::LUT_Count) - 1; - bool fogLUTDirty = false; + bool fogLUTDirty = true; std::array fogLUT; GPU(Memory& mem, EmulatorConfig& config); diff --git a/include/PICA/regs.hpp b/include/PICA/regs.hpp index 3185d350..5e7c1d2e 100644 --- a/include/PICA/regs.hpp +++ b/include/PICA/regs.hpp @@ -5,6 +5,7 @@ namespace PICA { namespace InternalRegs { enum : u32 { // Rasterizer registers + FaceCulling = 0x40, ViewportWidth = 0x41, ViewportInvw = 0x42, ViewportHeight = 0x43, @@ -72,6 +73,7 @@ namespace PICA { StencilTest = 0x105, StencilOp = 0x106, DepthAndColorMask = 0x107, + ColourBufferWrite = 0x113, // GPUREG_COLORBUFFER_WRITE: 0 = no colour writes at all (stencil/depth-only passes, e.g. shadow volumes), 0xF = enabled DepthBufferWrite = 0x115, DepthBufferFormat = 0x116, ColourBufferFormat = 0x117, diff --git a/include/cpu_dynarmic.hpp b/include/cpu_dynarmic.hpp index 24358533..24bd02f8 100644 --- a/include/cpu_dynarmic.hpp +++ b/include/cpu_dynarmic.hpp @@ -1,4 +1,6 @@ #pragma once +#include +#include #include @@ -14,6 +16,10 @@ class Emulator; class CPU; +// Env CPU_STATS=1: time spent in SVC handlers (Panda C++, includes frame waits), see CPU::runFrame +inline const bool cpuStats = std::getenv("CPU_STATS") != nullptr; +inline double cpuCallbackMs = 0; + class MyEnvironment final : public Dynarmic::A32::UserCallbacks { public: u64 ticksLeft = 0; @@ -79,7 +85,13 @@ class MyEnvironment final : public Dynarmic::A32::UserCallbacks { } void CallSVC(u32 swi) override { - kernel.serviceSVC(swi); + if (cpuStats) { + const auto t0 = std::chrono::steady_clock::now(); + kernel.serviceSVC(swi); + cpuCallbackMs += std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); + } else { + kernel.serviceSVC(swi); + } } void ExceptionRaised(u32 pc, Dynarmic::A32::Exception exception) override { @@ -180,6 +192,10 @@ class CPU { void addTicks(u64 ticks) { env.AddTicks(ticks); } + // End the current time slice: Run() returns after the current block so the scheduler can process due events + void stopSlice() { env.ticksLeft = 0; } + void stop(); + void clearCache() { jit->ClearCache(); } void clearCacheRange(u32 start, u32 size) { jit->InvalidateCacheRange(start, size); } diff --git a/include/emulator.hpp b/include/emulator.hpp index b1191f6e..5eb1dc7a 100644 --- a/include/emulator.hpp +++ b/include/emulator.hpp @@ -117,6 +117,7 @@ class Emulator { CPU& getCPU() { return cpu; } Memory& getMemory() { return memory; } + GPU& getGPU() { return gpu; } Kernel& getKernel() { return kernel; } Scheduler& getScheduler() { return scheduler; } Audio::DSPCore* getDSP() { return dsp.get(); } diff --git a/include/fs/archive_base.hpp b/include/fs/archive_base.hpp index 074f1ccd..725ad48c 100644 --- a/include/fs/archive_base.hpp +++ b/include/fs/archive_base.hpp @@ -234,6 +234,11 @@ class ArchiveBase { virtual u64 getFreeBytes() = 0; virtual HorizonResult createFile(const FSPath& path, u64 size) = 0; virtual HorizonResult deleteFile(const FSPath& path) = 0; + // Archives that keep their data in host folders override this; the default only warns and reports success (the old stubbed behaviour) + virtual HorizonResult deleteDirectory(const FSPath& path, bool recursive) { + Helpers::warn("Unimplemented DeleteDirectory for %s archive", name().c_str()); + return Result::Success; + } virtual Rust::Result getFormatInfo(const FSPath& path) { Helpers::panic("Unimplemented GetFormatInfo for %s archive", name().c_str()); diff --git a/include/fs/archive_ext_save_data.hpp b/include/fs/archive_ext_save_data.hpp index d2e3d974..3ac6b541 100644 --- a/include/fs/archive_ext_save_data.hpp +++ b/include/fs/archive_ext_save_data.hpp @@ -14,6 +14,7 @@ class ExtSaveDataArchive : public ArchiveBase { HorizonResult createDirectory(const FSPath& path) override; HorizonResult createFile(const FSPath& path, u64 size) override; HorizonResult deleteFile(const FSPath& path) override; + HorizonResult deleteDirectory(const FSPath& path, bool recursive) override; HorizonResult renameFile(const FSPath& oldPath, const FSPath& newPath) override; Rust::Result openArchive(const FSPath& path) override; diff --git a/include/kernel/kernel.hpp b/include/kernel/kernel.hpp index c6d5044e..eb6b172f 100644 --- a/include/kernel/kernel.hpp +++ b/include/kernel/kernel.hpp @@ -151,6 +151,7 @@ class Kernel { void createThread(); void controlMemory(); void duplicateHandle(); + void exitProcess(); void exitThread(); void mapMemoryBlock(); void unmapMemoryBlock(); diff --git a/include/memory.hpp b/include/memory.hpp index d21e4be2..e5a7fca5 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -313,6 +313,9 @@ class Memory { std::optional getProgramID(); + // Page tables (one host pointer per 4 KiB page, 0 = slow path); exposed so the WebAssembly JIT can inline guest memory accesses + const uintptr_t* getReadTableBase() const { return readTable.data(); } + const uintptr_t* getWriteTableBase() const { return writeTable.data(); } u8* getDSPMem() { return dspRam; } u8* getDSPDataMem() { return &dspRam[DSP_DATA_MEMORY_OFFSET]; } u8* getDSPCodeMem() { return &dspRam[DSP_CODE_MEMORY_OFFSET]; } diff --git a/include/renderer.hpp b/include/renderer.hpp index 27d6a437..43ca8fb1 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -32,7 +32,7 @@ class Renderer { const std::array& regs; const std::array& externalRegs; - std::array fbSize; // The size of the framebuffer (ie both the colour and depth buffer)' + std::array fbSize = {0, 0}; // The size of the framebuffer (ie both the colour and depth buffer)' u32 colourBufferLoc; // Location in 3DS VRAM for the colour buffer PICA::ColorFmt colourBufferFormat; // Format of the colours stored in the colour buffer diff --git a/include/renderer_gl/gl_state.hpp b/include/renderer_gl/gl_state.hpp index d6cbcbc0..92e19f19 100644 --- a/include/renderer_gl/gl_state.hpp +++ b/include/renderer_gl/gl_state.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include "helpers.hpp" @@ -190,11 +191,14 @@ struct GLStateManager { void useProgram(const OpenGL::Program& program) { useProgram(program.handle()); } void setColourMask(bool r, bool g, bool b, bool a) { + static const bool legacy = std::getenv("GL_LEGACY_COLOURMASK") != nullptr; // A/B aid: the old behaviour (colour masks ignored) + if (legacy) return; if (r != redMask || g != greenMask || b != blueMask || a != alphaMask) { - r = redMask; - g = greenMask; - b = blueMask; - a = alphaMask; + // (the assignments used to be the other way round: the cache never changed and the previous mask was re-applied, so no colour mask ever took effect) + redMask = r; + greenMask = g; + blueMask = b; + alphaMask = a; OpenGL::setColourMask(r, g, b, a); } diff --git a/include/renderer_gl/renderer_gl.hpp b/include/renderer_gl/renderer_gl.hpp index 2023966f..56285f97 100644 --- a/include/renderer_gl/renderer_gl.hpp +++ b/include/renderer_gl/renderer_gl.hpp @@ -11,6 +11,7 @@ #include "PICA/float_types.hpp" #include "PICA/pica_frag_config.hpp" +#include "PICA/pica_frag_uniforms.hpp" #include "PICA/pica_hash.hpp" #include "PICA/pica_vert_config.hpp" #include "PICA/pica_vertex.hpp" @@ -63,6 +64,7 @@ class RendererGL final : public Renderer { bool oldDepthmapEnable = false; // Set by prepareForDraw, tells us whether the current draw is using hw-accelerated shader bool usingAcceleratedShader = false; + bool logicOpPreservesColour = false; // OpenGL ES / WebGL2 have no logic ops: set when the guest selected NOOP, which we emulate by not writing colour at all bool performIndexedRender = false; bool usingShortIndices = false; @@ -104,9 +106,17 @@ class RendererGL final : public Renderer { // Current offset for our hw shader uniform UBO u32 hwShaderUniformUBOOffset = 0; + // Dirty tracking for fragment uniforms UBO + PICA::FragmentUniforms prevFragmentUniforms{}; + bool hasPrevFragmentUniforms = false; + u32 prevFragmentUBOOffset = 0; + // Cache of fixed attribute values so that we don't do any duplicate updates std::array, 16> fixedAttrValues; + // Buffer for lighting LUT upload (24 rows * 256 texels * 2 components (RG)) + std::array lightingLutBuffer{}; + // Cached recompiled fragment shader struct CachedProgram { OpenGL::Program program; diff --git a/include/renderer_gl/surfaces.hpp b/include/renderer_gl/surfaces.hpp index ad1a1f5f..04825185 100644 --- a/include/renderer_gl/surfaces.hpp +++ b/include/renderer_gl/surfaces.hpp @@ -47,8 +47,8 @@ struct ColourBuffer { fbo.createWithDrawTexture(texture); fbo.bind(OpenGL::DrawAndReadFramebuffer); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - Helpers::warn("ColourBuffer: Incomplete framebuffer"); + if (const GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); status != GL_FRAMEBUFFER_COMPLETE) { + Helpers::warn("ColourBuffer: Incomplete framebuffer (status 0x%X, %dx%d, format %d)", status, int(size.x()), int(size.y()), int(format)); } // TODO: This should not clear the framebuffer contents. It should load them from VRAM. @@ -153,8 +153,8 @@ struct DepthBuffer { glBindTexture(GL_TEXTURE_2D, prevTexture); fbo.createWithDrawTexture(texture, fmt == GL_DEPTH_STENCIL ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - Helpers::panic("Incomplete framebuffer"); + if (const GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); status != GL_FRAMEBUFFER_COMPLETE) { + Helpers::panic("Incomplete framebuffer (status 0x%X, %dx%d, depth format %d)", status, int(size.x()), int(size.y()), int(format)); } } diff --git a/include/services/am.hpp b/include/services/am.hpp index f72a5efc..386a4675 100644 --- a/include/services/am.hpp +++ b/include/services/am.hpp @@ -16,6 +16,9 @@ class AMService { void getDLCTitleInfo(u32 messagePointer); void getPatchTitleInfo(u32 messagePointer); void listTitleInfo(u32 messagePointer); + void getTitleCount(u32 messagePointer); + void getTitleList(u32 messagePointer); + void getTitleIdList(u32 messagePointer); public: AMService(Memory& mem) : mem(mem) {} diff --git a/include/services/service_manager.hpp b/include/services/service_manager.hpp index f51ddf2d..7b2834d1 100644 --- a/include/services/service_manager.hpp +++ b/include/services/service_manager.hpp @@ -108,6 +108,7 @@ class ServiceManager { // "srv:" commands void enableNotification(u32 messagePointer); void getServiceHandle(u32 messagePointer); + void isServiceRegistered(u32 messagePointer); void publishToSubscriber(u32 messagePointer); void receiveNotification(u32 messagePointer); void registerClient(u32 messagePointer); diff --git a/src/core/CPU/cpu_dynarmic.cpp b/src/core/CPU/cpu_dynarmic.cpp index 124647d8..d6c855b1 100644 --- a/src/core/CPU/cpu_dynarmic.cpp +++ b/src/core/CPU/cpu_dynarmic.cpp @@ -4,6 +4,10 @@ #include "arm_defs.hpp" #include "emulator.hpp" +#include +#include +#include + CPU::CPU(Memory& mem, Kernel& kernel, Emulator& emu) : mem(mem), emu(emu), scheduler(emu.getScheduler()), env(mem, kernel, emu.getScheduler()) { cp15 = std::make_shared(); mem.setCPUTicks(getTicksRef()); @@ -22,6 +26,12 @@ CPU::CPU(Memory& mem, Kernel& kernel, Emulator& emu) : mem(mem), emu(emu), sched config.fastmem_pointer = std::nullopt; } +#ifdef __EMSCRIPTEN__ + // WebAssembly JIT: let generated code index the guest page tables directly + config.wasm_read_table = reinterpret_cast(mem.getReadTableBase()); + config.wasm_write_table = reinterpret_cast(mem.getWriteTableBase()); +#endif + jit = std::make_unique(config); } @@ -40,12 +50,23 @@ void CPU::reset() { void CPU::runFrame() { emu.frameDone = false; + // Env CPU_STATS=1: every 120 frames print the wall time spent inside jit->Run() (guest code + host callbacks) and the guest ticks it covered + const bool stats = cpuStats; + static double runMs = 0; + static u64 runTicks = 0, runCalls = 0, statFrames = 0; + const u64 ts0 = scheduler.currentTimestamp; + while (!emu.frameDone) { // Run CPU until the next scheduler event env.ticksLeft = scheduler.nextTimestamp - scheduler.currentTimestamp; execute: + const auto t0 = stats ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}; const auto exitReason = jit->Run(); + if (stats) { + runMs += std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); + runCalls++; + } // Handle any scheduler events that need handling. emu.pollScheduler(); @@ -55,11 +76,29 @@ void CPU::runFrame() { // The goto might be terrible but it does guarantee that this does not recursively call run and crash, instead getting optimized to a jump if (Dynarmic::Has(exitReason, Dynarmic::HaltReason::CacheInvalidation)) { goto execute; + } else if (Dynarmic::Has(exitReason, Dynarmic::HaltReason::UserDefined1)) { + break; } else { Helpers::panic("Exit reason: %d\nPC: %08X", static_cast(exitReason), getReg(15)); } } } + + if (stats) { + runTicks += scheduler.currentTimestamp - ts0; + if (++statFrames % 120 == 0) { + std::printf("[cpu] frames %llu: %.2f ms in Run per frame (%.2f of it in SVC handlers, %.2f outside), %.0f ticks per frame, %.1f ns per tick, %.1f Run calls per frame\n", (unsigned long long)statFrames, runMs / 120.0, cpuCallbackMs / 120.0, (runMs - cpuCallbackMs) / 120.0, double(runTicks) / 120.0, runMs * 1e6 / double(runTicks), double(runCalls) / 120.0); + std::fflush(stdout); + runMs = 0; cpuCallbackMs = 0; runTicks = 0; runCalls = 0; + } + } +} + +void CPU::stop() { + emu.running = false; + emu.frameDone = true; + env.ticksLeft = 0; + jit->HaltExecution(Dynarmic::HaltReason::UserDefined1); } #endif // CPU_DYNARMIC \ No newline at end of file diff --git a/src/core/PICA/gpu.cpp b/src/core/PICA/gpu.cpp index e38f4416..07347178 100644 --- a/src/core/PICA/gpu.cpp +++ b/src/core/PICA/gpu.cpp @@ -75,6 +75,7 @@ GPU::GPU(Memory& mem, EmulatorConfig& config) : mem(mem), config(config) { void GPU::reset() { regs.fill(0); + regs[PICA::InternalRegs::ColourBufferWrite] = 0xF; // colour writes are allowed until the guest says otherwise shaderUnit.reset(); shaderJIT.reset(); shaderJIT.setAccurateMul(config.accurateShaderMul); @@ -82,6 +83,7 @@ void GPU::reset() { std::memset(vram, 0, vramSize); lightingLUT.fill(0); lightingLUTDirty = true; + lightingLUTDirtyMask = (1u << PICA::Lights::LUT_Count) - 1; fogLUT.fill(0); fogLUTDirty = true; diff --git a/src/core/PICA/regs.cpp b/src/core/PICA/regs.cpp index f63ee186..85eecc0f 100644 --- a/src/core/PICA/regs.cpp +++ b/src/core/PICA/regs.cpp @@ -144,8 +144,10 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { case FogLUTData6: case FogLUTData7: { const uint32_t index = regs[FogLUTIndex] & 0x7F; - fogLUT[index] = value; - fogLUTDirty = true; + if (fogLUT[index] != newValue) { + fogLUT[index] = newValue; + fogLUTDirty = true; + } regs[FogLUTIndex] = (index + 1) & 0x7F; break; } @@ -163,8 +165,12 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { uint32_t lutIndex = getBits<0, 8>(index); // And get the index inside the LUT we're writing to if (lutID < PICA::Lights::LUT_Count) { - lightingLUT[lutID * 256 + lutIndex] = newValue; - lightingLUTDirty = true; + const uint32_t offset = lutID * 256 + lutIndex; + if (lightingLUT[offset] != newValue) { + lightingLUT[offset] = newValue; + lightingLUTDirty = true; + lightingLUTDirtyMask |= (1u << lutID); + } } // Increment the bottom 8 bits of the lighting LUT index register diff --git a/src/core/PICA/shader_gen_glsl.cpp b/src/core/PICA/shader_gen_glsl.cpp index a9ad3a90..63141b4d 100644 --- a/src/core/PICA/shader_gen_glsl.cpp +++ b/src/core/PICA/shader_gen_glsl.cpp @@ -81,6 +81,8 @@ std::string FragmentGenerator::getDefaultVertexShader() { #ifndef USING_GLES out float gl_ClipDistance[2]; + #else + out vec2 v_clipDist; // GLES / WebGL2 have no gl_ClipDistance: the fragment shader discards instead #endif void main() { @@ -97,6 +99,8 @@ std::string FragmentGenerator::getDefaultVertexShader() { #ifndef USING_GLES gl_ClipDistance[0] = -a_coords.z; gl_ClipDistance[1] = dot(clipCoords, a_coords); + #else + v_clipDist = vec2(-a_coords.z, dot(clipCoords, a_coords)); #endif } )"; @@ -153,6 +157,9 @@ std::string FragmentGenerator::generate(const FragmentConfig& config, void* driv in vec2 v_texcoord1; in vec3 v_view; in vec2 v_texcoord2; + #ifdef USING_GLES + in vec2 v_clipDist; + #endif out vec4 fragColor; uniform sampler2D u_tex0; @@ -185,6 +192,10 @@ std::string FragmentGenerator::generate(const FragmentConfig& config, void* driv // When not initialized, source 13 is set to vec4(0.0) and 15 is set to the vertex colour ret += R"( void main() { + #ifdef USING_GLES + // Emulates the two PICA clip planes (near plane and the user plane): both must be >= 0 + if (v_clipDist.x < 0.0 || v_clipDist.y < 0.0) discard; + #endif vec4 combinerOutput = v_colour; vec4 previousBuffer = vec4(0.0); vec4 tevNextPreviousBuffer = tevBufferColor; @@ -408,6 +419,8 @@ void FragmentGenerator::getSource(std::string& shader, TexEnvConfig::Source sour case TexEnvConfig::Source::PrimaryFragmentColor: shader += "primaryColor"; break; case TexEnvConfig::Source::SecondaryFragmentColor: shader += "secondaryColor"; break; + case TexEnvConfig::Source::Texture3: shader += "vec4(1.0, 1.0, 1.0, 1.0)"; break; + default: Helpers::warn("Unimplemented TEV source: %d", static_cast(source)); shader += "vec4(1.0, 1.0, 1.0, 1.0)"; @@ -785,6 +798,8 @@ out vec2 v_texcoord2; #ifndef USING_GLES out float gl_ClipDistance[2]; +#else + out vec2 v_clipDist; // GLES / WebGL2 have no gl_ClipDistance: the fragment shader discards instead #endif void main() { @@ -807,6 +822,8 @@ void main() { #ifndef USING_GLES gl_ClipDistance[0] = -a_coords.z; gl_ClipDistance[1] = dot(clipCoords, a_coords); +#else + v_clipDist = vec2(-a_coords.z, dot(clipCoords, a_coords)); #endif })"; return ret; diff --git a/src/core/audio/hle_core.cpp b/src/core/audio/hle_core.cpp index 092f94a8..86100b05 100644 --- a/src/core/audio/hle_core.cpp +++ b/src/core/audio/hle_core.cpp @@ -1,6 +1,8 @@ #include "audio/hle_core.hpp" #include +#include +#include #include #include #include @@ -244,6 +246,27 @@ namespace Audio { generateFrame(frame); if (audioEnabled) { +#ifdef __EMSCRIPTEN__ + // Single-threaded web build: the audio device runs on this very thread (between page ticks, and not at all until the browser allows sound), + // so waiting for room would never end and froze the page. Drop the oldest audio instead, which also keeps the delay short. + while (sampleBuffer.size() + frame.size() * 2 > sampleBuffer.Capacity()) { + s16 discard[512]; + const usize need = sampleBuffer.size() + frame.size() * 2 - sampleBuffer.Capacity(); + if (sampleBuffer.pop(discard, std::min(need, 512)) == 0) break; + } + if (sampleBuffer.size() + frame.size() * 2 <= sampleBuffer.Capacity()) sampleBuffer.push(frame.data(), frame.size() * 2); + { + static const bool dbg = std::getenv("AUDIO_DEBUG") != nullptr; + static u64 frames = 0, loud = 0; static s32 peak = 0; + if (dbg) { + frames++; + for (usize i = 0; i < frame.size(); i++) { for (int c = 0; c < 2; c++) { const s32 v = std::abs(static_cast(frame[i][c])); if (v > peak) peak = v; } } + if (peak > 0) loud++; + if (frames % 400 == 0) { std::printf("[audio-dbg] dsp frames %llu, frames with sound %llu, peak %d, buffer fill %zu / %zu\n", (unsigned long long)frames, (unsigned long long)loud, peak, (size_t)sampleBuffer.size(), (size_t)sampleBuffer.Capacity()); peak = 0; } + } + } + return; +#endif // Wait until we've actually got room to push our frame while (sampleBuffer.size() + frame.size() * 2 > sampleBuffer.Capacity()) { std::this_thread::sleep_for(std::chrono::milliseconds{1}); diff --git a/src/core/audio/miniaudio_device.cpp b/src/core/audio/miniaudio_device.cpp index 8e2a1e71..22242f32 100644 --- a/src/core/audio/miniaudio_device.cpp +++ b/src/core/audio/miniaudio_device.cpp @@ -1,6 +1,8 @@ #include "audio/miniaudio_device.hpp" #include +#include +#include #include #include #include @@ -103,8 +105,50 @@ void MiniAudioDevice::init(Samples& samples, bool safe) { } s16* output = reinterpret_cast(out); +#ifdef __EMSCRIPTEN__ + { + // Dynamic rate control. The emulator makes sound a little slower or faster than the device plays it (59.3 fps measured against 59.83), and a stall + // of the page empties the buffer: either way the player hears gaps. So the device reads its input at 1 +/- 1.5 % of the nominal speed, depending on + // how full the buffer is, which keeps the fill near the middle without an audible change of pitch (linear interpolation between neighbours). + static double pos = 0.0; + static s16 prev[2] = {0, 0}, cur[2] = {0, 0}; + static u64 calls = 0, starvedTotal = 0; + const double target = double(self->samples->Capacity()) / 2.0 * 0.5; // in frames: half of the buffer + const double fill = double(self->samples->size()) / 2.0; + const double ratio = 1.0 + std::clamp((fill - target) / target, -1.0, 1.0) * 0.015; + float volume = self->audioSettings.getVolume(); + if (volume > 1.0f) volume = 0.6f + 20.0f * std::log10(volume); + else if (self->audioSettings.volumeCurve == AudioDeviceConfig::VolumeCurve::Cubic) volume = volume * volume * volume; + s16* o = output; + for (ma_uint32 i = 0; i < frameCount; i++) { + pos += ratio; + while (pos >= 1.0) { + prev[0] = cur[0]; prev[1] = cur[1]; + s16 frame[2]; + if (self->samples->pop(frame, 2) == 2) { cur[0] = frame[0]; cur[1] = frame[1]; } else { starvedTotal++; } + pos -= 1.0; + } + const float t = float(pos); + for (int c = 0; c < 2; c++) { + float v = (float(prev[c]) + (float(cur[c]) - float(prev[c])) * t) * volume; + *o++ = s16(std::clamp(v, -32768.0f, 32767.0f)); + } + } + static const bool dbg = std::getenv("AUDIO_DEBUG") != nullptr; + if (dbg && ++calls % 40 == 0) std::printf("[audio-dbg] rate control: fill %.0f frames (target %.0f), ratio %.4f, starved frames so far %llu\n", fill, target, ratio, (unsigned long long)starvedTotal); + return; + } +#endif usize samplesWritten = 0; samplesWritten += self->samples->pop(output, frameCount * channelCount); + { + static const bool dbg = std::getenv("AUDIO_DEBUG") != nullptr; + static u64 calls = 0, got = 0, wanted = 0; + if (dbg) { + calls++; got += samplesWritten; wanted += frameCount * channelCount; + if (calls % 100 == 0) std::printf("[audio-dbg] device callbacks %llu, samples wanted %llu, got %llu\n", (unsigned long long)calls, (unsigned long long)wanted, (unsigned long long)got); + } + } // Get the last sample for underrun handling if (samplesWritten != 0) { diff --git a/src/core/fs/archive_ext_save_data.cpp b/src/core/fs/archive_ext_save_data.cpp index 2aaa157e..a1fe95ee 100644 --- a/src/core/fs/archive_ext_save_data.cpp +++ b/src/core/fs/archive_ext_save_data.cpp @@ -126,6 +126,22 @@ HorizonResult ExtSaveDataArchive::renameFile(const FSPath& oldPath, const FSPath return Result::Success; } +HorizonResult ExtSaveDataArchive::deleteDirectory(const FSPath& path, bool recursive) { + if (!path.isTextPath() || !isSafeTextPath(path)) { + Helpers::warn("ExtSaveData::DeleteDirectory: unsupported path"); + return Result::FS::FileNotFoundAlt; + } + + fs::path p = IOFile::getAppData() / backingFolder; + appendPath(p, path); + + std::error_code ec; + if (!fs::is_directory(p, ec)) return Result::FS::FileNotFoundAlt; + + if (recursive) fs::remove_all(p, ec); else fs::remove(p, ec); + return ec ? Result::FS::UnexpectedFileOrDir : Result::Success; +} + HorizonResult ExtSaveDataArchive::createDirectory(const FSPath& path) { if (path.isTextPath()) { if (!isSafeTextPath(path)) { @@ -143,8 +159,10 @@ HorizonResult ExtSaveDataArchive::createDirectory(const FSPath& path) { Helpers::panic("File path passed to ExtSaveData::CreateDirectory"); } - bool success = fs::create_directory(p); - return success ? Result::Success : Result::FS::UnexpectedFileOrDir; + // The throwing overloads abort the whole emulator when exceptions are disabled (web build): use error codes, and make missing parents too + std::error_code ec; + fs::create_directories(p, ec); + return ec ? Result::FS::UnexpectedFileOrDir : Result::Success; } else { Helpers::panic("Unimplemented ExtSaveData::CreateDirectory"); } diff --git a/src/core/fs/archive_ncch.cpp b/src/core/fs/archive_ncch.cpp index 5844bbda..712ca471 100644 --- a/src/core/fs/archive_ncch.cpp +++ b/src/core/fs/archive_ncch.cpp @@ -3,8 +3,11 @@ #include "fs/country_list.hpp" #include "fs/mii_data.hpp" #include +#include #include +CMRC_DECLARE(ConsoleFonts); + namespace PathType { enum : u32 { RomFS = 0, @@ -54,8 +57,11 @@ Rust::Result NCCHArchive::openArchive(const FSPath& } const u32 mediaType = path.binary[8]; - if (mediaType != 0) - Helpers::panic("NCCH archive. Tried to access a mediatype other than the NAND. Type: %d", mediaType); + // Games probe the SD card / game card for installed DLC and update titles. We have none: answer "not found" instead of aborting. + if (mediaType != 0) { + Helpers::warn("NCCH archive: mediatype %d is not supported (no such title installed), returning FileNotFoundAlt", mediaType); + return Err(Result::FS::FileNotFoundAlt); + } return Ok((ArchiveBase*)this); } @@ -93,16 +99,26 @@ std::optional NCCHArchive::readFile(FileSession* file, u64 offset, u32 size Helpers::warn("Read from Shared Data archive 00010602"); return 0; } - else Helpers::panic("[NCCH archive] Read unimplemented NAND file. ID: %08X", lowProgramID); + else if ((lowProgramID & 0xFFFF00) == 0x00014000) { + // System shared font (JPN 00014002, CHN 00014102, KOR 00014202, TWN 00014302) + auto fonts = cmrc::ConsoleFonts::get_filesystem(); + auto font = fonts.open("SharedFontReplacement.bin"); + fileData = std::vector(font.begin(), font.end()); + } + else { + Helpers::warn("[NCCH archive] Read unimplemented NAND file. ID: %08X", lowProgramID); + return 0; + } } else if (highProgramID == systemDataArchive && lowProgramID == badWordList) { fileData = std::vector(std::begin(BAD_WORD_LIST_DATA), std::end(BAD_WORD_LIST_DATA)); } else { - Helpers::panic("[NCCH archive] Read from unimplemented NCCH archive file. High program ID: %08X, low ID: %08X", + Helpers::warn("[NCCH archive] Read from unimplemented NCCH archive file. High program ID: %08X, low ID: %08X", highProgramID, lowProgramID); + return 0; } if (offset >= fileData.size()) { - Helpers::panic("[NCCH archive] Out of bounds read from NAND file"); + return 0; } u32 availableBytes = u32(fileData.size() - offset); // How many bytes we can read from the file @@ -113,7 +129,8 @@ std::optional NCCHArchive::readFile(FileSession* file, u64 offset, u32 size return bytesRead; } else { - Helpers::panic("NCCH archive tried to read non-NAND file"); + Helpers::warn("NCCH archive tried to read non-NAND file"); + return 0; } // Code below is for mediaType == 2 (gamecard). Currently unused diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 35bc93e4..68830c0c 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -55,7 +55,9 @@ HorizonResult SaveDataArchive::createDirectory(const FSPath& path) { Helpers::panic("File path passed to SaveData::CreateDirectory"); } - bool success = fs::create_directory(p); + std::error_code ec; // error-code overload: the throwing one aborts the web build + fs::create_directories(p, ec); + bool success = !ec; return success ? Result::Success : Result::FS::UnexpectedFileOrDir; } else { Helpers::panic("Unimplemented SaveData::CreateDirectory"); diff --git a/src/core/fs/archive_system_save_data.cpp b/src/core/fs/archive_system_save_data.cpp index 038b2fc8..09138afc 100644 --- a/src/core/fs/archive_system_save_data.cpp +++ b/src/core/fs/archive_system_save_data.cpp @@ -101,7 +101,9 @@ HorizonResult SystemSaveDataArchive::createDirectory(const FSPath& path) { Helpers::panic("File path passed to SystemSaveData::CreateDirectory"); } - bool success = fs::create_directory(p); + std::error_code ec; // error-code overload: the throwing one aborts the web build + fs::create_directories(p, ec); + bool success = !ec; return success ? Result::Success : Result::FS::UnexpectedFileOrDir; } else { Helpers::panic("Unimplemented SystemSaveData::CreateDirectory"); diff --git a/src/core/fs/archive_user_save_data.cpp b/src/core/fs/archive_user_save_data.cpp index dc558954..b2a16f57 100644 --- a/src/core/fs/archive_user_save_data.cpp +++ b/src/core/fs/archive_user_save_data.cpp @@ -52,7 +52,9 @@ HorizonResult UserSaveDataArchive::createDirectory(const FSPath& path) { Helpers::panic("File path passed to UserSaveData::CreateDirectory"); } - bool success = fs::create_directory(p); + std::error_code ec; // error-code overload: the throwing one aborts the web build + fs::create_directories(p, ec); + bool success = !ec; return success ? Result::Success : Result::FS::UnexpectedFileOrDir; } else { Helpers::panic("Unimplemented UserSaveData::CreateDirectory"); diff --git a/src/core/kernel/file_operations.cpp b/src/core/kernel/file_operations.cpp index becf88e6..693d9b76 100644 --- a/src/core/kernel/file_operations.cpp +++ b/src/core/kernel/file_operations.cpp @@ -109,7 +109,8 @@ void Kernel::readFile(u32 messagePointer, Handle fileHandle) { auto archive = file->archive; std::optional bytesRead = archive->readFile(file, offset, size, dataPointer); if (!bytesRead.has_value()) { - Helpers::panic("Kernel::ReadFile failed"); + Helpers::warn("Kernel::ReadFile failed"); + mem.write32(messagePointer + 4, Result::FS::FileNotFound); } else { mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 8, bytesRead.value()); diff --git a/src/core/kernel/kernel.cpp b/src/core/kernel/kernel.cpp index 7a0aacc0..2ac9f003 100644 --- a/src/core/kernel/kernel.cpp +++ b/src/core/kernel/kernel.cpp @@ -33,6 +33,7 @@ void Kernel::serviceSVC(u32 svc) { switch (svc) { case 0x01: controlMemory(); break; case 0x02: queryMemory(); break; + case 0x03: exitProcess(); break; case 0x08: createThread(); break; case 0x09: exitThread(); break; case 0x0A: svcSleepThread(); break; @@ -423,3 +424,8 @@ std::string Kernel::getProcessName(u32 pid) { } Scheduler& Kernel::getScheduler() { return cpu.getScheduler(); } + +void Kernel::exitProcess() { + logSVC("ExitProcess\n"); + cpu.stop(); +} diff --git a/src/core/kernel/threads.cpp b/src/core/kernel/threads.cpp index 5ada30e3..12b1028f 100644 --- a/src/core/kernel/threads.cpp +++ b/src/core/kernel/threads.cpp @@ -400,6 +400,10 @@ void Kernel::sleepThread(s64 ns) { u64 idleCycles = timestamp - scheduler.currentTimestamp; cpu.addTicks(idleCycles); } + + // We have skipped to the next event, so end the CPU's time slice and let the scheduler handle it now. Without this the + // slice (sized for the whole frame when it started) keeps running and the idle thread spins through the remainder of it. + cpu.stopSlice(); } } } else { // If we're sleeping for >= 0 ns diff --git a/src/core/renderer_gl/renderer_gl.cpp b/src/core/renderer_gl/renderer_gl.cpp index 659a1e1d..775be017 100644 --- a/src/core/renderer_gl/renderer_gl.cpp +++ b/src/core/renderer_gl/renderer_gl.cpp @@ -1,5 +1,12 @@ #include "renderer_gl/renderer_gl.hpp" +#include +#include +#include +#include +#include +#include + #include #include @@ -58,22 +65,30 @@ void RendererGL::reset() { void RendererGL::initGraphicsContextInternal() { gl.reset(); + hasPrevFragmentUniforms = false; auto gl_resources = cmrc::RendererGL::get_filesystem(); - auto vertexShaderSource = gl_resources.open("opengl_vertex_shader.vert"); - auto fragmentShaderSource = gl_resources.open("opengl_fragment_shader.frag"); + // The ubershader is desktop GLSL (#version 410 core) and is turned off on OpenGL ES, so do not even try to compile it there + // (a WebGL2 context throws on the invalid shader object instead of just logging an error) + if (!driverInfo.usingGLES) { + auto vertexShaderSource = gl_resources.open("opengl_vertex_shader.vert"); + auto fragmentShaderSource = gl_resources.open("opengl_fragment_shader.frag"); - OpenGL::Shader vert({vertexShaderSource.begin(), vertexShaderSource.size()}, OpenGL::Vertex); - OpenGL::Shader frag({fragmentShaderSource.begin(), fragmentShaderSource.size()}, OpenGL::Fragment); - triangleProgram.create({vert, frag}); - initUbershader(triangleProgram); + OpenGL::Shader vert({vertexShaderSource.begin(), vertexShaderSource.size()}, OpenGL::Vertex); + OpenGL::Shader frag({fragmentShaderSource.begin(), fragmentShaderSource.size()}, OpenGL::Fragment); + triangleProgram.create({vert, frag}); + initUbershader(triangleProgram); + } compileDisplayShader(); // Create stream buffers for vertex, index and uniform buffers - static constexpr usize hwIndexBufferSize = 2_MB; - static constexpr usize hwVertexBufferSize = 16_MB; - static constexpr usize hwShaderUniformUBOSize = 4_MB; - static constexpr usize shadergenFragmentUBOSize = 4_MB; + // Sizes matter on WebGL2/ANGLE-Metal: a write into a buffer that the GPU still uses costs about as much as copying the whole buffer + // (measured on Super Mario 3D Land: 16 MB vertex ring = ~0.4 ms per draw although only ~300 bytes are written per draw), so keep the rings small. + // A ring only has to hold the draws between two wraps (orphaning on wrap is cheap). + static constexpr usize hwIndexBufferSize = 256_KB; + static constexpr usize hwVertexBufferSize = 1_MB; + static constexpr usize hwShaderUniformUBOSize = 256_KB; + static constexpr usize shadergenFragmentUBOSize = 256_KB; hwIndexBuffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, hwIndexBufferSize); hwVertexBuffer = StreamBuffer::Create(GL_ARRAY_BUFFER, hwVertexBufferSize); @@ -232,9 +247,20 @@ void RendererGL::setupBlending() { const u32 logicOp = getBits<0, 4>(regs[PICA::InternalRegs::LogicOp]); gl.setLogicOp(logicOps[logicOp]); - // If logic ops are enabled we don't need to disable blending because they override it + // If logic ops are enabled we don't need to disable blending on desktop GL because they override it. OpenGL ES / WebGL2 have no logic ops + // (enableLogicOp is a no-op there), so blending must be switched off explicitly or the previous draw's blend state would stay in effect. + gl.disableBlend(); gl.enableLogicOp(); + // GLES / WebGL2 cannot do logic ops. The two that games really use are COPY (plain replace, which blending-off gives us) and NOOP, which + // stencil-only passes such as the shadow volumes in Super Mario 3D Land rely on (colour must stay untouched: without this they show up as black prisms). + logicOpPreservesColour = driverInfo.usingGLES && logicOps[logicOp] == GL_NOOP; + if (driverInfo.usingGLES && logicOps[logicOp] == GL_CLEAR) { // result is zero: blend (0 * src + 0 * dst) + gl.enableBlend(); + gl.setBlendEquation(GL_FUNC_ADD, GL_FUNC_ADD); + gl.setBlendFunc(GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO); + } } else { + logicOpPreservesColour = false; gl.enableBlend(); gl.disableLogicOp(); @@ -275,14 +301,20 @@ void RendererGL::setupStencilTest(bool stencilEnable) { const u32 stencilConfig = regs[PICA::InternalRegs::StencilTest]; const u32 stencilFunc = getBits<4, 3>(stencilConfig); - const s32 reference = s8(getBits<16, 8>(stencilConfig)); // Signed reference value + // 3dbrew calls the reference value signed, but OpenGL takes the reference as 0..255 and clamps negative numbers to 0, which broke every value >= 128 + const s32 reference = s32(getBits<16, 8>(stencilConfig)); const u32 stencilRefMask = getBits<24, 8>(stencilConfig); - const bool stencilWrite = regs[PICA::InternalRegs::DepthBufferWrite]; + const bool stencilWrite = (regs[PICA::InternalRegs::DepthBufferWrite] & 1) != 0; // GPUREG_DEPTHBUFFER_WRITE: bit 0 = stencil write, bit 1 = depth write (3dbrew) const u32 stencilBufferMask = stencilWrite ? getBits<8, 8>(stencilConfig) : 0; // TODO: Throw stencilFunc/stencilOp to the GL state manager - glStencilFunc(stencilFuncs[stencilFunc], reference, stencilRefMask); + // Debug aid (env GL_SHADOW_FORCE=1): the shadow darkening pass of Super Mario 3D Land (stencil != 0, reference 0) ignores the stencil buffer, to see whether the + // pass itself is drawn (the whole quad darkens) or the stencil counts are what stays at zero + static const bool shadowForce = std::getenv("GL_SHADOW_FORCE") != nullptr; + GLenum stencilFuncGL = stencilFuncs[stencilFunc]; + if (shadowForce && stencilFunc == 3 && reference == 0) stencilFuncGL = GL_ALWAYS; + glStencilFunc(stencilFuncGL, reference, stencilRefMask); gl.setStencilMask(stencilBufferMask); static constexpr std::array stencilOps = { @@ -381,16 +413,48 @@ void RendererGL::bindTexturesToSlots() { void RendererGL::updateLightingLUT() { gpu.lightingLUTDirty = false; - std::array lightingLut; - - for (int i = 0; i < lightingLut.size(); i += 2) { - uint64_t value = gpu.lightingLUT[i >> 1] & 0xFFF; - lightingLut[i] = (float)(value << 4) / 65535.0f; - } + const u32 dirtyMask = gpu.lightingLUTDirtyMask; + gpu.lightingLUTDirtyMask = 0; + if (!dirtyMask) return; glActiveTexture(GL_TEXTURE0 + 3); LUTTexture.bind(); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, Lights::LUT_Count, GL_RG, GL_FLOAT, lightingLut.data()); + + // If all 24 rows are dirty, upload everything in one call + if (dirtyMask == ((1u << Lights::LUT_Count) - 1)) { + for (size_t i = 0; i < Lights::LUT_Count * 256; ++i) { + uint32_t value = gpu.lightingLUT[i] & 0xFFF; + lightingLutBuffer[i * 2] = (float)(value << 4) / 65535.0f; + lightingLutBuffer[i * 2 + 1] = 0.0f; + } + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, Lights::LUT_Count, GL_RG, GL_FLOAT, lightingLutBuffer.data()); + } else { + // Upload only dirty rows (in contiguous spans) + int r = 0; + while (r < Lights::LUT_Count) { + if (!(dirtyMask & (1u << r))) { + r++; + continue; + } + int start = r; + while (r < Lights::LUT_Count && (dirtyMask & (1u << r))) { + r++; + } + int count = r - start; + for (int row = 0; row < count; row++) { + const int lutID = start + row; + const size_t lutBase = lutID * 256; + const size_t outBase = (start + row) * 256 * 2; + for (size_t i = 0; i < 256; i++) { + uint32_t value = gpu.lightingLUT[lutBase + i] & 0xFFF; + lightingLutBuffer[outBase + i * 2] = (float)(value << 4) / 65535.0f; + lightingLutBuffer[outBase + i * 2 + 1] = 0.0f; + } + } + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, start, 256, count, GL_RG, GL_FLOAT, &lightingLutBuffer[start * 256 * 2]); + } + } + glActiveTexture(GL_TEXTURE0); } @@ -422,6 +486,51 @@ void RendererGL::updateFogLUT() { } void RendererGL::drawVertices(PICA::PrimType primType, std::span vertices) { + static const bool skipDraws = std::getenv("GL_SKIP_DRAWS") != nullptr; // measuring aid: skip all 3D draws (the rest of the frame stays) + if (skipDraws) return; + // A game may draw before it has configured the framebuffer (size 0, or uninitialised memory in a non-zeroed heap): skip such draws + if (fbSize[0] == 0 || fbSize[1] == 0 || fbSize[0] > 4096 || fbSize[1] > 4096) return; + + // Measuring aid (env GL_SKIP_BOTTOM_DRAWS=1): do not draw into a lower-screen sized colour buffer (240x320) at all + static const bool skipBottomDraws = std::getenv("GL_SKIP_BOTTOM_DRAWS") != nullptr; + if (skipBottomDraws && fbSize[0] == 240 && fbSize[1] == 320) return; + + // Debug aid (env GL_DRAW_STATS=1): which colour buffers (address, size) the draws go to; prints every 600 draws + static const bool dbgDraws = std::getenv("GL_DRAW_STATS") != nullptr; + if (dbgDraws) { + static std::map seenBufs; + static int nDraws = 0; + char key[120]; + std::snprintf(key, sizeof key, "loc=%08x fb=%ux%u verts=%s", colourBufferLoc, unsigned(fbSize[0]), unsigned(fbSize[1]), vertices.size() <= 6 ? "<=6" : (vertices.size() <= 60 ? "<=60" : ">60")); + seenBufs[key]++; + if (++nDraws % 600 == 0) { + for (auto& [k, v] : seenBufs) std::printf("[drawbuf] %4d x %s\n", v, k.c_str()); + std::printf("[drawbuf] ---\n"); + seenBufs.clear(); + } + } + + // Debug aid (env GL_DEBUG_STATE=1): print the most frequent combinations of the output-merger registers every 3000 draws + static const bool dbgState = std::getenv("GL_DEBUG_STATE") != nullptr; + if (dbgState) { + static std::map seen; + static int drawCount = 0; + char key[300]; + std::snprintf(key, sizeof key, "cull=%x blendCol=%08x texCfg=%08x colOp=%08x stencil=%08x stencilOp=%08x depthColorMask=%08x depthWrite=%x colourWrite=%x depthRead=%x colourRead=%x alphaTest=%08x clipEn=%x logicOp=%x blendFunc=%08x vertices=%s", + regs[PICA::InternalRegs::FaceCulling] & 3, regs[PICA::InternalRegs::BlendColour], regs[PICA::InternalRegs::TexUnitCfg], regs[PICA::InternalRegs::ColourOperation], regs[PICA::InternalRegs::StencilTest], regs[PICA::InternalRegs::StencilOp], regs[PICA::InternalRegs::DepthAndColorMask], + regs[PICA::InternalRegs::DepthBufferWrite], regs[PICA::InternalRegs::ColourBufferWrite], regs[0x114], regs[0x112], regs[PICA::InternalRegs::AlphaTestConfig], + regs[PICA::InternalRegs::ClipEnable] & 1, regs[PICA::InternalRegs::LogicOp] & 0xF, regs[PICA::InternalRegs::BlendFunc], vertices.size() <= 3 ? "<=3" : (vertices.size() <= 12 ? "<=12" : ">12")); + seen[key]++; + if (++drawCount % 3000 == 0) { + std::vector> top; + for (auto& [k, v] : seen) top.emplace_back(v, k); + std::sort(top.begin(), top.end(), [](auto& a, auto& b) { return a.first > b.first; }); + for (size_t i = 0; i < top.size() && i < 10; i++) std::printf("[state] %5d x %s\n", top[i].first, top[i].second.c_str()); + std::printf("[state] ---\n"); + seen.clear(); + } + } + // The fourth type is meant to be "Geometry primitive". TODO: Find out what that is static constexpr std::array primTypes = { OpenGL::Triangle, @@ -449,12 +558,14 @@ void RendererGL::drawVertices(PICA::PrimType primType, std::span v poop->fbo.bind(OpenGL::DrawAndReadFramebuffer); const u32 depthControl = regs[PICA::InternalRegs::DepthAndColorMask]; - const bool depthWrite = regs[PICA::InternalRegs::DepthBufferWrite]; + const bool depthWrite = (regs[PICA::InternalRegs::DepthBufferWrite] & 2) != 0; const bool depthEnable = depthControl & 1; const bool depthWriteEnable = getBit<12>(depthControl); const int depthFunc = getBits<4, 3>(depthControl); const int colourMask = getBits<8, 4>(depthControl); - gl.setColourMask(colourMask & 1, colourMask & 2, colourMask & 4, colourMask & 8); + // GPUREG_COLORBUFFER_WRITE can forbid every colour write regardless of the per-channel mask (used for stencil-only passes such as shadow volumes) + const bool colourWriteAllowed = (regs[PICA::InternalRegs::ColourBufferWrite] & 0xF) != 0 && !logicOpPreservesColour; + gl.setColourMask(colourWriteAllowed && (colourMask & 1), colourWriteAllowed && (colourMask & 2), colourWriteAllowed && (colourMask & 4), colourWriteAllowed && (colourMask & 8)); static constexpr std::array depthModes = { GL_NEVER, GL_ALWAYS, GL_EQUAL, GL_NOTEQUAL, GL_LESS, GL_LEQUAL, GL_GREATER, GL_GEQUAL, @@ -503,6 +614,37 @@ void RendererGL::drawVertices(PICA::PrimType primType, std::span v setupStencilTest(stencilEnable); + // GPUREG_FACECULLING_CONFIG: 0 keep all, 1 keep clockwise, 2 keep counter-clockwise. Without it the stencil shadow volumes of Super Mario 3D Land + // (one pass INCR on the front faces, one DECR on the back faces) count every face twice and cancel out: no shadow under the characters. + // Env GL_NOCULL=1 switches this off (A/B aid), GL_CULL=2 reverses the winding (experiment). + static const bool noCull = std::getenv("GL_NOCULL") != nullptr; + static const bool reverseCull = std::getenv("GL_CULL") && std::atoi(std::getenv("GL_CULL")) == 2; + const u32 cullMode = regs[PICA::InternalRegs::FaceCulling] & 3; + // Only the stencil passes that count shadow volumes (an INCR / DECR operation) are culled for now: with the winding used below, the main geometry of some + // homebrew (a citro3d cube, mode 2) disappears completely while Super Mario 3D Land's (also mode 2) is fine, so the general rule is not understood yet. + // Env GL_CULL_ALL=1 applies the culling to every draw (experiment). + static const bool cullAll = std::getenv("GL_CULL_ALL") != nullptr; + bool countsStencil = false; + if (stencilEnable) { + const u32 stencilOps = regs[PICA::InternalRegs::StencilOp]; + for (u32 shift : {0u, 4u, 8u}) { + const u32 op = (stencilOps >> shift) & 7; + countsStencil |= (op == 3 || op == 4 || op == 6 || op == 7); + } + } + // The stencil shadow volumes need the main geometry culled as well (its depth values decide where the volumes count), so once a game has issued such a + // pass, culling stays on for all its draws. Games that never do (the citro3d cube) keep drawing without culling. + static bool shadowVolumesSeen = false; + if (countsStencil && cullMode != 0 && cullMode != 3) shadowVolumesSeen = true; + if (noCull || cullMode == 0 || cullMode == 3 || !(cullAll || shadowVolumesSeen)) { + glDisable(GL_CULL_FACE); + } else { + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + const bool keepClockwise = (cullMode == 1) != reverseCull; + glFrontFace(keepClockwise ? GL_CW : GL_CCW); + } + if (!usingAcceleratedShader) { vbo.bufferVertsSub(vertices); OpenGL::draw(primitiveTopology, GLsizei(vertices.size())); @@ -563,7 +705,8 @@ void RendererGL::display() { const u32 bottomScreenAddr = externalRegs[bottomActiveFb == 0 ? Framebuffer1AFirstAddr : Framebuffer1ASecondAddr]; auto bottomScreen = colourBufferCache.findFromAddress(bottomScreenAddr); - if (bottomScreen) { + static const bool skipBottom = std::getenv("GL_SKIP_BOTTOM") != nullptr; // measuring aid: what does drawing the lower screen cost? + if (bottomScreen && !skipBottom) { bottomScreen->get().texture.bind(); OpenGL::setViewport(40, 0, 320, 240); OpenGL::draw(OpenGL::TriangleStrip, 4); @@ -722,7 +865,12 @@ OpenGL::Texture RendererGL::getTexture(Texture& tex) { return blankTexture; } - const auto textureData = std::span{startPointer, tex.sizeInBytes()}; // Get pointer to the texture data in 3DS memory + const auto textureData = std::span{startPointer, static_cast(tex.sizeInBytes())}; // Get pointer to the texture data in 3DS memory + static const bool texDebug = std::getenv("GL_TEX_DEBUG") != nullptr; + if (texDebug) { + static u64 misses = 0; + std::printf("[texmiss %llu] loc=%08x fmt=%d %ux%u hash=%llx\n", (unsigned long long)misses++, tex.location, (int)tex.format, tex.size.u(), tex.size.v(), (unsigned long long)tex.hash); + } Texture& newTex = textureCache.add(tex); newTex.decodeTexture(textureData); @@ -938,6 +1086,7 @@ OpenGL::Program& RendererGL::getSpecializedShader() { // Upload uniform data to our shader's UBO PICA::FragmentUniforms uniforms; + if (!std::getenv("GL_UNIFORMS_UNINIT")) std::memset(&uniforms, 0, sizeof(uniforms)); // the struct is compared with memcmp below: unset members / padding must not hold stack garbage uniforms.alphaReference = Helpers::getBits<8, 8>(regs[InternalRegs::AlphaTestConfig]); // Set up the texenv buffer color @@ -955,6 +1104,9 @@ OpenGL::Program& RendererGL::getSpecializedShader() { uniforms.clipCoords[1] = f24::fromRaw(regs[PICA::InternalRegs::ClipData1] & 0xffffff).toFloat32(); uniforms.clipCoords[2] = f24::fromRaw(regs[PICA::InternalRegs::ClipData2] & 0xffffff).toFloat32(); uniforms.clipCoords[3] = f24::fromRaw(regs[PICA::InternalRegs::ClipData3] & 0xffffff).toFloat32(); + } else { + // The user clip plane is disabled: a zero plane gives distance 0, which never discards (matters on GLES, where the plane is emulated in the fragment shader) + uniforms.clipCoords[0] = uniforms.clipCoords[1] = uniforms.clipCoords[2] = uniforms.clipCoords[3] = 0.0f; } // Set up the constant color for the 6 TEV stages @@ -1018,16 +1170,22 @@ OpenGL::Program& RendererGL::getSpecializedShader() { } } - // Upload fragment uniforms to UBO - shadergenFragmentUBO->Bind(); - auto uboRes = shadergenFragmentUBO->Map(driverInfo.uboAlignment, sizeof(PICA::FragmentUniforms)); - std::memcpy(uboRes.pointer, &uniforms, sizeof(PICA::FragmentUniforms)); - shadergenFragmentUBO->Unmap(sizeof(PICA::FragmentUniforms)); + // Upload fragment uniforms to UBO if changed + if (!hasPrevFragmentUniforms || std::memcmp(&prevFragmentUniforms, &uniforms, sizeof(PICA::FragmentUniforms)) != 0) { + shadergenFragmentUBO->Bind(); + auto uboRes = shadergenFragmentUBO->Map(driverInfo.uboAlignment, sizeof(PICA::FragmentUniforms)); + std::memcpy(uboRes.pointer, &uniforms, sizeof(PICA::FragmentUniforms)); + shadergenFragmentUBO->Unmap(sizeof(PICA::FragmentUniforms)); - // Bind our UBOs - glBindBufferRange( - GL_UNIFORM_BUFFER, fsUBOBlockBinding, shadergenFragmentUBO->GetGLBufferId(), uboRes.buffer_offset, sizeof(PICA::FragmentUniforms) - ); + prevFragmentUBOOffset = uboRes.buffer_offset; + prevFragmentUniforms = uniforms; + hasPrevFragmentUniforms = true; + + // Bind our UBOs + glBindBufferRange( + GL_UNIFORM_BUFFER, fsUBOBlockBinding, shadergenFragmentUBO->GetGLBufferId(), uboRes.buffer_offset, sizeof(PICA::FragmentUniforms) + ); + } if (usingAcceleratedShader) { glBindBufferRange( @@ -1248,17 +1406,27 @@ void RendererGL::accelerateVertexUpload(ShaderUnit& shaderUnit, PICA::DrawAccele // Update index buffer if necessary if (accel->indexed) { - usingShortIndices = accel->useShortIndices; - const usize indexBufferSize = regs[PICA::InternalRegs::VertexCountReg] * (usingShortIndices ? sizeof(u16) : sizeof(u8)); + // Metal has no 8-bit indices, so ANGLE converts them on the CPU at every draw (env GL_INDEX16:1: widen them to 16 bits here instead, for A/B on the phone) + static const bool widen8 = std::getenv("GL_INDEX16") != nullptr; + const bool widenIndices = widen8 && !accel->useShortIndices; + usingShortIndices = accel->useShortIndices || widenIndices; + const u32 indexCount = regs[PICA::InternalRegs::VertexCountReg]; + const usize indexBufferSize = indexCount * (usingShortIndices ? sizeof(u16) : sizeof(u8)); hwIndexBuffer->Bind(); auto indexBufferRes = hwIndexBuffer->Map(4, indexBufferSize); hwIndexBufferOffset = reinterpret_cast(usize(indexBufferRes.buffer_offset)); - std::memcpy(indexBufferRes.pointer, accel->indexBuffer, indexBufferSize); + if (widenIndices) { + const u8* src = reinterpret_cast(accel->indexBuffer); + u16* dst = reinterpret_cast(indexBufferRes.pointer); + const u32 base = (glDrawRangeElementsBaseVertex == nullptr) ? accel->minimumIndex : 0; + for (u32 i = 0; i < indexCount; i++) dst[i] = u16(src[i] - base); + } else { + std::memcpy(indexBufferRes.pointer, accel->indexBuffer, indexBufferSize); + } // If we don't have glDrawRangeElementsBaseVertex, we must subtract the base index value from our index buffer manually - if (glDrawRangeElementsBaseVertex == nullptr) [[unlikely]] { - const u32 indexCount = regs[PICA::InternalRegs::VertexCountReg]; + if (!widenIndices && glDrawRangeElementsBaseVertex == nullptr) [[unlikely]] { usingShortIndices ? PICA::IndexBuffer::subtractBaseIndex((u8*)indexBufferRes.pointer, indexCount, accel->minimumIndex) : PICA::IndexBuffer::subtractBaseIndex((u8*)indexBufferRes.pointer, indexCount, accel->minimumIndex); } diff --git a/src/core/services/am.cpp b/src/core/services/am.cpp index 507532b7..0b705028 100644 --- a/src/core/services/am.cpp +++ b/src/core/services/am.cpp @@ -4,6 +4,9 @@ namespace AMCommands { enum : u32 { + GetTitleCount = 0x00180080, + GetTitleList = 0x00190102, + GetTitleIdList = 0x001A00C2, GetDLCTitleInfo = 0x10050084, ListTitleInfo = 0x10070102, GetPatchTitleInfo = 0x100D0084, @@ -14,14 +17,43 @@ void AMService::reset() {} void AMService::handleSyncRequest(u32 messagePointer) { const u32 command = mem.read32(messagePointer); - switch (command) { - case AMCommands::GetPatchTitleInfo: getPatchTitleInfo(messagePointer); break; - case AMCommands::GetDLCTitleInfo: getDLCTitleInfo(messagePointer); break; - case AMCommands::ListTitleInfo: listTitleInfo(messagePointer); break; - default: Helpers::panic("AM service requested. Command: %08X\n", command); + const u32 commandID = command >> 16; + switch (commandID) { + case 0x0018: getTitleCount(messagePointer); break; + case 0x0019: getTitleList(messagePointer); break; + case 0x001A: getTitleIdList(messagePointer); break; + case 0x1005: getDLCTitleInfo(messagePointer); break; + case 0x1007: listTitleInfo(messagePointer); break; + case 0x100D: getPatchTitleInfo(messagePointer); break; + default: + Helpers::warn("Unimplemented AM command: %08X\n", command); + mem.write32(messagePointer, IPC::responseHeader(commandID, 1, 0)); + mem.write32(messagePointer + 4, Result::FailurePlaceholder); + break; } } +void AMService::getTitleCount(u32 messagePointer) { + log("AM::GetTitleCount\n"); + mem.write32(messagePointer, IPC::responseHeader(0x0018, 2, 0)); + mem.write32(messagePointer + 4, Result::Success); + mem.write32(messagePointer + 8, 0); +} + +void AMService::getTitleList(u32 messagePointer) { + log("AM::GetTitleList\n"); + mem.write32(messagePointer, IPC::responseHeader(0x0019, 2, 0)); + mem.write32(messagePointer + 4, Result::Success); + mem.write32(messagePointer + 8, 0); +} + +void AMService::getTitleIdList(u32 messagePointer) { + log("AM::GetTitleIdList\n"); + mem.write32(messagePointer, IPC::responseHeader(0x001A, 2, 0)); + mem.write32(messagePointer + 4, Result::Success); + mem.write32(messagePointer + 8, 0); +} + void AMService::listTitleInfo(u32 messagePointer) { log("AM::ListDLCOrLicenseTicketInfos\n"); // Yes this is the actual name u32 ticketCount = mem.read32(messagePointer + 4); diff --git a/src/core/services/fs.cpp b/src/core/services/fs.cpp index 5d6cb44f..09ecb0fe 100644 --- a/src/core/services/fs.cpp +++ b/src/core/services/fs.cpp @@ -336,6 +336,10 @@ void FSService::openDirectory(u32 messagePointer) { mem.write32(messagePointer + 12, dir.unwrap()); } else { printf("FS::OpenDirectory failed\n"); + if (std::getenv("FS_DEBUG")) { + std::string p8(dirPath.utf16_string.begin(), dirPath.utf16_string.end()); + printf(" [fs] OpenDirectory archive=%s pathType=%u ascii='%s' utf16='%s'\n", archive->name().c_str(), dirPath.type, dirPath.string.c_str(), p8.c_str()); + } mem.write32(messagePointer + 4, static_cast(dir.unwrapErr())); } } @@ -363,7 +367,11 @@ void FSService::openFileDirectly(u32 messagePointer) { Rust::Result res = archive->openArchive(archivePath); if (res.isErr()) [[unlikely]] { - Helpers::panic("OpenFileDirectly: Failed to open archive with given path"); + // e.g. a game probing for DLC / update titles that are not installed: report the error to the game instead of aborting + Helpers::warn("OpenFileDirectly: Failed to open archive %d with given path, returning the archive error", archiveID); + mem.write32(messagePointer, IPC::responseHeader(0x803, 1, 2)); + mem.write32(messagePointer + 4, res.unwrapErr()); + return; } archive = res.unwrap(); @@ -371,6 +379,11 @@ void FSService::openFileDirectly(u32 messagePointer) { mem.write32(messagePointer, IPC::responseHeader(0x803, 1, 2)); if (!handle.has_value()) { printf("OpenFileDirectly failed\n"); + if (std::getenv("FS_DEBUG")) { + std::string p8(filePath.utf16_string.begin(), filePath.utf16_string.end()); + printf(" [fs] appData=%s archive=%s(%u) archivePathType=%u filePathType=%u ascii='%s' utf16='%s' binaryBytes=%zu perms=%u\n", IOFile::getAppData().string().c_str(), ArchiveID::toString(archiveID).c_str(), archiveID, archivePath.type, + filePath.type, filePath.string.c_str(), p8.c_str(), filePath.binary.size(), openFlags); + } mem.write32(messagePointer + 4, Result::FS::FileNotFound); } else { mem.write32(messagePointer + 4, Result::Success); @@ -432,9 +445,16 @@ void FSService::deleteDirectory(u32 messagePointer) { const u32 filePathPointer = mem.read32(messagePointer + 28); log("FS::DeleteDirectory\n"); - Helpers::warn("Stubbed FS::DeleteDirectory call!"); + auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive); mem.write32(messagePointer, IPC::responseHeader(0x806, 1, 0)); - mem.write32(messagePointer + 4, Result::Success); + if (archiveObject == nullptr) [[unlikely]] { + mem.write32(messagePointer + 4, Result::FailurePlaceholder); + return; + } + + ArchiveBase* archive = archiveObject->getData()->archive; + auto dirPath = readPath(filePathType, filePathPointer, filePathSize); + mem.write32(messagePointer + 4, static_cast(archive->deleteDirectory(dirPath, false))); } void FSService::getFormatInfo(u32 messagePointer) { diff --git a/src/core/services/ir/ir_user.cpp b/src/core/services/ir/ir_user.cpp index 48aed958..7bfdaf1e 100644 --- a/src/core/services/ir/ir_user.cpp +++ b/src/core/services/ir/ir_user.cpp @@ -55,7 +55,12 @@ void IRUserService::handleSyncRequest(u32 messagePointer) { case IRUserCommands::ClearReceiveBuffer: clearReceiveBuffer(messagePointer); break; case IRUserCommands::ClearSendBuffer: clearSendBuffer(messagePointer); break; case IRUserCommands::ReleaseReceivedData: releaseReceivedData(messagePointer); break; - default: Helpers::panic("ir:USER service requested. Command: %08X\n", command); + default: + Helpers::warn("Unimplemented ir:USER command: %08X\n", command); + const u32 cmdId = command >> 16; + mem.write32(messagePointer, IPC::responseHeader(cmdId, 1, 0)); + mem.write32(messagePointer + 4, Result::FailurePlaceholder); + break; } } diff --git a/src/core/services/service_manager.cpp b/src/core/services/service_manager.cpp index bd4488e9..dfb6e92c 100644 --- a/src/core/services/service_manager.cpp +++ b/src/core/services/service_manager.cpp @@ -87,6 +87,7 @@ void ServiceManager::handleSyncRequest(u32 messagePointer) { case Commands::Subscribe: subscribe(messagePointer); break; case Commands::Unsubscribe: unsubscribe(messagePointer); break; case Commands::PublishToSubscriber: publishToSubscriber(messagePointer); break; + case Commands::IsServiceRegistered: isServiceRegistered(messagePointer); break; default: Helpers::panic("Unknown \"srv:\" command: %08X", header); break; } } @@ -106,6 +107,8 @@ static const ServiceMapEntry serviceMapArray[] = { { "act:u", KernelHandles::ACT }, { "am:app", KernelHandles::AM }, { "am:sys", KernelHandles::AM }, + { "am:u", KernelHandles::AM }, + { "am:net", KernelHandles::AM }, { "APT:S", KernelHandles::APT }, // TODO: APT:A, APT:S and APT:U are slightly different { "APT:A", KernelHandles::APT }, { "APT:U", KernelHandles::APT }, @@ -135,6 +138,7 @@ static const ServiceMapEntry serviceMapArray[] = { { "ndm:u", KernelHandles::NDM }, { "news:u", KernelHandles::NEWS_U }, { "nfc:u", KernelHandles::NFC }, + { "nfc:m", KernelHandles::NFC }, { "ns:s", KernelHandles::NS_S }, { "nwm::EXT", KernelHandles::NWM_EXT }, { "nwm::UDS", KernelHandles::NWM_UDS }, @@ -161,15 +165,28 @@ void ServiceManager::getServiceHandle(u32 messagePointer) { std::string service = mem.readString(messagePointer + 4, 8); log("srv::getServiceHandle (Service: %s, nameLength: %d, flags: %d)\n", service.c_str(), nameLength, flags); - // Look up service handle in map, panic if it does not exist - if (auto search = serviceMapByName.find(service); search != serviceMapByName.end()) + // Look up service handle in map, return not found error if it does not exist + if (auto search = serviceMapByName.find(service); search != serviceMapByName.end()) { handle = search->second; - else - Helpers::panic("srv: GetServiceHandle with unknown service %s", service.c_str()); + mem.write32(messagePointer, IPC::responseHeader(0x5, 1, 2)); + mem.write32(messagePointer + 4, Result::Success); + mem.write32(messagePointer + 12, handle); + } else { + Helpers::warn("srv: GetServiceHandle with unknown service %s", service.c_str()); + mem.write32(messagePointer, IPC::responseHeader(0x5, 1, 0)); + mem.write32(messagePointer + 4, 0xD8E06406); // OS::NotFound + } +} + +// https://www.3dbrew.org/wiki/SRV:IsServiceRegistered +void ServiceManager::isServiceRegistered(u32 messagePointer) { + std::string service = mem.readString(messagePointer + 4, 8); + bool registered = (serviceMapByName.find(service) != serviceMapByName.end()); + log("srv::IsServiceRegistered (Service: %s) -> %d\n", service.c_str(), registered); - mem.write32(messagePointer, IPC::responseHeader(0x5, 1, 2)); + mem.write32(messagePointer, IPC::responseHeader(0xE, 2, 0)); mem.write32(messagePointer + 4, Result::Success); - mem.write32(messagePointer + 12, handle); + mem.write32(messagePointer + 8, registered ? 1 : 0); } void ServiceManager::enableNotification(u32 messagePointer) { diff --git a/src/host_shaders/opengl_es_display.frag b/src/host_shaders/opengl_es_display.frag index 600ebfcd..72b83e9f 100644 --- a/src/host_shaders/opengl_es_display.frag +++ b/src/host_shaders/opengl_es_display.frag @@ -1,4 +1,4 @@ -#version 310 es +#version 300 es precision mediump float; in vec2 UV; diff --git a/src/host_shaders/opengl_es_display.vert b/src/host_shaders/opengl_es_display.vert index 04fadfc6..c232e61e 100644 --- a/src/host_shaders/opengl_es_display.vert +++ b/src/host_shaders/opengl_es_display.vert @@ -1,4 +1,4 @@ -#version 310 es +#version 300 es precision mediump float; out vec2 UV; diff --git a/third_party/duckstation/gl/stream_buffer.cpp b/third_party/duckstation/gl/stream_buffer.cpp index b7a40603..a83ce2e0 100644 --- a/third_party/duckstation/gl/stream_buffer.cpp +++ b/third_party/duckstation/gl/stream_buffer.cpp @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) +#include #include "gl/stream_buffer.h" #include @@ -23,22 +24,49 @@ void OpenGLStreamBuffer::SetDebugName(std::string_view name) { } namespace { - // Uses glBufferSubData() to update. Preferred for drivers which don't support {ARB,EXT}_buffer_storage. + // Uses glBufferSubData() in a ring buffer (bump allocator) to avoid GPU pipeline stalls on WebGL2 / GLES. class BufferSubDataStreamBuffer final : public OpenGLStreamBuffer { public: ~BufferSubDataStreamBuffer() override { Common::alignedFree(m_cpu_buffer); } - MappingResult Map(u32 alignment, u32 min_size) override { return MappingResult{static_cast(m_cpu_buffer), 0, 0, m_size / alignment}; } + MappingResult Map(u32 alignment, u32 min_size) override { + if (min_size > m_size) { + // One request is bigger than the whole ring (the rings are kept small on purpose: on WebGL2/ANGLE-Metal a write costs about as much as + // copying the entire buffer). Grow once, with headroom, instead of writing past the end of the CPU copy. + const u32 new_size = Common::alignUp(min_size * 2, 64 * 1024); + u8* new_cpu = static_cast(Common::alignedMalloc(new_size, 32)); + if (!new_cpu) Panic("Failed to grow CPU storage for GL buffer"); + Common::alignedFree(m_cpu_buffer); + m_cpu_buffer = new_cpu; + m_size = new_size; + m_position = 0; + glBindBuffer(m_target, m_buffer_id); + glBufferData(m_target, m_size, nullptr, GL_STREAM_DRAW); + } + u32 offset = m_position > 0 ? Common::alignUp(m_position, alignment) : 0; + if (offset + min_size > m_size) { + // Wrap around: orphan the buffer to let previous in-flight GPU reads finish without stalling + glBindBuffer(m_target, m_buffer_id); + glBufferData(m_target, m_size, nullptr, GL_STREAM_DRAW); + offset = 0; + } + m_current_offset = offset; + const u32 space_aligned = (m_size - offset) / alignment; + return MappingResult{static_cast(m_cpu_buffer + offset), offset, offset / alignment, space_aligned}; + } u32 Unmap(u32 used_size) override { - if (used_size == 0) return 0; + if (used_size == 0) return m_current_offset; glBindBuffer(m_target, m_buffer_id); - glBufferSubData(m_target, 0, used_size, m_cpu_buffer); - return 0; + glBufferSubData(m_target, m_current_offset, used_size, m_cpu_buffer + m_current_offset); + + const u32 prev_offset = m_current_offset; + m_position = m_current_offset + used_size; + return prev_offset; } - u32 GetChunkSize() const override { return m_size; } + u32 GetChunkSize() const override { return 64 * 1024; } static std::unique_ptr Create(GLenum target, u32 size) { glGetError(); @@ -65,6 +93,8 @@ namespace { } u8* m_cpu_buffer; + u32 m_position = 0; + u32 m_current_offset = 0; }; // Uses BufferData() to orphan the buffer after every update. Used on Mali where BufferSubData forces a sync. @@ -72,7 +102,17 @@ namespace { public: ~BufferDataStreamBuffer() override { Common::alignedFree(m_cpu_buffer); } - MappingResult Map(u32 alignment, u32 min_size) override { return MappingResult{static_cast(m_cpu_buffer), 0, 0, m_size / alignment}; } + MappingResult Map(u32 alignment, u32 min_size) override { + if (min_size > m_size) { // one request is bigger than the CPU copy: grow it + const u32 new_size = Common::alignUp(min_size * 2, 64 * 1024); + u8* new_cpu = static_cast(Common::alignedMalloc(new_size, 32)); + if (!new_cpu) Panic("Failed to grow CPU storage for GL buffer"); + Common::alignedFree(m_cpu_buffer); + m_cpu_buffer = new_cpu; + m_size = new_size; + } + return MappingResult{static_cast(m_cpu_buffer), 0, 0, m_size / alignment}; + } u32 Unmap(u32 used_size) override { if (used_size == 0) return 0; @@ -111,6 +151,66 @@ namespace { u8* m_cpu_buffer; }; + // Rotates through a pool of buffers and fills the next one with glBufferSubData (no new allocation, no copy-on-write as long as the pool is big enough that the GPU + // has finished with a buffer by the time it comes round again). On ANGLE-Metal in the iPhone's browser this costs ~6 us a draw against ~17 us for orphaning with + // glBufferData (micro benchmark in web/index.html, env BENCH_DRAWS:1). Every Unmap switches m_buffer_id to the buffer it filled; callers bind GetGLBufferId() / Bind() afterwards. + class BufferPoolStreamBuffer final : public OpenGLStreamBuffer { + public: + static constexpr u32 kPoolSize = 640, kInitialCapacity = 4096; // env GL_POOL_SIZE overrides the pool size (experiments) + + ~BufferPoolStreamBuffer() override { + glDeleteBuffers(static_cast(m_pool.size()), m_pool.data()); + Common::alignedFree(m_cpu_buffer); + } + + MappingResult Map(u32 alignment, u32 min_size) override { + if (min_size > m_size) { + const u32 new_size = Common::alignUp(min_size * 2, 64 * 1024); + u8* new_cpu = static_cast(Common::alignedMalloc(new_size, 32)); + if (!new_cpu) Panic("Failed to grow CPU storage for GL buffer"); + Common::alignedFree(m_cpu_buffer); + m_cpu_buffer = new_cpu; + m_size = new_size; + } + return MappingResult{static_cast(m_cpu_buffer), 0, 0, m_size / alignment}; + } + + u32 Unmap(u32 used_size) override { + if (used_size == 0) return 0; + + const size_t i = m_next++ % m_pool.size(); + glBindBuffer(m_target, m_pool[i]); + if (used_size > m_capacity[i]) { // first use, or a draw bigger than anything this buffer held: (re)allocate once + m_capacity[i] = Common::alignUp(used_size * 2, kInitialCapacity); + glBufferData(m_target, m_capacity[i], nullptr, GL_DYNAMIC_DRAW); + } + glBufferSubData(m_target, 0, used_size, m_cpu_buffer); + m_buffer_id = m_pool[i]; + return 0; + } + + u32 GetChunkSize() const override { return m_size; } + + static std::unique_ptr Create(GLenum target, u32 size) { + const char* e = std::getenv("GL_POOL_SIZE"); + std::vector pool(e && std::atoi(e) > 0 ? static_cast(std::atoi(e)) : kPoolSize); + glGenBuffers(static_cast(pool.size()), pool.data()); + return std::unique_ptr(new BufferPoolStreamBuffer(target, std::move(pool), size)); + } + + private: + BufferPoolStreamBuffer(GLenum target, std::vector pool, u32 size) + : OpenGLStreamBuffer(target, pool[0], size), m_pool(std::move(pool)), m_capacity(m_pool.size(), 0) { + m_cpu_buffer = static_cast(Common::alignedMalloc(size, 32)); + if (!m_cpu_buffer) Panic("Failed to allocate CPU storage for GL buffer"); + } + + std::vector m_pool; + std::vector m_capacity; + size_t m_next = 0; + u8* m_cpu_buffer; + }; + // Base class for implementations which require syncing. class SyncingStreamBuffer : public OpenGLStreamBuffer { public: @@ -273,16 +373,19 @@ std::unique_ptr OpenGLStreamBuffer::Create(GLenum target, u3 if (buf) return buf; } - // BufferSubData is slower on all drivers except NVIDIA... -#if 0 - const char* vendor = reinterpret_cast(glGetString(GL_VENDOR)); - if (std::strcmp(vendor, "ARM") == 0 || std::strcmp(vendor, "Qualcomm") == 0) { - // Mali and Adreno drivers can't do sub-buffer tracking... + // WebGL2 / ANGLE-Metal: a glBufferSubData into a ring buffer the GPU is still reading makes ANGLE copy the whole buffer, ~30-40 us per call (500+ calls a frame: 15-25 ms, + // measured in a visible Chromium window; invisible in headless Chrome, where the GPU is idle). Orphaning with glBufferData(size, data) per use costs ~1 us. + // Env GL_STREAM_SUBDATA=1 restores the ring for comparison; GL_STREAM_BUFFERDATA=1 forces the orphaning path on other platforms. +#ifdef __EMSCRIPTEN__ + // Default: orphaning with glBufferData per use. Env GL_STREAM_SUBDATA=1: the old ring of glBufferSubData; GL_STREAM_POOL=1: the buffer pool (GL_POOL_SIZE buffers). + // The pool is ~2.7x cheaper per draw in a micro benchmark but gave no gain in Mario 3D Land on the iPhone, and with 640 buffers per stream iOS killed the WebContent + // process (2 runs of 2, around frame 1700), with 96 it did not (1 run) - so it stays opt-in. + if (std::getenv("GL_STREAM_SUBDATA") == nullptr) { + if (std::getenv("GL_STREAM_POOL") != nullptr) return BufferPoolStreamBuffer::Create(target, size); return BufferDataStreamBuffer::Create(target, size); } - - return BufferSubDataStreamBuffer::Create(target, size); #else - return BufferDataStreamBuffer::Create(target, size); + if (std::getenv("GL_STREAM_BUFFERDATA") != nullptr) return BufferDataStreamBuffer::Create(target, size); #endif + return BufferSubDataStreamBuffer::Create(target, size); } \ No newline at end of file diff --git a/third_party/host_memory/host_memory.cpp b/third_party/host_memory/host_memory.cpp index 2aaa2f10..95a17d97 100644 --- a/third_party/host_memory/host_memory.cpp +++ b/third_party/host_memory/host_memory.cpp @@ -691,6 +691,14 @@ namespace Common { HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_, bool enableFastmem) : backing_size(backing_size_), virtual_size(virtual_size_) { +#ifdef __EMSCRIPTEN__ + // WebAssembly: there is no fastmem, and this build has C++ exceptions disabled (the fallback below is normally reached via throw). + (void)enableFastmem; + fallback_buffer = std::make_unique>(backing_size); + backing_base = fallback_buffer->data(); + virtual_base = nullptr; + return; +#endif try { // Fastmem is disabled, just throw bad alloc and use the VirtualBuffer fallback. if (!enableFastmem) { diff --git a/third_party/opengl/opengl.hpp b/third_party/opengl/opengl.hpp index 607815fa..3c81a555 100644 --- a/third_party/opengl/opengl.hpp +++ b/third_party/opengl/opengl.hpp @@ -602,22 +602,36 @@ namespace OpenGL { static void disableScissor() { glDisable(GL_SCISSOR_TEST); } static void enableBlend() { glEnable(GL_BLEND); } static void disableBlend() { glDisable(GL_BLEND); } +#ifndef __EMSCRIPTEN__ static void enableLogicOp() { glEnable(GL_COLOR_LOGIC_OP); } static void disableLogicOp() { glDisable(GL_COLOR_LOGIC_OP); } +#else + static void enableLogicOp() {} + static void disableLogicOp() {} +#endif static void enableDepth() { glEnable(GL_DEPTH_TEST); } static void disableDepth() { glDisable(GL_DEPTH_TEST); } static void enableStencil() { glEnable(GL_STENCIL_TEST); } static void disableStencil() { glDisable(GL_STENCIL_TEST); } +#ifndef __EMSCRIPTEN__ static void enableClipPlane(GLuint index) { glEnable(GL_CLIP_DISTANCE0 + index); } static void disableClipPlane(GLuint index) { glDisable(GL_CLIP_DISTANCE0 + index); } +#else + static void enableClipPlane(GLuint) {} + static void disableClipPlane(GLuint) {} +#endif static void setDepthFunc(DepthFunc func) { glDepthFunc(static_cast(func)); } static void setColourMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a) { glColorMask(r, g, b, a); } static void setDepthMask(GLboolean mask) { glDepthMask(mask); } // TODO: Add a proper enum for this - static void setLogicOp(GLenum op) { glLogicOp(op); } + static void setLogicOp(GLenum op) { +#ifndef __EMSCRIPTEN__ + glLogicOp(op); +#endif + } enum Primitives { Triangle = GL_TRIANGLES,