Skip to content

Commit

Permalink
More hw VAO work
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Aug 23, 2024
1 parent cf31f7b commit 74a341b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
15 changes: 15 additions & 0 deletions include/PICA/draw_acceleration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@

namespace PICA {
struct DrawAcceleration {
static constexpr u32 maxAttribCount = 12;

struct AttributeInfo {
u32 offset;

u8 type;
u8 componentCount;
bool fixed;

std::array<float, 4> fixedValue; // For fixed attributes
};

u8* vertexBuffer;
u8* indexBuffer;

// Minimum and maximum index in the index buffer for a draw call
u16 minimumIndex, maximumIndex;
u32 totalAttribCount;
u32 vertexDataSize;

std::array<AttributeInfo, maxAttribCount> attributeInfo;

bool canBeAccelerated;
bool indexed;
};
Expand Down
47 changes: 41 additions & 6 deletions src/core/PICA/draw_acceleration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) {
accel.indexed = indexed;
accel.totalAttribCount = totalAttribCount;

const u32 vertexBase = ((regs[PICA::InternalRegs::VertexAttribLoc] >> 1) & 0xfffffff) * 16;
const u32 vertexCount = regs[PICA::InternalRegs::VertexCountReg]; // Total # of vertices to transfer

Expand Down Expand Up @@ -47,23 +49,56 @@ void GPU::getAcceleratedDrawInfo(PICA::DrawAcceleration& accel, bool indexed) {
accel.maximumIndex = accel.minimumIndex + vertexCount - 1;
}

const u64 vertexCfg = u64(regs[PICA::InternalRegs::AttribFormatLow]) | (u64(regs[PICA::InternalRegs::AttribFormatHigh]) << 32);
int buffer = 0;
accel.vertexDataSize = 0;

for (int attrCount = 0; attrCount < totalAttribCount; attrCount++) {
bool fixedAttribute = (fixedAttribMask & (1 << attrCount)) != 0;
auto& attr = accel.attributeInfo[attrCount];
attr.fixed = (fixedAttribMask & (1 << attrCount)) != 0;

if (!fixedAttribute) {
auto& attr = attributeInfo[buffer]; // Get information for this attribute

if (attr.componentCount != 0) {
// Variable attribute attribute
if (!attr.fixed) {
auto& attrData = attributeInfo[buffer]; // Get information for this attribute
u64 attrCfg = attrData.getConfigFull(); // Get config1 | (config2 << 32)
u32 attributeOffset = attrData.offset;

if (attrData.componentCount != 0) {
// Size of the attribute in bytes multiplied by the total number of vertices
const u32 bytes = attr.size * vertexCount;
const u32 bytes = attrData.size * vertexCount;
// Add it to the total vertex data size, aligned to 4 bytes.
accel.vertexDataSize += (bytes + 3) & ~3;
}

for (int i = 0; i < attrData.componentCount; i++) {
uint index = (attrCfg >> (i * 4)) & 0xf; // Get index of attribute in vertexCfg

// Vertex attributes used as padding
// 12, 13, 14 and 15 are equivalent to 4, 8, 12 and 16 bytes of padding respectively
if (index >= 12) [[unlikely]] {
Helpers::panic("Padding attribute");
// Align attribute address up to a 4 byte boundary
attributeOffset = (attributeOffset + 3) & -4;
attributeOffset += (index - 11) << 2;
continue;
}

u32 attribInfo = (vertexCfg >> (index * 4)) & 0xf;
u32 attribType = attribInfo & 0x3; // Type of attribute(sbyte/ubyte/short/float)
u32 size = (attribInfo >> 2) + 1; // Total number of components

attr.componentCount = size;
attr.offset = attributeOffset;
attr.type = attribType;
}

buffer++;
} else {
vec4f& fixedAttr = shaderUnit.vs.fixedAttributes[attrCount];

for (int i = 0; i < 4; i++) {
attr.fixedValue[i] = fixedAttr[i].toFloat32();
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/core/renderer_gl/renderer_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,5 +1115,15 @@ void RendererGL::initUbershader(OpenGL::Program& program) {
}

void RendererGL::accelerateVertexUpload(ShaderUnit& shaderUnit, PICA::DrawAcceleration* accel) {
u32 buffer = 0; // Vertex buffer index for non-fixed attributes
u32 attrCount = 0;

const u32 totalAttribCount = accel->totalAttribCount;

static constexpr GLenum attributeFormats[4] = {
GL_BYTE, // 0: Signed byte
GL_UNSIGNED_BYTE, // 1: Unsigned byte
GL_SHORT, // 2: Short
GL_FLOAT, // 3: Float
};
}

0 comments on commit 74a341b

Please sign in to comment.