From 7b6cd90d369c047357d9c98190f2a5bd9c541ea6 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 15 Jul 2023 04:56:43 +0300 Subject: [PATCH] Cleanup for #98 --- .../PICA/dynapica/shader_rec_emitter_x64.hpp | 5 ++- include/PICA/gpu.hpp | 4 +- include/PICA/shader.hpp | 15 +++++-- include/emulator.hpp | 9 +++- include/renderer_gl/renderer_gl.hpp | 1 - src/core/PICA/gpu.cpp | 6 +-- src/core/PICA/regs.cpp | 45 +++++++++++++++---- src/core/PICA/shader_interpreter.cpp | 25 ++++++++--- src/core/renderer_gl/renderer_gl.cpp | 12 +++-- src/emulator.cpp | 6 +-- 10 files changed, 95 insertions(+), 33 deletions(-) diff --git a/include/PICA/dynapica/shader_rec_emitter_x64.hpp b/include/PICA/dynapica/shader_rec_emitter_x64.hpp index 109fddac..47e011d6 100644 --- a/include/PICA/dynapica/shader_rec_emitter_x64.hpp +++ b/include/PICA/dynapica/shader_rec_emitter_x64.hpp @@ -49,6 +49,7 @@ class ShaderEmitter : public Xbyak::CodeGenerator { const u32 opcode = instruction >> 26; return (opcode == ShaderOpcodes::CALL) || (opcode == ShaderOpcodes::CALLC) || (opcode == ShaderOpcodes::CALLU); } + // Scan the shader code for call instructions to fill up the returnPCs vector before starting compilation void scanForCalls(const PICAShader& shaderUnit); @@ -106,9 +107,11 @@ class ShaderEmitter : public Xbyak::CodeGenerator { MAKE_LOG_FUNCTION(log, shaderJITLogger) public: - using InstructionCallback = const void (*)(PICAShader& shaderUnit); // Callback type used for instructions + // Callback type used for instructions + using InstructionCallback = const void (*)(PICAShader& shaderUnit); // Callback type used for the JIT prologue. This is what the caller will call using PrologueCallback = const void (*)(PICAShader& shaderUnit, InstructionCallback cb); + PrologueCallback prologueCb = nullptr; // Initialize our emitter with "allocSize" bytes of RWX memory diff --git a/include/PICA/gpu.hpp b/include/PICA/gpu.hpp index 753ec728..d4e54358 100644 --- a/include/PICA/gpu.hpp +++ b/include/PICA/gpu.hpp @@ -103,7 +103,9 @@ class GPU { // TODO: Emulate the transfer engine & its registers // Then this can be emulated by just writing the appropriate values there - void clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { renderer->clearBuffer(startAddress, endAddress, value, control); } + void clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { + renderer->clearBuffer(startAddress, endAddress, value, control); + } // TODO: Emulate the transfer engine & its registers // Then this can be emulated by just writing the appropriate values there diff --git a/include/PICA/shader.hpp b/include/PICA/shader.hpp index 06d529c9..0f3154f1 100644 --- a/include/PICA/shader.hpp +++ b/include/PICA/shader.hpp @@ -7,7 +7,10 @@ #include "PICA/pica_hash.hpp" #include "helpers.hpp" -enum class ShaderType { Vertex, Geometry }; +enum class ShaderType { + Vertex, + Geometry, +}; namespace ShaderOpcodes { enum : u32 { @@ -221,11 +224,13 @@ class PICAShader { void finalize() { std::memcpy(&loadedShader[0], &bufferedShader[0], 4096 * sizeof(u32)); } void setBufferIndex(u32 index) { bufferIndex = index & 0xfff; } - void setOpDescriptorIndex(u32 index) { opDescriptorIndex = index & 0x7f; } void uploadWord(u32 word) { - if (bufferIndex >= 4095) Helpers::panic("o no, shader upload overflew"); + if (bufferIndex >= 4095) { + Helpers::panic("o no, shader upload overflew"); + } + bufferedShader[bufferIndex++] = word; bufferIndex &= 0xfff; @@ -247,7 +252,9 @@ class PICAShader { void uploadFloatUniform(u32 word) { floatUniformBuffer[floatUniformWordCount++] = word; - if (floatUniformIndex >= 96) Helpers::panic("[PICA] Tried to write float uniform %d", floatUniformIndex); + if (floatUniformIndex >= 96) { + Helpers::panic("[PICA] Tried to write float uniform %d", floatUniformIndex); + } if ((f32UniformTransfer && floatUniformWordCount >= 4) || (!f32UniformTransfer && floatUniformWordCount >= 3)) { vec4f& uniform = floatUniforms[floatUniformIndex++]; diff --git a/include/emulator.hpp b/include/emulator.hpp index 034b0034..f27cd990 100644 --- a/include/emulator.hpp +++ b/include/emulator.hpp @@ -17,7 +17,12 @@ #include "httpserver.hpp" #endif -enum class ROMType { None, ELF, NCSD, CXI }; +enum class ROMType { + None, + ELF, + NCSD, + CXI, +}; class Emulator { CPU cpu; @@ -29,7 +34,7 @@ class Emulator { EmulatorConfig config; SDL_Window* window; -#if PANDA3DS_ENABLE_OPENGL +#ifdef PANDA3DS_ENABLE_OPENGL SDL_GLContext glContext; #endif diff --git a/include/renderer_gl/renderer_gl.hpp b/include/renderer_gl/renderer_gl.hpp index d34bbc94..0e7f7bcb 100644 --- a/include/renderer_gl/renderer_gl.hpp +++ b/include/renderer_gl/renderer_gl.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include diff --git a/src/core/PICA/gpu.cpp b/src/core/PICA/gpu.cpp index b4fb644e..15c99c42 100644 --- a/src/core/PICA/gpu.cpp +++ b/src/core/PICA/gpu.cpp @@ -8,7 +8,7 @@ #include "PICA/float_types.hpp" #include "PICA/regs.hpp" -#if PANDA3DS_ENABLE_OPENGL +#ifdef PANDA3DS_ENABLE_OPENGL #include "renderer_gl/renderer_gl.hpp" #endif @@ -20,8 +20,8 @@ GPU::GPU(Memory& mem, EmulatorConfig& config) : mem(mem), config(config) { vram = new u8[vramSize]; mem.setVRAM(vram); // Give the bus a pointer to our VRAM - // TODO: configurable backend -#if PANDA3DS_ENABLE_OPENGL + // TODO: Configurable backend +#ifdef PANDA3DS_ENABLE_OPENGL renderer.reset(new RendererGL(*this, regs)); #endif } diff --git a/src/core/PICA/regs.cpp b/src/core/PICA/regs.cpp index bbffa99a..d245f8af 100644 --- a/src/core/PICA/regs.cpp +++ b/src/core/PICA/regs.cpp @@ -134,7 +134,10 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { break; } - case VertexFloatUniformIndex: shaderUnit.vs.setFloatUniformIndex(value); break; + case VertexFloatUniformIndex: { + shaderUnit.vs.setFloatUniformIndex(value); + break; + } case VertexFloatUniformData0: case VertexFloatUniformData1: @@ -143,7 +146,10 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { case VertexFloatUniformData4: case VertexFloatUniformData5: case VertexFloatUniformData6: - case VertexFloatUniformData7: shaderUnit.vs.uploadFloatUniform(value); break; + case VertexFloatUniformData7: { + shaderUnit.vs.uploadFloatUniform(value); + break; + } case FixedAttribIndex: fixedAttribCount = 0; @@ -208,7 +214,10 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { switch (primType) { // Triangle or geometry primitive. Draw a triangle and discard all vertices case 0: - case 3: immediateModeVertIndex = 0; break; + case 3: { + immediateModeVertIndex = 0; + break; + } // Triangle strip. Draw triangle, discard first vertex and keep the last 2 case 1: @@ -233,7 +242,10 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { break; - case VertexShaderOpDescriptorIndex: shaderUnit.vs.setOpDescriptorIndex(value); break; + case VertexShaderOpDescriptorIndex: { + shaderUnit.vs.setOpDescriptorIndex(value); + break; + } case VertexShaderOpDescriptorData0: case VertexShaderOpDescriptorData1: @@ -242,14 +254,23 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { case VertexShaderOpDescriptorData4: case VertexShaderOpDescriptorData5: case VertexShaderOpDescriptorData6: - case VertexShaderOpDescriptorData7: shaderUnit.vs.uploadDescriptor(value); break; + case VertexShaderOpDescriptorData7: { + shaderUnit.vs.uploadDescriptor(value); + break; + } - case VertexBoolUniform: shaderUnit.vs.boolUniform = value & 0xffff; break; + case VertexBoolUniform: { + shaderUnit.vs.boolUniform = value & 0xffff; + break; + } case VertexIntUniform0: case VertexIntUniform1: case VertexIntUniform2: - case VertexIntUniform3: shaderUnit.vs.uploadIntUniform(index - VertexIntUniform0, value); break; + case VertexIntUniform3: { + shaderUnit.vs.uploadIntUniform(index - VertexIntUniform0, value); + break; + } case VertexShaderData0: case VertexShaderData1: @@ -258,9 +279,15 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) { case VertexShaderData4: case VertexShaderData5: case VertexShaderData6: - case VertexShaderData7: shaderUnit.vs.uploadWord(value); break; + case VertexShaderData7: { + shaderUnit.vs.uploadWord(value); + break; + } - case VertexShaderEntrypoint: shaderUnit.vs.entrypoint = value & 0xffff; break; + case VertexShaderEntrypoint: { + shaderUnit.vs.entrypoint = value & 0xffff; + break; + } case VertexShaderTransferEnd: if (value != 0) shaderUnit.vs.finalize(); diff --git a/src/core/PICA/shader_interpreter.cpp b/src/core/PICA/shader_interpreter.cpp index 28eee3c7..9fed6bba 100644 --- a/src/core/PICA/shader_interpreter.cpp +++ b/src/core/PICA/shader_interpreter.cpp @@ -20,7 +20,11 @@ void PICAShader::run() { case ShaderOpcodes::CALLC: callc(instruction); break; case ShaderOpcodes::CALLU: callu(instruction); break; case ShaderOpcodes::CMP1: - case ShaderOpcodes::CMP2: cmp(instruction); break; + case ShaderOpcodes::CMP2: { + cmp(instruction); + break; + } + case ShaderOpcodes::DP3: dp3(instruction); break; case ShaderOpcodes::DP4: dp4(instruction); break; case ShaderOpcodes::DPHI: dphi(instruction); break; @@ -52,7 +56,10 @@ void PICAShader::run() { case 0x34: case 0x35: case 0x36: - case 0x37: madi(instruction); break; + case 0x37: { + madi(instruction); + break; + } case 0x38: case 0x39: @@ -61,7 +68,10 @@ void PICAShader::run() { case 0x3C: case 0x3D: case 0x3E: - case 0x3F: mad(instruction); break; + case 0x3F: { + mad(instruction); + break; + } default: Helpers::panic("Unimplemented PICA instruction %08X (Opcode = %02X)", instruction, opcode); } @@ -588,7 +598,10 @@ void PICAShader::cmp(u32 instruction) { cmpRegister[i] = srcVec1[i] >= srcVec2[i]; break; - default: cmpRegister[i] = true; break; + default: { + cmpRegister[i] = true; + break; + } } } } @@ -682,7 +695,9 @@ void PICAShader::loop(u32 instruction) { } void PICAShader::jmpc(u32 instruction) { - if (isCondTrue(instruction)) pc = getBits<10, 12>(instruction); + if (isCondTrue(instruction)) { + pc = getBits<10, 12>(instruction); + } } void PICAShader::jmpu(u32 instruction) { diff --git a/src/core/renderer_gl/renderer_gl.cpp b/src/core/renderer_gl/renderer_gl.cpp index 0083364b..631313aa 100644 --- a/src/core/renderer_gl/renderer_gl.cpp +++ b/src/core/renderer_gl/renderer_gl.cpp @@ -1,5 +1,7 @@ #include "renderer_gl/renderer_gl.hpp" +#include + #include "PICA/float_types.hpp" #include "PICA/gpu.hpp" #include "PICA/regs.hpp" @@ -841,9 +843,14 @@ void RendererGL::updateLightingLUT() { void RendererGL::drawVertices(PICA::PrimType primType, std::span vertices) { // The fourth type is meant to be "Geometry primitive". TODO: Find out what that is - static constexpr std::array primTypes = {OpenGL::Triangle, OpenGL::TriangleStrip, OpenGL::TriangleFan, OpenGL::Triangle}; - const auto primitiveTopology = primTypes[static_cast(primType)]; + static constexpr std::array primTypes = { + OpenGL::Triangle, + OpenGL::TriangleStrip, + OpenGL::TriangleFan, + OpenGL::Triangle, + }; + const auto primitiveTopology = primTypes[static_cast(primType)]; gl.disableScissor(); gl.bindVBO(vbo); gl.bindVAO(vao); @@ -1048,7 +1055,6 @@ void RendererGL::screenshot(const std::string& name) { std::vector pixels, flippedPixels; pixels.resize(width * height * 4); flippedPixels.resize(pixels.size()); - ; OpenGL::bindScreenFramebuffer(); glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, pixels.data()); diff --git a/src/emulator.cpp b/src/emulator.cpp index 0f9c8c54..d58635fb 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -1,8 +1,6 @@ #include "emulator.hpp" -#include - -#if PANDA3DS_ENABLE_OPENGL +#ifdef PANDA3DS_ENABLE_OPENGL #include #endif @@ -27,7 +25,7 @@ Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory Helpers::warn("Failed to initialize SDL2 GameController: %s", SDL_GetError()); } -#if PANDA3DS_ENABLE_OPENGL +#ifdef PANDA3DS_ENABLE_OPENGL // Request OpenGL 4.1 Core (Max available on MacOS) // MacOS gets mad if we don't explicitly demand a core profile SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);