From 057aa57422e520c1f3bd54bbd8bf071d749d0fd4 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Fri, 23 Sep 2022 02:43:51 +0300 Subject: [PATCH] [PICA] Start implementing shader interpreter --- CMakeLists.txt | 4 +++- include/PICA/shader.hpp | 9 +++++++-- include/PICA/shader_unit.hpp | 5 +++-- src/core/PICA/gpu.cpp | 6 +++++- src/core/PICA/shader_interpreter.cpp | 14 ++++++++++++++ 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 src/core/PICA/shader_interpreter.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e084e7e0..3f0d30c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,9 @@ set(SERVICE_SOURCE_FILES src/core/services/service_manager.cpp src/core/services src/core/services/fs.cpp src/core/services/gsp_gpu.cpp src/core/services/gsp_lcd.cpp src/core/services/ndm.cpp ) -set(PICA_SOURCE_FILES src/core/PICA/gpu.cpp src/core/PICA/regs.cpp src/core/PICA/shader_unit.cpp) +set(PICA_SOURCE_FILES src/core/PICA/gpu.cpp src/core/PICA/regs.cpp src/core/PICA/shader_unit.cpp + src/core/PICA/shader_interpreter.cpp +) set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/opengl.hpp include/termcolor.hpp include/cpu.hpp include/cpu_dynarmic.hpp include/memory.hpp include/kernel/kernel.hpp diff --git a/include/PICA/shader.hpp b/include/PICA/shader.hpp index 8a876a95..e521b057 100644 --- a/include/PICA/shader.hpp +++ b/include/PICA/shader.hpp @@ -10,12 +10,13 @@ enum class ShaderType { Vertex, Geometry }; -template class PICAShader { - int bufferIndex; // Index of the next instruction to overwrite using f24 = Floats::f24; using vec4f = OpenGL::Vector; + int bufferIndex; // Index of the next instruction to overwrite for shader uploads + ShaderType type; + public: std::array loadedShader; // Currently loaded & active shader std::array bufferedShader; // Shader to be transferred when the SH_CODETRANSFER_END reg gets written to @@ -28,6 +29,8 @@ public: std::array attributes; // Attributes past to the shader std::array outputs; + PICAShader(ShaderType type) : type(type) {} + void reset() { loadedShader.fill(0); bufferedShader.fill(0); @@ -55,4 +58,6 @@ public: bufferedShader[bufferIndex++] = word; bufferIndex &= 511; } + + void run(); }; \ No newline at end of file diff --git a/include/PICA/shader_unit.hpp b/include/PICA/shader_unit.hpp index b1e7e3f2..d8d93160 100644 --- a/include/PICA/shader_unit.hpp +++ b/include/PICA/shader_unit.hpp @@ -4,8 +4,9 @@ class ShaderUnit { public: - PICAShader vs; // Vertex shader - PICAShader gs; // Geometry shader + PICAShader vs; // Vertex shader + PICAShader gs; // Geometry shader + ShaderUnit() : vs(ShaderType::Vertex), gs(ShaderType::Geometry) {} void reset(); }; \ No newline at end of file diff --git a/src/core/PICA/gpu.cpp b/src/core/PICA/gpu.cpp index f8cd60ef..80733b55 100644 --- a/src/core/PICA/gpu.cpp +++ b/src/core/PICA/gpu.cpp @@ -71,7 +71,9 @@ void GPU::drawArrays() { for (int attrCount = 0; attrCount < totalAttribCount; attrCount++) { // Check if attribute is fixed or not if (fixedAttribMask & (1 << attrCount)) { // Fixed attribute - + vec4f& fixedAttr = shaderUnit.vs.fixedAttributes[attrCount]; // TODO: Is this how it works? + vec4f& inputAttr = shaderUnit.vs.attributes[attrCount]; + std::memcpy(&inputAttr, &fixedAttr, sizeof(vec4f)); // Copy fixed attr to input attr } else { // Non-fixed attribute auto& attr = attributeInfo[attrCount]; // Get information for this attribute u64 attrCfg = attr.getConfigFull(); // Get config1 | (config2 << 32) @@ -113,5 +115,7 @@ void GPU::drawArrays() { printf("Attribute %d, type: %d, component count: %d\n", attrCount, attribType, componentCount); } } + + shaderUnit.vs.run(); // Run vertex shader for vertex } } \ No newline at end of file diff --git a/src/core/PICA/shader_interpreter.cpp b/src/core/PICA/shader_interpreter.cpp new file mode 100644 index 00000000..f1ca0601 --- /dev/null +++ b/src/core/PICA/shader_interpreter.cpp @@ -0,0 +1,14 @@ +#include "PICA/shader.hpp" + +void PICAShader::run() { + u32 pc = 0; // Program counter + + while (true) { + const u32 instruction = loadedShader[pc++]; + const u32 opcode = instruction >> 26; // Top 6 bits are the opcode + + switch (opcode) { + default:Helpers::panic("Unimplemented PICA instruction %08X (Opcode = %02X)", instruction, opcode); + } + } +} \ No newline at end of file