Merge branch 'master' into dynapica

This commit is contained in:
wheremyfoodat 2023-06-10 12:55:43 +03:00
commit bf125bf2cf
9 changed files with 180 additions and 147 deletions

View file

@ -61,7 +61,7 @@ void GPU::drawArrays() {
// Configures the type of primitive and the number of vertex shader outputs
const u32 primConfig = regs[PICAInternalRegs::PrimitiveConfig];
const u32 primType = (primConfig >> 8) & 3;
const u32 primType = Helpers::getBits<8, 2>(primConfig);
if (primType != 0 && primType != 1 && primType != 3) Helpers::panic("[PICA] Tried to draw unimplemented shape %d\n", primType);
if (vertexCount > Renderer::vertexBufferSize) Helpers::panic("[PICA] vertexCount > vertexBufferSize");
@ -72,7 +72,7 @@ void GPU::drawArrays() {
// Get the configuration for the index buffer, used only for indexed drawing
u32 indexBufferConfig = regs[PICAInternalRegs::IndexBufferConfig];
u32 indexBufferPointer = vertexBase + (indexBufferConfig & 0xfffffff);
bool shortIndex = (indexBufferConfig >> 31) & 1; // Indicates whether vert indices are 16-bit or 8-bit
bool shortIndex = Helpers::getBit<31>(indexBufferConfig); // Indicates whether vert indices are 16-bit or 8-bit
// Stuff the global attribute config registers in one u64 to make attr parsing easier
// TODO: Cache this when the vertex attribute format registers are written to

View file

@ -2,6 +2,7 @@
#include "PICA/regs.hpp"
using namespace Floats;
using namespace Helpers;
u32 GPU::readReg(u32 address) {
if (address >= 0x1EF01000 && address < 0x1EF01C00) { // Internal registers
@ -56,7 +57,7 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) {
case AttribFormatHigh:
totalAttribCount = (value >> 28) + 1; // Total number of vertex attributes
fixedAttribMask = (value >> 16) & 0xfff; // Determines which vertex attributes are fixed for all vertices
fixedAttribMask = getBits<16, 12>(value); // Determines which vertex attributes are fixed for all vertices
break;
case ColourBufferLoc: {
@ -66,7 +67,7 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) {
};
case ColourBufferFormat: {
u32 format = (value >> 16) & 7;
u32 format = getBits<16, 3>(value);
renderer.setColourFormat(format);
break;
}
@ -85,7 +86,7 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) {
case FramebufferSize: {
const u32 width = value & 0x7ff;
const u32 height = ((value >> 12) & 0x3ff) + 1;
const u32 height = getBits<12, 10>(value) + 1;
renderer.setFBSize(width, height);
break;
}
@ -151,7 +152,7 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) {
// Get primitive type
const u32 primConfig = regs[PICAInternalRegs::PrimitiveConfig];
const u32 primType = (primConfig >> 8) & 3;
const u32 primType = getBits<8, 2>(primConfig);
// If we've reached 3 verts, issue a draw call
// Handle rendering depending on the primitive type
@ -253,7 +254,7 @@ void GPU::writeInternalReg(u32 index, u32 value, u32 mask) {
break;
case 2:
attr.config2 = value;
attr.size = (value >> 16) & 0xff;
attr.size = getBits<16, 8>(value);
attr.componentCount = value >> 28;
break;
}
@ -295,8 +296,8 @@ void GPU::startCommandList(u32 addr, u32 size) {
u32 header = *cmdBuffCurr++;
u32 id = header & 0xffff;
u32 paramMaskIndex = (header >> 16) & 0xf;
u32 paramCount = (header >> 20) & 0xff; // Number of additional parameters
u32 paramMaskIndex = getBits<16, 4>(header);
u32 paramCount = getBits<20, 8>(header); // Number of additional parameters
// Bit 31 tells us whether this command is going to write to multiple sequential registers (if the bit is 1)
// Or if all written values will go to the same register (If the bit is 0). It's essentially the value that
// gets added to the "id" field after each register write

View file

@ -1,6 +1,8 @@
#include "PICA/shader.hpp"
#include <cmath>
using namespace Helpers;
void PICAShader::run() {
pc = entrypoint;
loopIndex = 0;
@ -126,9 +128,9 @@ PICAShader::vec4f& PICAShader::getDest(u32 dest) {
}
bool PICAShader::isCondTrue(u32 instruction) {
u32 condition = (instruction >> 22) & 3;
bool refY = ((instruction >> 24) & 1) != 0;
bool refX = ((instruction >> 25) & 1) != 0;
u32 condition = getBits<22, 2>(instruction);
bool refY = (getBit<24>(instruction)) != 0;
bool refX = (getBit<25>(instruction)) != 0;
switch (condition) {
case 0: // Either cmp register matches
@ -144,10 +146,10 @@ bool PICAShader::isCondTrue(u32 instruction) {
void PICAShader::add(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src1 = getIndexedSource(src1, idx);
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -165,10 +167,10 @@ void PICAShader::add(u32 instruction) {
void PICAShader::mul(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src1 = getIndexedSource(src1, idx);
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -186,9 +188,9 @@ void PICAShader::mul(u32 instruction) {
void PICAShader::flr(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src = (instruction >> 12) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src = getBits<12, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src = getIndexedSource(src, idx);
vec4f srcVector = getSourceSwizzled<1>(src, operandDescriptor);
@ -204,10 +206,10 @@ void PICAShader::flr(u32 instruction) {
void PICAShader::max(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
const u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
if (idx) Helpers::panic("[PICA] MAX: idx != 0");
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -226,10 +228,10 @@ void PICAShader::max(u32 instruction) {
void PICAShader::min(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
const u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
if (idx) Helpers::panic("[PICA] MIN: idx != 0");
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -248,9 +250,9 @@ void PICAShader::min(u32 instruction) {
void PICAShader::mov(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src = (instruction >> 12) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src = getBits<12, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src = getIndexedSource(src, idx);
vec4f srcVector = getSourceSwizzled<1>(src, operandDescriptor);
@ -266,8 +268,8 @@ void PICAShader::mov(u32 instruction) {
void PICAShader::mova(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src = (instruction >> 12) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 src = getBits<12, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
if (idx) Helpers::panic("[PICA] MOVA: idx != 0");
vec4f srcVector = getSourceSwizzled<1>(src, operandDescriptor);
@ -281,10 +283,10 @@ void PICAShader::mova(u32 instruction) {
void PICAShader::dp3(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src1 = getIndexedSource(src1, idx);
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -303,10 +305,10 @@ void PICAShader::dp3(u32 instruction) {
void PICAShader::dp4(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src1 = getIndexedSource(src1, idx);
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -325,9 +327,9 @@ void PICAShader::dp4(u32 instruction) {
void PICAShader::rcp(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 12) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
const u32 src1 = getBits<12, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
if (idx) Helpers::panic("[PICA] RCP: idx != 0");
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -345,9 +347,9 @@ void PICAShader::rcp(u32 instruction) {
void PICAShader::rsq(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 12) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
const u32 src1 = getBits<12, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
if (idx) Helpers::panic("[PICA] RSQ: idx != 0");
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -365,11 +367,11 @@ void PICAShader::rsq(u32 instruction) {
void PICAShader::mad(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x1f];
const u32 src1 = (instruction >> 17) & 0x1f;
u32 src2 = (instruction >> 10) & 0x7f;
const u32 src3 = (instruction >> 5) & 0x1f;
const u32 idx = (instruction >> 22) & 3;
const u32 dest = (instruction >> 24) & 0x1f;
const u32 src1 = getBits<17, 5>(instruction);
u32 src2 = getBits<10, 7>(instruction);
const u32 src3 = getBits<5, 5>(instruction);
const u32 idx = getBits<22, 2>(instruction);
const u32 dest = getBits<24, 5>(instruction);
src2 = getIndexedSource(src2, idx);
@ -388,11 +390,11 @@ void PICAShader::mad(u32 instruction) {
void PICAShader::madi(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x1f];
const u32 src1 = (instruction >> 17) & 0x1f;
const u32 src2 = (instruction >> 12) & 0x1f;
u32 src3 = (instruction >> 5) & 0x7f;
const u32 idx = (instruction >> 22) & 3;
const u32 dest = (instruction >> 24) & 0x1f;
const u32 src1 = getBits<17, 5>(instruction);
const u32 src2 = getBits<12, 5>(instruction);
u32 src3 = getBits<5, 7>(instruction);
const u32 idx = getBits<22, 2>(instruction);
const u32 dest = getBits<24, 5>(instruction);
src3 = getIndexedSource(src3, idx);
@ -411,10 +413,10 @@ void PICAShader::madi(u32 instruction) {
void PICAShader::slt(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src1 = getIndexedSource(src1, idx);
vec4f srcVec1 = getSourceSwizzled<1>(src1, operandDescriptor);
@ -431,10 +433,10 @@ void PICAShader::slt(u32 instruction) {
void PICAShader::sgei(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 14) & 0x1f;
u32 src2 = (instruction >> 7) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
const u32 src1 = getBits<14, 5>(instruction);
u32 src2 = getBits<7, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src2 = getIndexedSource(src2, idx);
@ -452,10 +454,10 @@ void PICAShader::sgei(u32 instruction) {
void PICAShader::slti(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 14) & 0x1f;
u32 src2 = (instruction >> 7) & 0x7f;
const u32 idx = (instruction >> 19) & 3;
const u32 dest = (instruction >> 21) & 0x1f;
const u32 src1 = getBits<14, 5>(instruction);
u32 src2 = getBits<7, 7>(instruction);
const u32 idx = getBits<19, 2>(instruction);
const u32 dest = getBits<21, 5>(instruction);
src2 = getIndexedSource(src2, idx);
@ -473,11 +475,11 @@ void PICAShader::slti(u32 instruction) {
void PICAShader::cmp(u32 instruction) {
const u32 operandDescriptor = operandDescriptors[instruction & 0x7f];
const u32 src1 = (instruction >> 12) & 0x7f;
const u32 src2 = (instruction >> 7) & 0x1f; // src2 coming first because PICA moment
const u32 idx = (instruction >> 19) & 3;
const u32 cmpY = (instruction >> 21) & 7;
const u32 cmpX = (instruction >> 24) & 7;
const u32 src1 = getBits<12, 7>(instruction);
const u32 src2 = getBits<7, 5>(instruction); // src2 coming first because PICA moment
const u32 idx = getBits<19, 2>(instruction);
const u32 cmpY = getBits<21, 3>(instruction);
const u32 cmpX = getBits<24, 3>(instruction);
const u32 cmpOperations[2] = { cmpX, cmpY };
if (idx) Helpers::panic("[PICA] CMP: idx != 0");
@ -518,7 +520,7 @@ void PICAShader::cmp(u32 instruction) {
}
void PICAShader::ifc(u32 instruction) {
const u32 dest = (instruction >> 10) & 0xfff;
const u32 dest = getBits<10, 12>(instruction);
if (isCondTrue(instruction)) {
if (ifIndex >= 8) [[unlikely]]
@ -535,8 +537,8 @@ void PICAShader::ifc(u32 instruction) {
}
void PICAShader::ifu(u32 instruction) {
const u32 dest = (instruction >> 10) & 0xfff;
const u32 bit = (instruction >> 22) & 0xf; // Bit of the bool uniform to check
const u32 dest = getBits<10, 12>(instruction);
const u32 bit = getBits<22, 4>(instruction); // Bit of the bool uniform to check
if (boolUniform & (1 << bit)) {
if (ifIndex >= 8) [[unlikely]]
@ -558,7 +560,7 @@ void PICAShader::call(u32 instruction) {
Helpers::panic("[PICA] Overflowed CALL stack");
const u32 num = instruction & 0xff;
const u32 dest = (instruction >> 10) & 0xfff;
const u32 dest = getBits<10, 12>(instruction);
auto& block = callInfo[callIndex++];
block.endingPC = dest + num;
@ -574,14 +576,14 @@ void PICAShader::callc(u32 instruction) {
}
void PICAShader::callu(u32 instruction) {
const u32 bit = (instruction >> 22) & 0xf; // Bit of the bool uniform to check
const u32 bit = getBits<22, 4>(instruction); // Bit of the bool uniform to check
if (boolUniform & (1 << bit)) {
if (callIndex >= 4) [[unlikely]]
Helpers::panic("[PICA] Overflowed CALL stack");
const u32 num = instruction & 0xff;
const u32 dest = (instruction >> 10) & 0xfff;
const u32 dest = getBits<10, 12>(instruction);
auto& block = callInfo[callIndex++];
block.endingPC = dest + num;
@ -595,8 +597,8 @@ void PICAShader::loop(u32 instruction) {
if (loopIndex >= 4) [[unlikely]]
Helpers::panic("[PICA] Overflowed loop stack");
u32 dest = (instruction >> 10) & 0xfff;
auto& uniform = intUniforms[(instruction >> 22) & 3]; // The uniform we'll get loop info from
u32 dest = getBits<10, 12>(instruction);
auto& uniform = intUniforms[getBits<22, 2>(instruction)]; // The uniform we'll get loop info from
loopCounter = uniform.y();
auto& loop = loopInfo[loopIndex++];
@ -608,13 +610,13 @@ void PICAShader::loop(u32 instruction) {
void PICAShader::jmpc(u32 instruction) {
if (isCondTrue(instruction))
pc = (instruction >> 10) & 0xfff;
pc = getBits<10, 12>(instruction);
}
void PICAShader::jmpu(u32 instruction) {
const u32 test = (instruction & 1) ^ 1; // If the LSB is 0 we want to compare to true, otherwise compare to false
const u32 dest = (instruction >> 10) & 0xfff;
const u32 bit = (instruction >> 22) & 0xf; // Bit of the bool uniform to check
const u32 dest = getBits<10, 12>(instruction);
const u32 bit = getBits<22, 4>(instruction); // Bit of the bool uniform to check
if (((boolUniform >> bit) & 1) == test) // Jump if the bool uniform is the value we want
pc = dest;